@tmagic/editor 1.8.0-beta.5 → 1.8.0-beta.7

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 (46) hide show
  1. package/dist/es/fields/CodeSelectCol.vue_vue_type_script_setup_true_lang.js +4 -1
  2. package/dist/es/fields/DataSourceFieldSelect/FieldSelect.vue_vue_type_script_setup_true_lang.js +8 -2
  3. package/dist/es/fields/DataSourceFields.vue_vue_type_script_setup_true_lang.js +25 -8
  4. package/dist/es/fields/DataSourceMethodSelect.vue_vue_type_script_setup_true_name_true_lang.js +6 -4
  5. package/dist/es/fields/DataSourceMethods.vue_vue_type_script_setup_true_lang.js +25 -12
  6. package/dist/es/fields/StyleSetter/components/Border.vue_vue_type_script_setup_true_lang.js +27 -5
  7. package/dist/es/index.js +2 -2
  8. package/dist/es/layouts/history-list/HistoryListPanel.vue_vue_type_script_setup_true_lang.js +10 -14
  9. package/dist/es/layouts/history-list/InitialRow.vue_vue_type_script_setup_true_lang.js +12 -4
  10. package/dist/es/layouts/history-list/composables.js +28 -77
  11. package/dist/es/layouts/sidebar/Sidebar.vue_vue_type_script_setup_true_lang.js +2 -2
  12. package/dist/es/layouts/sidebar/data-source/DataSourceConfigPanel.vue_vue_type_script_setup_true_lang.js +16 -3
  13. package/dist/es/layouts/sidebar/data-source/DataSourceListPanel.vue_vue_type_script_setup_true_lang.js +23 -1
  14. package/dist/es/services/codeBlock.js +30 -19
  15. package/dist/es/services/dataSource.js +29 -21
  16. package/dist/es/services/editor.js +52 -33
  17. package/dist/es/services/history.js +10 -15
  18. package/dist/es/style.css +4 -1
  19. package/dist/es/utils/data-source/index.js +2 -0
  20. package/dist/es/utils/editor.js +68 -48
  21. package/dist/es/utils/history.js +42 -33
  22. package/dist/style.css +4 -1
  23. package/dist/tmagic-editor.umd.cjs +412 -295
  24. package/package.json +7 -7
  25. package/src/fields/CodeSelectCol.vue +7 -1
  26. package/src/fields/DataSourceFieldSelect/FieldSelect.vue +16 -2
  27. package/src/fields/DataSourceFields.vue +37 -8
  28. package/src/fields/DataSourceMethodSelect.vue +9 -4
  29. package/src/fields/DataSourceMethods.vue +42 -21
  30. package/src/fields/StyleSetter/components/Border.vue +15 -6
  31. package/src/layouts/history-list/HistoryListPanel.vue +12 -25
  32. package/src/layouts/history-list/InitialRow.vue +3 -0
  33. package/src/layouts/history-list/composables.ts +34 -94
  34. package/src/layouts/sidebar/Sidebar.vue +2 -2
  35. package/src/layouts/sidebar/data-source/DataSourceConfigPanel.vue +31 -2
  36. package/src/layouts/sidebar/data-source/DataSourceListPanel.vue +26 -1
  37. package/src/services/codeBlock.ts +28 -22
  38. package/src/services/dataSource.ts +29 -24
  39. package/src/services/editor.ts +41 -37
  40. package/src/services/history.ts +19 -25
  41. package/src/theme/style-setter/border.scss +4 -1
  42. package/src/type.ts +38 -26
  43. package/src/utils/data-source/index.ts +2 -0
  44. package/src/utils/editor.ts +125 -59
  45. package/src/utils/history.ts +48 -45
  46. package/types/index.d.ts +109 -112
package/src/type.ts CHANGED
@@ -335,6 +335,7 @@ export interface EditorNodeInfo {
335
335
  node: MNode | null;
336
336
  parent: MContainer | null;
337
337
  page: MPage | MPageFragment | null;
338
+ path: MNode[];
338
339
  }
339
340
  // #endregion EditorNodeInfo
340
341
 
@@ -736,6 +737,14 @@ export type HistoryOpSource =
736
737
  | (string & {});
737
738
  // #endregion HistoryOpSource
738
739
 
740
+ // #region DslOpWithHistoryIdsResult
741
+ /** *AndGetHistoryId 系列方法返回值:原操作结果 + 本次写入历史记录的 uuid 列表(未入栈时为 `[]`)。 */
742
+ export type DslOpWithHistoryIdsResult<T> = {
743
+ result: T;
744
+ historyIds: string[];
745
+ };
746
+ // #endregion DslOpWithHistoryIdsResult
747
+
739
748
  // #region StepDiffItem
740
749
  /**
741
750
  * 单条变更的 diff 描述,统一表达「页面节点 / 代码块 / 数据源」的变化内容,
@@ -953,32 +962,26 @@ export interface PageHistoryGroup {
953
962
  }
954
963
 
955
964
  /**
956
- * 代码块历史面板分组。
957
- * - 同一 codeBlockId 的栈内,相邻的 'update' 操作会合并成一个 group;
958
- * - 'add' / 'remove' 始终独立成组(语义上是一次性事件)。
959
- */
960
- export interface CodeBlockHistoryGroup {
961
- kind: 'code-block';
962
- /** 关联的 codeBlock id */
963
- id: Id;
964
- /** 该分组的操作类型 */
965
- opType: HistoryOpType;
966
- /** 组内所有步骤,按时间正序 */
967
- steps: { step: CodeBlockStepValue; index: number; applied: boolean; isCurrent?: boolean }[];
968
- /** 组内最后一步是否已应用,用于整组的状态展示 */
969
- applied: boolean;
970
- /** 是否为当前所在的分组(包含该栈最近一次已应用步骤的那一组)。 */
971
- isCurrent?: boolean;
972
- }
973
-
974
- /**
975
- * 数据源历史面板分组,结构同 CodeBlockHistoryGroup。
965
+ * 数据源 / 代码块历史面板分组(按 id 分栈展示)。
966
+ * 二者结构完全一致,仅 `kind` step 类型不同,统一由该泛型描述:
967
+ * - 数据源:`StackHistoryGroup<DataSourceStepValue, 'data-source'>`;
968
+ * - 代码块:`StackHistoryGroup<CodeBlockStepValue, 'code-block'>`。
969
+ *
970
+ * 每条操作记录独立成组,不做相邻合并(与页面历史 {@link PageHistoryGroup} 不同),故 `steps` 恒为单元素。
976
971
  */
977
- export interface DataSourceHistoryGroup {
978
- kind: 'data-source';
972
+ export interface StackHistoryGroup<
973
+ T extends BaseStepValue = BaseStepValue,
974
+ K extends 'code-block' | 'data-source' = 'code-block' | 'data-source',
975
+ > {
976
+ /** 区分代码块 / 数据源。 */
977
+ kind: K;
978
+ /** 关联的代码块 / 数据源 id。 */
979
979
  id: Id;
980
+ /** 该分组的操作类型。 */
980
981
  opType: HistoryOpType;
981
- steps: { step: DataSourceStepValue; index: number; applied: boolean; isCurrent?: boolean }[];
982
+ /** 组内所有步骤,按时间正序(不做相邻合并,恒为单元素)。 */
983
+ steps: { step: T; index: number; applied: boolean; isCurrent?: boolean }[];
984
+ /** 组内最后一步是否已应用,用于整组的状态展示。 */
982
985
  applied: boolean;
983
986
  /** 是否为当前所在的分组(包含该栈最近一次已应用步骤的那一组)。 */
984
987
  isCurrent?: boolean;
@@ -1147,6 +1150,8 @@ export type SyncHookPlugin<
1147
1150
 
1148
1151
  export interface EventBusEvent {
1149
1152
  'edit-data-source': [id: string];
1153
+ 'edit-data-source-method': [id: string, methodName: string];
1154
+ 'edit-data-source-field': [id: string, fieldPath: string[]];
1150
1155
  'remove-data-source': [id: string];
1151
1156
  'edit-code': [id: string];
1152
1157
  }
@@ -1180,7 +1185,11 @@ export type CustomContentMenuFunction = (
1180
1185
  ) => (MenuButton | MenuComponent)[];
1181
1186
 
1182
1187
  export interface EditorEvents {
1183
- 'root-change': [value: StoreState['root'], preValue?: StoreState['root']];
1188
+ 'root-change': [
1189
+ value: StoreState['root'],
1190
+ preValue?: StoreState['root'],
1191
+ options?: { historySource?: HistoryOpSource },
1192
+ ];
1184
1193
  select: [node: MNode | null];
1185
1194
  add: [nodes: MNode[]];
1186
1195
  remove: [nodes: MNode[]];
@@ -1281,8 +1290,11 @@ export interface DiffDialogPayload {
1281
1290
  * 各自实现一份,作为整体注入,避免把 describe* / isStep* 拆成多个独立 props 反复透传。
1282
1291
  */
1283
1292
  export interface HistoryRowDescriptor<T extends BaseStepValue = BaseStepValue> {
1284
- /** 组级描述文案生成器,接收一个 group,返回展示文本。 */
1285
- describeGroup: (_group: any) => string;
1293
+ /**
1294
+ * 组级描述文案生成器,接收一个 group,返回展示文本。
1295
+ * 不传时回退到对组内最后一步调用 {@link describeStep}(适用于不做相邻合并、每组恒为单步的历史,如数据源/代码块)。
1296
+ */
1297
+ describeGroup?: (_group: any) => string;
1286
1298
  /** 单步描述文案生成器,接收一个 step,返回展示文本(合并组展开后的子步列表用)。 */
1287
1299
  describeStep: (_step: T) => string;
1288
1300
  /** 判断某个 step 是否可查看差异(前后值都存在)。不传则一律不展示差异入口。 */
@@ -10,6 +10,7 @@ const dataSourceFormConfig: TabConfig = {
10
10
  items: [
11
11
  {
12
12
  title: '数据定义',
13
+ status: 'fields',
13
14
  items: [
14
15
  {
15
16
  name: 'fields',
@@ -20,6 +21,7 @@ const dataSourceFormConfig: TabConfig = {
20
21
  },
21
22
  {
22
23
  title: '方法定义',
24
+ status: 'methods',
23
25
  items: [
24
26
  {
25
27
  name: 'methods',
@@ -132,6 +132,34 @@ const getMiddleTop = (node: MNode, parentNode: MNode, stage: StageCore | null) =
132
132
  return (Math.min(parentHeight, wrapperHeightDeal) - height) / 2;
133
133
  };
134
134
 
135
+ // 同时存在一对相反的定位属性(如 left/right)时只保留一个,避免元素被拉伸
136
+ const removeConflictPosition = (style: Record<string, any>, primary: string, secondary: string) => {
137
+ // '' / 0 / undefined / null 视为无效值
138
+ const isInvalid = (value: any) => value === '' || value === 0 || value === undefined || value === null;
139
+
140
+ if (!(primary in style) || !(secondary in style)) {
141
+ return;
142
+ }
143
+
144
+ const primaryValue = style[primary];
145
+ const secondaryValue = style[secondary];
146
+ const primaryInvalid = isInvalid(primaryValue);
147
+ const secondaryInvalid = isInvalid(secondaryValue);
148
+
149
+ if (primaryInvalid && !secondaryInvalid) {
150
+ delete style[primary];
151
+ } else if (secondaryInvalid && !primaryInvalid) {
152
+ delete style[secondary];
153
+ } else if (primaryInvalid && secondaryInvalid) {
154
+ // 都是无效值时优先保留 0,否则保留 primary(left/top)
155
+ if (secondaryValue === 0 && primaryValue !== 0) {
156
+ delete style[primary];
157
+ } else {
158
+ delete style[secondary];
159
+ }
160
+ }
161
+ };
162
+
135
163
  export const getInitPositionStyle = (style: Record<string, any> = {}, layout: Layout) => {
136
164
  if (layout === Layout.ABSOLUTE) {
137
165
  const newStyle: Record<string, any> = {
@@ -139,6 +167,9 @@ export const getInitPositionStyle = (style: Record<string, any> = {}, layout: La
139
167
  position: 'absolute',
140
168
  };
141
169
 
170
+ removeConflictPosition(newStyle, 'left', 'right');
171
+ removeConflictPosition(newStyle, 'top', 'bottom');
172
+
142
173
  if (typeof newStyle.left === 'undefined' && typeof newStyle.right === 'undefined') {
143
174
  newStyle.left = 0;
144
175
  }
@@ -178,43 +209,68 @@ export const setLayout = (node: MNode, layout: Layout) => {
178
209
  return node;
179
210
  };
180
211
 
181
- export const change2Fixed = (node: MNode, root: MApp) => {
182
- const style = {
183
- ...(node.style || {}),
184
- };
212
+ type PositionKey = 'left' | 'top' | 'right' | 'bottom';
185
213
 
186
- const path = getNodePath(node.id, root.items);
187
- const offset = {
188
- left: 0,
189
- top: 0,
190
- };
214
+ // 判断 style 是否显式设置了某个定位属性,使用 typeof 判断以避免将 0(如 right: 0)误判为未设置
215
+ const hasPositionValue = (style: Record<string, any> | undefined, key: PositionKey): boolean =>
216
+ typeof style?.[key] !== 'undefined' && style?.[key] !== '';
191
217
 
192
- if (!node.style?.right && isNumber(node.style?.left || 0)) {
193
- for (const value of path) {
194
- if (value.style?.right || !isNumber(value.style?.left || 0)) {
195
- offset.left = 0;
196
- break;
197
- }
198
- offset.left = offset.left + Number(value.style?.left || 0);
218
+ /**
219
+ * 沿节点路径累加(或抵消)某一方向上的定位偏移量
220
+ * 当路径上的祖先使用了反方向定位(例如计算 left 时祖先用了 right)或非数值定位时,
221
+ * 无法简单地通过求和换算坐标,此时返回 0 表示放弃补偿、保持原值
222
+ * @param path 从根到目标节点的路径(含节点自身)
223
+ * @param dir 需要累加的定位方向
224
+ * @param opposite 反方向定位属性
225
+ * @param sign change2Fixed 取 1(累加祖先偏移),Fixed2Other 取 -1(抵消祖先偏移)
226
+ * @param init 偏移量初始值
227
+ */
228
+ const accumulatePositionOffset = (
229
+ path: MNode[],
230
+ dir: PositionKey,
231
+ opposite: PositionKey,
232
+ sign: 1 | -1,
233
+ init = 0,
234
+ ): number => {
235
+ let offset = init;
236
+ for (const value of path) {
237
+ if (hasPositionValue(value.style, opposite) || !isNumber(value.style?.[dir] || 0)) {
238
+ return 0;
199
239
  }
240
+ offset = offset + sign * Number(value.style?.[dir] || 0);
200
241
  }
242
+ return offset;
243
+ };
201
244
 
202
- if (!node.style?.bottom && isNumber(node.style?.top || 0)) {
203
- for (const value of path) {
204
- if (value.style?.bottom || !isNumber(value.style?.top || 0)) {
205
- offset.top = 0;
206
- break;
207
- }
208
- offset.top = offset.top + Number(value.style?.top || 0);
209
- }
210
- }
245
+ export const change2Fixed = (node: MNode, path: MNode[]) => {
246
+ const style = {
247
+ ...(node.style || {}),
248
+ };
211
249
 
212
- if (offset.left) {
213
- style.left = offset.left;
250
+ // 水平方向:以 left 锚定则累加祖先 left,以 right 锚定则累加祖先 right
251
+ if (!hasPositionValue(node.style, 'right') && isNumber(node.style?.left || 0)) {
252
+ const left = accumulatePositionOffset(path, 'left', 'right', 1);
253
+ if (left) {
254
+ style.left = left;
255
+ }
256
+ } else if (hasPositionValue(node.style, 'right') && isNumber(node.style?.right || 0)) {
257
+ const right = accumulatePositionOffset(path, 'right', 'left', 1);
258
+ if (right) {
259
+ style.right = right;
260
+ }
214
261
  }
215
262
 
216
- if (offset.top) {
217
- style.top = offset.top;
263
+ // 垂直方向:以 top 锚定则累加祖先 top,以 bottom 锚定则累加祖先 bottom
264
+ if (!hasPositionValue(node.style, 'bottom') && isNumber(node.style?.top || 0)) {
265
+ const top = accumulatePositionOffset(path, 'top', 'bottom', 1);
266
+ if (top) {
267
+ style.top = top;
268
+ }
269
+ } else if (hasPositionValue(node.style, 'bottom') && isNumber(node.style?.bottom || 0)) {
270
+ const bottom = accumulatePositionOffset(path, 'bottom', 'top', 1);
271
+ if (bottom) {
272
+ style.bottom = bottom;
273
+ }
218
274
  }
219
275
 
220
276
  return style;
@@ -222,34 +278,29 @@ export const change2Fixed = (node: MNode, root: MApp) => {
222
278
 
223
279
  export const Fixed2Other = async (
224
280
  node: MNode,
225
- root: MApp,
281
+ path: MNode[],
226
282
  getLayout: (parent: MNode, node?: MNode) => Promise<Layout>,
227
283
  ) => {
228
- const path = getNodePath(node.id, root.items);
229
284
  const cur = path.pop();
230
285
  const offset = {
231
- left: cur?.style?.left || 0,
232
- top: cur?.style?.top || 0,
286
+ left: 0,
287
+ top: 0,
288
+ right: 0,
289
+ bottom: 0,
233
290
  };
234
291
 
235
- if (!node.style?.right && isNumber(node.style?.left || 0)) {
236
- for (const value of path) {
237
- if (value.style?.right || !isNumber(value.style?.left || 0)) {
238
- offset.left = 0;
239
- break;
240
- }
241
- offset.left = offset.left - Number(value.style?.left || 0);
242
- }
292
+ // 水平方向:抵消祖先在对应锚定方向上的偏移量,初始值为节点自身的偏移
293
+ if (!hasPositionValue(node.style, 'right') && isNumber(node.style?.left || 0)) {
294
+ offset.left = accumulatePositionOffset(path, 'left', 'right', -1, Number(cur?.style?.left || 0));
295
+ } else if (hasPositionValue(node.style, 'right') && isNumber(node.style?.right || 0)) {
296
+ offset.right = accumulatePositionOffset(path, 'right', 'left', -1, Number(cur?.style?.right || 0));
243
297
  }
244
298
 
245
- if (!node.style?.bottom && isNumber(node.style?.top || 0)) {
246
- for (const value of path) {
247
- if (value.style?.bottom || !isNumber(value.style?.top || 0)) {
248
- offset.top = 0;
249
- break;
250
- }
251
- offset.top = offset.top - Number(value.style?.top || 0);
252
- }
299
+ // 垂直方向:同上
300
+ if (!hasPositionValue(node.style, 'bottom') && isNumber(node.style?.top || 0)) {
301
+ offset.top = accumulatePositionOffset(path, 'top', 'bottom', -1, Number(cur?.style?.top || 0));
302
+ } else if (hasPositionValue(node.style, 'bottom') && isNumber(node.style?.bottom || 0)) {
303
+ offset.bottom = accumulatePositionOffset(path, 'bottom', 'top', -1, Number(cur?.style?.bottom || 0));
253
304
  }
254
305
 
255
306
  const style = node.style || {};
@@ -269,6 +320,14 @@ export const Fixed2Other = async (
269
320
  style.top = offset.top;
270
321
  }
271
322
 
323
+ if (offset.right) {
324
+ style.right = offset.right;
325
+ }
326
+
327
+ if (offset.bottom) {
328
+ style.bottom = offset.bottom;
329
+ }
330
+
272
331
  return {
273
332
  ...style,
274
333
  position: 'absolute',
@@ -316,11 +375,18 @@ export const fixNodePosition = (config: MNode, parent: MContainer, stage: StageC
316
375
  return config.style;
317
376
  }
318
377
 
319
- return {
320
- ...(config.style || {}),
321
- top: getMiddleTop(config, parent, stage),
322
- left: fixNodeLeft(config, parent, stage?.renderer?.contentWindow?.document),
323
- };
378
+ const style = { ...(config.style || {}) };
379
+ const baseStyle = config.style || {};
380
+
381
+ if (!('right' in baseStyle)) {
382
+ style.left = fixNodeLeft(config, parent, stage?.renderer?.contentWindow?.document);
383
+ }
384
+
385
+ if (!('top' in baseStyle) && !('bottom' in baseStyle)) {
386
+ style.top = getMiddleTop(config, parent, stage);
387
+ }
388
+
389
+ return style;
324
390
  };
325
391
 
326
392
  // 序列化配置
@@ -460,11 +526,11 @@ export const resolveSelectedNode = (
460
526
  throw new Error('没有ID,无法选中');
461
527
  }
462
528
 
463
- const { node, parent, page } = getNodeInfoFn(id);
529
+ const { node, parent, page, path } = getNodeInfoFn(id);
464
530
  if (!node) throw new Error('获取不到组件信息');
465
531
  if (node.id === rootId) throw new Error('不能选根节点');
466
532
 
467
- return { node, parent, page };
533
+ return { node, parent, page, path };
468
534
  };
469
535
 
470
536
  /**
@@ -479,16 +545,16 @@ export const resolveSelectedNode = (
479
545
  export const toggleFixedPosition = async (
480
546
  dist: MNode,
481
547
  src: MNode,
482
- root: MApp,
548
+ path: MNode[],
483
549
  getLayoutFn: (parent: MNode, node?: MNode | null) => Promise<Layout>,
484
550
  ): Promise<MNode> => {
485
551
  const newConfig = cloneDeep(dist);
486
552
 
487
553
  if (!isPop(src) && newConfig.style?.position) {
488
554
  if (isFixed(newConfig.style) && !isFixed(src.style || {})) {
489
- newConfig.style = change2Fixed(newConfig, root);
555
+ newConfig.style = change2Fixed(newConfig, path);
490
556
  } else if (!isFixed(newConfig.style) && isFixed(src.style || {})) {
491
- newConfig.style = await Fixed2Other(newConfig, root, getLayoutFn);
557
+ newConfig.style = await Fixed2Other(newConfig, path, getLayoutFn);
492
558
  }
493
559
  }
494
560
 
@@ -17,6 +17,7 @@
17
17
  */
18
18
 
19
19
  import { cloneDeep } from 'lodash-es';
20
+ import serialize from 'serialize-javascript';
20
21
 
21
22
  import type { Id } from '@tmagic/core';
22
23
  import type { ChangeRecord } from '@tmagic/form';
@@ -25,9 +26,9 @@ import { guid } from '@tmagic/utils';
25
26
  import type {
26
27
  BaseStepValue,
27
28
  HistoryOpSource,
28
- HistoryOpType,
29
29
  PageHistoryGroup,
30
30
  PageHistoryStepEntry,
31
+ StackHistoryGroup,
31
32
  StepDiffItem,
32
33
  StepValue,
33
34
  } from '@editor/type';
@@ -121,8 +122,7 @@ export const markStackSaved = <S extends { saved?: boolean }>(undoRedo?: UndoRed
121
122
 
122
123
  /**
123
124
  * 把单个「按 id 分栈」的历史栈(代码块 / 数据源)拆成若干 group:
124
- * - 把"新增/删除"独立成组(语义上属于一次性事件,不应与 update 合并);
125
- * - 连续 'update' 合并到同一组,组内 steps 顺序就是发生顺序。
125
+ * 每条操作记录独立成组,不做相邻 update 合并(与页面历史的合并策略不同)。
126
126
  *
127
127
  * 代码块与数据源除 `kind` 外结构完全一致,统一由本方法处理;`kind` 决定返回的具体分组类型。
128
128
  */
@@ -131,46 +131,20 @@ export const mergeStackSteps = <S extends BaseStepValue, K extends 'code-block'
131
131
  id: Id,
132
132
  list: S[],
133
133
  cursor: number,
134
- ): {
135
- kind: K;
136
- id: Id;
137
- opType: HistoryOpType;
138
- steps: { step: S; index: number; applied: boolean; isCurrent?: boolean }[];
139
- applied: boolean;
140
- isCurrent?: boolean;
141
- }[] => {
142
- type Group = {
143
- kind: K;
144
- id: Id;
145
- opType: HistoryOpType;
146
- steps: { step: S; index: number; applied: boolean; isCurrent?: boolean }[];
147
- applied: boolean;
148
- isCurrent?: boolean;
149
- };
150
- const groups: Group[] = [];
151
- let current: Group | null = null;
134
+ ): StackHistoryGroup<S, K>[] => {
152
135
  const currentIndex = cursor - 1;
153
- list.forEach((step, index) => {
154
- const { opType } = step;
136
+ return list.map((step, index) => {
155
137
  const applied = index < cursor;
156
138
  const isCurrent = index === currentIndex;
157
- if (opType === 'update' && current?.opType === 'update') {
158
- current.steps.push({ step, index, applied, isCurrent });
159
- current.applied = applied;
160
- if (isCurrent) current.isCurrent = true;
161
- } else {
162
- current = {
163
- kind,
164
- id,
165
- opType,
166
- steps: [{ step, index, applied, isCurrent }],
167
- applied,
168
- isCurrent,
169
- };
170
- groups.push(current);
171
- }
139
+ return {
140
+ kind,
141
+ id,
142
+ opType: step.opType,
143
+ steps: [{ step, index, applied, isCurrent }],
144
+ applied,
145
+ isCurrent,
146
+ };
172
147
  });
173
- return groups;
174
148
  };
175
149
 
176
150
  /**
@@ -254,11 +228,25 @@ export const detectPageTargetName = (step: StepValue): string | undefined => {
254
228
  return undefined;
255
229
  };
256
230
 
257
- /** 把 `Record<Id, UndoRedo>` 整体序列化为 `Record<Id, SerializedUndoRedo>`。 */
258
- export const serializeStacks = <T>(stacks: Record<Id, UndoRedo<T>>) => {
231
+ /**
232
+ * `Record<Id, UndoRedo>` 整体序列化为 `Record<Id, SerializedUndoRedo>`。
233
+ *
234
+ * 序列化(深克隆)的同一趟里,只把每条 step 中可能含函数的 `diff` 用 serialize-javascript 序列化成字符串,
235
+ * 其余字段(uuid / opType / timestamp / `modifiedNodeIds` Map 等)原样保留,交给 IndexedDB 结构化克隆。
236
+ * 这样既能写入函数,又避免序列化整份快照的开销;读取时再由 {@link parseStacksStepDiff} 还原 diff。
237
+ * 不含 `diff` 的元素(如通用栈)原样透传。
238
+ */
239
+ export const serializeStacks = <T extends { diff?: unknown }>(stacks: Record<Id, UndoRedo<T>>) => {
259
240
  const result: Record<Id, ReturnType<UndoRedo<T>['serialize']>> = {};
260
241
  Object.entries(stacks).forEach(([id, undoRedo]) => {
261
- if (undoRedo) result[id] = undoRedo.serialize();
242
+ if (!undoRedo) return;
243
+ const serialized = undoRedo.serialize();
244
+ result[id] = {
245
+ ...serialized,
246
+ elementList: serialized.elementList.map((step) =>
247
+ step.diff === undefined ? step : Object.assign({}, step, { diff: serialize(step.diff) }),
248
+ ),
249
+ };
262
250
  });
263
251
  return result;
264
252
  };
@@ -266,15 +254,27 @@ export const serializeStacks = <T>(stacks: Record<Id, UndoRedo<T>>) => {
266
254
  /**
267
255
  * 把 `Record<Id, SerializedUndoRedo>` 整体还原为 `Record<Id, UndoRedo>`。
268
256
  * 还原时把每个栈的游标定位到最近一条已保存(`saved === true`)记录之后。
257
+ *
258
+ * 与 {@link serializeStacks} 相反:当传入 `parse`(parseDSL)时,把每条 step 中以字符串形式存储的 `diff`
259
+ * 解析回真实对象(含函数);不含 `diff` 的元素(如通用栈)原样透传。
269
260
  */
270
261
  export const deserializeStacks = <T extends { saved?: boolean }>(
271
262
  stacks: Record<Id, ReturnType<UndoRedo<T>['serialize']>> = {},
263
+ parse?: (serialized: string) => unknown,
272
264
  ): Record<Id, UndoRedo<T>> => {
273
265
  const result: Record<Id, UndoRedo<T>> = {};
274
266
  Object.entries(stacks).forEach(([id, serialized]) => {
275
- if (serialized) {
276
- result[id] = UndoRedo.fromSerialized<T>(serialized, { isSavedStep: (element) => element.saved === true });
277
- }
267
+ if (!serialized) return;
268
+ const elementList = parse
269
+ ? serialized.elementList.map((step) => {
270
+ const { diff } = step as { diff?: unknown };
271
+ return typeof diff === 'string' ? Object.assign({}, step, { diff: parse(`(${diff})`) }) : step;
272
+ })
273
+ : serialized.elementList;
274
+ result[id] = UndoRedo.fromSerialized<T>(
275
+ { ...serialized, elementList },
276
+ { isSavedStep: (element) => element.saved === true },
277
+ );
278
278
  });
279
279
  return result;
280
280
  };
@@ -296,3 +296,6 @@ export const getOrCreateStack = <T>(stacks: Record<Id, UndoRedo<T>>, id: Id): Un
296
296
  export const undoFloor = (undoRedo: UndoRedo<StepValue>): number => {
297
297
  return undoRedo.getElementList()[0]?.opType === 'initial' ? 1 : 0;
298
298
  };
299
+
300
+ /** 将单次 push 产生的 history uuid(或 null)转为 *AndGetHistoryId 返回用的 uuid 列表。 */
301
+ export const getLastPushedHistoryIds = (historyId: string | null): string[] => (historyId ? [historyId] : []);