diff --git a/packages/engine/src/node/entry.ts b/packages/engine/src/node/entry.ts index 2b0c8542..4c6f9ed7 100644 --- a/packages/engine/src/node/entry.ts +++ b/packages/engine/src/node/entry.ts @@ -442,11 +442,11 @@ class NodeEntry implements NodeInterface { on = EventListener>( eventType: string, listener: F, - rewrite?: boolean | undefined, + options?: boolean | AddEventListenerOptions, ): NodeInterface { this.each((node, i) => { - node.addEventListener(eventType, listener, false); - if (this.events[i]) this.events[i].on(eventType, listener, rewrite); + node.addEventListener(eventType, listener, options); + if (this.events[i]) this.events[i].on(eventType, listener, options); }); return this; } @@ -457,10 +457,15 @@ class NodeEntry implements NodeInterface { * @param {Function} listener 事件函数 * @return 返回当前实例 */ - off(eventType: string, listener: EventListener): NodeInterface { + off( + eventType: string, + listener: EventListener, + options?: boolean | EventListenerOptions, + ): NodeInterface { this.each((node, i) => { - node.removeEventListener(eventType, listener, false); - if (this.events[i]) this.events[i].off(eventType, listener); + node.removeEventListener(eventType, listener, options); + if (this.events[i]) + this.events[i].off(eventType, listener, options); }); return this; } diff --git a/packages/engine/src/node/event.ts b/packages/engine/src/node/event.ts index 27e3a8dc..cf7425e4 100644 --- a/packages/engine/src/node/event.ts +++ b/packages/engine/src/node/event.ts @@ -15,13 +15,13 @@ class Event implements EventInterface { on = EventListener>( eventType: string, listener: F, - rewrite?: boolean, + options?: boolean | AddEventListenerOptions, ): void { - if (!this.listeners[eventType] || rewrite) { + if (!this.listeners[eventType]) { this.listeners[eventType] = []; } - - this.listeners[eventType].push(listener); + if (typeof options !== 'object' || !options.once) + this.listeners[eventType].push(listener); } /** diff --git a/packages/engine/src/types/node.ts b/packages/engine/src/types/node.ts index dabad5a7..b8f75143 100644 --- a/packages/engine/src/types/node.ts +++ b/packages/engine/src/types/node.ts @@ -24,14 +24,18 @@ export interface EventInterface { on = EventListener>( eventType: string, listener: F, - rewrite?: boolean, + options?: boolean | AddEventListenerOptions, ): void; /** * 解除绑定 * @param eventType * @param listener */ - off(eventType: string, listener: EventListener): void; + off( + eventType: string, + listener: EventListener, + options?: boolean | EventListenerOptions, + ): void; /** * 触发事件 * @param eventType 事件类型 @@ -285,7 +289,7 @@ export interface NodeInterface { on = EventListener>( eventType: string, listener: F, - rewrite?: boolean | undefined, + options?: boolean | AddEventListenerOptions, ): NodeInterface; /** @@ -294,7 +298,11 @@ export interface NodeInterface { * @param {Function} listener 事件函数 * @return {NodeInterface} 返回当前实例 */ - off(eventType: string, listener: EventListener): NodeInterface; + off( + eventType: string, + listener: EventListener, + options?: boolean | EventListenerOptions, + ): NodeInterface; /** * 获取当前元素节点相对于视口的位置 diff --git a/plugins/mention/src/component/collapse.ts b/plugins/mention/src/component/collapse.ts index dca028a0..9fd6c60b 100644 --- a/plugins/mention/src/component/collapse.ts +++ b/plugins/mention/src/component/collapse.ts @@ -190,6 +190,7 @@ class CollapseComponent implements CollapseComponentInterface { node.attributes({ 'data-name': escape(name), }); + node.removeAllEvents(); node.on( 'click', (event: MouseEvent) => { @@ -198,7 +199,9 @@ class CollapseComponent implements CollapseComponentInterface { event.preventDefault(); if (onSelect) onSelect(event, data); }, - true, + { + once: true, + }, ); node.on( 'mouseenter', @@ -209,7 +212,9 @@ class CollapseComponent implements CollapseComponentInterface { .removeClass('data-mention-item-active'); node.addClass('data-mention-item-active'); }, - true, + { + once: true, + }, ); return node; };