diff --git a/packages/core/src/App.ts b/packages/core/src/App.ts index 9bd559a3..55298321 100644 --- a/packages/core/src/App.ts +++ b/packages/core/src/App.ts @@ -18,7 +18,7 @@ import { EventEmitter } from 'events'; -import type { EventItemConfig, Id, MApp } from '@tmagic/schema'; +import type { CodeBlockDSL, EventItemConfig, Id, MApp } from '@tmagic/schema'; import Env from './Env'; import { bindCommonEventListener, isCommonMethod, triggerCommonMethod } from './events'; @@ -44,7 +44,7 @@ interface EventCache { class App extends EventEmitter { public env; - + public codeDsl: CodeBlockDSL | undefined; public pages = new Map(); public page: Page | undefined; @@ -140,8 +140,8 @@ class App extends EventEmitter { * @param curPage 当前页面id */ public setConfig(config: MApp, curPage?: Id) { + this.codeDsl = config.methods; this.pages = new Map(); - config.items?.forEach((page) => { this.pages.set( page.id, diff --git a/packages/core/src/Node.ts b/packages/core/src/Node.ts index 340a98c2..d3a4e5a1 100644 --- a/packages/core/src/Node.ts +++ b/packages/core/src/Node.ts @@ -51,7 +51,6 @@ class Node extends EventEmitter { const { events } = options.config; this.data = options.config; this.events = events; - this.listenLifeSafe(); this.once('destroy', () => { @@ -65,15 +64,20 @@ class Node extends EventEmitter { } private listenLifeSafe() { - this.once('created', (instance: any) => { + this.once('created', async (instance: any) => { this.instance = instance; - - if (typeof this.data.created === 'function') { - this.data.created(this); + if (Array.isArray(this.data.created)) { + await Promise.all( + this.data.created.map(async (codeId) => { + if (this.app?.codeDsl[codeId] && typeof this.app?.codeDsl[codeId]?.content === 'function') { + await this.app.codeDsl[codeId].content(this); + } + }), + ); } }); - this.once('mounted', (instance: any) => { + this.once('mounted', async (instance: any) => { this.instance = instance; const eventConfigQueue = this.app.eventQueueMap[instance.config.id] || []; @@ -82,8 +86,14 @@ class Node extends EventEmitter { this.app.eventHandler(eventConfig.eventConfig, eventConfig.fromCpt, eventConfig.args); } - if (typeof this.data.mounted === 'function') { - this.data.mounted(this); + if (Array.isArray(this.data.mounted)) { + await Promise.all( + this.data.mounted.map(async (codeId) => { + if (this.app?.codeDsl[codeId] && typeof this.app?.codeDsl[codeId]?.content === 'function') { + await this.app.codeDsl[codeId].content(this); + } + }), + ); } }); } diff --git a/playground/src/configs/dsl.ts b/playground/src/configs/dsl.ts index d38f1469..0483697d 100644 --- a/playground/src/configs/dsl.ts +++ b/playground/src/configs/dsl.ts @@ -23,7 +23,8 @@ export default { methods: { l7znj1q24wilb357ay6: { name: 'getData', - content: `() => {\n console.log("this is getData function")\n}`, + // eslint-disable-next-line no-eval + content: eval(`(vm) => {\n console.log("this is getData function")\n}`), }, }, items: [ @@ -50,7 +51,7 @@ export default { fontWeight: '', }, events: [], - created: '', + created: ['l7znj1q24wilb357ay6'], items: [ { type: 'text', @@ -75,7 +76,7 @@ export default { text: 'Tmagic editor 营销活动编辑器', multiple: true, events: [], - created: '', + created: [], }, { type: 'qrcode', @@ -99,7 +100,7 @@ export default { name: '二维码', url: 'https://github.com/Tencent/tmagic-editor', events: [], - created: '', + created: [], }, { type: 'img', @@ -124,7 +125,7 @@ export default { src: 'https://vfiles.gtimg.cn/vupload/20210811/388ed01628667545737.png', url: '', events: [], - created: '', + created: [], }, { type: 'button', @@ -156,7 +157,7 @@ export default { method: 'openOverlay', }, ], - created: '', + created: [], }, { type: 'overlay', @@ -222,7 +223,7 @@ export default { method: 'closeOverlay', }, ], - created: '', + created: [], }, { type: 'img', @@ -261,12 +262,12 @@ export default { text: 'Tmagic', multiple: false, events: [], - created: '', + created: [], }, ], layout: 'absolute', events: [], - created: '', + created: [], }, ], },