mirror of
https://github.com/Tencent/tmagic-editor.git
synced 2025-12-13 12:12:54 +00:00
32 lines
847 B
TypeScript
32 lines
847 B
TypeScript
import { inject, nextTick, reactive, ref } from 'vue-demi';
|
|
|
|
import Core from '@tmagic/core';
|
|
import type { ChangeEvent } from '@tmagic/data-source';
|
|
import type { MNode } from '@tmagic/schema';
|
|
import { isPage, replaceChildNode } from '@tmagic/utils';
|
|
|
|
export const useDsl = () => {
|
|
const app = inject<Core | undefined>('app');
|
|
const pageConfig = ref(app?.page?.data || {});
|
|
|
|
app?.dataSourceManager?.on('update-data', (nodes: MNode[], sourceId: string, changeEvent: ChangeEvent) => {
|
|
nodes.forEach((node) => {
|
|
if (isPage(node)) {
|
|
pageConfig.value = node;
|
|
} else {
|
|
replaceChildNode(reactive(node), [pageConfig.value as MNode]);
|
|
}
|
|
});
|
|
|
|
if (!app) return;
|
|
|
|
nextTick(() => {
|
|
app.emit('replaced-node', { nodes, sourceId, ...changeEvent });
|
|
});
|
|
});
|
|
|
|
return {
|
|
pageConfig,
|
|
};
|
|
};
|