diff --git a/packages/core/src/EventHelper.ts b/packages/core/src/EventHelper.ts index b521e598..b329bb2e 100644 --- a/packages/core/src/EventHelper.ts +++ b/packages/core/src/EventHelper.ts @@ -32,38 +32,11 @@ import { type EventActionItem, type EventConfig, } from '@tmagic/schema'; -import { DATA_SOURCE_FIELDS_CHANGE_EVENT_PREFIX, getIdFromEl } from '@tmagic/utils'; +import { DATA_SOURCE_FIELDS_CHANGE_EVENT_PREFIX } from '@tmagic/utils'; import type { default as TMagicApp } from './App'; import FlowState from './FlowState'; import type { default as TMagicNode } from './Node'; -import { COMMON_EVENT_PREFIX, isCommonMethod, triggerCommonMethod } from './utils'; - -const getCommonEventName = (commonEventName: string) => { - if (commonEventName.startsWith(COMMON_EVENT_PREFIX)) return commonEventName; - return `${COMMON_EVENT_PREFIX}${commonEventName}`; -}; - -// 点击在组件内的某个元素上,需要向上寻找到当前组件 -const getDirectComponent = (element: HTMLElement | null, app: TMagicApp): TMagicNode | undefined => { - if (!element) { - return; - } - - const id = getIdFromEl()(element); - - if (!id) { - return getDirectComponent(element.parentElement, app); - } - - const node = app.getNode( - id, - element.dataset.tmagicIteratorContainerId?.split(','), - element.dataset.tmagicIteratorIndex?.split(',').map((i) => globalThis.parseInt(i, 10)), - ); - - return node; -}; export default class EventHelper extends EventEmitter { public app: TMagicApp; @@ -75,19 +48,11 @@ export default class EventHelper extends EventEmitter { super(); this.app = app; - - if (app.jsEngine === 'browser') { - globalThis.document.body.addEventListener('click', this.commonClickEventHandler); - } } public destroy() { this.removeNodeEvents(); this.removeAllListeners(); - - if (this.app.jsEngine === 'browser') { - globalThis.document.body.removeEventListener('click', this.commonClickEventHandler); - } } public bindNodeEvents(node: TMagicNode) { @@ -243,10 +208,6 @@ export default class EventHelper extends EventEmitter { const toNode = this.app.getNode(to); if (!toNode) throw `ID为${to}的组件不存在`; - if (isCommonMethod(methodName)) { - return triggerCommonMethod(methodName, toNode); - } - if (toNode.instance) { if (typeof toNode.instance[methodName] === 'function') { await toNode.instance[methodName](fromCpt, ...args); @@ -259,17 +220,4 @@ export default class EventHelper extends EventEmitter { }); } } - - private commonClickEventHandler = (e: MouseEvent) => { - if (!e.target) { - return; - } - - const node = getDirectComponent(e.target as HTMLElement, this.app); - - const eventName = `${getCommonEventName('click')}_${node?.data.id}`; - if (node?.eventKeys.has(eventName)) { - this.emit(node.eventKeys.get(eventName)!, node); - } - }; } diff --git a/packages/core/src/utils.ts b/packages/core/src/utils.ts index b3210410..82d7c01c 100644 --- a/packages/core/src/utils.ts +++ b/packages/core/src/utils.ts @@ -18,8 +18,6 @@ import { JsEngine } from '@tmagic/schema'; import { isNumber } from '@tmagic/utils'; -import type { default as TMagicNode } from './Node'; - export const style2Obj = (style: string) => { if (typeof style !== 'string') { return style; @@ -119,47 +117,7 @@ export const transformStyle = (style: Record | string, jsEngine: Js export const COMMON_EVENT_PREFIX = 'magic:common:events:'; export const COMMON_METHOD_PREFIX = 'magic:common:actions:'; -export const CommonMethod = { - SHOW: 'show', - HIDE: 'hide', - SCROLL_TO_VIEW: 'scrollIntoView', - SCROLL_TO_TOP: 'scrollToTop', -}; - -export const isCommonMethod = (methodName: string) => methodName.startsWith(COMMON_METHOD_PREFIX); - -export const triggerCommonMethod = (methodName: string, node: TMagicNode) => { - const { instance } = node; - - if (!instance) return; - - switch (methodName.replace(COMMON_METHOD_PREFIX, '')) { - case CommonMethod.SHOW: - instance.show(); - break; - - case CommonMethod.HIDE: - instance.hide(); - break; - - case CommonMethod.SCROLL_TO_VIEW: - instance.$el?.scrollIntoView({ behavior: 'smooth' }); - break; - - case CommonMethod.SCROLL_TO_TOP: - window.scrollTo({ top: 0, behavior: 'smooth' }); - break; - - default: - break; - } -}; - export interface EventOption { label: string; value: string; } - -export const DEFAULT_EVENTS: EventOption[] = [{ label: '点击', value: `${COMMON_EVENT_PREFIX}click` }]; - -export const DEFAULT_METHODS: EventOption[] = []; diff --git a/packages/editor/src/services/events.ts b/packages/editor/src/services/events.ts index d24b6ea2..ae59e6c4 100644 --- a/packages/editor/src/services/events.ts +++ b/packages/editor/src/services/events.ts @@ -19,11 +19,9 @@ import { reactive } from 'vue'; import { cloneDeep } from 'lodash-es'; -import { DEFAULT_EVENTS, DEFAULT_METHODS, EventOption } from '@tmagic/core'; +import { type EventOption } from '@tmagic/core'; import { toLine } from '@tmagic/utils'; -import type { ComponentGroup } from '@editor/type'; - import BaseService from './BaseService'; let eventMap: Record = reactive({}); @@ -34,20 +32,6 @@ class Events extends BaseService { super([]); } - public init(componentGroupList: ComponentGroup[]) { - componentGroupList.forEach((group) => { - group.items.forEach((element) => { - const type = toLine(element.type); - if (!this.getEvent(type)) { - this.setEvent(type, DEFAULT_EVENTS); - } - if (!this.getMethod(type)) { - this.setMethod(type, DEFAULT_METHODS); - } - }); - }); - } - public setEvents(events: Record) { Object.keys(events).forEach((type: string) => { this.setEvent(toLine(type), events[type] || []); @@ -55,11 +39,11 @@ class Events extends BaseService { } public setEvent(type: string, events: EventOption[]) { - eventMap[toLine(type)] = [...DEFAULT_EVENTS, ...events]; + eventMap[toLine(type)] = [...events]; } public getEvent(type: string): EventOption[] { - return cloneDeep(eventMap[toLine(type)] || DEFAULT_EVENTS); + return cloneDeep(eventMap[toLine(type)]) || []; } public setMethods(methods: Record) { @@ -69,11 +53,11 @@ class Events extends BaseService { } public setMethod(type: string, method: EventOption[]) { - methodMap[toLine(type)] = [...DEFAULT_METHODS, ...method]; + methodMap[toLine(type)] = [...method]; } public getMethod(type: string) { - return cloneDeep(methodMap[toLine(type)] || DEFAULT_METHODS); + return cloneDeep(methodMap[toLine(type)]); } public resetState() {