@tmagic/editor 1.8.6 → 1.8.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.
package/package.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "1.8.6",
2
+ "version": "1.8.7",
3
3
  "name": "@tmagic/editor",
4
4
  "type": "module",
5
5
  "sideEffects": [
@@ -58,11 +58,11 @@
58
58
  ],
59
59
  "dependencies": {
60
60
  "@element-plus/icons-vue": "^2.3.2",
61
- "@tmagic/design": "1.8.6",
62
- "@tmagic/form": "1.8.6",
63
- "@tmagic/stage": "1.8.6",
64
- "@tmagic/table": "1.8.6",
65
- "@tmagic/utils": "1.8.6",
61
+ "@tmagic/design": "1.8.7",
62
+ "@tmagic/form": "1.8.7",
63
+ "@tmagic/stage": "1.8.7",
64
+ "@tmagic/table": "1.8.7",
65
+ "@tmagic/utils": "1.8.7",
66
66
  "buffer": "^6.0.3",
67
67
  "deep-object-diff": "^1.1.9",
68
68
  "emmet-monaco-es": "^5.7.0",
@@ -82,7 +82,7 @@
82
82
  "@vue/test-utils": "^2.5.1"
83
83
  },
84
84
  "peerDependencies": {
85
- "@tmagic/core": "1.8.6",
85
+ "@tmagic/core": "1.8.7",
86
86
  "monaco-editor": "^0.58.0",
87
87
  "type-fest": "^5.2.0",
88
88
  "typescript": "^6.0.3",
@@ -26,6 +26,8 @@
26
26
  <InitialRow
27
27
  v-if="config.showInitial !== false"
28
28
  :is-current="isInitial"
29
+ :initial-reachable="initialReachable"
30
+ :marker="marker"
29
31
  :goto-enabled="config.gotoEnabled"
30
32
  @goto-initial="$emit('goto-initial', bucketId)"
31
33
  />
@@ -36,7 +38,7 @@
36
38
  <script lang="ts" setup generic="T extends BaseStepValue = BaseStepValue">
37
39
  import { computed } from 'vue';
38
40
 
39
- import type { BaseStepValue, HistoryBucketConfig } from '@editor/type';
41
+ import type { BaseStepValue, HistoryBucketConfig, StepValue } from '@editor/type';
40
42
 
41
43
  import type { HistoryBucketGroup, HistoryRowGroup } from './composables';
42
44
  import { isHistoryGroupExpanded, toRowGroup } from './composables';
@@ -47,19 +49,27 @@ defineOptions({
47
49
  name: 'MEditorHistoryListBucket',
48
50
  });
49
51
 
50
- const props = defineProps<{
51
- /**
52
- * 该类历史的整体渲染配置(title / prefix / describe* / isStep* / showInitial / gotoEnabled)。
53
- * 由父组件按业务类型注入,组件内部按需读取,避免逐项透传多个 props。
54
- */
55
- config: HistoryBucketConfig<T>;
56
- /** 当前 bucket 对应的目标 id(dataSource.id 或 codeBlock.id),同时用于组装子项的 key。 */
57
- bucketId: string | number;
58
- /** 当前 bucket 下的所有历史分组,按时间倒序展示(最近的操作在前)。 */
59
- groups: HistoryBucketGroup<T>[];
60
- /** 共享的折叠状态表(key -> 是否展开,缺省或 true 为展开、false 为收起),由顶层 panel 统一维护以便跨 tab 复用。 */
61
- expanded: Record<string, boolean>;
62
- }>();
52
+ const props = withDefaults(
53
+ defineProps<{
54
+ /**
55
+ * 该类历史的整体渲染配置(title / prefix / describe* / isStep* / showInitial / gotoEnabled)。
56
+ * 由父组件按业务类型注入,组件内部按需读取,避免逐项透传多个 props。
57
+ */
58
+ config: HistoryBucketConfig<T>;
59
+ /** 当前 bucket 对应的目标 id(dataSource.id 或 codeBlock.id),同时用于组装子项的 key。 */
60
+ bucketId: string | number;
61
+ /** 当前 bucket 下的所有历史分组,按时间倒序展示(最近的操作在前)。 */
62
+ groups: HistoryBucketGroup<T>[];
63
+ /** 共享的折叠状态表(key -> 是否展开,缺省或 true 为展开、false 为收起),由顶层 panel 统一维护以便跨 tab 复用。 */
64
+ expanded: Record<string, boolean>;
65
+ /** 该 bucket 对应栈的初始基线(若有),与 PageTab 一致用于初始行的文案 / 时间。 */
66
+ marker?: StepValue;
67
+ /** 「回到初始」是否可达,由顶层 panel 按栈查询注入;缺省为可达。无基线的栈也能据此如实提示。 */
68
+ initialReachable?: boolean;
69
+ }>(),
70
+ // Boolean 类型的 prop 未传时会被 Vue 转成 false,必须显式声明默认值
71
+ { initialReachable: true },
72
+ );
63
73
 
64
74
  defineEmits<{
65
75
  /** 透传子组件 GroupRow 的 toggle,由上层 panel 更新 expanded。 */
@@ -86,6 +96,10 @@ const rowKey = (group: HistoryBucketGroup<T>) => `${props.config.prefix}-${props
86
96
  /** 把原始分组派生为 GroupRow 直接消费的视图模型。 */
87
97
  const toRow = (group: HistoryBucketGroup<T>): HistoryRowGroup => toRowGroup(group, rowKey(group), props.config);
88
98
 
89
- /** 该 bucket 是否处于初始状态(栈 cursor=0),等价于全部 group 都未 applied。 */
90
- const isInitial = computed(() => props.groups.length > 0 && props.groups.every((g) => !g.applied));
99
+ /**
100
+ * 该 bucket 是否处于初始状态:全部 group 都未 applied,且撤销链未因容量裁剪断开。
101
+ * 与 PageTab 使用同一判断条件——空列表 `every` 返回 true,即只有基线、没有任何真实步骤时也视为初始状态;
102
+ * 没有基线的栈也通过 initialReachable 参与判断。
103
+ */
104
+ const isInitial = computed(() => props.initialReachable && props.groups.every((g) => !g.applied));
91
105
  </script>
@@ -14,6 +14,8 @@
14
14
  :bucket-id="bucket.id"
15
15
  :groups="bucket.groups"
16
16
  :expanded="expanded"
17
+ :marker="initialState?.[bucket.id]?.marker"
18
+ :initial-reachable="initialState?.[bucket.id]?.initialReachable"
17
19
  @toggle="(key: string) => $emit('toggle', key)"
18
20
  @goto="(id: string | number, index: number) => $emit('goto', id, index)"
19
21
  @goto-initial="(id: string | number) => $emit('goto-initial', id)"
@@ -27,7 +29,7 @@
27
29
  <script lang="ts" setup generic="T extends BaseStepValue = BaseStepValue">
28
30
  import { TMagicScrollbar } from '@tmagic/design';
29
31
 
30
- import type { BaseStepValue, HistoryBucketConfig } from '@editor/type';
32
+ import type { BaseStepValue, HistoryBucketConfig, StepValue } from '@editor/type';
31
33
 
32
34
  import Bucket from './Bucket.vue';
33
35
  import type { HistoryBucketGroup } from './composables';
@@ -53,6 +55,11 @@ defineProps<{
53
55
  * 这样历史数据更新后已展开的分组状态仍能正确保持。
54
56
  */
55
57
  expanded: Record<string, boolean>;
58
+ /**
59
+ * bucket id -> 对应栈的初始基线与「回到初始」可达性,由顶层 panel 基于 historyService 派生。
60
+ * 缺省时按「有基线 / 可达」渲染,保持纯展示组件的可复用性。
61
+ */
62
+ initialState?: Record<string | number, { marker?: StepValue; initialReachable: boolean }>;
56
63
  }>();
57
64
 
58
65
  defineEmits<{
@@ -26,6 +26,7 @@
26
26
  :list="pageGroupsDisplay"
27
27
  :expanded="expanded"
28
28
  :marker="pageMarker"
29
+ :initial-reachable="pageInitialReachable"
29
30
  @toggle="toggleGroup"
30
31
  @goto="onPageGoto"
31
32
  @goto-initial="onPageGotoInitial"
@@ -51,6 +52,7 @@
51
52
  :config="dataSourceConfig"
52
53
  :buckets="dataSourceGroupsByTarget"
53
54
  :expanded="expanded"
55
+ :initial-state="dataSourceInitialState"
54
56
  @toggle="toggleGroup"
55
57
  @goto="onDataSourceGoto"
56
58
  @goto-initial="onDataSourceGotoInitial"
@@ -75,6 +77,7 @@
75
77
  :config="codeBlockConfig"
76
78
  :buckets="codeBlockGroupsByTarget"
77
79
  :expanded="expanded"
80
+ :initial-state="codeBlockInitialState"
78
81
  @toggle="toggleGroup"
79
82
  @goto="onCodeBlockGoto"
80
83
  @goto-initial="onCodeBlockGotoInitial"
@@ -142,6 +145,7 @@ import type {
142
145
  DataSourceStepValue,
143
146
  HistoryBucketConfig,
144
147
  HistoryListExtraTab,
148
+ StepValue,
145
149
  } from '@editor/type';
146
150
 
147
151
  import BucketTab from './BucketTab.vue';
@@ -209,6 +213,29 @@ const {
209
213
  */
210
214
  const pageMarker = computed(() => historyService.getMarker('page', editorService.get('page')?.id));
211
215
 
216
+ /** 当前活动页「回到初始」是否可达(基于栈实例状态,无基线的栈同样适用)。 */
217
+ const pageInitialReachable = computed(() => historyService.isInitialReachable('page', editorService.get('page')?.id));
218
+
219
+ /**
220
+ * 数据源 / 代码块按目标 id 聚合初始基线与可达性,透传给 BucketTab → Bucket,
221
+ * 让这两类 tab 的初始行与 PageTab 使用同一套高亮 / 不可达判定。
222
+ */
223
+ const bucketInitialState = (stepType: 'dataSource' | 'codeBlock') =>
224
+ computed<Record<string, { marker?: StepValue; initialReachable: boolean }>>(() =>
225
+ Object.fromEntries(
226
+ Object.keys(historyService.state.steps[stepType]).map((id) => [
227
+ id,
228
+ {
229
+ marker: historyService.getMarker(stepType, id),
230
+ initialReachable: historyService.isInitialReachable(stepType, id),
231
+ },
232
+ ]),
233
+ ),
234
+ );
235
+
236
+ const dataSourceInitialState = bucketInitialState('dataSource');
237
+ const codeBlockInitialState = bucketInitialState('codeBlock');
238
+
212
239
  /**
213
240
  * 各历史类型的展示名称(页面 / 数据源 / 代码块),由 historyService.state.stepNames 配置,
214
241
  * 业务方可通过 historyService.setStepName / registerStepType 覆盖。基于 reactive state 派生,配置变更后自动刷新。
@@ -1,14 +1,14 @@
1
1
  <template>
2
2
  <li
3
3
  class="m-editor-history-list-item m-editor-history-list-initial"
4
- :class="{ 'is-current': isCurrent, 'is-clickable': !isCurrent }"
4
+ :class="{ 'is-current': isCurrent, 'is-clickable': !isCurrent && initialReachable }"
5
5
  :title="rowTitle"
6
6
  >
7
7
  <span class="m-editor-history-list-item-index" title="历史步骤编号 #0(未修改的初始状态)">#0</span>
8
8
  <span class="m-editor-history-list-item-op op-initial">初始</span>
9
9
  <span class="m-editor-history-list-item-desc">{{ desc }}</span>
10
10
  <span v-if="saved" class="m-editor-history-list-item-saved" title="该记录为最近一次保存的状态">已保存</span>
11
- <span v-if="gotoEnabled && !isCurrent" class="m-editor-history-list-item-actions">
11
+ <span v-if="gotoEnabled && !isCurrent && initialReachable" class="m-editor-history-list-item-actions">
12
12
  <span class="m-editor-history-list-item-goto" title="回到该记录" @click.stop="onClick">回到</span>
13
13
  </span>
14
14
  <span v-if="time" class="m-editor-history-list-item-time" :title="timeTitle">{{ time }}</span>
@@ -24,6 +24,8 @@
24
24
  * 用标记的文案与时间渲染本行;标记不进入撤销/重做栈,仅作为该页基线展示。
25
25
  *
26
26
  * 该行不是真实 step,仅作为 UI 入口;上层负责把"点击"翻译为 `service.goto*(0)`。
27
+ * 若上层注入 `initialReachable === false`(早期记录已被容量裁剪),
28
+ * 则如实显示「不可达」并禁用「回到」,避免把撤销下限误报成初始状态。
27
29
  */
28
30
  import { computed } from 'vue';
29
31
 
@@ -40,21 +42,31 @@ const props = withDefaults(
40
42
  /** 当前对应栈是否已经处于初始状态 (cursor === 0)。true 时用蓝条高亮并禁用点击。 */
41
43
  isCurrent: boolean;
42
44
  gotoEnabled?: boolean;
43
- /** 该页面的「加载/初始」基线记录(设置 root 时生成的 `opType: 'initial'` StepValue);提供时用其文案与时间展示。 */
45
+ /** 「回到初始」是否可达,由上层按栈查询 `historyService.isInitialReachable` 注入;缺省为可达。 */
46
+ initialReachable?: boolean;
47
+ /** 该栈的「加载/初始」基线记录(设置 root 时生成的 `opType: 'initial'` StepValue);提供时用其文案与时间展示。 */
44
48
  marker?: StepValue;
45
49
  }>(),
46
50
  {
47
51
  gotoEnabled: true,
52
+ initialReachable: true,
48
53
  marker: undefined,
49
54
  },
50
55
  );
51
56
 
52
- const desc = computed(() => props.marker?.historyDescription || '未修改的初始状态');
57
+ /**
58
+ * 早期真实步骤被容量裁剪后撤销链已断:撤到底只能停在剩余最老一步的 oldSchema,
59
+ * 不是加载时的初始 DSL,此时该行需明确标注不可达且不再提供「回到」。
60
+ */
61
+ const desc = computed(() =>
62
+ props.initialReachable ? props.marker?.historyDescription || '未修改的初始状态' : '初始状态不可达(早期记录已清理)',
63
+ );
53
64
  /** 基线(初始状态)是否为最近一次保存点:仅页面栈的 `initial` 基线 step 会被 markSaved 标记。 */
54
65
  const saved = computed(() => Boolean(props.marker?.saved));
55
66
  const time = computed(() => formatHistoryTime(props.marker?.timestamp));
56
67
  const timeTitle = computed(() => formatHistoryFullTime(props.marker?.timestamp));
57
68
  const rowTitle = computed(() => {
69
+ if (!props.initialReachable) return '早期历史记录已被容量裁剪,无法回到加载时的初始 DSL';
58
70
  const base = props.marker?.historyDescription || '未修改的初始状态';
59
71
  return props.isCurrent ? `当前已回到${base}` : `点击回到${base}`;
60
72
  });
@@ -65,7 +77,7 @@ const emit = defineEmits<{
65
77
  }>();
66
78
 
67
79
  const onClick = () => {
68
- if (props.isCurrent) return;
80
+ if (props.isCurrent || !props.initialReachable) return;
69
81
  emit('goto-initial');
70
82
  };
71
83
  </script>
@@ -23,7 +23,12 @@
23
23
  作为"未修改"零点。当所有 group 都未 applied 时它即为当前位置。
24
24
  设置 root 时生成的「未修改的初始状态」标记(marker)会作为该行的文案与时间来源。
25
25
  -->
26
- <InitialRow :is-current="isInitial" :marker="marker" @goto-initial="$emit('goto-initial')" />
26
+ <InitialRow
27
+ :is-current="isInitial"
28
+ :initial-reachable="initialReachable"
29
+ :marker="marker"
30
+ @goto-initial="$emit('goto-initial')"
31
+ />
27
32
  </ul>
28
33
  </TMagicScrollbar>
29
34
  </template>
@@ -51,21 +56,27 @@ defineOptions({
51
56
  name: 'MEditorHistoryListPageTab',
52
57
  });
53
58
 
54
- const props = defineProps<{
55
- /** 当前活动页面的历史分组列表,已按时间倒序排好(最新一组在最前)。空数组时显示空态。 */
56
- list: HistoryGroup<StepValue>[];
57
- /**
58
- * 共享的折叠状态表(key -> 是否展开,缺省或 true 为展开、false 为收起),由顶层 panel 统一维护。
59
- * 本 tab 使用 `pg-${组内首步 index}` 作为 key——以稳定的 step 索引而非展示位置标识分组,
60
- * 这样历史数据更新(新增 / 撤销重做导致列表顺序变化)后,已展开的分组状态仍能正确保持。
61
- */
62
- expanded: Record<string, boolean>;
63
- /**
64
- * 当前活动页的「加载/初始」基线记录(设置 root 时生成的 `opType: 'initial'` StepValue)。
65
- * 提供时即使没有任何操作记录也会展示底部初始行,并用其文案 / 时间渲染。
66
- */
67
- marker?: StepValue;
68
- }>();
59
+ const props = withDefaults(
60
+ defineProps<{
61
+ /** 当前活动页面的历史分组列表,已按时间倒序排好(最新一组在最前)。空数组时显示空态。 */
62
+ list: HistoryGroup<StepValue>[];
63
+ /**
64
+ * 共享的折叠状态表(key -> 是否展开,缺省或 true 为展开、false 为收起),由顶层 panel 统一维护。
65
+ * 本 tab 使用 `pg-${组内首步 index}` 作为 key——以稳定的 step 索引而非展示位置标识分组,
66
+ * 这样历史数据更新(新增 / 撤销重做导致列表顺序变化)后,已展开的分组状态仍能正确保持。
67
+ */
68
+ expanded: Record<string, boolean>;
69
+ /**
70
+ * 当前活动页的「加载/初始」基线记录(设置 root 时生成的 `opType: 'initial'` StepValue)。
71
+ * 提供时即使没有任何操作记录也会展示底部初始行,并用其文案 / 时间渲染。
72
+ */
73
+ marker?: StepValue;
74
+ /** 「回到初始」是否可达,由顶层 panel 按当前页栈查询注入;缺省为可达。 */
75
+ initialReachable?: boolean;
76
+ }>(),
77
+ // Boolean 类型的 prop 未传时会被 Vue 转成 false,必须显式声明默认值
78
+ { initialReachable: true },
79
+ );
69
80
 
70
81
  defineEmits<{
71
82
  /** 透传 GroupRow 的 toggle 事件给上层 panel,由其更新 expanded。 */
@@ -110,9 +121,12 @@ const rowKey = (group: HistoryGroup<StepValue>) => `pg-${group.steps[0]?.index}`
110
121
  const toRow = (group: HistoryGroup<StepValue>): HistoryRowGroup => toRowGroup(group, rowKey(group), descriptor);
111
122
 
112
123
  /**
113
- * 是否处于"初始状态"——即对应页面历史栈 cursor===0:
124
+ * 是否处于"初始状态"——即对应页面历史栈撤到底:
114
125
  * 当 list 中所有 group 的 applied 都为 false 时即为该状态(空列表 `every` 返回 true,
115
126
  * 即仅有 marker、无任何操作记录时也视为处于初始状态)。
127
+ *
128
+ * 例外:早期真实步骤被容量裁剪后(initialReachable === false),
129
+ * 撤到底只能停在剩余最老一步的 oldSchema,并非加载时的初始 DSL,此时不再高亮初始行。
116
130
  */
117
- const isInitial = computed(() => props.list.every((g) => !g.applied));
131
+ const isInitial = computed(() => props.initialReachable && props.list.every((g) => !g.applied));
118
132
  </script>
@@ -86,6 +86,16 @@ import { Protocol } from './WebStorage';
86
86
 
87
87
  type MoveItem = { node: MNode; parent: MContainer; pageForOp: { name: string; id: Id } | null };
88
88
 
89
+ /**
90
+ * 撤销 / 重做回放的内部标记键:不导出,也不出现在公开的 {@link HistoryOpOptions} 里。
91
+ * 回放链路用它把 `isHistoryOp` 带进 `change` 事件;外部调用方即使传同名字符串字段也不会被读取,
92
+ * 避免一次真实编辑被误标成历史回放,从而绕过业务侧的加锁 / 埋点 / 自动保存。
93
+ */
94
+ const HISTORY_OP = Symbol('isHistoryOp');
95
+
96
+ /** 读取仅供编辑器内部使用的回放标记。 */
97
+ const isReplay = (options: object): boolean => (options as { [HISTORY_OP]?: boolean })[HISTORY_OP] === true;
98
+
89
99
  /** 把插入下标收敛到 [0, length]:非整数取整,非法 / 越界(含未记录)一律追加到末尾 */
90
100
  const clampIndex = (index: number | undefined, length: number): number => {
91
101
  const normalized = typeof index === 'number' ? Math.trunc(index) : length;
@@ -522,6 +532,7 @@ class Editor extends BaseService {
522
532
  historyDescription,
523
533
  historySource,
524
534
  } = options;
535
+ const isHistoryOp = isReplay(options);
525
536
 
526
537
  const stage = this.get('stage');
527
538
 
@@ -621,6 +632,7 @@ class Editor extends BaseService {
621
632
  data: newNodes.map((node) => ({ node, page: this.getPageOfNode(node.id) })),
622
633
  historySource,
623
634
  doNotPushHistory,
635
+ isHistoryOp,
624
636
  });
625
637
 
626
638
  // 页面 / 页面片新增不入历史栈(见上方 isPageOrFragment 分支),这里合并补发一次结构变更通知
@@ -724,6 +736,7 @@ class Editor extends BaseService {
724
736
  */
725
737
  public async remove(nodeOrNodeList: MNode | MNode[], options: DslOpOptions = {}): Promise<void> {
726
738
  const { doNotPushHistory = false, historyDescription, historySource } = options;
739
+ const isHistoryOp = isReplay(options);
727
740
  this.captureSelectionBeforeOp();
728
741
 
729
742
  const nodes = Array.isArray(nodeOrNodeList) ? nodeOrNodeList : [nodeOrNodeList];
@@ -769,7 +782,7 @@ class Editor extends BaseService {
769
782
  }
770
783
 
771
784
  this.emit('remove', nodes);
772
- this.emit('change', { type: 'remove', data: changeItems, historySource, doNotPushHistory });
785
+ this.emit('change', { type: 'remove', data: changeItems, historySource, doNotPushHistory, isHistoryOp });
773
786
 
774
787
  // 页面 / 页面片删除不入历史栈(见上方 isPageOrFragment 分支),这里合并补发一次结构变更通知
775
788
  const removedPages = nodes.filter((node) => isPageOrFragment(node)) as (MPage | MPageFragment)[];
@@ -875,6 +888,7 @@ class Editor extends BaseService {
875
888
  historySource,
876
889
  invalidInfo,
877
890
  } = data;
891
+ const isHistoryOp = isReplay(data);
878
892
 
879
893
  const nodes = Array.isArray(config) ? config : [config];
880
894
 
@@ -924,6 +938,7 @@ class Editor extends BaseService {
924
938
  data: updateData.map((node) => ({ node, page: this.getPageOfNode(node.newNode.id) })),
925
939
  historySource,
926
940
  doNotPushHistory,
941
+ isHistoryOp,
927
942
  });
928
943
  return Array.isArray(config) ? updateData.map((item) => item.newNode) : updateData[0].newNode;
929
944
  }
@@ -1113,10 +1128,9 @@ class Editor extends BaseService {
1113
1128
  * @param options 可选配置
1114
1129
  * @param options.doNotPushHistory 是否不写入历史记录(默认 false)
1115
1130
  */
1116
- public async moveLayer(
1117
- offset: number | LayerOffset,
1118
- { doNotPushHistory = false, historyDescription, historySource }: DslOpOptions = {},
1119
- ): Promise<void> {
1131
+ public async moveLayer(offset: number | LayerOffset, options: DslOpOptions = {}): Promise<void> {
1132
+ const { doNotPushHistory = false, historyDescription, historySource } = options;
1133
+ const isHistoryOp = isReplay(options);
1120
1134
  this.captureSelectionBeforeOp();
1121
1135
 
1122
1136
  const root = this.get('root');
@@ -1176,6 +1190,7 @@ class Editor extends BaseService {
1176
1190
  offset,
1177
1191
  historySource,
1178
1192
  doNotPushHistory,
1193
+ isHistoryOp,
1179
1194
  });
1180
1195
  }
1181
1196
 
@@ -1290,8 +1305,10 @@ class Editor extends BaseService {
1290
1305
  config: MNode | MNode[],
1291
1306
  targetParent: MContainer,
1292
1307
  targetIndex: number,
1293
- { doNotPushHistory = false, historyDescription, historySource }: DslOpOptions = {},
1308
+ options: DslOpOptions = {},
1294
1309
  ) {
1310
+ const { doNotPushHistory = false, historyDescription, historySource } = options;
1311
+ const isHistoryOp = isReplay(options);
1295
1312
  this.captureSelectionBeforeOp();
1296
1313
 
1297
1314
  if (!targetParent || !Array.isArray(targetParent.items)) return;
@@ -1379,6 +1396,7 @@ class Editor extends BaseService {
1379
1396
  targetParent,
1380
1397
  historySource,
1381
1398
  doNotPushHistory,
1399
+ isHistoryOp,
1382
1400
  });
1383
1401
  }
1384
1402
 
@@ -1501,7 +1519,8 @@ class Editor extends BaseService {
1501
1519
  * @returns 反向后产生的新 step;目标不存在 / 未应用 / 反向失败时返回 null
1502
1520
  */
1503
1521
  public async revertPageStep(index: number): Promise<StepValue | null> {
1504
- const list = this.ctx.history.getStepList('page', this.get('page')?.id);
1522
+ const pageId = this.get('page')?.id;
1523
+ const list = this.ctx.history.getStepList('page', pageId);
1505
1524
  const entry = list[index];
1506
1525
  if (!entry?.applied) return null;
1507
1526
 
@@ -1517,13 +1536,13 @@ class Editor extends BaseService {
1517
1536
  if (!items.length || !items.every((item) => item.changeRecords?.length)) return null;
1518
1537
  }
1519
1538
 
1520
- // 反向应用产生的新 step 由内部 pushOpHistory 触发 history `change` 事件,监听一次以拿到引用。
1539
+ // 反向应用产生的新 step 由内部 pushOpHistory 触发 history `change` 事件,签名 `(step, stepType, id)`。
1540
+ // 只关心当前页面栈产生的第一条;期间其它类型/其它页面的 push 不应覆盖结果。
1521
1541
  let revertedStep: StepValue | null = null;
1522
- // page 的 `change` 事件回调签名为 `(pageId, step)`,这里只关心被回滚产生的新 step。
1523
- const captureRevert = (_pageId: Id, s: StepValue) => {
1524
- revertedStep = s;
1542
+ const captureRevert = (s: StepValue, type: string, id: Id) => {
1543
+ if (type === 'page' && id === pageId && !revertedStep) revertedStep = s;
1525
1544
  };
1526
- this.ctx.history.once('change', captureRevert);
1545
+ this.ctx.history.on('change', captureRevert);
1527
1546
 
1528
1547
  const historyDescription = `回滚 #${index + 1}: ${describeStepForRevert(step)}`;
1529
1548
  // revert 走 public add/remove/update,让操作以一条普通新 step 入栈;不要切换选区与页面,避免打断用户。
@@ -1938,9 +1957,11 @@ class Editor extends BaseService {
1938
1957
  /**
1939
1958
  * 构造一条页面级「set root」历史记录(不携带选区 / modifiedNodeIds 上下文)并落到该页面自己的栈。
1940
1959
  *
1941
- * 连续 set root 替换:若该页栈最新一条已是**同来源**的 set root 记录({@link StepValue.rootStep} 且 `source` 相同),
1942
- * 则用本次记录**替换**它而非新增,避免源码反复保存 / 外部重设 DSL 时堆积多条 root 记录;
1943
- * 来源不同则照常新增(initial 基线不是 rootStep,不在此列)。
1960
+ * 连续 set root 替换:若该页栈最新一条已是**同来源、同为整页 update**的 set root 记录
1961
+ * ({@link StepValue.rootStep} 且 `source` 相同),则用本次记录**替换**它而非新增,避免源码反复保存时堆积记录。
1962
+ * 替换时必须把 `oldSchema` 追溯到最早那一跳,并沿用原 uuid:否则只剩「上一版 → 本版」,
1963
+ * 撤销到基线时 DSL 会停在中途版本且无法继续回退,已发出的 historyId 也会失效。
1964
+ * 其余情况(来源不同 / 页面增删)照常新增,保留完整变更链条。
1944
1965
  */
1945
1966
  private pushPageDiffStep(
1946
1967
  opType: HistoryOpType,
@@ -1963,11 +1984,16 @@ class Editor extends BaseService {
1963
1984
  if (source) step.source = source;
1964
1985
 
1965
1986
  const top = this.ctx.history.getCurrentPageStep(page.id);
1966
- if (top?.rootStep && top.source === source) {
1967
- this.ctx.history.replaceCurrentStep('page', step, page.id);
1968
- } else {
1969
- this.ctx.history.push('page', step, page.id);
1987
+ const earliestOldSchema = top?.diff?.[0]?.oldSchema;
1988
+ if (top?.rootStep && top.source === source && opType === 'update' && top.opType === 'update' && earliestOldSchema) {
1989
+ this.ctx.history.replaceCurrentStep(
1990
+ 'page',
1991
+ { ...step, uuid: top.uuid, diff: [{ ...diffItem, oldSchema: earliestOldSchema }] },
1992
+ page.id,
1993
+ );
1994
+ return;
1970
1995
  }
1996
+ this.ctx.history.push('page', step, page.id);
1971
1997
  }
1972
1998
 
1973
1999
  private pushOpHistory(
@@ -2019,8 +2045,8 @@ class Editor extends BaseService {
2019
2045
  * 「重新插回节点」(撤销 remove / 重做 add)需按 step 记录的 parentId / index 精确还原,
2020
2046
  * 不走 this.add,由手工 splice + stage.add 完成后补发等价的 add / change 事件(见 emitHistoryInsertEvents)。
2021
2047
  *
2022
- * 注意:这些路径都会发出 add / remove / update / change 事件,业务侧若需要区分"用户操作"与"撤销重做触发",
2023
- * 请监听 `history-change` 事件配合判断。
2048
+ * 注意:这些路径都会发出 add / remove / update / change 事件,且 `change` 事件带 `isHistoryOp: true`,
2049
+ * 业务侧可据此跳过仅针对「用户主动编辑」的副作用(加锁 / 埋点 / 自动保存等)。
2024
2050
  *
2025
2051
  * @param step 操作记录
2026
2052
  * @param reverse true = 撤销,false = 重做
@@ -2038,6 +2064,7 @@ class Editor extends BaseService {
2038
2064
  doNotSelect: true,
2039
2065
  doNotSwitchPage: true,
2040
2066
  doNotPushHistory: true,
2067
+ [HISTORY_OP]: true,
2041
2068
  historySource: step.source,
2042
2069
  } as const;
2043
2070
 
@@ -2144,7 +2171,7 @@ class Editor extends BaseService {
2144
2171
  return cloneDeep(reverse ? oldNode : newNode);
2145
2172
  });
2146
2173
  if (configs.length) {
2147
- await this.update(configs, { doNotPushHistory: true, historySource: step.source });
2174
+ await this.update(configs, commonOpts);
2148
2175
  }
2149
2176
  break;
2150
2177
  }
@@ -2197,6 +2224,7 @@ class Editor extends BaseService {
2197
2224
  historySource: source,
2198
2225
  // 撤销/重做本身不再写入历史记录
2199
2226
  doNotPushHistory: true,
2227
+ isHistoryOp: true,
2200
2228
  });
2201
2229
  }
2202
2230