1
0
mirror of synced 2025-12-23 04:37:55 +08:00

feat: 将ui-react中的组件独立成包

This commit is contained in:
roymondchen
2024-08-13 20:35:14 +08:00
committed by roymondchen
parent 60d2b64aa5
commit cab36b49a3
93 changed files with 1772 additions and 741 deletions

View File

@@ -0,0 +1,66 @@
import { cloneDeep } from 'lodash-es';
import Core from '@tmagic/core';
import type { Id, MApp } from '@tmagic/schema';
import type { Magic, RemoveData, SortEventData, UpdateData } from '@tmagic/stage';
import { getElById, replaceChildNode } from '@tmagic/utils';
declare global {
interface Window {
magic?: Magic;
}
}
export const useEditorDsl = (app: Core | undefined, renderDom: () => void) => {
let curPageId: Id = '';
const updateConfig = (root: MApp) => {
app?.setConfig(root, curPageId);
renderDom();
};
window.magic?.onRuntimeReady({
getApp() {
return app;
},
updateRootConfig(root: MApp) {
app?.setConfig(root);
},
updatePageId(id: Id) {
curPageId = id;
app?.setPage(curPageId);
renderDom();
},
select(id: Id) {
const el = getElById()(document, id);
if (el) return el;
// 未在当前文档下找到目标元素,可能是还未渲染,等待渲染完成后再尝试获取
return new Promise((resolve) => {
setTimeout(() => {
resolve(getElById()(document, id));
}, 0);
});
},
add({ root }: UpdateData) {
updateConfig(root);
},
update({ config, root, parentId }: UpdateData) {
const newNode = app?.dataSourceManager?.compiledNode(config, undefined, true) || config;
replaceChildNode(newNode, [root], parentId);
updateConfig(cloneDeep(root));
},
sortNode({ root }: SortEventData) {
root && updateConfig(root);
},
remove({ root }: RemoveData) {
updateConfig(root);
},
});
};