@tmagic/editor 1.8.0-beta.7 → 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 (36) 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 +158 -301
  7. package/dist/es/layouts/history-list/composables.js +23 -55
  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/services/codeBlock.js +32 -28
  11. package/dist/es/services/dataSource.js +43 -34
  12. package/dist/es/services/editor.js +31 -26
  13. package/dist/es/services/history.js +268 -384
  14. package/dist/es/style.css +12 -0
  15. package/dist/es/utils/history.js +35 -47
  16. package/dist/style.css +12 -0
  17. package/dist/tmagic-editor.umd.cjs +3774 -3003
  18. package/package.json +7 -7
  19. package/src/components/CompareForm.vue +14 -6
  20. package/src/index.ts +2 -0
  21. package/src/layouts/NavMenu.vue +2 -2
  22. package/src/layouts/history-list/GroupRow.vue +10 -0
  23. package/src/layouts/history-list/HistoryDiffDialog.vue +6 -1
  24. package/src/layouts/history-list/HistoryListPanel.vue +53 -250
  25. package/src/layouts/history-list/PageTab.vue +4 -4
  26. package/src/layouts/history-list/composables.ts +52 -90
  27. package/src/layouts/history-list/useHistoryList.ts +60 -0
  28. package/src/layouts/history-list/useHistoryRevert.ts +400 -0
  29. package/src/services/codeBlock.ts +30 -29
  30. package/src/services/dataSource.ts +37 -37
  31. package/src/services/editor.ts +32 -29
  32. package/src/services/history.ts +340 -430
  33. package/src/theme/history-list-panel.scss +14 -0
  34. package/src/type.ts +174 -92
  35. package/src/utils/history.ts +52 -67
  36. package/types/index.d.ts +429 -281
@@ -1,5 +1,5 @@
1
1
  import BaseService from "./BaseService.js";
2
- import { describeRevertStep, getLastPushedHistoryIds } from "../utils/history.js";
2
+ import { createStackStep, describeRevertStep, getLastPushedHistoryIds } from "../utils/history.js";
3
3
  import history_default from "./history.js";
4
4
  import storage_default, { Protocol } from "./storage.js";
5
5
  import { COPY_DS_STORAGE_KEY } from "../utils/editor.js";
@@ -92,12 +92,15 @@ var DataSource = class extends BaseService {
92
92
  id: config.id && !this.getDataSourceById(config.id) ? config.id : this.createId()
93
93
  };
94
94
  this.get("dataSources").push(newConfig);
95
- if (!doNotPushHistory) this.lastPushedHistoryId = history_default.pushDataSource(newConfig.id, {
96
- oldSchema: null,
97
- newSchema: newConfig,
98
- historyDescription,
99
- source: historySource
100
- })?.uuid ?? null;
95
+ if (!doNotPushHistory) {
96
+ const step = createStackStep(newConfig.id, {
97
+ oldValue: null,
98
+ newValue: newConfig,
99
+ historyDescription,
100
+ source: historySource
101
+ });
102
+ this.lastPushedHistoryId = (step ? history_default.push("dataSource", step, newConfig.id) : null)?.uuid ?? null;
103
+ }
101
104
  this.emit("add", newConfig);
102
105
  return newConfig;
103
106
  }
@@ -115,13 +118,16 @@ var DataSource = class extends BaseService {
115
118
  const oldConfig = dataSources[index];
116
119
  const newConfig = cloneDeep$1(config);
117
120
  dataSources[index] = newConfig;
118
- if (!doNotPushHistory) this.lastPushedHistoryId = history_default.pushDataSource(newConfig.id, {
119
- oldSchema: oldConfig ? cloneDeep$1(oldConfig) : null,
120
- newSchema: newConfig,
121
- changeRecords,
122
- historyDescription,
123
- source: historySource
124
- })?.uuid ?? null;
121
+ if (!doNotPushHistory) {
122
+ const step = createStackStep(newConfig.id, {
123
+ oldValue: oldConfig ? cloneDeep$1(oldConfig) : null,
124
+ newValue: newConfig,
125
+ changeRecords,
126
+ historyDescription,
127
+ source: historySource
128
+ });
129
+ this.lastPushedHistoryId = (step ? history_default.push("dataSource", step, newConfig.id) : null)?.uuid ?? null;
130
+ }
125
131
  this.emit("update", newConfig, {
126
132
  oldConfig,
127
133
  changeRecords
@@ -140,12 +146,15 @@ var DataSource = class extends BaseService {
140
146
  const index = dataSources.findIndex((ds) => ds.id === id);
141
147
  const oldConfig = index !== -1 ? dataSources[index] : null;
142
148
  dataSources.splice(index, 1);
143
- if (oldConfig && !doNotPushHistory) this.lastPushedHistoryId = history_default.pushDataSource(id, {
144
- oldSchema: cloneDeep$1(oldConfig),
145
- newSchema: null,
146
- historyDescription,
147
- source: historySource
148
- })?.uuid ?? null;
149
+ if (oldConfig && !doNotPushHistory) {
150
+ const step = createStackStep(id, {
151
+ oldValue: cloneDeep$1(oldConfig),
152
+ newValue: null,
153
+ historyDescription,
154
+ source: historySource
155
+ });
156
+ this.lastPushedHistoryId = (step ? history_default.push("dataSource", step, id) : null)?.uuid ?? null;
157
+ }
149
158
  this.emit("remove", id);
150
159
  }
151
160
  /**
@@ -191,7 +200,7 @@ var DataSource = class extends BaseService {
191
200
  * @returns 撤销的 step;栈不存在或已无可撤销时返回 null
192
201
  */
193
202
  undo(id) {
194
- const step = history_default.undoDataSource(id);
203
+ const step = history_default.undo("dataSource", id);
195
204
  if (!step) return null;
196
205
  this.applyHistoryStep(step, true);
197
206
  return step;
@@ -202,18 +211,18 @@ var DataSource = class extends BaseService {
202
211
  * @returns 重做的 step;栈不存在或已无可重做时返回 null
203
212
  */
204
213
  redo(id) {
205
- const step = history_default.redoDataSource(id);
214
+ const step = history_default.redo("dataSource", id);
206
215
  if (!step) return null;
207
216
  this.applyHistoryStep(step, false);
208
217
  return step;
209
218
  }
210
219
  /** 是否可对指定数据源撤销。 */
211
220
  canUndo(id) {
212
- return history_default.canUndoDataSource(id);
221
+ return history_default.canUndo("dataSource", id);
213
222
  }
214
223
  /** 是否可对指定数据源重做。 */
215
224
  canRedo(id) {
216
- return history_default.canRedoDataSource(id);
225
+ return history_default.canRedo("dataSource", id);
217
226
  }
218
227
  /**
219
228
  * 跳转指定数据源的历史栈到目标游标。语义同 editor.gotoPageStep。
@@ -223,7 +232,7 @@ var DataSource = class extends BaseService {
223
232
  * @returns 实际移动到的最终游标位置
224
233
  */
225
234
  goto(id, targetCursor) {
226
- let cursor = history_default.getDataSourceCursor(id);
235
+ let cursor = history_default.getCursor("dataSource", id);
227
236
  const target = Math.max(0, targetCursor);
228
237
  while (cursor > target) {
229
238
  if (!this.undo(id)) break;
@@ -246,11 +255,11 @@ var DataSource = class extends BaseService {
246
255
  * @returns 反向后产生的新 step;目标不存在 / 未应用时返回 null
247
256
  */
248
257
  revert(id, index) {
249
- const entry = history_default.getDataSourceStepList(id)[index];
258
+ const entry = history_default.getStepList("dataSource", id)[index];
250
259
  if (!entry?.applied) return null;
251
260
  const { oldSchema, newSchema, changeRecords } = entry.step.diff?.[0] ?? {};
252
261
  if (oldSchema && newSchema && !changeRecords?.length) return null;
253
- const description = `回滚 #${index + 1}: ${describeRevertStep(entry.step.id, entry.step.diff?.[0], (s) => s.title)}`;
262
+ const description = `回滚 #${index + 1}: ${describeRevertStep(entry.step.data.id, entry.step.diff?.[0], (s) => s.title)}`;
254
263
  return this.applyRevertStep(entry.step, description);
255
264
  }
256
265
  /**
@@ -262,7 +271,7 @@ var DataSource = class extends BaseService {
262
271
  */
263
272
  revertById(uuids) {
264
273
  return uuids.map((uuid) => {
265
- const location = history_default.findDataSourceStepLocationByUuid(uuid);
274
+ const location = history_default.findStepLocationByUuid("dataSource", uuid);
266
275
  if (!location) return null;
267
276
  return this.revert(location.id, location.index);
268
277
  });
@@ -325,21 +334,21 @@ var DataSource = class extends BaseService {
325
334
  * 同构,差异仅在于走对应的公共 add / update / remove 而不是带 doNotPushHistory 的版本。
326
335
  */
327
336
  applyRevertStep(step, historyDescription) {
328
- const { id } = step;
337
+ const { id } = step.data;
329
338
  const { oldSchema, newSchema, changeRecords } = step.diff?.[0] ?? {};
330
339
  if (!oldSchema && newSchema) {
331
340
  this.remove(`${id}`, {
332
341
  historyDescription,
333
342
  historySource: "rollback"
334
343
  });
335
- return history_default.getDataSourceStepList(id).slice(-1)[0]?.step ?? null;
344
+ return history_default.getStepList("dataSource", id).slice(-1)[0]?.step ?? null;
336
345
  }
337
346
  if (oldSchema && !newSchema) {
338
347
  this.add(cloneDeep$1(oldSchema), {
339
348
  historyDescription,
340
349
  historySource: "rollback"
341
350
  });
342
- return history_default.getDataSourceStepList(id).slice(-1)[0]?.step ?? null;
351
+ return history_default.getStepList("dataSource", id).slice(-1)[0]?.step ?? null;
343
352
  }
344
353
  if (!oldSchema || !newSchema) return null;
345
354
  if (changeRecords?.length) {
@@ -360,13 +369,13 @@ var DataSource = class extends BaseService {
360
369
  historyDescription,
361
370
  historySource: "rollback"
362
371
  });
363
- return history_default.getDataSourceStepList(id).slice(-1)[0]?.step ?? null;
372
+ return history_default.getStepList("dataSource", id).slice(-1)[0]?.step ?? null;
364
373
  }
365
374
  this.update(cloneDeep$1(oldSchema), {
366
375
  historyDescription,
367
376
  historySource: "rollback"
368
377
  });
369
- return history_default.getDataSourceStepList(id).slice(-1)[0]?.step ?? null;
378
+ return history_default.getStepList("dataSource", id).slice(-1)[0]?.step ?? null;
370
379
  }
371
380
  /**
372
381
  * 把一条历史 step 应用到当前数据源服务上。
@@ -383,7 +392,7 @@ var DataSource = class extends BaseService {
383
392
  * @param reverse true=撤销,false=重做
384
393
  */
385
394
  applyHistoryStep(step, reverse) {
386
- const { id } = step;
395
+ const { id } = step.data;
387
396
  const { oldSchema, newSchema, changeRecords } = step.diff?.[0] ?? {};
388
397
  if (!oldSchema && newSchema) {
389
398
  if (reverse) this.remove(`${id}`, { doNotPushHistory: true });
@@ -74,7 +74,7 @@ var Editor = class extends BaseService {
74
74
  this.state.stageLoading = this.state.pageLength !== 0;
75
75
  if (preValue && !isEmpty(preValue)) this.pushRootDiffHistory(preValue, app, options.historySource);
76
76
  else app.items?.forEach((pageNode) => {
77
- if (pageNode?.id !== void 0 && !history_default.getPageMarker(pageNode.id)) history_default.setPageMarker(pageNode.id, {
77
+ if (pageNode?.id !== void 0 && !history_default.getMarker("page", pageNode.id)) history_default.setMarker("page", pageNode.id, {
78
78
  name: pageNode.name,
79
79
  source: options.historySource
80
80
  });
@@ -176,8 +176,6 @@ var Editor = class extends BaseService {
176
176
  this.set("nodes", node ? [node] : []);
177
177
  this.set("page", page);
178
178
  this.set("parent", parent);
179
- if (page) history_default.changePage(toRaw(page));
180
- else history_default.resetState();
181
179
  if (node?.id) this.get("stage")?.renderer?.runtime?.getApp?.()?.page?.emit("editor:select", {
182
180
  node,
183
181
  page,
@@ -368,10 +366,7 @@ var Editor = class extends BaseService {
368
366
  if (pages[0]) {
369
367
  await this.select(pages[0]);
370
368
  stage?.select(pages[0].id);
371
- } else {
372
- this.selectRoot();
373
- history_default.resetPage();
374
- }
369
+ } else this.selectRoot();
375
370
  };
376
371
  const rootItems = root.items || [];
377
372
  if (isPage(node)) {
@@ -875,7 +870,9 @@ var Editor = class extends BaseService {
875
870
  * @returns 被撤销的操作
876
871
  */
877
872
  async undo() {
878
- const value = history_default.undo();
873
+ const pageId = this.get("page")?.id;
874
+ if (pageId === void 0) return null;
875
+ const value = history_default.undo("page", pageId);
879
876
  if (value) await this.applyHistoryOp(value, true);
880
877
  return value;
881
878
  }
@@ -884,7 +881,9 @@ var Editor = class extends BaseService {
884
881
  * @returns 被恢复的操作
885
882
  */
886
883
  async redo() {
887
- const value = history_default.redo();
884
+ const pageId = this.get("page")?.id;
885
+ if (pageId === void 0) return null;
886
+ const value = history_default.redo("page", pageId);
888
887
  if (value) await this.applyHistoryOp(value, false);
889
888
  return value;
890
889
  }
@@ -905,7 +904,7 @@ var Editor = class extends BaseService {
905
904
  * @returns 反向后产生的新 step;目标不存在 / 未应用 / 反向失败时返回 null
906
905
  */
907
906
  async revertPageStep(index) {
908
- const entry = history_default.getPageStepList()[index];
907
+ const entry = history_default.getStepList("page", this.get("page")?.id)[index];
909
908
  if (!entry?.applied) return null;
910
909
  const { step } = entry;
911
910
  if (step.opType === "initial") return null;
@@ -915,7 +914,7 @@ var Editor = class extends BaseService {
915
914
  if (!items.length || !items.every((item) => item.changeRecords?.length)) return null;
916
915
  }
917
916
  let revertedStep = null;
918
- const captureRevert = (s) => {
917
+ const captureRevert = (_pageId, s) => {
919
918
  revertedStep = s;
920
919
  };
921
920
  history_default.once("change", captureRevert);
@@ -985,9 +984,10 @@ var Editor = class extends BaseService {
985
984
  */
986
985
  async revertPageStepById(uuids) {
987
986
  const results = [];
987
+ const pageId = this.get("page")?.id;
988
988
  for (const uuid of uuids) {
989
- const index = history_default.getPageStepIndexByUuid(uuid);
990
- results.push(index < 0 ? null : await this.revertPageStep(index));
989
+ const location = history_default.findStepLocationByUuid("page", uuid, pageId);
990
+ results.push(!location ? null : await this.revertPageStep(location.index));
991
991
  }
992
992
  return results;
993
993
  }
@@ -1001,8 +1001,9 @@ var Editor = class extends BaseService {
1001
1001
  * @returns 实际移动到的最终游标位置
1002
1002
  */
1003
1003
  async gotoPageStep(targetCursor) {
1004
- let cursor = history_default.getPageCursor();
1005
- const { length } = history_default.getPageStepList();
1004
+ const pageId = this.get("page")?.id;
1005
+ let cursor = history_default.getCursor("page", pageId);
1006
+ const { length } = history_default.getStepList("page", pageId);
1006
1007
  const target = Math.max(0, Math.min(targetCursor, length));
1007
1008
  while (cursor > target) {
1008
1009
  if (!await this.undo()) break;
@@ -1117,30 +1118,34 @@ var Editor = class extends BaseService {
1117
1118
  id: page.id
1118
1119
  },
1119
1120
  opType,
1120
- selectedBefore: [],
1121
- selectedAfter: [],
1122
- modifiedNodeIds: /* @__PURE__ */ new Map(),
1121
+ extra: {
1122
+ selectedBefore: [],
1123
+ selectedAfter: [],
1124
+ modifiedNodeIds: /* @__PURE__ */ new Map()
1125
+ },
1123
1126
  diff: [diffItem],
1124
1127
  rootStep: true
1125
1128
  };
1126
1129
  if (source) step.source = source;
1127
1130
  const top = history_default.getCurrentPageStep(page.id);
1128
- if (top?.rootStep && top.source === source) history_default.replaceCurrentPageStep(step, page.id);
1129
- else history_default.push(step, page.id);
1131
+ if (top?.rootStep && top.source === source) history_default.replaceCurrentStep("page", step, page.id);
1132
+ else history_default.push("page", step, page.id);
1130
1133
  }
1131
1134
  pushOpHistory(opType, { diff, pageData, historyDescription, source }) {
1132
1135
  const step = {
1133
1136
  uuid: guid(),
1134
1137
  data: pageData,
1135
1138
  opType,
1136
- selectedBefore: this.selectionBeforeOp ?? [],
1137
- selectedAfter: this.get("nodes").map((n) => n.id),
1138
- modifiedNodeIds: new Map(this.get("modifiedNodeIds")),
1139
+ extra: {
1140
+ selectedBefore: this.selectionBeforeOp ?? [],
1141
+ selectedAfter: this.get("nodes").map((n) => n.id),
1142
+ modifiedNodeIds: new Map(this.get("modifiedNodeIds"))
1143
+ },
1139
1144
  diff
1140
1145
  };
1141
1146
  if (historyDescription) step.historyDescription = historyDescription;
1142
1147
  if (source) step.source = source;
1143
- const historyId = history_default.push(step, pageData.id) ? step.uuid : null;
1148
+ const historyId = history_default.push("page", step, pageData.id) ? step.uuid : null;
1144
1149
  this.lastPushedHistoryId = historyId;
1145
1150
  this.selectionBeforeOp = null;
1146
1151
  return historyId;
@@ -1241,10 +1246,10 @@ var Editor = class extends BaseService {
1241
1246
  break;
1242
1247
  }
1243
1248
  }
1244
- this.set("modifiedNodeIds", step.modifiedNodeIds);
1249
+ this.set("modifiedNodeIds", step.extra?.modifiedNodeIds ?? /* @__PURE__ */ new Map());
1245
1250
  const page = toRaw(this.get("page"));
1246
1251
  if (page) {
1247
- const selectIds = reverse ? step.selectedBefore : step.selectedAfter;
1252
+ const selectIds = (reverse ? step.extra?.selectedBefore : step.extra?.selectedAfter) ?? [];
1248
1253
  setTimeout(() => {
1249
1254
  if (!selectIds.length) return;
1250
1255
  if (selectIds.length > 1) {