feat(core): 新增节点操作方法

This commit is contained in:
roymondchen 2023-03-15 15:06:58 +08:00
parent 763038cc11
commit f1f100f952
2 changed files with 28 additions and 8 deletions

View File

@ -18,7 +18,7 @@
import { EventEmitter } from 'events'; import { EventEmitter } from 'events';
import type { CodeBlockDSL, EventItemConfig, Id, MApp } from '@tmagic/schema'; import type { CodeBlockDSL, EventItemConfig, Id, MApp, MPage } from '@tmagic/schema';
import Env from './Env'; import Env from './Env';
import { bindCommonEventListener, isCommonMethod, triggerCommonMethod } from './events'; import { bindCommonEventListener, isCommonMethod, triggerCommonMethod } from './events';
@ -144,18 +144,22 @@ class App extends EventEmitter {
this.codeDsl = config.codeBlocks; this.codeDsl = config.codeBlocks;
this.pages = new Map(); this.pages = new Map();
config.items?.forEach((page) => { config.items?.forEach((page) => {
this.pages.set( this.addPage(page);
page.id,
new Page({
config: page,
app: this,
}),
);
}); });
this.setPage(curPage || this.page?.data?.id); this.setPage(curPage || this.page?.data?.id);
} }
public addPage(config: MPage) {
this.pages.set(
config.id,
new Page({
config,
app: this,
}),
);
}
public setPage(id?: Id) { public setPage(id?: Id) {
let page; let page;
@ -174,6 +178,17 @@ class App extends EventEmitter {
} }
} }
public deletePage(id: Id) {
this.pages.delete(id);
if (!this.pages.size) {
this.page = undefined;
}
}
public getPage(id: Id) {
return this.pages.get(id);
}
public registerComponent(type: string, Component: any) { public registerComponent(type: string, Component: any) {
this.components.set(type, Component); this.components.set(type, Component);
} }

View File

@ -65,6 +65,11 @@ class Node extends EventEmitter {
}); });
} }
public setData(data: MComponent | MContainer | MPage) {
this.data = data;
this.emit('updata-data');
}
private listenLifeSafe() { private listenLifeSafe() {
this.once('created', async (instance: any) => { this.once('created', async (instance: any) => {
this.instance = instance; this.instance = instance;