@tmagic/editor 1.8.0-beta.1 → 1.8.0-beta.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (232) hide show
  1. package/dist/es/Editor.vue_vue_type_script_setup_true_lang.js +31 -5
  2. package/dist/es/components/CodeBlockEditor.vue_vue_type_script_setup_true_lang.js +21 -113
  3. package/dist/es/components/CodeParams.vue_vue_type_script_setup_true_lang.js +7 -1
  4. package/dist/es/components/CompareForm.js +5 -0
  5. package/dist/es/components/CompareForm.vue_vue_type_script_setup_true_lang.js +196 -0
  6. package/dist/es/components/ContentMenu.vue_vue_type_script_setup_true_lang.js +1 -1
  7. package/dist/es/components/FloatingBox.vue_vue_type_script_setup_true_lang.js +2 -2
  8. package/dist/es/components/Icon.vue_vue_type_script_setup_true_lang.js +1 -1
  9. package/dist/es/components/Resizer.vue_vue_type_script_setup_true_lang.js +1 -1
  10. package/dist/es/components/ScrollBar.vue_vue_type_script_setup_true_lang.js +1 -1
  11. package/dist/es/components/ScrollViewer.vue_vue_type_script_setup_true_lang.js +1 -1
  12. package/dist/es/components/SearchInput.vue_vue_type_script_setup_true_lang.js +1 -1
  13. package/dist/es/components/SplitView.vue_vue_type_script_setup_true_lang.js +1 -1
  14. package/dist/es/components/ToolButton.vue_vue_type_script_setup_true_lang.js +7 -7
  15. package/dist/es/components/Tree.vue_vue_type_script_setup_true_lang.js +1 -1
  16. package/dist/es/components/TreeNode.vue_vue_type_script_setup_true_lang.js +1 -1
  17. package/dist/es/editorProps.js +2 -0
  18. package/dist/es/fields/Code.vue_vue_type_script_setup_true_lang.js +20 -4
  19. package/dist/es/fields/CodeLink.vue_vue_type_script_setup_true_lang.js +3 -6
  20. package/dist/es/fields/CodeSelect.vue_vue_type_script_setup_true_lang.js +19 -3
  21. package/dist/es/fields/CodeSelectCol.vue_vue_type_script_setup_true_lang.js +42 -8
  22. package/dist/es/fields/CondOpSelect.vue_vue_type_script_setup_true_lang.js +1 -1
  23. package/dist/es/fields/DataSourceFieldSelect/FieldSelect.vue_vue_type_script_setup_true_lang.js +14 -6
  24. package/dist/es/fields/DataSourceFieldSelect/Index.vue_vue_type_script_setup_true_lang.js +3 -3
  25. package/dist/es/fields/DataSourceFields.vue_vue_type_script_setup_true_lang.js +41 -16
  26. package/dist/es/fields/DataSourceInput.vue_vue_type_script_setup_true_lang.js +2 -2
  27. package/dist/es/fields/DataSourceMethodSelect.vue_vue_type_script_setup_true_name_true_lang.js +9 -5
  28. package/dist/es/fields/DataSourceMethods.vue_vue_type_script_setup_true_lang.js +40 -19
  29. package/dist/es/fields/DataSourceMocks.vue_vue_type_script_setup_true_lang.js +17 -9
  30. package/dist/es/fields/DataSourceSelect.vue_vue_type_script_setup_true_lang.js +4 -2
  31. package/dist/es/fields/DisplayConds.vue_vue_type_script_setup_true_lang.js +1 -1
  32. package/dist/es/fields/EventSelect.vue_vue_type_script_setup_true_lang.js +59 -12
  33. package/dist/es/fields/KeyValue.vue_vue_type_script_setup_true_lang.js +12 -7
  34. package/dist/es/fields/PageFragmentSelect.vue_vue_type_script_setup_true_lang.js +1 -1
  35. package/dist/es/fields/StyleSetter/Index.vue_vue_type_script_setup_true_lang.js +1 -1
  36. package/dist/es/fields/StyleSetter/components/BackgroundPosition.vue_vue_type_script_setup_true_lang.js +1 -1
  37. package/dist/es/fields/StyleSetter/components/Border.vue_vue_type_script_setup_true_lang.js +28 -6
  38. package/dist/es/fields/StyleSetter/components/Box.vue_vue_type_script_setup_true_lang.js +1 -1
  39. package/dist/es/fields/StyleSetter/icons/background-repeat/NoRepeat.js +1 -1
  40. package/dist/es/fields/StyleSetter/icons/background-repeat/Repeat.js +1 -1
  41. package/dist/es/fields/StyleSetter/icons/background-repeat/RepeatX.js +1 -1
  42. package/dist/es/fields/StyleSetter/icons/background-repeat/RepeatY.js +1 -1
  43. package/dist/es/fields/StyleSetter/icons/display/Block.js +1 -1
  44. package/dist/es/fields/StyleSetter/icons/display/Flex.js +1 -1
  45. package/dist/es/fields/StyleSetter/icons/display/Inline.js +1 -1
  46. package/dist/es/fields/StyleSetter/icons/display/InlineBlock.js +1 -1
  47. package/dist/es/fields/StyleSetter/icons/display/None.js +1 -1
  48. package/dist/es/fields/StyleSetter/icons/flex-direction/Column.js +1 -1
  49. package/dist/es/fields/StyleSetter/icons/flex-direction/ColumnReverse.js +1 -1
  50. package/dist/es/fields/StyleSetter/icons/flex-direction/Row.js +1 -1
  51. package/dist/es/fields/StyleSetter/icons/flex-direction/RowReverse.js +1 -1
  52. package/dist/es/fields/StyleSetter/icons/justify-content/Center.js +1 -1
  53. package/dist/es/fields/StyleSetter/icons/justify-content/FlexEnd.js +1 -1
  54. package/dist/es/fields/StyleSetter/icons/justify-content/FlexStart.js +1 -1
  55. package/dist/es/fields/StyleSetter/icons/justify-content/SpaceAround.js +1 -1
  56. package/dist/es/fields/StyleSetter/icons/justify-content/SpaceBetween.js +1 -1
  57. package/dist/es/fields/StyleSetter/icons/text-align/Center.js +1 -1
  58. package/dist/es/fields/StyleSetter/icons/text-align/Left.js +1 -1
  59. package/dist/es/fields/StyleSetter/icons/text-align/Right.js +1 -1
  60. package/dist/es/fields/StyleSetter/pro/Background.vue_vue_type_script_setup_true_lang.js +1 -1
  61. package/dist/es/fields/StyleSetter/pro/Border.vue_vue_type_script_setup_true_lang.js +1 -1
  62. package/dist/es/fields/StyleSetter/pro/Font.vue_vue_type_script_setup_true_lang.js +1 -1
  63. package/dist/es/fields/StyleSetter/pro/Layout.vue_vue_type_script_setup_true_lang.js +1 -1
  64. package/dist/es/fields/StyleSetter/pro/Position.vue_vue_type_script_setup_true_lang.js +1 -1
  65. package/dist/es/fields/StyleSetter/pro/Transform.vue_vue_type_script_setup_true_lang.js +1 -1
  66. package/dist/es/fields/UISelect.vue_vue_type_script_setup_true_lang.js +9 -6
  67. package/dist/es/hooks/use-code-block-edit.js +7 -4
  68. package/dist/es/hooks/use-data-source-edit.js +5 -2
  69. package/dist/es/hooks/use-stage.js +22 -10
  70. package/dist/es/icons/CenterIcon.vue_vue_type_script_setup_true_lang.js +1 -1
  71. package/dist/es/icons/CodeIcon.vue_vue_type_script_setup_true_lang.js +1 -1
  72. package/dist/es/icons/FolderMinusIcon.vue_vue_type_script_setup_true_lang.js +1 -1
  73. package/dist/es/index.js +14 -3
  74. package/dist/es/initService.js +7 -7
  75. package/dist/es/layouts/AddPageBox.vue_vue_type_script_setup_true_lang.js +1 -1
  76. package/dist/es/layouts/CodeEditor.vue_vue_type_script_setup_true_lang.js +7 -6
  77. package/dist/es/layouts/Framework.vue_vue_type_script_setup_true_lang.js +14 -9
  78. package/dist/es/layouts/NavMenu.vue_vue_type_script_setup_true_lang.js +12 -4
  79. package/dist/es/layouts/NavMenuColumn.vue_vue_type_script_setup_true_lang.js +1 -1
  80. package/dist/es/layouts/history-list/Bucket.js +5 -0
  81. package/dist/es/layouts/history-list/Bucket.vue_vue_type_script_setup_true_lang.js +71 -0
  82. package/dist/es/layouts/history-list/BucketTab.js +5 -0
  83. package/dist/es/layouts/history-list/BucketTab.vue_vue_type_script_setup_true_lang.js +62 -0
  84. package/dist/es/layouts/history-list/GroupRow.js +5 -0
  85. package/dist/es/layouts/history-list/GroupRow.vue_vue_type_script_setup_true_lang.js +269 -0
  86. package/dist/es/layouts/history-list/HistoryDiffDialog.js +5 -0
  87. package/dist/es/layouts/history-list/HistoryDiffDialog.vue_vue_type_script_setup_true_lang.js +294 -0
  88. package/dist/es/layouts/history-list/HistoryListPanel.js +5 -0
  89. package/dist/es/layouts/history-list/HistoryListPanel.vue_vue_type_script_setup_true_lang.js +327 -0
  90. package/dist/es/layouts/history-list/InitialRow.js +5 -0
  91. package/dist/es/layouts/history-list/InitialRow.vue_vue_type_script_setup_true_lang.js +83 -0
  92. package/dist/es/layouts/history-list/PageTab.js +5 -0
  93. package/dist/es/layouts/history-list/PageTab.vue_vue_type_script_setup_true_lang.js +95 -0
  94. package/dist/es/layouts/history-list/composables.js +184 -0
  95. package/dist/es/layouts/history-list/useHistoryList.js +56 -0
  96. package/dist/es/layouts/history-list/useHistoryRevert.js +313 -0
  97. package/dist/es/layouts/page-bar/AddButton.vue_vue_type_script_setup_true_lang.js +1 -1
  98. package/dist/es/layouts/page-bar/PageBar.vue_vue_type_script_setup_true_lang.js +5 -3
  99. package/dist/es/layouts/page-bar/PageBarScrollContainer.vue_vue_type_script_setup_true_lang.js +1 -1
  100. package/dist/es/layouts/page-bar/PageList.vue_vue_type_script_setup_true_lang.js +1 -1
  101. package/dist/es/layouts/page-bar/Search.vue_vue_type_script_setup_true_lang.js +2 -2
  102. package/dist/es/layouts/props-panel/FormPanel.vue_vue_type_script_setup_true_lang.js +1 -1
  103. package/dist/es/layouts/props-panel/PropsPanel.vue_vue_type_script_setup_true_lang.js +6 -2
  104. package/dist/es/layouts/sidebar/ComponentListPanel.vue_vue_type_script_setup_true_lang.js +2 -2
  105. package/dist/es/layouts/sidebar/Sidebar.vue_vue_type_script_setup_true_lang.js +8 -4
  106. package/dist/es/layouts/sidebar/code-block/CodeBlockList.vue_vue_type_script_setup_true_lang.js +4 -4
  107. package/dist/es/layouts/sidebar/code-block/CodeBlockListPanel.vue_vue_type_script_setup_true_lang.js +2 -2
  108. package/dist/es/layouts/sidebar/code-block/useContentMenu.js +1 -1
  109. package/dist/es/layouts/sidebar/data-source/DataSourceAddButton.vue_vue_type_script_setup_true_lang.js +1 -1
  110. package/dist/es/layouts/sidebar/data-source/DataSourceConfigPanel.vue_vue_type_script_setup_true_lang.js +19 -6
  111. package/dist/es/layouts/sidebar/data-source/DataSourceList.vue_vue_type_script_setup_true_lang.js +1 -1
  112. package/dist/es/layouts/sidebar/data-source/DataSourceListPanel.vue_vue_type_script_setup_true_lang.js +25 -3
  113. package/dist/es/layouts/sidebar/data-source/useContentMenu.js +1 -1
  114. package/dist/es/layouts/sidebar/layer/LayerMenu.vue_vue_type_script_setup_true_lang.js +6 -6
  115. package/dist/es/layouts/sidebar/layer/LayerNodeTool.vue_vue_type_script_setup_true_lang.js +2 -2
  116. package/dist/es/layouts/sidebar/layer/LayerPanel.vue_vue_type_script_setup_true_lang.js +1 -1
  117. package/dist/es/layouts/sidebar/layer/use-node-status.js +1 -1
  118. package/dist/es/layouts/workspace/Breadcrumb.vue_vue_type_script_setup_true_lang.js +1 -1
  119. package/dist/es/layouts/workspace/Workspace.vue_vue_type_script_setup_true_lang.js +1 -1
  120. package/dist/es/layouts/workspace/viewer/NodeListMenu.vue_vue_type_script_setup_true_lang.js +1 -1
  121. package/dist/es/layouts/workspace/viewer/Stage.vue_vue_type_script_setup_true_lang.js +2 -2
  122. package/dist/es/layouts/workspace/viewer/StageOverlay.vue_vue_type_script_setup_true_lang.js +1 -1
  123. package/dist/es/layouts/workspace/viewer/ViewerMenu.vue_vue_type_script_setup_true_lang.js +9 -9
  124. package/dist/es/services/BaseService.js +31 -68
  125. package/dist/es/services/codeBlock.js +286 -8
  126. package/dist/es/services/componentList.js +2 -2
  127. package/dist/es/services/dataSource.js +287 -8
  128. package/dist/es/services/dep.js +21 -9
  129. package/dist/es/services/editor.js +655 -194
  130. package/dist/es/services/events.js +2 -2
  131. package/dist/es/services/history.js +390 -47
  132. package/dist/es/services/keybinding.js +5 -5
  133. package/dist/es/services/props.js +8 -8
  134. package/dist/es/services/stageOverlay.js +2 -2
  135. package/dist/es/services/storage.js +2 -2
  136. package/dist/es/services/ui.js +5 -2
  137. package/dist/es/style.css +456 -8
  138. package/dist/es/utils/code-block.js +122 -0
  139. package/dist/es/utils/content-menu.js +19 -12
  140. package/dist/es/utils/data-source/index.js +2 -0
  141. package/dist/es/utils/dep/worker.js +1 -1
  142. package/dist/es/utils/editor.js +105 -51
  143. package/dist/es/utils/history.js +220 -0
  144. package/dist/es/utils/indexed-db.js +86 -0
  145. package/dist/es/utils/undo-redo.js +80 -1
  146. package/dist/style.css +456 -8
  147. package/dist/tmagic-editor.umd.cjs +11149 -6646
  148. package/package.json +8 -8
  149. package/src/Editor.vue +25 -1
  150. package/src/components/CodeBlockEditor.vue +20 -109
  151. package/src/components/CodeParams.vue +6 -0
  152. package/src/components/CompareForm.vue +277 -0
  153. package/src/components/ToolButton.vue +2 -2
  154. package/src/editorProps.ts +9 -0
  155. package/src/fields/Code.vue +19 -3
  156. package/src/fields/CodeLink.vue +2 -5
  157. package/src/fields/CodeSelect.vue +17 -1
  158. package/src/fields/CodeSelectCol.vue +37 -3
  159. package/src/fields/DataSourceFieldSelect/FieldSelect.vue +23 -3
  160. package/src/fields/DataSourceFieldSelect/Index.vue +2 -2
  161. package/src/fields/DataSourceFields.vue +48 -10
  162. package/src/fields/DataSourceInput.vue +3 -3
  163. package/src/fields/DataSourceMethodSelect.vue +12 -4
  164. package/src/fields/DataSourceMethods.vue +55 -24
  165. package/src/fields/DataSourceMocks.vue +13 -3
  166. package/src/fields/DataSourceSelect.vue +4 -1
  167. package/src/fields/EventSelect.vue +57 -8
  168. package/src/fields/KeyValue.vue +17 -4
  169. package/src/fields/StyleSetter/components/Border.vue +15 -6
  170. package/src/fields/UISelect.vue +5 -2
  171. package/src/hooks/use-code-block-edit.ts +9 -5
  172. package/src/hooks/use-data-source-edit.ts +2 -2
  173. package/src/hooks/use-stage.ts +26 -9
  174. package/src/index.ts +8 -0
  175. package/src/initService.ts +1 -1
  176. package/src/layouts/CodeEditor.vue +17 -5
  177. package/src/layouts/Framework.vue +11 -6
  178. package/src/layouts/NavMenu.vue +12 -3
  179. package/src/layouts/history-list/Bucket.vue +91 -0
  180. package/src/layouts/history-list/BucketTab.vue +72 -0
  181. package/src/layouts/history-list/GroupRow.vue +304 -0
  182. package/src/layouts/history-list/HistoryDiffDialog.vue +281 -0
  183. package/src/layouts/history-list/HistoryListPanel.vue +367 -0
  184. package/src/layouts/history-list/InitialRow.vue +71 -0
  185. package/src/layouts/history-list/PageTab.vue +118 -0
  186. package/src/layouts/history-list/composables.ts +309 -0
  187. package/src/layouts/history-list/useHistoryList.ts +60 -0
  188. package/src/layouts/history-list/useHistoryRevert.ts +421 -0
  189. package/src/layouts/page-bar/PageBar.vue +4 -2
  190. package/src/layouts/props-panel/PropsPanel.vue +5 -1
  191. package/src/layouts/sidebar/ComponentListPanel.vue +9 -5
  192. package/src/layouts/sidebar/Sidebar.vue +8 -3
  193. package/src/layouts/sidebar/code-block/CodeBlockList.vue +5 -5
  194. package/src/layouts/sidebar/code-block/CodeBlockListPanel.vue +1 -1
  195. package/src/layouts/sidebar/code-block/useContentMenu.ts +1 -1
  196. package/src/layouts/sidebar/data-source/DataSourceConfigPanel.vue +31 -2
  197. package/src/layouts/sidebar/data-source/DataSourceListPanel.vue +27 -2
  198. package/src/layouts/sidebar/data-source/useContentMenu.ts +1 -1
  199. package/src/layouts/sidebar/layer/LayerMenu.vue +19 -11
  200. package/src/layouts/sidebar/layer/LayerNodeTool.vue +7 -4
  201. package/src/layouts/sidebar/layer/use-node-status.ts +9 -6
  202. package/src/layouts/workspace/viewer/Stage.vue +1 -1
  203. package/src/layouts/workspace/viewer/ViewerMenu.vue +8 -8
  204. package/src/services/BaseService.ts +35 -86
  205. package/src/services/codeBlock.ts +361 -5
  206. package/src/services/dataSource.ts +336 -6
  207. package/src/services/dep.ts +32 -9
  208. package/src/services/editor.ts +834 -194
  209. package/src/services/history.ts +509 -49
  210. package/src/services/keybinding.ts +3 -3
  211. package/src/services/props.ts +2 -2
  212. package/src/services/ui.ts +7 -0
  213. package/src/theme/history-list-panel.scss +557 -0
  214. package/src/theme/page-bar.scss +0 -4
  215. package/src/theme/props-panel.scss +3 -3
  216. package/src/theme/style-setter/border.scss +4 -1
  217. package/src/theme/theme.scss +1 -0
  218. package/src/type.ts +613 -31
  219. package/src/utils/code-block.ts +150 -0
  220. package/src/utils/content-menu.ts +25 -13
  221. package/src/utils/data-source/index.ts +2 -0
  222. package/src/utils/dep/worker.ts +5 -3
  223. package/src/utils/editor.ts +166 -60
  224. package/src/utils/history.ts +286 -0
  225. package/src/utils/index.ts +3 -0
  226. package/src/utils/indexed-db.ts +122 -0
  227. package/src/utils/undo-redo.ts +111 -0
  228. package/types/index.d.ts +3197 -896
  229. package/dist/es/utils/compose.js +0 -44
  230. package/dist/es/utils/editor-history.js +0 -100
  231. package/src/utils/compose.ts +0 -52
  232. package/src/utils/editor-history.ts +0 -138
@@ -17,81 +17,287 @@
17
17
  */
18
18
 
19
19
  import { reactive } from 'vue';
20
+ import type { Writable } from 'type-fest';
20
21
 
21
- import type { MPage, MPageFragment } from '@tmagic/core';
22
+ import type { Id } from '@tmagic/core';
23
+ import { guid } from '@tmagic/utils';
22
24
 
23
- import type { HistoryState, StepValue } from '@editor/type';
25
+ import type {
26
+ BaseStepValue,
27
+ CodeBlockStepValue,
28
+ DataSourceStepValue,
29
+ HistoryEvents,
30
+ HistoryGroup,
31
+ HistoryOpSource,
32
+ HistoryPersistOptions,
33
+ HistoryState,
34
+ HistoryStepEntry,
35
+ HistoryStepType,
36
+ PersistedHistoryState,
37
+ StepValue,
38
+ SyncHookPlugin,
39
+ } from '@editor/type';
40
+ import { getEditorConfig } from '@editor/utils/config';
41
+ import {
42
+ deserializeStacks,
43
+ getOrCreateStack,
44
+ markStackSaved,
45
+ mergeSteps,
46
+ serializeStacks,
47
+ undoFloor,
48
+ } from '@editor/utils/history';
49
+ import { idbGet, idbSet } from '@editor/utils/indexed-db';
24
50
  import { UndoRedo } from '@editor/utils/undo-redo';
25
51
 
26
52
  import BaseService from './BaseService';
53
+ import editorService from './editor';
54
+
55
+ const canUsePluginMethods = {
56
+ sync: ['push', 'undo', 'redo'] as const,
57
+ };
58
+
59
+ /** 各内置历史类型的默认展示名称(用于历史面板 tab / 分组标题等)。扩展类型可通过 registerStepType / setStepName 登记。 */
60
+ const DEFAULT_STEP_NAMES: Record<string, string> = {
61
+ page: '页面',
62
+ codeBlock: '代码块',
63
+ dataSource: '数据源',
64
+ };
65
+
66
+ /** 各历史类型对应的分组 `kind`(展示用):page→page,codeBlock→code-block,dataSource→data-source。扩展类型缺省回退到 stepType 本身。 */
67
+ const STEP_TYPE_KIND: Record<string, string> = {
68
+ page: 'page',
69
+ codeBlock: 'code-block',
70
+ dataSource: 'data-source',
71
+ };
72
+
73
+ type SyncMethodName = Writable<(typeof canUsePluginMethods)['sync']>;
74
+
75
+ /** 历史记录持久化快照的默认存储位置与结构版本。 */
76
+ const DEFAULT_DB_NAME = 'tmagic-editor';
77
+ const DEFAULT_STORE_NAME = 'history';
78
+ const DEFAULT_KEY: IDBValidKey = 'default';
79
+ // 仅把每条 step 的 diff 序列化成字符串,其余字段交给 IndexedDB 结构化克隆原生存储(见 saveToIndexedDB);
80
+ // 全部历史栈统一收敛到 steps 字段(见 HistorySteps)。
81
+ const PERSIST_VERSION = 3;
27
82
 
28
83
  class History extends BaseService {
29
84
  public state = reactive<HistoryState>({
30
- pageSteps: {},
31
- pageId: undefined,
32
- canRedo: false,
33
- canUndo: false,
85
+ steps: {
86
+ page: {},
87
+ codeBlock: {},
88
+ dataSource: {},
89
+ },
90
+ stepNames: { ...DEFAULT_STEP_NAMES },
34
91
  });
35
92
 
36
93
  constructor() {
37
- super([]);
94
+ super([...canUsePluginMethods.sync.map((methodName) => ({ name: methodName, isAsync: false }))]);
95
+ }
96
+
97
+ /**
98
+ * 注册一个扩展历史类型,使其可与内置 `page` / `codeBlock` / `dataSource` 一样走统一的
99
+ * {@link push} / {@link undo} / {@link redo}(按 id 分栈、独立 undo/redo)。
100
+ *
101
+ * @param stepType 自定义历史类型标识(勿与内置类型重名)
102
+ * @param options.event push/undo/redo 后派发的事件名,缺省为 `${stepType}-history-change`
103
+ * @param options.name 历史面板中的展示名称(tab / 分组标题等),缺省回退到 stepType 本身
104
+ */
105
+ public registerStepType(stepType: string, options: { event?: string; name?: string } = {}): void {
106
+ this.getStepBucket(stepType);
107
+ if (options.name !== undefined) this.state.stepNames[stepType] = options.name;
108
+ }
38
109
 
39
- this.on('change', this.setCanUndoRedo);
110
+ /**
111
+ * 读取指定历史类型的展示名称(用于历史面板 tab / 分组标题等)。
112
+ * 未登记名称时回退到 stepType 本身。
113
+ */
114
+ public getStepName(stepType: HistoryStepType): string {
115
+ return this.state.stepNames[stepType] ?? `${stepType}`;
116
+ }
117
+
118
+ /**
119
+ * 设置指定历史类型的展示名称(用于历史面板 tab / 分组标题等)。
120
+ * 内置 `page` / `codeBlock` / `dataSource` 也可在此覆盖默认中文名。
121
+ */
122
+ public setStepName(stepType: HistoryStepType, name: string): void {
123
+ this.state.stepNames[stepType] = name;
40
124
  }
41
125
 
42
126
  public reset() {
43
- this.state.pageSteps = {};
44
- this.resetPage();
127
+ this.clearAllSteps();
45
128
  }
46
129
 
47
- public resetPage() {
48
- this.state.pageId = undefined;
49
- this.state.canRedo = false;
50
- this.state.canUndo = false;
130
+ public resetState(): void {
131
+ this.clearAllSteps();
51
132
  }
52
133
 
53
- public changePage(page: MPage | MPageFragment): void {
54
- if (!page) return;
134
+ /**
135
+ * 为指定历史栈(默认 `page`,也适用于 codeBlock / dataSource / 扩展类型)种入一条「初始基线」记录。
136
+ *
137
+ * 该记录是一条 `opType: 'initial'` 的 step,作为对应栈 **index 0 的固定底线**:
138
+ * - 它是一条真实入栈的 step(随栈一起持久化),但被钉为撤销/回滚的下限——cursor 永不低于它,
139
+ * 因此不会被 undo / goto / revert 触达(详见 {@link undo} / {@link undoFloor});
140
+ * - 历史面板把它过滤出分组列表(见 {@link getHistoryGroups}),改由底部「初始」行展示。
141
+ *
142
+ * 仅当目标栈为空时种入(保证 initial 一定位于 index 0);已存在 initial 底线时不重复种入。
143
+ *
144
+ * @param stepType 历史类型
145
+ * @param id 目标栈 id(page 为 pageId,其余类型为对应资源 id)
146
+ * @param options 基线展示信息(名称 / 描述 / 来源)
147
+ */
148
+ public setMarker(
149
+ stepType: HistoryStepType,
150
+ id: Id,
151
+ options: { name?: string; description?: string; source?: HistoryOpSource; extra?: Record<string, any> } = {},
152
+ ): StepValue | null {
153
+ if (!this.isValidStackId(id)) return null;
55
154
 
56
- this.state.pageId = page.id;
155
+ const existing = this.getMarker(stepType, id);
156
+ if (existing) return existing;
57
157
 
58
- if (!this.state.pageSteps[this.state.pageId]) {
59
- this.state.pageSteps[this.state.pageId] = new UndoRedo<StepValue>();
60
- }
158
+ const stack = getOrCreateStack(this.getStepBucket(stepType), id);
159
+ // initial 必须是 index 0;栈非空(已有真实记录、却无 initial,如旧数据)时不强行前插,优雅降级为无基线。
160
+ if (stack.getLength() > 0) return null;
61
161
 
62
- this.setCanUndoRedo();
162
+ const marker: StepValue = {
163
+ uuid: guid(),
164
+ opType: 'initial',
165
+ diff: [],
166
+ data: { name: options.name || '', id },
167
+ extra: {
168
+ ...(options.extra || {}),
169
+ ...(stepType === 'page'
170
+ ? {
171
+ selectedBefore: [],
172
+ selectedAfter: [],
173
+ modifiedNodeIds: new Map(),
174
+ }
175
+ : {}),
176
+ },
177
+ historyDescription: options.description || '未修改的初始状态',
178
+ timestamp: Date.now(),
179
+ ...(options.source ? { source: options.source } : {}),
180
+ };
181
+ stack.pushElement(marker);
182
+ this.emit('marker-change', { id, marker, stepType });
183
+ return marker;
184
+ }
63
185
 
64
- this.emit('page-change', this.state.pageSteps[this.state.pageId]);
186
+ /**
187
+ * 读取指定历史栈的初始基线 step(栈 index 0 且 `opType: 'initial'`);
188
+ * 不存在或 id 缺省时返回 undefined。
189
+ */
190
+ public getMarker(stepType: HistoryStepType, id?: Id): StepValue | undefined {
191
+ if (!this.isValidStackId(id)) return undefined;
192
+ const first = this.state.steps[stepType]?.[id]?.getElementList()[0];
193
+ return first?.opType === 'initial' ? (first as StepValue) : undefined;
65
194
  }
66
195
 
67
- public resetState(): void {
68
- this.state.pageId = undefined;
69
- this.state.pageSteps = {};
70
- this.state.canRedo = false;
71
- this.state.canUndo = false;
196
+ /**
197
+ * 把一条步骤推入指定历史栈。统一入口,所有类型(page / codeBlock / dataSource / 扩展)行为完全一致:
198
+ * `stepType` 选择目标栈类型,按 `id`(必填)选择具体栈,按需建栈后入栈,并派发对应的历史变更事件
199
+ * (`page` 为 `change`,其余见 {@link registerStepType}),回调签名统一为 `(id, step)`。
200
+ *
201
+ * 跨页 / 跨资源操作(如 `moveToContainer` 把节点搬到其它页)必须显式传入目标 id,
202
+ * 否则无法落到正确的栈。step 可由 `createStackStep` 等构造后传入。
203
+ *
204
+ * @param stepType 历史类型
205
+ * @param step 已构造好的历史记录(缺省自动补全 uuid / timestamp)
206
+ * @param id 目标栈 id(page 为 pageId,其余类型为对应资源 id),必填
207
+ */
208
+ public push(stepType: 'page', step: StepValue, id: Id): StepValue | null;
209
+ public push(stepType: 'codeBlock', step: CodeBlockStepValue, id: Id): CodeBlockStepValue | null;
210
+ public push(stepType: 'dataSource', step: DataSourceStepValue, id: Id): DataSourceStepValue | null;
211
+ public push<T extends BaseStepValue>(stepType: string, step: T, id: Id): T | null;
212
+ public push(stepType: HistoryStepType, step: BaseStepValue, id: Id): BaseStepValue | null {
213
+ if (!this.isValidStackId(id)) return null;
214
+ this.fillStepMeta(step);
215
+ getOrCreateStack(this.getStepBucket(stepType), id).pushElement(step);
216
+ this.emit('change', step, stepType, id);
217
+ return step;
72
218
  }
73
219
 
74
- public push(state: StepValue): StepValue | null {
75
- const undoRedo = this.getUndoRedo();
220
+ /**
221
+ * 撤销指定历史栈的最近一次变更。统一入口,所有类型行为一致:
222
+ * 按 `id`(必填)+ `stepType` 定位栈,不会越过 index 0 的 initial 基线(所有类型同等适用),
223
+ * 仅在确有可撤销 step 时派发对应的历史变更事件(`page` 为 `change`,回调签名 `(id, step)`)。
224
+ *
225
+ * @param stepType 历史类型
226
+ * @param id 目标栈 id(page 为 pageId,其余类型为对应资源 id),必填
227
+ */
228
+ public undo(stepType: 'page', id: Id): StepValue | null;
229
+ public undo(stepType: 'codeBlock', id: Id): CodeBlockStepValue | null;
230
+ public undo(stepType: 'dataSource', id: Id): DataSourceStepValue | null;
231
+ public undo<T extends BaseStepValue>(stepType: string, id: Id): T | null;
232
+ public undo(stepType: HistoryStepType, id: Id): BaseStepValue | null {
233
+ if (!this.isValidStackId(id)) return null;
234
+ const undoRedo = this.state.steps[stepType]?.[id];
76
235
  if (!undoRedo) return null;
77
- undoRedo.pushElement(state);
78
- this.emit('change', state);
79
- return state;
236
+ // 不允许撤销越过初始基线(index 0 的 initial step);无基线时 floor 为 0。
237
+ if (undoRedo.getCursor() <= undoFloor(undoRedo)) return null;
238
+ const step = undoRedo.undo();
239
+ if (step) this.emit('change', step, stepType, id);
240
+ return step;
80
241
  }
81
242
 
82
- public undo(): StepValue | null {
83
- const undoRedo = this.getUndoRedo();
243
+ /**
244
+ * 重做指定历史栈的下一次变更。语义与 {@link undo} 对称,详见其说明。
245
+ *
246
+ * @param stepType 历史类型
247
+ * @param id 目标栈 id(page 为 pageId,其余类型为对应资源 id),必填
248
+ */
249
+ public redo(stepType: 'page', id: Id): StepValue | null;
250
+ public redo(stepType: 'codeBlock', id: Id): CodeBlockStepValue | null;
251
+ public redo(stepType: 'dataSource', id: Id): DataSourceStepValue | null;
252
+ public redo<T extends BaseStepValue>(stepType: string, id: Id): T | null;
253
+ public redo(stepType: HistoryStepType, id: Id): BaseStepValue | null {
254
+ if (!this.isValidStackId(id)) return null;
255
+ const undoRedo = this.state.steps[stepType]?.[id];
84
256
  if (!undoRedo) return null;
85
- const state = undoRedo.undo();
86
- this.emit('change', state);
87
- return state;
257
+ const step = undoRedo.redo();
258
+ if (step) this.emit('change', step, stepType, id);
259
+ return step;
88
260
  }
89
261
 
90
- public redo(): StepValue | null {
91
- const undoRedo = this.getUndoRedo();
92
- if (!undoRedo) return null;
93
- const state = undoRedo.redo();
94
- this.emit('change', state);
262
+ /**
263
+ * 是否可对指定历史栈撤销(cursor 高于 initial 基线底线)。
264
+ * @param stepType 历史类型
265
+ * @param id 目标栈 id;缺省 / 无效时返回 false
266
+ */
267
+ public canUndo(stepType: HistoryStepType, id?: Id): boolean {
268
+ if (!this.isValidStackId(id)) return false;
269
+ const undoRedo = this.state.steps[stepType]?.[id];
270
+ return undoRedo ? undoRedo.getCursor() > undoFloor(undoRedo) : false;
271
+ }
272
+
273
+ /**
274
+ * 是否可对指定历史栈重做。
275
+ * @param stepType 历史类型
276
+ * @param id 目标栈 id;缺省 / 无效时返回 false
277
+ */
278
+ public canRedo(stepType: HistoryStepType, id?: Id): boolean {
279
+ if (!this.isValidStackId(id)) return false;
280
+ return this.state.steps[stepType]?.[id]?.canRedo() ?? false;
281
+ }
282
+
283
+ /** 读取指定页面历史栈当前游标所在的 step(cursor - 1);无则返回 null。 */
284
+ public getCurrentPageStep(pageId: Id): StepValue | null {
285
+ if (!this.isValidStackId(pageId)) return null;
286
+ return this.state.steps.page[pageId]?.getCurrentElement() ?? null;
287
+ }
288
+
289
+ /**
290
+ * 用 `state` 替换指定页面栈当前游标所在的 step(并丢弃其后的重做尾部),游标不变。
291
+ * 用于「连续 set root 记录合并」等就地替换最新一条的场景;替换成功后派发 `change`。
292
+ */
293
+ public replaceCurrentStep(stepType: HistoryStepType, state: StepValue, id: Id): StepValue | null {
294
+ if (!this.isValidStackId(id)) return null;
295
+ const undoRedo = getOrCreateStack(this.getStepBucket(stepType), id);
296
+
297
+ this.fillStepMeta(state);
298
+
299
+ if (!undoRedo.replaceCurrentElement(state)) return null;
300
+ this.emit('change', state, stepType, id);
95
301
  return state;
96
302
  }
97
303
 
@@ -101,15 +307,269 @@ class History extends BaseService {
101
307
  this.removeAllPlugins();
102
308
  }
103
309
 
104
- private getUndoRedo() {
105
- if (!this.state.pageId) return null;
106
- return this.state.pageSteps[this.state.pageId];
310
+ /**
311
+ * 清空历史记录栈。统一入口,所有类型(page / codeBlock / dataSource / 扩展)行为一致:
312
+ * - 传入 `id`:仅清空 `stepType` 下该 id 对应的栈;
313
+ * - 缺省 `id`:清空 `stepType` 下的全部栈。
314
+ *
315
+ * 仅删除撤销/重做记录,不会改动 DSL / 代码块 / 数据源本身。清空时会**保留各栈原有的
316
+ * initial 基线**(文案 / 来源),无基线时清空成空栈。清空后派发 `clear`(签名 `(id, stepType)`)。
317
+ *
318
+ * @param stepType 历史类型
319
+ * @param id 目标栈 id;缺省表示清空该类型下全部栈
320
+ */
321
+ public clear(stepType: HistoryStepType, id?: Id): void {
322
+ const bucket = this.state.steps[stepType];
323
+ if (!bucket) return;
324
+
325
+ if (this.isValidStackId(id)) {
326
+ this.clearStack(stepType, id);
327
+ } else if (id === undefined) {
328
+ Object.keys(bucket).forEach((stackId) => this.clearStack(stepType, stackId as Id));
329
+ } else {
330
+ return;
331
+ }
332
+ this.emit('clear', { id: id as Id, stepType });
333
+ }
334
+
335
+ /**
336
+ * 标记历史记录为「已保存」(把对应栈当前游标所在的记录标为 `saved`)。统一入口:
337
+ * - 缺省 `id`:标记**全部类型、全部栈**(整份 DSL 落库场景),派发 `mark-saved` 且 `kind: 'all'`;
338
+ * - 传入 `id`:仅标记 `stepType` 下该 id 对应的栈,派发 `mark-saved` 且 `kind` 为 `stepType`。
339
+ *
340
+ * 同一栈内任意时刻最多只有一条记录为 `saved`;从 IndexedDB 恢复时游标会被定位到最近一条已保存记录之后。
341
+ *
342
+ * @param stepType 历史类型
343
+ * @param id 目标栈 id;缺省表示标记全部类型 / 全部栈
344
+ */
345
+ public markSaved(stepType: HistoryStepType, id?: Id): void {
346
+ if (id === undefined) {
347
+ Object.values(this.state.steps).forEach((bucket) => Object.values(bucket).forEach(markStackSaved));
348
+ this.emit('mark-saved', { kind: 'all' });
349
+ return;
350
+ }
351
+ if (!this.isValidStackId(id)) return;
352
+ markStackSaved(this.state.steps[stepType]?.[id]);
353
+ this.emit('mark-saved', { kind: stepType, id });
354
+ }
355
+
356
+ /**
357
+ * 把当前内存中的全部历史栈(页面 / 代码块 / 数据源 / 扩展类型)序列化后写入本地 IndexedDB。
358
+ *
359
+ * - 每个 UndoRedo 栈连同其游标、容量一并保存,恢复后可继续 undo/redo;
360
+ * - `key` 用于区分不同活动页 / 项目(同一 store 下可保存多份快照),缺省为 `default`;
361
+ * - 返回写入成功的快照对象,便于调用方记录 savedAt 等信息;
362
+ * - 不支持 IndexedDB 的环境(如 SSR)会 reject。
363
+ */
364
+ public async saveToIndexedDB(options: HistoryPersistOptions = {}): Promise<PersistedHistoryState> {
365
+ const { dbName = DEFAULT_DB_NAME, storeName = DEFAULT_STORE_NAME, key = DEFAULT_KEY, appId } = options;
366
+
367
+ // serializeStacks 会在序列化各栈时只把每条 step 的 diff(可能含函数)序列化成字符串,其余字段原样保留,
368
+ // 因此整份快照可直接按对象写入 IndexedDB(结构化克隆),避免序列化整份快照的开销;读取时再用 parseDSL 还原 diff。
369
+ const steps: PersistedHistoryState['steps'] = { page: {}, codeBlock: {}, dataSource: {} };
370
+ Object.entries(this.state.steps).forEach(([stepType, bucket]) => {
371
+ steps[stepType] = serializeStacks(bucket);
372
+ });
373
+
374
+ const snapshot: PersistedHistoryState = {
375
+ version: PERSIST_VERSION,
376
+ steps,
377
+ savedAt: Date.now(),
378
+ };
379
+
380
+ await idbSet(this.resolveDbName(dbName, appId), storeName, key, snapshot);
381
+ this.emit('save-to-indexed-db', snapshot);
382
+ return snapshot;
383
+ }
384
+
385
+ /**
386
+ * 从本地 IndexedDB 读取此前保存的历史快照并重建全部撤销/重做栈。
387
+ *
388
+ * - 读取到的每个栈都会经 {@link UndoRedo.fromSerialized} 还原(含游标),随后可直接 undo/redo;
389
+ * - 会整体覆盖当前内存中的历史状态(活动页由 editorService 维护,不在此恢复);
390
+ * - 找不到对应记录时返回 null,且不改动当前状态;
391
+ * - 不支持 IndexedDB 的环境(如 SSR)会 reject。
392
+ */
393
+ public async restoreFromIndexedDB(options: HistoryPersistOptions = {}): Promise<PersistedHistoryState | null> {
394
+ const { dbName = DEFAULT_DB_NAME, storeName = DEFAULT_STORE_NAME, key = DEFAULT_KEY, appId } = options;
395
+
396
+ const snapshot = await idbGet<PersistedHistoryState>(this.resolveDbName(dbName, appId), storeName, key);
397
+ if (!snapshot) return null;
398
+
399
+ // 各 step 的 diff 以序列化字符串存储(含函数),由 deserializeStacks 逐条用 parseDSL 还原。
400
+ const parseDSL = getEditorConfig('parseDSL');
401
+
402
+ const steps: HistoryState['steps'] = { page: {}, codeBlock: {}, dataSource: {} };
403
+ Object.entries(snapshot.steps).forEach(([stepType, bucket]) => {
404
+ steps[stepType] = deserializeStacks(bucket, parseDSL);
405
+ });
406
+ this.state.steps = steps;
407
+ // initial 基线作为各栈 index 0 的 step 随 steps 一并还原,无需单独恢复;活动页由 editorService 维护。
408
+ this.emit('restore-from-indexed-db', snapshot);
409
+ return snapshot;
410
+ }
411
+
412
+ /**
413
+ * 取出指定历史类型(页面 / 代码块 / 数据源 / 扩展类型)某个栈的平铺步骤列表(含 applied 标记)。
414
+ * 统一入口,替代旧的 `getPageStepList` / `getCodeBlockStepList` / `getDataSourceStepList`。
415
+ *
416
+ * 列表按时间正序,最早一步在最前面;`applied` 表示该步骤处于栈游标之前(已应用)。
417
+ * 供 revert / goto 等按 index 索引步骤的场景使用。通常 UI 应使用
418
+ * {@link getHistoryGroups} 取已合并分组的版本;本方法仅为兼容/调试保留。
419
+ *
420
+ * @param stepType 历史类型
421
+ * @param id 目标栈 id(page 为 pageId,其余类型为对应资源 id);缺省 / 无效时返回空数组
422
+ */
423
+ public getStepList(stepType: 'page', id?: Id): HistoryStepEntry<StepValue>[];
424
+ public getStepList(stepType: 'codeBlock', id: Id): HistoryStepEntry<CodeBlockStepValue>[];
425
+ public getStepList(stepType: 'dataSource', id: Id): HistoryStepEntry<DataSourceStepValue>[];
426
+ public getStepList<T extends BaseStepValue>(stepType: HistoryStepType, id?: Id): HistoryStepEntry<T>[];
427
+ public getStepList(stepType: HistoryStepType, id?: Id): HistoryStepEntry<any>[] {
428
+ if (!this.isValidStackId(id)) return [];
429
+ const undoRedo = this.state.steps[stepType]?.[id];
430
+ if (!undoRedo) return [];
431
+ const list = undoRedo.getElementList();
432
+ const cursor = undoRedo.getCursor();
433
+ return list.map((step, index) => ({ step, index, applied: index < cursor }));
434
+ }
435
+
436
+ /**
437
+ * 取出指定历史类型的历史分组(页面 / 代码块 / 数据源 / 扩展类型统一入口)。
438
+ *
439
+ * 把目标栈的步骤列表按"目标"做相邻合并(连续修改同一目标的 update 合并为一组,组内可展开查看每步;
440
+ * add / remove / 多实体 update 始终独立成组),并过滤掉 index 0 的 initial 基线(底部「初始」行由
441
+ * {@link getMarker} 驱动)。各类型行为完全一致,仅 `kind` 与 step 快照类型不同。
442
+ *
443
+ * 作用域:
444
+ * - 传入 `id`:仅取该 id 对应的单个栈(页面历史按活动页查看,传入 pageId);
445
+ * - 缺省 `id`:遍历该类型下全部栈并汇总(代码块 / 数据源按全部资源分桶展示)。
446
+ *
447
+ * @param stepType 历史类型,缺省 `page`
448
+ * @param id 目标栈 id;缺省表示遍历该类型下全部栈
449
+ */
450
+ public getHistoryGroups(stepType: 'page', id?: Id): HistoryGroup<StepValue>[];
451
+ public getHistoryGroups(stepType: 'codeBlock', id?: Id): HistoryGroup<CodeBlockStepValue>[];
452
+ public getHistoryGroups(stepType: 'dataSource', id?: Id): HistoryGroup<DataSourceStepValue>[];
453
+ public getHistoryGroups<T extends BaseStepValue>(stepType: HistoryStepType, id?: Id): HistoryGroup<T>[];
454
+ public getHistoryGroups(stepType: HistoryStepType, id?: Id): HistoryGroup[] {
455
+ const bucket = this.state.steps[stepType];
456
+ if (!bucket) return [];
457
+ const kind = STEP_TYPE_KIND[stepType] ?? stepType;
458
+ const collect = (stackId: Id): HistoryGroup[] => {
459
+ const undoRedo = bucket[stackId];
460
+ if (!undoRedo) return [];
461
+ const list = undoRedo.getElementList();
462
+ if (!list.length) return [];
463
+ // initial 基线(index 0)不作为普通操作组展示,过滤掉;其余真实 step 的 index 保持不变,
464
+ // 以便面板 goto(index+1) / revert(index) 仍直接对应栈内位置。
465
+ return mergeSteps(kind, stackId, list, undoRedo.getCursor()).filter((group) => group.opType !== 'initial');
466
+ };
467
+ if (this.isValidStackId(id)) return collect(id);
468
+ return Object.keys(bucket).flatMap((stackId) => collect(stackId as Id));
469
+ }
470
+
471
+ /**
472
+ * 读取指定历史栈的当前游标(已应用步骤数量)。统一入口,替代旧的
473
+ * `getPageCursor` / `getCodeBlockCursor` / `getDataSourceCursor`。
474
+ * - `id` 缺省或非法、或对应栈不存在时返回 0;
475
+ * - `stepType` 支持 `page` / `codeBlock` / `dataSource` / 扩展类型。
476
+ */
477
+ public getCursor(stepType: HistoryStepType, id?: Id): number {
478
+ if (!this.isValidStackId(id)) return 0;
479
+ return this.state.steps[stepType]?.[id]?.getCursor() ?? 0;
480
+ }
481
+
482
+ /**
483
+ * 按历史记录 uuid 在指定历史类型的栈中查找其所属 id 与索引,统一入口,替代旧的
484
+ * `getPageStepIndexByUuid` / `findCodeBlockStepLocationByUuid` / `findDataSourceStepLocationByUuid`。
485
+ *
486
+ * - 传入 `id`:仅在该 id 对应的单个栈中查找(如页面历史按活动页查看,传入 pageId);
487
+ * - 缺省 `id`:遍历该类型下全部栈查找(代码块 / 数据源等按全部资源分桶的场景)。
488
+ *
489
+ * 找不到时返回 null。供「按 uuid 回滚」等需要把 uuid 映射回 (id, index) 的场景使用。
490
+ *
491
+ * @param stepType 历史类型
492
+ * @param uuid 目标历史记录的 uuid
493
+ * @param id 目标栈 id;缺省表示遍历该类型下全部栈
494
+ */
495
+ public findStepLocationByUuid(stepType: HistoryStepType, uuid: string, id?: Id): { id: Id; index: number } | null {
496
+ if (!uuid) return null;
497
+ const bucket = this.state.steps[stepType];
498
+ if (!bucket) return null;
499
+
500
+ if (this.isValidStackId(id)) {
501
+ const index = this.getStepList(stepType, id).findIndex((entry) => entry.step.uuid === uuid);
502
+ return index >= 0 ? { id, index } : null;
503
+ }
504
+
505
+ for (const stackId of Object.keys(bucket)) {
506
+ const index = this.getStepList(stepType, stackId as Id).findIndex((entry) => entry.step.uuid === uuid);
507
+ if (index >= 0) return { id: stackId as Id, index };
508
+ }
509
+ return null;
510
+ }
511
+
512
+ public usePlugin(options: SyncHookPlugin<SyncMethodName, History>): void {
513
+ super.usePlugin(options);
514
+ }
515
+
516
+ public emit<Name extends keyof HistoryEvents, Param extends HistoryEvents[Name]>(eventName: Name, ...args: Param) {
517
+ return super.emit(eventName, ...args);
518
+ }
519
+
520
+ /**
521
+ * 取出指定历史类型的栈桶(`Record<id, UndoRedo>`);不存在时按需创建空桶,
522
+ * 从而支持通过 {@link registerStepType} 或首次 push 动态扩展历史类型。
523
+ */
524
+ private getStepBucket(stepType: string): Record<Id, UndoRedo<any>> {
525
+ if (!this.state.steps[stepType]) {
526
+ this.state.steps[stepType] = {};
527
+ }
528
+ return this.state.steps[stepType];
529
+ }
530
+
531
+ /**
532
+ * 清空单个历史栈并按需重建其 initial 基线(保留原基线的文案 / 来源)。
533
+ */
534
+ private clearStack(stepType: HistoryStepType, id: Id): void {
535
+ // 保留原 initial 基线的文案 / 来源(仅清空其后的真实操作记录),无基线时清空成空栈。
536
+ const marker = this.getMarker(stepType, id);
537
+ this.getStepBucket(stepType)[id] = new UndoRedo<any>();
538
+ if (marker) {
539
+ this.setMarker(stepType, id, {
540
+ name: marker.data?.name,
541
+ description: marker.historyDescription,
542
+ source: marker.source,
543
+ });
544
+ }
545
+ }
546
+
547
+ /** 入栈前补全 step 的 uuid / timestamp(调用方未指定时)。 */
548
+ private fillStepMeta(step: BaseStepValue): void {
549
+ if (step.uuid === undefined) step.uuid = guid();
550
+ if (step.timestamp === undefined) step.timestamp = Date.now();
551
+ }
552
+
553
+ /** 校验「按 id 分栈」类型(codeBlock / dataSource / 扩展)的 id 是否有效。 */
554
+ private isValidStackId(id?: Id): id is Id {
555
+ return id !== undefined && id !== null && `${id}` !== '';
556
+ }
557
+
558
+ /** 清空全部历史栈内容(保留已注册的类型键,使扩展类型在 reset 后仍可用)。 */
559
+ private clearAllSteps(): void {
560
+ Object.keys(this.state.steps).forEach((stepType) => {
561
+ this.state.steps[stepType] = {};
562
+ });
107
563
  }
108
564
 
109
- private setCanUndoRedo(): void {
110
- const undoRedo = this.getUndoRedo();
111
- this.state.canRedo = undoRedo?.canRedo() || false;
112
- this.state.canUndo = undoRedo?.canUndo() || false;
565
+ /**
566
+ * 把基础 dbName 与当前 DSL(root app)的 id 拼成最终库名,实现不同应用历史隔离。
567
+ * 取不到 app id(如尚未加载 DSL)时退回基础 dbName。
568
+ */
569
+ private resolveDbName(dbName: string, appId?: Id): string {
570
+ // 优先用显式传入的 appId(「先恢复再 set root」时 root 尚未就绪);否则回退到当前 root.id。
571
+ const resolvedAppId = appId ?? editorService.get('root')?.id;
572
+ return resolvedAppId ? `${dbName}-${resolvedAppId}` : dbName;
113
573
  }
114
574
  }
115
575
 
@@ -20,7 +20,7 @@ class Keybinding extends BaseService {
20
20
  const nodes = editorService.get('nodes');
21
21
 
22
22
  if (!nodes || isPage(nodes[0]) || isPageFragment(nodes[0])) return;
23
- editorService.remove(nodes);
23
+ editorService.remove(nodes, { historySource: 'shortcut' });
24
24
  },
25
25
  [KeyBindingCommand.COPY_NODE]: () => {
26
26
  const nodes = editorService.get('nodes');
@@ -31,11 +31,11 @@ class Keybinding extends BaseService {
31
31
 
32
32
  if (!nodes || isPage(nodes[0]) || isPageFragment(nodes[0])) return;
33
33
  editorService.copy(nodes);
34
- editorService.remove(nodes);
34
+ editorService.remove(nodes, { historySource: 'shortcut' });
35
35
  },
36
36
  [KeyBindingCommand.PASTE_NODE]: () => {
37
37
  const nodes = editorService.get('nodes');
38
- nodes && editorService.paste({ offsetX: 10, offsetY: 10 });
38
+ nodes && editorService.paste({ offsetX: 10, offsetY: 10 }, undefined, { historySource: 'shortcut' });
39
39
  },
40
40
  [KeyBindingCommand.UNDO]: () => {
41
41
  editorService.undo();
@@ -97,9 +97,9 @@ class Props extends BaseService {
97
97
  return this.state.propsConfigMap;
98
98
  }
99
99
 
100
- public async fillConfig(config: FormConfig, labelWidth?: string) {
100
+ public async fillConfig(config: FormConfig, labelWidth = '80px') {
101
101
  return fillConfig(config, {
102
- labelWidth: typeof labelWidth !== 'function' ? labelWidth : '80px',
102
+ labelWidth,
103
103
  disabledDataSource: this.getDisabledDataSource(),
104
104
  disabledCodeBlock: this.getDisabledCodeBlock(),
105
105
  });
@@ -62,6 +62,7 @@ const state = shallowReactive<UiState>({
62
62
  showPageListButton: true,
63
63
  hideSlideBar: false,
64
64
  sideBarItems: [],
65
+ sideBarActiveTabName: '',
65
66
  navMenuRect: {
66
67
  left: 0,
67
68
  top: 0,
@@ -104,7 +105,13 @@ class Ui extends BaseService {
104
105
  mask?.showRule(value as unknown as boolean);
105
106
  }
106
107
 
108
+ const preValue = state[name];
109
+
107
110
  state[name] = value;
111
+
112
+ if (preValue !== value) {
113
+ this.emit('state-change', name, value, preValue);
114
+ }
108
115
  }
109
116
 
110
117
  public get<K extends keyof UiState>(name: K) {