roymondchen a7198e2731 fix(editor): 修复连续 add 与批量插入时节点顺序错乱
通过 lastAdded 记忆插入锚点、批量 add 改为串行,并向 runtime 传递 index 以保证编辑器与画布 DSL 顺序一致。
2026-08-07 11:59:06 +08:00

174 lines
5.2 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { computed, nextTick, onBeforeUnmount, reactive, ref } from 'vue';
import type { Id, MApp, MNode } from '@tmagic/core';
import TMagicApp, { NodeType } from '@tmagic/core';
import type { FormConfig, MForm, RemoveData, UpdateData } from '@tmagic/editor';
import { getElById, getNodePath, initValue, replaceChildNode } from '@tmagic/editor';
import { AppProps } from './types';
export const useFormConfig = (props: AppProps) => {
const { contentWindow } = props.stage.renderer!;
const mForm = ref<InstanceType<typeof MForm>>();
const root = ref<MApp>();
const values = ref({});
const curPageId = ref<Id>();
const selectedId = ref<Id>();
const config = computed(
() => root.value?.items?.find((item: MNode) => item.id === curPageId.value) || root.value?.items?.[0],
);
// @ts-ignore
const formConfig = computed(() => props.fillConfig((config.value?.items || []) as FormConfig, mForm));
let app: TMagicApp | undefined = new TMagicApp({
ua: contentWindow?.navigator.userAgent,
disabledFlexible: true,
platform: 'editor',
});
// @ts-ignore
app.mForm = mForm;
const resetValues = () => {
initValue(mForm.value?.formState, {
initValues: {},
config: formConfig.value,
}).then((value) => {
values.value = value;
});
};
const runtimeReadyHandler = ({ data }: any) => {
if (!data.tmagicRuntimeReady) {
return;
}
contentWindow?.magic?.onRuntimeReady({
getApp() {
return app;
},
updateRootConfig(config: MApp) {
root.value = config;
app?.setConfig(config, curPageId.value);
},
updatePageId(id: Id) {
curPageId.value = id;
app?.setPage(id);
},
select(id: Id) {
selectedId.value = id;
if (app?.getPage(id)) {
this.updatePageId?.(id);
}
const el = getElById()(document, `${id}`);
if (el) return el;
// 未在当前文档下找到目标元素,可能是还未渲染,等待渲染完成后再尝试获取
return nextTick().then(() => getElById()(document, `${id}`) as HTMLElement);
},
add({ config, parentId, root: appConfig, index }: UpdateData) {
if (!root.value) {
if (appConfig) {
root.value = appConfig;
resetValues();
return;
}
throw new Error('error');
}
if (!selectedId.value) throw new Error('error');
if (!parentId) throw new Error('error');
const parent = getNodePath(parentId, [root.value]).pop();
if (!parent) throw new Error('未找到父节点');
if (config.type !== 'page') {
const parentNode = app?.page?.getNode(parent.id, { strict: true });
parentNode && app?.page?.initNode(config, parentNode);
}
// 编辑器传了 index 就按它对齐;老版本编辑器不带 index,保持原有的「接在选中节点之后」逻辑
if (typeof index === 'number' && index >= 0 && index <= (parent.items?.length ?? 0)) {
parent.items?.splice(index, 0, config);
} else if (parent.id !== selectedId.value) {
const selectedIndex = parent.items?.findIndex((child: MNode) => child.id === selectedId.value);
parent.items?.splice(selectedIndex + 1, 0, config);
} else {
// 新增节点添加到配置中
parent.items?.push(config);
}
resetValues();
},
update({ config, parentId, root: appConfig }: UpdateData) {
if (!root.value) {
if (appConfig) {
root.value = appConfig;
resetValues();
return;
}
throw new Error('error');
}
if (!app) throw new Error('error');
const newNode = app.dataSourceManager?.compiledNode(config) || config;
replaceChildNode(reactive(newNode), [root.value], parentId);
const nodeInstance = app.page?.getNode(config.id, { strict: true });
if (nodeInstance) {
nodeInstance.setData(config);
}
resetValues();
},
remove({ id, parentId }: RemoveData) {
if (!root.value) throw new Error('error');
const node = getNodePath(id, [root.value]).pop();
if (!node) throw new Error('未找到目标元素');
const parent = getNodePath(parentId, [root.value]).pop();
if (!parent) throw new Error('未找到父元素');
// 页面与页面片都由 app.page 承载,只有删的正是当前渲染的那个才销毁实例,避免误清其它页的画布
if (node.type === NodeType.PAGE || node.type === NodeType.PAGE_FRAGMENT) {
if (`${app?.page?.data.id}` === `${node.id}`) {
app?.deletePage();
}
} else {
app?.page?.deleteNode(node.id);
}
const index = parent.items?.findIndex((child: MNode) => child.id === node.id);
parent.items.splice(index, 1);
resetValues();
},
});
};
contentWindow?.addEventListener('message', runtimeReadyHandler);
onBeforeUnmount(() => {
app?.destroy();
app = undefined;
contentWindow?.removeEventListener('message', runtimeReadyHandler);
});
return {
mForm,
config,
formConfig,
values,
};
};