update: add event type

This commit is contained in:
yanmao 2021-12-29 01:45:16 +08:00
parent 39888efa15
commit 9523789394
6 changed files with 47 additions and 17 deletions

View File

@ -262,7 +262,11 @@ class Engine implements EngineInterface {
this.change.range.blur();
}
on(eventType: string, listener: EventListener, rewrite?: boolean) {
on<F extends EventListener = EventListener>(
eventType: string,
listener: F,
rewrite?: boolean,
) {
this.event.on(eventType, listener, rewrite);
return this;
}
@ -272,7 +276,7 @@ class Engine implements EngineInterface {
return this;
}
trigger(eventType: string, ...args: any) {
trigger<R = any>(eventType: string, ...args: any): R {
return this.event.trigger(eventType, ...args);
}

View File

@ -438,7 +438,10 @@ class NodeEntry implements NodeInterface {
* @param {Function} listener
* @return
*/
on(eventType: string, listener: EventListener): NodeInterface {
on<F extends EventListener = EventListener>(
eventType: string,
listener: F,
): NodeInterface {
this.each((node, i) => {
node.addEventListener(eventType, listener, false);
if (this.events[i]) this.events[i].on(eventType, listener);

View File

@ -12,7 +12,11 @@ class Event implements EventInterface {
* @param {Function} listener
* @param {boolean} rewrite
*/
on(eventType: string, listener: EventListener, rewrite?: boolean): void {
on<F extends EventListener = EventListener>(
eventType: string,
listener: F,
rewrite?: boolean,
): void {
if (!this.listeners[eventType] || rewrite) {
this.listeners[eventType] = [];
}
@ -45,18 +49,18 @@ class Event implements EventInterface {
* @param eventType
* @param args
*/
trigger(eventType: string, ...args: any) {
trigger<R = any>(eventType: string, ...args: any): R {
const listeners = this.listeners[eventType];
if (listeners) {
let result;
let result: R = undefined as any;
listeners.every((listener) => {
result = listener(...args);
return result !== false;
result = listener(...args) as R;
return typeof result !== 'boolean' || result !== false;
});
return result;
}
return;
return undefined as any;
}
}

View File

@ -143,7 +143,11 @@ export interface EditorInterface {
* @param listener
* @param rewrite
*/
on(eventType: string, listener: EventListener, rewrite?: boolean): void;
on<F extends EventListener = EventListener>(
eventType: string,
listener: F,
rewrite?: boolean,
): void;
/**
* ctrl+a键按下false
* @param eventType
@ -597,7 +601,11 @@ export interface EngineInterface extends EditorInterface {
* @param listener
* @param rewrite
*/
on(eventType: string, listener: EventListener, rewrite?: boolean): void;
on<F extends EventListener = EventListener>(
eventType: string,
listener: F,
rewrite?: boolean,
): void;
/**
* ctrl+a键按下false
* @param eventType

View File

@ -21,7 +21,11 @@ export interface EventInterface {
* @param listener
* @param rewrite
*/
on(eventType: string, listener: EventListener, rewrite?: boolean): void;
on<F extends EventListener = EventListener>(
eventType: string,
listener: F,
rewrite?: boolean,
): void;
/**
*
* @param eventType
@ -33,7 +37,7 @@ export interface EventInterface {
* @param eventType
* @param args
*/
trigger<T = any>(eventType: string, ...args: any): T;
trigger<R = any>(eventType: string, ...args: any): R;
}
export type Selector =
| string
@ -274,7 +278,10 @@ export interface NodeInterface {
* @param {Function} listener
* @return {NodeInterface}
*/
on(eventType: string, listener: EventListener): NodeInterface;
on<F extends EventListener = EventListener>(
eventType: string,
listener: F,
): NodeInterface;
/**
*

View File

@ -131,7 +131,11 @@ class View implements ViewInterface {
return this.#_scrollNode;
}
on(eventType: string, listener: EventListener, rewrite?: boolean) {
on<F extends EventListener = EventListener>(
eventType: string,
listener: F,
rewrite?: boolean,
) {
this.event.on(eventType, listener, rewrite);
return this;
}
@ -141,8 +145,8 @@ class View implements ViewInterface {
return this;
}
trigger(eventType: string, ...args: any) {
return this.event.trigger(eventType, ...args);
trigger<R = any>(eventType: string, ...args: any): R {
return this.event.trigger<R>(eventType, ...args);
}
render(content: string, trigger: boolean = true) {