fix(playground): 修复历史恢复后加载 DSL 时缺少页面基线记录

初始 set root 时标记各页面历史栈为已保存,并用 dsl.id 恢复 IndexedDB 历史,避免 restore 后再赋值时走 diff 分支导致无 initial 基线。
This commit is contained in:
roymondchen 2026-06-15 20:43:53 +08:00
parent ffcc734102
commit 4a8289137b

View File

@ -51,10 +51,11 @@ import { computed, onBeforeUnmount, onMounted, ref, shallowRef, toRaw } from 'vu
import { debounce } from 'lodash-es';
import type { MApp, MContainer, MNode } from '@tmagic/core';
import type { DatasourceTypeOption } from '@tmagic/editor';
import type { DatasourceTypeOption, HistoryOpSource, StoreState } from '@tmagic/editor';
import {
editorService,
historyService,
markStackSaved,
propsService,
serializeConfig,
TMagicDialog,
@ -148,16 +149,29 @@ const persistHistory = () => {
// 编辑(尤其是本次会话新增的代码块 / 数据源历史)丢失,这里改为变更即落库以保证恢复完整。
const schedulePersist = debounce(persistHistory, 500);
// 进入页面时从 IndexedDB 恢复历史记录。此时 root 尚未设置,需显式传入待加载 DSL 的 id 以选中
// 按 app 隔离的库;页面对齐由编辑器挂载后 select(defaultSelected) 触发的 changePage 负责。
// 进入页面时从 IndexedDB 恢复历史记录。此时 root 尚未设置(value 在 restore 之后才赋值),
// 需显式传入待加载 DSL 的 id 以选中按 app 隔离的库;页面对齐由编辑器挂载后
// select(defaultSelected) 触发的 changePage 负责。
const restoreHistory = async () => {
try {
await historyService.restoreFromIndexedDB({ appId: value.value?.id });
await historyService.restoreFromIndexedDB({ appId: dsl.id });
} catch (error) {
console.error('恢复历史记录失败', error);
}
};
const rootChangeHandler = (
_v: StoreState['root'],
_pV?: StoreState['root'],
{ historySource }: { historySource?: HistoryOpSource } = {},
) => {
if (historySource === 'initial') {
Object.values(historyService.state.pageSteps).forEach(markStackSaved);
}
};
editorService.on('root-change', rootChangeHandler);
onMounted(async () => {
await restoreHistory();
value.value = dsl;
@ -177,6 +191,7 @@ onBeforeUnmount(() => {
window.removeEventListener('beforeunload', persistHistory);
window.removeEventListener('pagehide', persistHistory);
editorService.removeAllPlugins();
editorService.off('root-change', rootChangeHandler);
});
const propsSubmitErrorHandler = async (e: any) => {