fix(core): 修复多组件同一事件监听问题修改后导致的共通点击失效问题

统一自定义事件和共通事件行为。
This commit is contained in:
i33 2022-09-21 15:39:29 +08:00 committed by jia000
parent fa4a6d71f6
commit a96d547c20
2 changed files with 7 additions and 22 deletions

View File

@ -21,13 +21,7 @@ import { EventEmitter } from 'events';
import type { EventItemConfig, Id, MApp } from '@tmagic/schema'; import type { EventItemConfig, Id, MApp } from '@tmagic/schema';
import Env from './Env'; import Env from './Env';
import { import { bindCommonEventListener, isCommonMethod, triggerCommonMethod } from './events';
bindCommonEventListener,
DEFAULT_EVENTS,
getCommonEventName,
isCommonMethod,
triggerCommonMethod,
} from './events';
import type Node from './Node'; import type Node from './Node';
import Page from './Page'; import Page from './Page';
import { fillBackgroundImage, isNumber, style2Obj } from './utils'; import { fillBackgroundImage, isNumber, style2Obj } from './utils';
@ -202,13 +196,8 @@ class App extends EventEmitter {
} }
public bindEvent(event: EventItemConfig, id: string) { public bindEvent(event: EventItemConfig, id: string) {
let { name: eventName } = event; const { name } = event;
if (DEFAULT_EVENTS.findIndex((defaultEvent) => defaultEvent.value === eventName) > -1) { this.on(`${name}_${id}`, (fromCpt: Node, ...args) => {
// common 事件名通过 node id 避免重复触发
eventName = getCommonEventName(eventName, id);
}
this.on(`${eventName}_${id}`, (fromCpt: Node, ...args) => {
this.eventHandler(event, fromCpt, args); this.eventHandler(event, fromCpt, args);
}); });
} }

View File

@ -41,12 +41,9 @@ export const DEFAULT_EVENTS: EventOption[] = [{ label: '点击', value: `${COMMO
export const DEFAULT_METHODS: EventOption[] = []; export const DEFAULT_METHODS: EventOption[] = [];
export const getCommonEventName = (commonEventName: string, nodeId: string | number) => { export const getCommonEventName = (commonEventName: string) => {
const returnName = `${commonEventName}:${nodeId}`; if (commonEventName.startsWith(COMMON_EVENT_PREFIX)) return commonEventName;
return `${COMMON_EVENT_PREFIX}${commonEventName}`;
if (commonEventName.startsWith(COMMON_EVENT_PREFIX)) return returnName;
return `${COMMON_EVENT_PREFIX}${returnName}`;
}; };
export const isCommonMethod = (methodName: string) => methodName.startsWith(COMMON_METHOD_PREFIX); export const isCommonMethod = (methodName: string) => methodName.startsWith(COMMON_METHOD_PREFIX);
@ -73,8 +70,7 @@ const commonClickEventHandler = (app: App, eventName: string, e: any) => {
const node = getDirectComponent(e.target, app); const node = getDirectComponent(e.target, app);
if (node) { if (node) {
const { instance, data } = node as Node; app.emit(getCommonEventName(eventName), node);
app.emit(getCommonEventName(eventName, data.id), instance);
} }
}; };