@tmagic/editor 1.8.0-beta.6 → 1.8.0-beta.8

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 (42) hide show
  1. package/dist/es/components/CompareForm.vue_vue_type_script_setup_true_lang.js +10 -6
  2. package/dist/es/index.js +6 -4
  3. package/dist/es/layouts/NavMenu.vue_vue_type_script_setup_true_lang.js +2 -2
  4. package/dist/es/layouts/history-list/GroupRow.vue_vue_type_script_setup_true_lang.js +38 -26
  5. package/dist/es/layouts/history-list/HistoryDiffDialog.vue_vue_type_script_setup_true_lang.js +5 -1
  6. package/dist/es/layouts/history-list/HistoryListPanel.vue_vue_type_script_setup_true_lang.js +163 -310
  7. package/dist/es/layouts/history-list/composables.js +47 -128
  8. package/dist/es/layouts/history-list/useHistoryList.js +56 -0
  9. package/dist/es/layouts/history-list/useHistoryRevert.js +304 -0
  10. package/dist/es/layouts/sidebar/Sidebar.vue_vue_type_script_setup_true_lang.js +2 -2
  11. package/dist/es/layouts/sidebar/data-source/DataSourceConfigPanel.vue_vue_type_script_setup_true_lang.js +3 -3
  12. package/dist/es/services/codeBlock.js +32 -28
  13. package/dist/es/services/dataSource.js +43 -34
  14. package/dist/es/services/editor.js +31 -26
  15. package/dist/es/services/history.js +268 -384
  16. package/dist/es/style.css +12 -0
  17. package/dist/es/utils/editor.js +2 -2
  18. package/dist/es/utils/history.js +35 -47
  19. package/dist/style.css +12 -0
  20. package/dist/tmagic-editor.umd.cjs +3808 -3090
  21. package/package.json +7 -7
  22. package/src/components/CompareForm.vue +14 -6
  23. package/src/index.ts +2 -0
  24. package/src/layouts/NavMenu.vue +2 -2
  25. package/src/layouts/history-list/GroupRow.vue +10 -0
  26. package/src/layouts/history-list/HistoryDiffDialog.vue +6 -1
  27. package/src/layouts/history-list/HistoryListPanel.vue +60 -270
  28. package/src/layouts/history-list/PageTab.vue +4 -4
  29. package/src/layouts/history-list/composables.ts +82 -180
  30. package/src/layouts/history-list/useHistoryList.ts +60 -0
  31. package/src/layouts/history-list/useHistoryRevert.ts +400 -0
  32. package/src/layouts/sidebar/Sidebar.vue +2 -2
  33. package/src/layouts/sidebar/data-source/DataSourceConfigPanel.vue +5 -4
  34. package/src/services/codeBlock.ts +30 -29
  35. package/src/services/dataSource.ts +37 -37
  36. package/src/services/editor.ts +32 -29
  37. package/src/services/history.ts +340 -431
  38. package/src/theme/history-list-panel.scss +14 -0
  39. package/src/type.ts +179 -100
  40. package/src/utils/editor.ts +2 -2
  41. package/src/utils/history.ts +52 -74
  42. package/types/index.d.ts +435 -309
@@ -1,18 +1,12 @@
1
- import { computed, reactive } from 'vue';
2
-
1
+ import { tMagicMessageBox } from '@tmagic/design';
3
2
  import { datetimeFormatter } from '@tmagic/form';
4
3
 
5
- import { useServices } from '@editor/hooks/use-services';
6
4
  import type {
7
5
  BaseStepValue,
8
- CodeBlockHistoryGroup,
9
- CodeBlockStepValue,
10
- DataSourceHistoryGroup,
11
- DataSourceStepValue,
6
+ HistoryGroup,
12
7
  HistoryOpSource,
13
8
  HistoryOpType,
14
9
  HistoryRowDescriptor,
15
- PageHistoryGroup,
16
10
  StepValue,
17
11
  } from '@editor/type';
18
12
 
@@ -31,51 +25,49 @@ export interface HistoryBucketGroup<T extends BaseStepValue = BaseStepValue> {
31
25
  steps: { index: number; applied: boolean; isCurrent?: boolean; step: T }[];
32
26
  }
33
27
 
34
- /** GroupRow 渲染所需的单个子步视图模型(已由 {@link toRowGroup} 预先派生,组件内部不再触碰原始 step)。 */
35
- export interface HistoryRowStep {
36
- /** 该子步在所属栈中的稳定索引。 */
37
- index: number;
28
+ /**
29
+ * GroupRow 的组头部与子步共用的展示字段(均由 {@link toRowGroup} 预先派生)。
30
+ * 抽出公共部分避免 {@link HistoryRowStep} / {@link HistoryRowGroup} 重复声明,
31
+ * 也便于消费方用本类型收窄「组头 / 子步」通用渲染逻辑的入参。
32
+ */
33
+ export interface HistoryRowDisplay {
38
34
  /** 是否已应用(false 表示已被 undo,UI 灰态)。 */
39
35
  applied: boolean;
40
- /** 是否为当前所在步骤。 */
41
- isCurrent?: boolean;
42
- /** 是否为最近一次保存的记录。 */
43
- saved?: boolean;
44
- /** 子步描述文案。 */
36
+ /** 是否为当前所在步骤 / 分组。 */
37
+ isCurrent: boolean;
38
+ /** 描述文案。 */
45
39
  desc: string;
46
- /** 是否可查看差异。 */
47
- diffable?: boolean;
48
- /** 是否可回滚。 */
49
- revertable?: boolean;
50
40
  /** 操作途径。 */
51
41
  source?: HistoryOpSource;
42
+ /** 操作人。 */
43
+ operator?: string;
52
44
  /** 时间文案。 */
53
45
  time?: string;
54
46
  /** 时间的完整 title 提示。 */
55
47
  timeTitle?: string;
56
48
  }
57
49
 
50
+ /** GroupRow 渲染所需的单个子步视图模型(已由 {@link toRowGroup} 预先派生,组件内部不再触碰原始 step)。 */
51
+ export interface HistoryRowStep extends HistoryRowDisplay {
52
+ /** 该子步在所属栈中的稳定索引。 */
53
+ index: number;
54
+ /** 是否为最近一次保存的记录。 */
55
+ saved?: boolean;
56
+ /** 是否可查看差异。 */
57
+ diffable?: boolean;
58
+ /** 是否可回滚。 */
59
+ revertable?: boolean;
60
+ }
61
+
58
62
  /**
59
63
  * GroupRow 渲染所需的整组视图模型(由 {@link toRowGroup} 统一派生)。
60
64
  * 把原先 GroupRow 上十多个扁平 props 收敛为单一对象,header 信息与子步列表一并携带。
61
65
  */
62
- export interface HistoryRowGroup {
66
+ export interface HistoryRowGroup extends HistoryRowDisplay {
63
67
  /** 分组的稳定 key,作为 toggle 事件 payload 与折叠状态的索引。 */
64
68
  key: string;
65
- /** 组内最后一步是否已应用。 */
66
- applied: boolean;
67
- /** 是否为当前所在分组。 */
68
- isCurrent: boolean;
69
69
  /** 操作类型,用于徽标颜色与文案。 */
70
70
  opType: HistoryOpType;
71
- /** 组整体描述文案。 */
72
- desc: string;
73
- /** 组的操作途径(取组内最近一步)。 */
74
- source?: HistoryOpSource;
75
- /** 组头部时间文案(取组内最近一步)。 */
76
- time?: string;
77
- /** 组头部时间的完整 title 提示。 */
78
- timeTitle?: string;
79
71
  /** 子步列表(时间正序);其长度即合并步数,length > 1 即为合并组。 */
80
72
  subSteps: HistoryRowStep[];
81
73
  }
@@ -83,63 +75,6 @@ export interface HistoryRowGroup {
83
75
  /** 合并组默认展开;仅当 expanded[key] === false 时为收起。 */
84
76
  export const isHistoryGroupExpanded = (expanded: Record<string, boolean>, key: string) => expanded[key] !== false;
85
77
 
86
- /**
87
- * 历史记录面板共享逻辑:
88
- * - 暴露三类历史的聚合数据(页面 / 数据源 / 代码块);
89
- * - 提供折叠状态管理;
90
- * - 提供操作描述文案生成器。
91
- *
92
- * 所有数据基于 historyService 的 reactive state 派生,自动跟随历史变化刷新。
93
- */
94
- export const useHistoryList = () => {
95
- const { historyService } = useServices();
96
-
97
- /**
98
- * 折叠状态:key 形如 `pg-${组内首步 index}` / `ds-${id}-${组内首步 index}` / `cb-${id}-${组内首步 index}`。
99
- * 用组内首步的稳定 index(而非展示位置)作为 key,确保历史数据更新后已展开的分组状态保持不变。
100
- * 合并组默认展开;仅当值为 `false` 时表示收起。
101
- */
102
- const expanded = reactive<Record<string, boolean>>({});
103
- const toggleGroup = (key: string) => {
104
- expanded[key] = expanded[key] === false;
105
- };
106
-
107
- const pageGroups = computed(() => historyService.getPageHistoryGroups());
108
- const dataSourceGroups = computed(() => historyService.getDataSourceHistoryGroups());
109
- const codeBlockGroups = computed(() => historyService.getCodeBlockHistoryGroups());
110
-
111
- /** 页面 tab 倒序展示(最新一组在最上面)。 */
112
- const pageGroupsDisplay = computed(() => pageGroups.value.slice().reverse());
113
-
114
- /**
115
- * 把按时间正序的 group 列表,再按 id 聚拢成 bucket(同 id 的所有分组放一起)。
116
- * 每个 bucket 内部仍然按时间倒序展示(最近的操作最先看到)。
117
- */
118
- const groupByTarget = <G extends { id: string | number }>(groups: G[]) => {
119
- const map = new Map<string | number, G[]>();
120
- groups.forEach((g) => {
121
- const list = map.get(g.id) ?? [];
122
- list.push(g);
123
- map.set(g.id, list);
124
- });
125
- return Array.from(map.entries()).map(([id, gs]) => ({ id, groups: gs.slice().reverse() }));
126
- };
127
-
128
- const dataSourceGroupsByTarget = computed(() => groupByTarget(dataSourceGroups.value));
129
- const codeBlockGroupsByTarget = computed(() => groupByTarget(codeBlockGroups.value));
130
-
131
- return {
132
- expanded,
133
- toggleGroup,
134
- pageGroups,
135
- dataSourceGroups,
136
- codeBlockGroups,
137
- pageGroupsDisplay,
138
- dataSourceGroupsByTarget,
139
- codeBlockGroupsByTarget,
140
- };
141
- };
142
-
143
78
  /**
144
79
  * 历史面板的时间展示:
145
80
  * - 当天的记录只显示 `HH:mm:ss`;
@@ -207,7 +142,11 @@ export const sourceLabel = (source: HistoryOpSource = 'unknown'): string => {
207
142
  export const groupSource = (group: { steps: { step: { source?: HistoryOpSource } }[] }): HistoryOpSource | undefined =>
208
143
  group.steps[group.steps.length - 1]?.step.source;
209
144
 
210
- /** {@link toRowGroup} 接受的最小分组结构,PageHistoryGroup 与 HistoryBucketGroup 均满足。 */
145
+ /** 取一组历史步骤里最后一步(最近一次)的操作人,用于组头部展示。 */
146
+ export const groupOperator = (group: { steps: { step: { operator?: string } }[] }): string | undefined =>
147
+ group.steps[group.steps.length - 1]?.step.operator;
148
+
149
+ /** {@link toRowGroup} 接受的最小分组结构 */
211
150
  interface RowGroupInput<T extends BaseStepValue = BaseStepValue> {
212
151
  applied: boolean;
213
152
  isCurrent?: boolean;
@@ -227,24 +166,28 @@ export const toRowGroup = <T extends BaseStepValue = BaseStepValue>(
227
166
  ): HistoryRowGroup => {
228
167
  const { describeGroup, describeStep, isStepDiffable, isStepRevertable } = descriptor;
229
168
  const timestamp = groupTimestamp(group);
169
+ // 无 describeGroup 时回退到组内最后一步的 describeStep:数据源/代码块不做相邻合并,每组恒为单步,二者等价。
170
+ const lastStep = group.steps[group.steps.length - 1]?.step;
230
171
  return {
231
172
  key,
232
173
  applied: group.applied,
233
174
  isCurrent: Boolean(group.isCurrent),
234
175
  opType: group.opType,
235
- desc: describeGroup(group),
176
+ desc: describeGroup ? describeGroup(group) : describeStep(lastStep),
236
177
  source: groupSource(group),
178
+ operator: groupOperator(group),
237
179
  time: formatHistoryTime(timestamp),
238
180
  timeTitle: formatHistoryFullTime(timestamp),
239
181
  subSteps: group.steps.map((s) => ({
240
182
  index: s.index,
241
183
  applied: s.applied,
242
- isCurrent: s.isCurrent,
184
+ isCurrent: Boolean(s.isCurrent),
243
185
  saved: s.step.saved,
244
186
  desc: describeStep(s.step),
245
187
  diffable: isStepDiffable ? isStepDiffable(s.step) : false,
246
188
  revertable: s.applied && (isStepRevertable ? isStepRevertable(s.step) : true),
247
189
  source: s.step.source,
190
+ operator: s.step.operator,
248
191
  time: formatHistoryTime(s.step.timestamp),
249
192
  timeTitle: formatHistoryFullTime(s.step.timestamp),
250
193
  })),
@@ -273,102 +216,55 @@ const pickLastDescription = (descs: (string | undefined)[]): string | undefined
273
216
  return undefined;
274
217
  };
275
218
 
276
- export const describePageStep = (step: StepValue) => {
219
+ /**
220
+ * 页面 / 数据源 / 代码块三类历史共用的单步描述核心。
221
+ * 各类型只在「取展示名」与「实体单位名」上有差异,通过参数注入,文案模板完全一致:
222
+ * - 新增 / 删除:单实体展示「label」,多实体(仅页面可能出现)退化为「N 个X」;
223
+ * - 修改:展示「label · propPath」,无 diff 时兜底「X」,多实体退化为「N 个X」。
224
+ * 操作类型(新增 / 删除 / 修改)已由列表行的 op 徽标单独展示,故描述文案不再重复动词。
225
+ * 展示 id 统一取 schema.id;调用方显式传入的 historyDescription 永远优先。
226
+ */
227
+ export const describeStep = <T>(
228
+ step: BaseStepValue<T>,
229
+ getLabel: (_schema?: T) => string | number | undefined,
230
+ unit: string,
231
+ ): string => {
277
232
  if (step.historyDescription) return step.historyDescription;
278
- const { opType } = step;
279
233
  const items = step.diff ?? [];
280
- if (opType === 'add') {
281
- const count = items.length;
234
+ const label = (schema?: T) => labelWithId(getLabel(schema), (schema as { id?: string | number } | undefined)?.id);
235
+
236
+ if (step.opType === 'add') {
282
237
  const node = items[0]?.newSchema;
283
- return `新增 ${count} 个节点${count === 1 && node ? `(${labelWithId(nameOf(node), node.id)})` : ''}`;
238
+ return items.length === 1 && node ? label(node) : `${items.length} 个${unit}`;
284
239
  }
285
- if (opType === 'remove') {
286
- const count = items.length;
240
+ if (step.opType === 'remove') {
287
241
  const node = items[0]?.oldSchema;
288
- return `删除 ${count} 个节点${count === 1 && node ? `(${labelWithId(nameOf(node), node.id)})` : ''}`;
242
+ return items.length === 1 && node ? label(node) : `${items.length} 个${unit}`;
289
243
  }
290
- if (!items.length) return '修改节点';
244
+ if (!items.length) return unit;
291
245
  if (items.length === 1) {
292
- const { newSchema, changeRecords } = items[0];
293
- const propPath = changeRecords?.[0]?.propPath;
294
- const target = labelWithId(nameOf(newSchema), newSchema?.id);
295
- return `修改 ${target}${propPath ? ` · ${propPath}` : ''}`;
246
+ const { newSchema, oldSchema, changeRecords } = items[0];
247
+ const propPath = changeRecords?.map((changeRecord) => changeRecord.propPath).join(',');
248
+ const target = label(newSchema ?? oldSchema);
249
+ return propPath ? `${target} · ${propPath}` : target;
296
250
  }
297
- return `修改 ${items.length} 个节点`;
251
+ return `${items.length} 个${unit}`;
298
252
  };
299
253
 
254
+ export const describePageStep = (step: StepValue): string => describeStep(step, (node) => nameOf(node), '节点');
255
+
300
256
  /**
301
257
  * 合并组的展示文案:
302
258
  * - 若组内任一步显式提供了 historyDescription:取最后一条非空 historyDescription(最近一次的描述更准确);
303
259
  * - 单步组:复用 describePageStep;
304
260
  * - 多步组(连续修改同一节点):展示节点名 + 涉及的前几个 propPath。
305
261
  */
306
- export const describePageGroup = (group: PageHistoryGroup) => {
262
+ export const describePageGroup = (group: HistoryGroup<StepValue>) => {
307
263
  const lastDesc = pickLastDescription(group.steps.map((s) => s.step.historyDescription));
308
264
  if (lastDesc) return lastDesc;
309
265
  if (group.steps.length === 1) return describePageStep(group.steps[0].step);
310
- const paths = new Set<string>();
311
- group.steps.forEach((s) => {
312
- s.step.diff?.[0]?.changeRecords?.forEach((r) => r.propPath && paths.add(r.propPath));
313
- });
314
- const pathList = Array.from(paths).slice(0, 3).join(', ');
315
- const target = labelWithId(
316
- group.targetName ?? (group.targetId !== undefined ? `${group.targetId}` : '节点'),
317
- group.targetId,
318
- );
319
- return pathList ? `修改 ${target} · ${pathList}${paths.size > 3 ? '…' : ''}` : `修改 ${target}`;
320
- };
321
266
 
322
- export const describeDataSourceStep = (step: DataSourceStepValue) => {
323
- if (step.historyDescription) return step.historyDescription;
324
- const { oldSchema: oldSchema, newSchema: newSchema, changeRecords } = step.diff?.[0] ?? {};
325
- if (!oldSchema && newSchema) return `创建 ${labelWithId(newSchema.title, newSchema.id ?? step.id)}`;
326
- if (!newSchema && oldSchema) return `删除 ${labelWithId(oldSchema.title, oldSchema.id ?? step.id)}`;
327
- const propPath = changeRecords?.[0]?.propPath;
328
- const title = labelWithId(newSchema?.title || oldSchema?.title, step.id);
329
- return propPath ? `修改 ${title} · ${propPath}` : `修改 ${title}`;
330
- };
331
-
332
- export const describeDataSourceGroup = (group: DataSourceHistoryGroup) => {
333
- const lastDesc = pickLastDescription(group.steps.map((s) => s.step.historyDescription));
334
- if (lastDesc) return lastDesc;
335
- if (group.steps.length === 1) return describeDataSourceStep(group.steps[0].step);
336
- const paths = new Set<string>();
337
- group.steps.forEach((s) => {
338
- s.step.diff?.[0]?.changeRecords?.forEach((r) => r.propPath && paths.add(r.propPath));
339
- });
340
- const pathList = Array.from(paths).slice(0, 3).join(', ');
341
- const rawTitle =
342
- group.steps[group.steps.length - 1].step.diff?.[0]?.newSchema?.title ||
343
- group.steps[0].step.diff?.[0]?.oldSchema?.title;
344
- const target = labelWithId(rawTitle, group.id);
345
- return pathList ? `修改 ${target} · ${pathList}${paths.size > 3 ? '…' : ''}` : `修改 ${target}`;
346
- };
347
-
348
- export const describeCodeBlockStep = (step: CodeBlockStepValue) => {
349
- if (step.historyDescription) return step.historyDescription;
350
- const { oldSchema: oldContent, newSchema: newContent, changeRecords } = step.diff?.[0] ?? {};
351
- if (!oldContent && newContent) return `创建 ${labelWithId(newContent.name, newContent.id ?? step.id)}`;
352
- if (!newContent && oldContent) return `删除 ${labelWithId(oldContent.name, oldContent.id ?? step.id)}`;
353
- const propPath = changeRecords?.[0]?.propPath;
354
- const title = labelWithId(newContent?.name || oldContent?.name, step.id);
355
- return propPath ? `修改 ${title} · ${propPath}` : `修改 ${title}`;
356
- };
357
-
358
- export const describeCodeBlockGroup = (group: CodeBlockHistoryGroup) => {
359
- const lastDesc = pickLastDescription(group.steps.map((s) => s.step.historyDescription));
360
- if (lastDesc) return lastDesc;
361
- if (group.steps.length === 1) return describeCodeBlockStep(group.steps[0].step);
362
- const paths = new Set<string>();
363
- group.steps.forEach((s) => {
364
- s.step.diff?.[0]?.changeRecords?.forEach((r) => r.propPath && paths.add(r.propPath));
365
- });
366
- const pathList = Array.from(paths).slice(0, 3).join(', ');
367
- const rawName =
368
- group.steps[group.steps.length - 1].step.diff?.[0]?.newSchema?.name ||
369
- group.steps[0].step.diff?.[0]?.oldSchema?.name;
370
- const target = labelWithId(rawName, group.id);
371
- return pathList ? `修改 ${target} · ${pathList}${paths.size > 3 ? '…' : ''}` : `修改 ${target}`;
267
+ return labelWithId(group.targetName ?? (group.targetId !== undefined ? `${group.targetId}` : '节点'), group.targetId);
372
268
  };
373
269
 
374
270
  /**
@@ -385,23 +281,29 @@ export const isPageStepRevertable = (step: StepValue): boolean => {
385
281
  };
386
282
 
387
283
  /**
388
- * 数据源 step 是否支持「回滚」:
284
+ * diff 项历史(数据源 / 代码块)是否支持「回滚」:
389
285
  * - 新增(无 oldSchema)/ 删除(无 newSchema):不依赖 changeRecords,始终可回滚;
390
- * - 更新(前后 schema 都存在):必须有 changeRecords 才支持局部反向 patch,否则不支持回滚。
286
+ * - 更新(前后内容都存在):必须有 changeRecords 才支持局部反向 patch,否则不支持回滚。
391
287
  */
392
- export const isDataSourceStepRevertable = (step: DataSourceStepValue): boolean => {
288
+ export const isSingleDiffStepRevertable = (step: BaseStepValue): boolean => {
393
289
  const item = step.diff?.[0];
394
290
  if (!item?.oldSchema || !item?.newSchema) return true;
395
291
  return Boolean(item.changeRecords?.length);
396
292
  };
397
293
 
398
294
  /**
399
- * 代码块 step 是否支持「回滚」:
400
- * - 新增(无 oldSchema)/ 删除(无 newSchema):不依赖 changeRecords,始终可回滚;
401
- * - 更新(前后 content 都存在):必须有 changeRecords 才支持局部反向 patch,否则不支持回滚。
295
+ * 通用二次确认弹窗:清空历史 / 无法差异对比的回滚等会改变状态的操作,先弹出确认框,
296
+ * 用户点击「确定」返回 true,取消(confirm reject)时返回 false 并静默忽略。
402
297
  */
403
- export const isCodeBlockStepRevertable = (step: CodeBlockStepValue): boolean => {
404
- const item = step.diff?.[0];
405
- if (!item?.oldSchema || !item?.newSchema) return true;
406
- return Boolean(item.changeRecords?.length);
298
+ export const confirmHistoryAction = async (message: string): Promise<boolean> => {
299
+ try {
300
+ await tMagicMessageBox.confirm(message, '提示', {
301
+ confirmButtonText: '确定',
302
+ cancelButtonText: '取消',
303
+ type: 'warning',
304
+ });
305
+ return true;
306
+ } catch {
307
+ return false;
308
+ }
407
309
  };
@@ -0,0 +1,60 @@
1
+ import { computed, reactive } from 'vue';
2
+
3
+ import { useServices } from '@editor/hooks/use-services';
4
+
5
+ /**
6
+ * 历史记录面板共享逻辑:
7
+ * - 暴露三类历史的聚合数据(页面 / 数据源 / 代码块);
8
+ * - 提供折叠状态管理;
9
+ * - 提供操作描述文案生成器。
10
+ *
11
+ * 所有数据基于 historyService 的 reactive state 派生,自动跟随历史变化刷新。
12
+ */
13
+ export const useHistoryList = () => {
14
+ const { editorService, historyService } = useServices();
15
+
16
+ /**
17
+ * 折叠状态:key 形如 `pg-${组内首步 index}` / `ds-${id}-${组内首步 index}` / `cb-${id}-${组内首步 index}`。
18
+ * 用组内首步的稳定 index(而非展示位置)作为 key,确保历史数据更新后已展开的分组状态保持不变。
19
+ * 合并组默认展开;仅当值为 `false` 时表示收起。
20
+ */
21
+ const expanded = reactive<Record<string, boolean>>({});
22
+ const toggleGroup = (key: string) => {
23
+ expanded[key] = expanded[key] === false;
24
+ };
25
+
26
+ const pageGroups = computed(() => historyService.getHistoryGroups('page', editorService.get('page')?.id));
27
+ const dataSourceGroups = computed(() => historyService.getHistoryGroups('dataSource'));
28
+ const codeBlockGroups = computed(() => historyService.getHistoryGroups('codeBlock'));
29
+
30
+ /** 页面 tab 倒序展示(最新一组在最上面)。 */
31
+ const pageGroupsDisplay = computed(() => pageGroups.value.slice().reverse());
32
+
33
+ /**
34
+ * 把按时间正序的 group 列表,再按 id 聚拢成 bucket(同 id 的所有分组放一起)。
35
+ * 每个 bucket 内部仍然按时间倒序展示(最近的操作最先看到)。
36
+ */
37
+ const groupByTarget = <G extends { id: string | number }>(groups: G[]) => {
38
+ const map = new Map<string | number, G[]>();
39
+ groups.forEach((g) => {
40
+ const list = map.get(g.id) ?? [];
41
+ list.push(g);
42
+ map.set(g.id, list);
43
+ });
44
+ return Array.from(map.entries()).map(([id, gs]) => ({ id, groups: gs.slice().reverse() }));
45
+ };
46
+
47
+ const dataSourceGroupsByTarget = computed(() => groupByTarget(dataSourceGroups.value));
48
+ const codeBlockGroupsByTarget = computed(() => groupByTarget(codeBlockGroups.value));
49
+
50
+ return {
51
+ expanded,
52
+ toggleGroup,
53
+ pageGroups,
54
+ dataSourceGroups,
55
+ codeBlockGroups,
56
+ pageGroupsDisplay,
57
+ dataSourceGroupsByTarget,
58
+ codeBlockGroupsByTarget,
59
+ };
60
+ };