@tmagic/editor 1.8.0-beta.0 → 1.8.0-beta.2

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 (138) hide show
  1. package/dist/es/Editor.vue_vue_type_script_setup_true_lang.js +11 -2
  2. package/dist/es/components/CodeBlockEditor.vue_vue_type_script_setup_true_lang.js +18 -110
  3. package/dist/es/components/CodeParams.vue_vue_type_script_setup_true_lang.js +6 -0
  4. package/dist/es/components/CompareForm.js +5 -0
  5. package/dist/es/components/CompareForm.vue_vue_type_script_setup_true_lang.js +167 -0
  6. package/dist/es/fields/Code.vue_vue_type_script_setup_true_lang.js +19 -3
  7. package/dist/es/fields/CodeSelect.vue_vue_type_script_setup_true_lang.js +18 -2
  8. package/dist/es/fields/CodeSelectCol.vue_vue_type_script_setup_true_lang.js +37 -6
  9. package/dist/es/fields/DataSourceFieldSelect/FieldSelect.vue_vue_type_script_setup_true_lang.js +3 -1
  10. package/dist/es/fields/DataSourceFieldSelect/Index.vue_vue_type_script_setup_true_lang.js +12 -3
  11. package/dist/es/fields/DataSourceFields.vue_vue_type_script_setup_true_lang.js +14 -6
  12. package/dist/es/fields/DataSourceMethodSelect.vue_vue_type_script_setup_true_name_true_lang.js +3 -1
  13. package/dist/es/fields/DataSourceMethods.vue_vue_type_script_setup_true_lang.js +15 -7
  14. package/dist/es/fields/DataSourceMocks.vue_vue_type_script_setup_true_lang.js +14 -6
  15. package/dist/es/fields/DataSourceSelect.vue_vue_type_script_setup_true_lang.js +3 -1
  16. package/dist/es/fields/EventSelect.vue_vue_type_script_setup_true_lang.js +58 -11
  17. package/dist/es/fields/KeyValue.vue_vue_type_script_setup_true_lang.js +11 -6
  18. package/dist/es/fields/StyleSetter/Index.vue_vue_type_script_setup_true_lang.js +8 -2
  19. package/dist/es/fields/StyleSetter/components/Border.vue_vue_type_script_setup_true_lang.js +10 -2
  20. package/dist/es/fields/StyleSetter/components/Box.vue_vue_type_script_setup_true_lang.js +3 -1
  21. package/dist/es/fields/StyleSetter/pro/Background.vue_vue_type_script_setup_true_lang.js +10 -2
  22. package/dist/es/fields/StyleSetter/pro/Border.vue_vue_type_script_setup_true_lang.js +16 -3
  23. package/dist/es/fields/StyleSetter/pro/Font.vue_vue_type_script_setup_true_lang.js +10 -2
  24. package/dist/es/fields/StyleSetter/pro/Layout.vue_vue_type_script_setup_true_lang.js +14 -2
  25. package/dist/es/fields/StyleSetter/pro/Position.vue_vue_type_script_setup_true_lang.js +10 -2
  26. package/dist/es/fields/StyleSetter/pro/Transform.vue_vue_type_script_setup_true_lang.js +10 -2
  27. package/dist/es/fields/UISelect.vue_vue_type_script_setup_true_lang.js +8 -5
  28. package/dist/es/hooks/use-code-block-edit.js +2 -2
  29. package/dist/es/hooks/use-stage.js +15 -7
  30. package/dist/es/index.js +3 -1
  31. package/dist/es/initService.js +6 -6
  32. package/dist/es/layouts/CodeEditor.vue_vue_type_script_setup_true_lang.js +4 -0
  33. package/dist/es/layouts/Framework.vue_vue_type_script_setup_true_lang.js +12 -7
  34. package/dist/es/layouts/NavMenu.vue_vue_type_script_setup_true_lang.js +8 -0
  35. package/dist/es/layouts/history-list/Bucket.js +5 -0
  36. package/dist/es/layouts/history-list/Bucket.vue_vue_type_script_setup_true_lang.js +84 -0
  37. package/dist/es/layouts/history-list/CodeBlockTab.js +5 -0
  38. package/dist/es/layouts/history-list/CodeBlockTab.vue_vue_type_script_setup_true_lang.js +62 -0
  39. package/dist/es/layouts/history-list/DataSourceTab.js +5 -0
  40. package/dist/es/layouts/history-list/DataSourceTab.vue_vue_type_script_setup_true_lang.js +62 -0
  41. package/dist/es/layouts/history-list/GroupRow.js +5 -0
  42. package/dist/es/layouts/history-list/GroupRow.vue_vue_type_script_setup_true_lang.js +180 -0
  43. package/dist/es/layouts/history-list/HistoryDiffDialog.js +5 -0
  44. package/dist/es/layouts/history-list/HistoryDiffDialog.vue_vue_type_script_setup_true_lang.js +195 -0
  45. package/dist/es/layouts/history-list/HistoryListPanel.js +5 -0
  46. package/dist/es/layouts/history-list/HistoryListPanel.vue_vue_type_script_setup_true_lang.js +270 -0
  47. package/dist/es/layouts/history-list/InitialRow.js +5 -0
  48. package/dist/es/layouts/history-list/InitialRow.vue_vue_type_script_setup_true_lang.js +48 -0
  49. package/dist/es/layouts/history-list/PageTab.js +5 -0
  50. package/dist/es/layouts/history-list/PageTab.vue_vue_type_script_setup_true_lang.js +99 -0
  51. package/dist/es/layouts/history-list/composables.js +154 -0
  52. package/dist/es/layouts/sidebar/layer/use-node-status.js +38 -4
  53. package/dist/es/services/BaseService.js +31 -68
  54. package/dist/es/services/codeBlock.js +205 -8
  55. package/dist/es/services/componentList.js +2 -2
  56. package/dist/es/services/dataSource.js +217 -8
  57. package/dist/es/services/dep.js +21 -9
  58. package/dist/es/services/editor.js +350 -160
  59. package/dist/es/services/events.js +2 -2
  60. package/dist/es/services/history.js +407 -12
  61. package/dist/es/services/keybinding.js +2 -2
  62. package/dist/es/services/props.js +8 -8
  63. package/dist/es/services/stageOverlay.js +2 -2
  64. package/dist/es/services/storage.js +2 -2
  65. package/dist/es/services/ui.js +2 -2
  66. package/dist/es/style.css +347 -1
  67. package/dist/es/utils/code-block.js +122 -0
  68. package/dist/es/utils/content-menu.js +4 -5
  69. package/dist/es/utils/dep/worker.js +1 -1
  70. package/dist/es/utils/editor.js +3 -3
  71. package/dist/es/utils/undo-redo.js +20 -0
  72. package/dist/style.css +347 -1
  73. package/dist/tmagic-editor.umd.cjs +3273 -766
  74. package/package.json +7 -7
  75. package/src/Editor.vue +7 -0
  76. package/src/components/CodeBlockEditor.vue +20 -109
  77. package/src/components/CodeParams.vue +6 -0
  78. package/src/components/CompareForm.vue +225 -0
  79. package/src/editorProps.ts +2 -0
  80. package/src/fields/Code.vue +19 -3
  81. package/src/fields/CodeSelect.vue +17 -1
  82. package/src/fields/CodeSelectCol.vue +30 -2
  83. package/src/fields/DataSourceFieldSelect/FieldSelect.vue +7 -1
  84. package/src/fields/DataSourceFieldSelect/Index.vue +9 -2
  85. package/src/fields/DataSourceFields.vue +12 -3
  86. package/src/fields/DataSourceMethodSelect.vue +4 -1
  87. package/src/fields/DataSourceMethods.vue +14 -4
  88. package/src/fields/DataSourceMocks.vue +13 -3
  89. package/src/fields/DataSourceSelect.vue +4 -1
  90. package/src/fields/EventSelect.vue +57 -8
  91. package/src/fields/KeyValue.vue +17 -4
  92. package/src/fields/StyleSetter/Index.vue +6 -0
  93. package/src/fields/StyleSetter/components/Border.vue +15 -1
  94. package/src/fields/StyleSetter/components/Box.vue +2 -0
  95. package/src/fields/StyleSetter/pro/Background.vue +15 -1
  96. package/src/fields/StyleSetter/pro/Border.vue +24 -2
  97. package/src/fields/StyleSetter/pro/Font.vue +15 -1
  98. package/src/fields/StyleSetter/pro/Layout.vue +17 -1
  99. package/src/fields/StyleSetter/pro/Position.vue +15 -1
  100. package/src/fields/StyleSetter/pro/Transform.vue +15 -1
  101. package/src/fields/UISelect.vue +5 -2
  102. package/src/hooks/use-code-block-edit.ts +5 -2
  103. package/src/hooks/use-stage.ts +23 -7
  104. package/src/index.ts +1 -0
  105. package/src/layouts/CodeEditor.vue +15 -0
  106. package/src/layouts/Framework.vue +10 -5
  107. package/src/layouts/NavMenu.vue +9 -0
  108. package/src/layouts/history-list/Bucket.vue +99 -0
  109. package/src/layouts/history-list/CodeBlockTab.vue +61 -0
  110. package/src/layouts/history-list/DataSourceTab.vue +61 -0
  111. package/src/layouts/history-list/GroupRow.vue +207 -0
  112. package/src/layouts/history-list/HistoryDiffDialog.vue +212 -0
  113. package/src/layouts/history-list/HistoryListPanel.vue +271 -0
  114. package/src/layouts/history-list/InitialRow.vue +41 -0
  115. package/src/layouts/history-list/PageTab.vue +94 -0
  116. package/src/layouts/history-list/composables.ts +195 -0
  117. package/src/layouts/sidebar/layer/use-node-status.ts +64 -12
  118. package/src/services/BaseService.ts +35 -86
  119. package/src/services/codeBlock.ts +256 -5
  120. package/src/services/dataSource.ts +257 -6
  121. package/src/services/dep.ts +32 -9
  122. package/src/services/editor.ts +454 -171
  123. package/src/services/history.ts +452 -8
  124. package/src/services/props.ts +2 -2
  125. package/src/theme/history-list-panel.scss +423 -0
  126. package/src/theme/theme.scss +1 -0
  127. package/src/type.ts +179 -6
  128. package/src/utils/code-block.ts +150 -0
  129. package/src/utils/content-menu.ts +8 -5
  130. package/src/utils/dep/worker.ts +5 -3
  131. package/src/utils/editor.ts +4 -1
  132. package/src/utils/index.ts +1 -0
  133. package/src/utils/undo-redo.ts +23 -0
  134. package/types/index.d.ts +2107 -810
  135. package/dist/es/utils/compose.js +0 -44
  136. package/dist/es/utils/editor-history.js +0 -100
  137. package/src/utils/compose.ts +0 -52
  138. package/src/utils/editor-history.ts +0 -138
@@ -17,20 +17,190 @@
17
17
  */
18
18
 
19
19
  import { reactive } from 'vue';
20
+ import { cloneDeep } from 'lodash-es';
20
21
 
21
- import type { MPage, MPageFragment } from '@tmagic/core';
22
+ import type { CodeBlockContent, DataSourceSchema, Id, MPage, MPageFragment } from '@tmagic/core';
23
+ import type { ChangeRecord } from '@tmagic/form';
22
24
 
23
- import type { HistoryState, StepValue } from '@editor/type';
25
+ import type {
26
+ CodeBlockHistoryGroup,
27
+ CodeBlockStepValue,
28
+ DataSourceHistoryGroup,
29
+ DataSourceStepValue,
30
+ HistoryState,
31
+ PageHistoryGroup,
32
+ PageHistoryStepEntry,
33
+ StepValue,
34
+ } from '@editor/type';
24
35
  import { UndoRedo } from '@editor/utils/undo-redo';
25
36
 
26
37
  import BaseService from './BaseService';
27
38
 
28
39
  class History extends BaseService {
40
+ /**
41
+ * 把单个代码块栈拆成若干 group:
42
+ * - 把"新增/删除"独立成组(语义上属于一次性事件,不应与 update 合并);
43
+ * - 连续 'update' 合并到同一组,组内 steps 顺序就是发生顺序。
44
+ */
45
+ private static mergeCodeBlockSteps(
46
+ codeBlockId: Id,
47
+ list: CodeBlockStepValue[],
48
+ cursor: number,
49
+ ): CodeBlockHistoryGroup[] {
50
+ const groups: CodeBlockHistoryGroup[] = [];
51
+ let current: CodeBlockHistoryGroup | null = null;
52
+ const currentIndex = cursor - 1;
53
+ list.forEach((step, index) => {
54
+ const opType = History.detectOpType(step.oldContent, step.newContent);
55
+ const applied = index < cursor;
56
+ const isCurrent = index === currentIndex;
57
+ if (opType === 'update' && current?.opType === 'update') {
58
+ current.steps.push({ step, index, applied, isCurrent });
59
+ current.applied = applied;
60
+ if (isCurrent) current.isCurrent = true;
61
+ } else {
62
+ current = {
63
+ kind: 'code-block',
64
+ id: codeBlockId,
65
+ opType,
66
+ steps: [{ step, index, applied, isCurrent }],
67
+ applied,
68
+ isCurrent,
69
+ };
70
+ groups.push(current);
71
+ }
72
+ });
73
+ return groups;
74
+ }
75
+
76
+ private static mergeDataSourceSteps(
77
+ dataSourceId: Id,
78
+ list: DataSourceStepValue[],
79
+ cursor: number,
80
+ ): DataSourceHistoryGroup[] {
81
+ const groups: DataSourceHistoryGroup[] = [];
82
+ let current: DataSourceHistoryGroup | null = null;
83
+ const currentIndex = cursor - 1;
84
+ list.forEach((step, index) => {
85
+ const opType = History.detectOpType(step.oldSchema, step.newSchema);
86
+ const applied = index < cursor;
87
+ const isCurrent = index === currentIndex;
88
+ if (opType === 'update' && current?.opType === 'update') {
89
+ current.steps.push({ step, index, applied, isCurrent });
90
+ current.applied = applied;
91
+ if (isCurrent) current.isCurrent = true;
92
+ } else {
93
+ current = {
94
+ kind: 'data-source',
95
+ id: dataSourceId,
96
+ opType,
97
+ steps: [{ step, index, applied, isCurrent }],
98
+ applied,
99
+ isCurrent,
100
+ };
101
+ groups.push(current);
102
+ }
103
+ });
104
+ return groups;
105
+ }
106
+
107
+ /**
108
+ * 根据 old/new 是否为 null 推断 opType(与 push 时的约定一致)。
109
+ */
110
+ private static detectOpType(oldVal: unknown, newVal: unknown): 'add' | 'remove' | 'update' {
111
+ if (oldVal === null && newVal !== null) return 'add';
112
+ if (oldVal !== null && newVal === null) return 'remove';
113
+ return 'update';
114
+ }
115
+
116
+ /**
117
+ * 把页面栈拆成若干 group:
118
+ * - 单节点的 'update' 按 targetId 与相邻同 targetId 的 update 合并到一个 group;
119
+ * - 'add' / 'remove' 始终独立成组(语义上是结构变更,不应被收纳进单节点修改组);
120
+ * - 多节点 'update'(如批量改属性)也独立成组(无明确单一目标,避免误合并)。
121
+ */
122
+ private static mergePageSteps(pageId: Id, list: StepValue[], cursor: number): PageHistoryGroup[] {
123
+ const groups: PageHistoryGroup[] = [];
124
+ let current: PageHistoryGroup | null = null;
125
+ const currentIndex = cursor - 1;
126
+ list.forEach((step, index) => {
127
+ const applied = index < cursor;
128
+ const isCurrent = index === currentIndex;
129
+ const targetId = History.detectPageTargetId(step);
130
+ const targetName = History.detectPageTargetName(step);
131
+ const entry: PageHistoryStepEntry = { step, index, applied, isCurrent };
132
+
133
+ // 仅"单节点 update"参与合并;其它情形(add/remove/多节点 update)始终独立成组。
134
+ const mergeable = step.opType === 'update' && targetId !== undefined;
135
+ if (mergeable && current?.opType === 'update' && current.targetId === targetId) {
136
+ current.steps.push(entry);
137
+ current.applied = applied;
138
+ if (isCurrent) current.isCurrent = true;
139
+ // 保持目标名为最近一次的(节点重命名时也能反映)
140
+ if (targetName) current.targetName = targetName;
141
+ } else {
142
+ current = {
143
+ kind: 'page',
144
+ pageId,
145
+ opType: step.opType,
146
+ targetId: mergeable ? targetId : undefined,
147
+ targetName,
148
+ steps: [entry],
149
+ applied,
150
+ isCurrent,
151
+ };
152
+ groups.push(current);
153
+ }
154
+ });
155
+ return groups;
156
+ }
157
+
158
+ /**
159
+ * 解析 StepValue 中的"目标节点 id"用于合并:
160
+ * - 单节点 update:取唯一一项 updatedItems 的节点 id;
161
+ * - 其它情形(多节点 update / add / remove):返回 undefined,表示不参与合并。
162
+ */
163
+ private static detectPageTargetId(step: StepValue): Id | undefined {
164
+ if (step.opType !== 'update') return undefined;
165
+ const items = step.updatedItems;
166
+ if (items?.length !== 1) return undefined;
167
+ return items[0].newNode?.id ?? items[0].oldNode?.id;
168
+ }
169
+
170
+ /** 解析 StepValue 中的目标节点可读名(用于 UI 展示)。 */
171
+ private static detectPageTargetName(step: StepValue): string | undefined {
172
+ if (step.opType === 'update') {
173
+ const items = step.updatedItems;
174
+ if (items?.length === 1) {
175
+ const node = items[0].newNode || items[0].oldNode;
176
+ return (node?.name as string) || (node?.type as string) || (node?.id !== undefined ? `${node.id}` : undefined);
177
+ }
178
+ return items?.length ? `${items.length} 个节点` : undefined;
179
+ }
180
+ if (step.opType === 'add') {
181
+ if (step.nodes?.length === 1) {
182
+ const n = step.nodes[0];
183
+ return (n.name as string) || (n.type as string) || `${n.id}`;
184
+ }
185
+ return step.nodes?.length ? `${step.nodes.length} 个节点` : undefined;
186
+ }
187
+ if (step.opType === 'remove') {
188
+ if (step.removedItems?.length === 1) {
189
+ const n = step.removedItems[0].node;
190
+ return (n.name as string) || (n.type as string) || `${n.id}`;
191
+ }
192
+ return step.removedItems?.length ? `${step.removedItems.length} 个节点` : undefined;
193
+ }
194
+ return undefined;
195
+ }
196
+
29
197
  public state = reactive<HistoryState>({
30
198
  pageSteps: {},
31
199
  pageId: undefined,
32
200
  canRedo: false,
33
201
  canUndo: false,
202
+ codeBlockState: {},
203
+ dataSourceState: {},
34
204
  });
35
205
 
36
206
  constructor() {
@@ -41,6 +211,8 @@ class History extends BaseService {
41
211
 
42
212
  public reset() {
43
213
  this.state.pageSteps = {};
214
+ this.state.codeBlockState = {};
215
+ this.state.dataSourceState = {};
44
216
  this.resetPage();
45
217
  }
46
218
 
@@ -69,16 +241,146 @@ class History extends BaseService {
69
241
  this.state.pageSteps = {};
70
242
  this.state.canRedo = false;
71
243
  this.state.canUndo = false;
244
+ this.state.codeBlockState = {};
245
+ this.state.dataSourceState = {};
72
246
  }
73
247
 
74
- public push(state: StepValue): StepValue | null {
75
- const undoRedo = this.getUndoRedo();
248
+ /**
249
+ * 把一条步骤推入指定页面的栈;不指定 pageId 时落到当前活动页。
250
+ *
251
+ * 跨页操作(例如 `moveToContainer` 把节点搬到其它页)必须显式传入 `pageId`,
252
+ * 否则会把记录错误地落到操作发起页 / 当前激活页,破坏目标页 / 源页的撤销栈语义。
253
+ */
254
+ public push(state: StepValue, pageId?: Id): StepValue | null {
255
+ const undoRedo = this.getUndoRedo(pageId);
76
256
  if (!undoRedo) return null;
77
257
  undoRedo.pushElement(state);
78
- this.emit('change', state);
258
+ // 仅当推入的是当前活动页时才需要刷新 canUndo/canRedo —— 其它页栈对当前 UI 状态没影响。
259
+ if (pageId === undefined || `${pageId}` === `${this.state.pageId}`) {
260
+ this.emit('change', state);
261
+ }
79
262
  return state;
80
263
  }
81
264
 
265
+ /**
266
+ * 推入一条代码块变更记录(与页面/节点完全无关),按 `codeBlockId` 维度独立一份 UndoRedo 栈。
267
+ *
268
+ * - 新增:oldContent = null,newContent = 新内容
269
+ * - 更新:oldContent / newContent 都为对应内容
270
+ * - 删除:newContent = null,oldContent = 删除前内容
271
+ * - `changeRecords` 来自 form 端,撤销/重做时若有则按 propPath 局部覆盖;缺省才退化为整内容替换。
272
+ * - 不直接驱动 codeBlockService,调用方负责实际写回。
273
+ */
274
+ public pushCodeBlock(
275
+ codeBlockId: Id,
276
+ payload: {
277
+ oldContent: CodeBlockContent | null;
278
+ newContent: CodeBlockContent | null;
279
+ changeRecords?: ChangeRecord[];
280
+ /** 可选的人类可读描述(如「修改按钮颜色」),仅用于历史面板展示。 */
281
+ historyDescription?: string;
282
+ },
283
+ ): CodeBlockStepValue | null {
284
+ if (!codeBlockId) return null;
285
+
286
+ const step: CodeBlockStepValue = {
287
+ id: codeBlockId,
288
+ oldContent: payload.oldContent ? cloneDeep(payload.oldContent) : null,
289
+ newContent: payload.newContent ? cloneDeep(payload.newContent) : null,
290
+ changeRecords: payload.changeRecords?.length ? cloneDeep(payload.changeRecords) : undefined,
291
+ historyDescription: payload.historyDescription,
292
+ };
293
+
294
+ this.getCodeBlockUndoRedo(codeBlockId).pushElement(step);
295
+ this.emit('code-block-history-change', codeBlockId, step);
296
+ return step;
297
+ }
298
+
299
+ /**
300
+ * 推入一条数据源变更记录(与页面/节点完全无关),按 `dataSourceId` 维度独立一份 UndoRedo 栈。
301
+ * 行为同 pushCodeBlock(新增 oldSchema=null;删除 newSchema=null)。
302
+ */
303
+ public pushDataSource(
304
+ dataSourceId: Id,
305
+ payload: {
306
+ oldSchema: DataSourceSchema | null;
307
+ newSchema: DataSourceSchema | null;
308
+ changeRecords?: ChangeRecord[];
309
+ /** 可选的人类可读描述,仅用于历史面板展示。 */
310
+ historyDescription?: string;
311
+ },
312
+ ): DataSourceStepValue | null {
313
+ if (!dataSourceId) return null;
314
+
315
+ const step: DataSourceStepValue = {
316
+ id: dataSourceId,
317
+ oldSchema: payload.oldSchema ? cloneDeep(payload.oldSchema) : null,
318
+ newSchema: payload.newSchema ? cloneDeep(payload.newSchema) : null,
319
+ changeRecords: payload.changeRecords?.length ? cloneDeep(payload.changeRecords) : undefined,
320
+ historyDescription: payload.historyDescription,
321
+ };
322
+
323
+ this.getDataSourceUndoRedo(dataSourceId).pushElement(step);
324
+ this.emit('data-source-history-change', dataSourceId, step);
325
+ return step;
326
+ }
327
+
328
+ /** 撤销指定代码块的最近一次变更。 */
329
+ public undoCodeBlock(codeBlockId: Id): CodeBlockStepValue | null {
330
+ const undoRedo = this.state.codeBlockState[codeBlockId];
331
+ if (!undoRedo) return null;
332
+ const step = undoRedo.undo();
333
+ if (step) this.emit('code-block-history-change', codeBlockId, step);
334
+ return step;
335
+ }
336
+
337
+ /** 重做指定代码块的下一次变更。 */
338
+ public redoCodeBlock(codeBlockId: Id): CodeBlockStepValue | null {
339
+ const undoRedo = this.state.codeBlockState[codeBlockId];
340
+ if (!undoRedo) return null;
341
+ const step = undoRedo.redo();
342
+ if (step) this.emit('code-block-history-change', codeBlockId, step);
343
+ return step;
344
+ }
345
+
346
+ /** 是否可对指定代码块撤销。 */
347
+ public canUndoCodeBlock(codeBlockId: Id): boolean {
348
+ return this.state.codeBlockState[codeBlockId]?.canUndo() ?? false;
349
+ }
350
+
351
+ /** 是否可对指定代码块重做。 */
352
+ public canRedoCodeBlock(codeBlockId: Id): boolean {
353
+ return this.state.codeBlockState[codeBlockId]?.canRedo() ?? false;
354
+ }
355
+
356
+ /** 撤销指定数据源的最近一次变更。 */
357
+ public undoDataSource(dataSourceId: Id): DataSourceStepValue | null {
358
+ const undoRedo = this.state.dataSourceState[dataSourceId];
359
+ if (!undoRedo) return null;
360
+ const step = undoRedo.undo();
361
+ if (step) this.emit('data-source-history-change', dataSourceId, step);
362
+ return step;
363
+ }
364
+
365
+ /** 重做指定数据源的下一次变更。 */
366
+ public redoDataSource(dataSourceId: Id): DataSourceStepValue | null {
367
+ const undoRedo = this.state.dataSourceState[dataSourceId];
368
+ if (!undoRedo) return null;
369
+ const step = undoRedo.redo();
370
+ if (step) this.emit('data-source-history-change', dataSourceId, step);
371
+ return step;
372
+ }
373
+
374
+ /** 是否可对指定数据源撤销。 */
375
+ public canUndoDataSource(dataSourceId: Id): boolean {
376
+ return this.state.dataSourceState[dataSourceId]?.canUndo() ?? false;
377
+ }
378
+
379
+ /** 是否可对指定数据源重做。 */
380
+ public canRedoDataSource(dataSourceId: Id): boolean {
381
+ return this.state.dataSourceState[dataSourceId]?.canRedo() ?? false;
382
+ }
383
+
82
384
  public undo(): StepValue | null {
83
385
  const undoRedo = this.getUndoRedo();
84
386
  if (!undoRedo) return null;
@@ -101,9 +403,131 @@ class History extends BaseService {
101
403
  this.removeAllPlugins();
102
404
  }
103
405
 
104
- private getUndoRedo() {
105
- if (!this.state.pageId) return null;
106
- return this.state.pageSteps[this.state.pageId];
406
+ /**
407
+ * 取出当前活动页的历史步骤平铺列表(包含已应用 + 已撤销)。
408
+ * 列表按时间正序,最早一步在最前面。
409
+ * 通常 UI 应使用 `getPageHistoryGroups` 取已合并分组的版本;本方法仅为兼容/调试保留。
410
+ */
411
+ public getPageStepList(pageId?: Id): PageHistoryStepEntry[] {
412
+ const targetPageId = pageId ?? this.state.pageId;
413
+ if (!targetPageId) return [];
414
+ const undoRedo = this.state.pageSteps[targetPageId];
415
+ if (!undoRedo) return [];
416
+ const list = undoRedo.getElementList();
417
+ const cursor = undoRedo.getCursor();
418
+ return list.map((step, index) => ({
419
+ step,
420
+ index,
421
+ applied: index < cursor,
422
+ }));
423
+ }
424
+
425
+ /**
426
+ * 取出当前活动页的历史栈,按"目标节点"做相邻合并:
427
+ * - 连续修改同一节点(单节点 update)的多步合并为一个 group,组内可展开查看每步;
428
+ * - add / remove / 多节点 update 始终独立成组。
429
+ * 用于历史面板的"页面"tab 展示。
430
+ */
431
+ public getPageHistoryGroups(pageId?: Id): PageHistoryGroup[] {
432
+ const targetPageId = pageId ?? this.state.pageId;
433
+ if (!targetPageId) return [];
434
+ const undoRedo = this.state.pageSteps[targetPageId];
435
+ if (!undoRedo) return [];
436
+ const list = undoRedo.getElementList();
437
+ if (!list.length) return [];
438
+ const cursor = undoRedo.getCursor();
439
+ return History.mergePageSteps(targetPageId, list, cursor);
440
+ }
441
+
442
+ /**
443
+ * 取出全部代码块的历史栈,按 codeBlockId 分组。
444
+ * 同一栈内相邻、同 opType 且作用于同一 id 的多步会被合并为一个 group:
445
+ * - 这正是"代码块/数据源各自按 id 分栈"的天然表现,再叠加"连续修改同目标的相邻步骤合并展示"。
446
+ * - 合并后 group 暴露子步骤数组,UI 可展开查看每一步的 changeRecords。
447
+ * - applied 字段:组内最后一步是否处于已应用段。
448
+ */
449
+ public getCodeBlockHistoryGroups(): CodeBlockHistoryGroup[] {
450
+ const groups: CodeBlockHistoryGroup[] = [];
451
+ Object.entries(this.state.codeBlockState).forEach(([id, undoRedo]) => {
452
+ if (!undoRedo) return;
453
+ const list = undoRedo.getElementList();
454
+ if (!list.length) return;
455
+ const cursor = undoRedo.getCursor();
456
+ groups.push(...History.mergeCodeBlockSteps(id, list, cursor));
457
+ });
458
+ return groups;
459
+ }
460
+
461
+ /**
462
+ * 读取指定页面历史栈的当前游标(已应用步骤数量)。不传则取当前活动页。
463
+ * 没有对应栈时返回 0。
464
+ */
465
+ public getPageCursor(pageId?: Id): number {
466
+ const targetPageId = pageId ?? this.state.pageId;
467
+ if (!targetPageId) return 0;
468
+ return this.state.pageSteps[targetPageId]?.getCursor() ?? 0;
469
+ }
470
+
471
+ /** 读取指定代码块历史栈的当前游标。 */
472
+ public getCodeBlockCursor(codeBlockId: Id): number {
473
+ return this.state.codeBlockState[codeBlockId]?.getCursor() ?? 0;
474
+ }
475
+
476
+ /** 读取指定数据源历史栈的当前游标。 */
477
+ public getDataSourceCursor(dataSourceId: Id): number {
478
+ return this.state.dataSourceState[dataSourceId]?.getCursor() ?? 0;
479
+ }
480
+
481
+ /**
482
+ * 取出指定代码块历史栈的平铺步骤列表(含 applied 标记)。供 revert 等按 index 索引步骤使用。
483
+ */
484
+ public getCodeBlockStepList(codeBlockId: Id): { step: CodeBlockStepValue; index: number; applied: boolean }[] {
485
+ const undoRedo = this.state.codeBlockState[codeBlockId];
486
+ if (!undoRedo) return [];
487
+ const list = undoRedo.getElementList();
488
+ const cursor = undoRedo.getCursor();
489
+ return list.map((step, index) => ({ step, index, applied: index < cursor }));
490
+ }
491
+
492
+ /**
493
+ * 取出指定数据源历史栈的平铺步骤列表(含 applied 标记)。供 revert 等按 index 索引步骤使用。
494
+ */
495
+ public getDataSourceStepList(dataSourceId: Id): { step: DataSourceStepValue; index: number; applied: boolean }[] {
496
+ const undoRedo = this.state.dataSourceState[dataSourceId];
497
+ if (!undoRedo) return [];
498
+ const list = undoRedo.getElementList();
499
+ const cursor = undoRedo.getCursor();
500
+ return list.map((step, index) => ({ step, index, applied: index < cursor }));
501
+ }
502
+
503
+ /**
504
+ * 取出全部数据源的历史栈,按 dataSourceId 分组。同上。
505
+ */
506
+ public getDataSourceHistoryGroups(): DataSourceHistoryGroup[] {
507
+ const groups: DataSourceHistoryGroup[] = [];
508
+ Object.entries(this.state.dataSourceState).forEach(([id, undoRedo]) => {
509
+ if (!undoRedo) return;
510
+ const list = undoRedo.getElementList();
511
+ if (!list.length) return;
512
+ const cursor = undoRedo.getCursor();
513
+ groups.push(...History.mergeDataSourceSteps(id, list, cursor));
514
+ });
515
+ return groups;
516
+ }
517
+
518
+ /**
519
+ * 取出指定页面的栈;不传 pageId 时按当前活动页取。
520
+ *
521
+ * 跨页 push 时如果目标页的栈尚不存在(用户从未进入过该页),会按需创建一条空栈,
522
+ * 这样切到目标页时 Ctrl+Z 也能撤回该步骤。
523
+ */
524
+ private getUndoRedo(pageId?: Id) {
525
+ const targetPageId = pageId ?? this.state.pageId;
526
+ if (!targetPageId) return null;
527
+ if (!this.state.pageSteps[targetPageId]) {
528
+ this.state.pageSteps[targetPageId] = new UndoRedo<StepValue>();
529
+ }
530
+ return this.state.pageSteps[targetPageId];
107
531
  }
108
532
 
109
533
  private setCanUndoRedo(): void {
@@ -111,6 +535,26 @@ class History extends BaseService {
111
535
  this.state.canRedo = undoRedo?.canRedo() || false;
112
536
  this.state.canUndo = undoRedo?.canUndo() || false;
113
537
  }
538
+
539
+ /**
540
+ * 按 id 获取(或创建)指定代码块的 UndoRedo 栈。
541
+ */
542
+ private getCodeBlockUndoRedo(codeBlockId: Id): UndoRedo<CodeBlockStepValue> {
543
+ if (!this.state.codeBlockState[codeBlockId]) {
544
+ this.state.codeBlockState[codeBlockId] = new UndoRedo<CodeBlockStepValue>();
545
+ }
546
+ return this.state.codeBlockState[codeBlockId];
547
+ }
548
+
549
+ /**
550
+ * 按 id 获取(或创建)指定数据源的 UndoRedo 栈。
551
+ */
552
+ private getDataSourceUndoRedo(dataSourceId: Id): UndoRedo<DataSourceStepValue> {
553
+ if (!this.state.dataSourceState[dataSourceId]) {
554
+ this.state.dataSourceState[dataSourceId] = new UndoRedo<DataSourceStepValue>();
555
+ }
556
+ return this.state.dataSourceState[dataSourceId];
557
+ }
114
558
  }
115
559
 
116
560
  export type HistoryService = History;
@@ -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
  });