mirror of
https://github.com/Tencent/tmagic-editor.git
synced 2025-06-26 04:09:21 +08:00
feat(core): node新增registerMethod方法,用于组件注册供其他组件通过事件配置调用
This commit is contained in:
parent
c8e1cffca9
commit
ff07147270
@ -32,6 +32,10 @@ interface EventCache {
|
||||
args: any[];
|
||||
}
|
||||
|
||||
interface Methods {
|
||||
[key: string]: (...args: any[]) => any;
|
||||
}
|
||||
|
||||
export interface NodeOptions {
|
||||
config: MNode;
|
||||
page?: Page;
|
||||
@ -45,7 +49,7 @@ class Node extends EventEmitter {
|
||||
[key: string]: any;
|
||||
};
|
||||
public events: EventConfig[] = [];
|
||||
public instance?: any;
|
||||
public instance?: any = {};
|
||||
public page?: Page;
|
||||
public parent?: Node;
|
||||
public app: TMagicApp;
|
||||
@ -69,6 +73,7 @@ class Node extends EventEmitter {
|
||||
const { events, style } = data;
|
||||
this.events = events || [];
|
||||
this.style = style || {};
|
||||
this.instance.config = data;
|
||||
this.emit('update-data', data);
|
||||
}
|
||||
|
||||
@ -76,6 +81,22 @@ class Node extends EventEmitter {
|
||||
this.eventQueue.push(event);
|
||||
}
|
||||
|
||||
public registerMethod(methods: Methods) {
|
||||
if (!methods) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!this.instance) {
|
||||
this.instance = {};
|
||||
}
|
||||
|
||||
for (const [key, fn] of Object.entries(methods)) {
|
||||
if (typeof fn === 'function') {
|
||||
this.instance[key] = fn;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public async runHookCode(hook: string, params?: Record<string, any>) {
|
||||
if (typeof this.data[hook] === 'function') {
|
||||
// 兼容旧的数据格式
|
||||
@ -137,12 +158,23 @@ class Node extends EventEmitter {
|
||||
this.listenLifeSafe();
|
||||
});
|
||||
|
||||
this.instance = instance;
|
||||
if (instance) {
|
||||
this.registerMethod(instance);
|
||||
if (instance.config) {
|
||||
this.setData(instance.config);
|
||||
}
|
||||
}
|
||||
|
||||
await this.runHookCode('created');
|
||||
});
|
||||
|
||||
this.once('mounted', async (instance: any) => {
|
||||
this.instance = instance;
|
||||
if (instance) {
|
||||
this.registerMethod(instance);
|
||||
if (instance.config) {
|
||||
this.setData(instance.config);
|
||||
}
|
||||
}
|
||||
|
||||
for (let eventConfig = this.eventQueue.shift(); eventConfig; eventConfig = this.eventQueue.shift()) {
|
||||
if (typeof instance[eventConfig.method] === 'function') {
|
||||
|
Loading…
x
Reference in New Issue
Block a user