@tmagic/editor 1.8.0-beta.4 → 1.8.0-beta.5
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/dist/es/components/CompareForm.vue_vue_type_script_setup_true_lang.js +1 -1
- package/dist/es/components/ToolButton.vue_vue_type_script_setup_true_lang.js +6 -6
- package/dist/es/index.js +6 -3
- package/dist/es/initService.js +1 -1
- package/dist/es/layouts/Framework.vue_vue_type_script_setup_true_lang.js +1 -1
- package/dist/es/layouts/history-list/Bucket.vue_vue_type_script_setup_true_lang.js +19 -55
- package/dist/es/layouts/history-list/BucketTab.vue_vue_type_script_setup_true_lang.js +22 -39
- package/dist/es/layouts/history-list/GroupRow.vue_vue_type_script_setup_true_lang.js +149 -103
- package/dist/es/layouts/history-list/HistoryDiffDialog.vue_vue_type_script_setup_true_lang.js +32 -8
- package/dist/es/layouts/history-list/HistoryListPanel.vue_vue_type_script_setup_true_lang.js +333 -211
- package/dist/es/layouts/history-list/InitialRow.vue_vue_type_script_setup_true_lang.js +28 -7
- package/dist/es/layouts/history-list/PageTab.vue_vue_type_script_setup_true_lang.js +47 -60
- package/dist/es/layouts/history-list/composables.js +73 -32
- package/dist/es/layouts/page-bar/PageBar.vue_vue_type_script_setup_true_lang.js +4 -2
- package/dist/es/layouts/sidebar/Sidebar.vue_vue_type_script_setup_true_lang.js +5 -1
- package/dist/es/services/codeBlock.js +85 -37
- package/dist/es/services/dataSource.js +62 -26
- package/dist/es/services/editor.js +243 -100
- package/dist/es/services/history.js +270 -206
- package/dist/es/services/ui.js +3 -0
- package/dist/es/style.css +49 -6
- package/dist/es/utils/editor.js +35 -1
- package/dist/es/utils/history.js +223 -0
- package/dist/es/utils/indexed-db.js +86 -0
- package/dist/es/utils/undo-redo.js +60 -1
- package/dist/style.css +49 -6
- package/dist/tmagic-editor.umd.cjs +1799 -905
- package/package.json +7 -7
- package/src/components/CompareForm.vue +3 -1
- package/src/components/ToolButton.vue +2 -2
- package/src/index.ts +1 -0
- package/src/initService.ts +1 -1
- package/src/layouts/Framework.vue +1 -1
- package/src/layouts/history-list/Bucket.vue +34 -71
- package/src/layouts/history-list/BucketTab.vue +46 -54
- package/src/layouts/history-list/GroupRow.vue +146 -111
- package/src/layouts/history-list/HistoryDiffDialog.vue +94 -68
- package/src/layouts/history-list/HistoryListPanel.vue +296 -113
- package/src/layouts/history-list/InitialRow.vue +25 -9
- package/src/layouts/history-list/PageTab.vue +57 -51
- package/src/layouts/history-list/composables.ts +157 -36
- package/src/layouts/page-bar/PageBar.vue +4 -2
- package/src/layouts/sidebar/Sidebar.vue +6 -1
- package/src/services/codeBlock.ts +107 -37
- package/src/services/dataSource.ts +89 -40
- package/src/services/editor.ts +370 -134
- package/src/services/history.ts +305 -203
- package/src/services/ui.ts +7 -0
- package/src/theme/history-list-panel.scss +72 -5
- package/src/theme/page-bar.scss +0 -4
- package/src/type.ts +167 -63
- package/src/utils/editor.ts +41 -1
- package/src/utils/history.ts +298 -0
- package/src/utils/index.ts +2 -0
- package/src/utils/indexed-db.ts +122 -0
- package/src/utils/undo-redo.ts +88 -0
- package/types/index.d.ts +783 -291
package/src/services/history.ts
CHANGED
|
@@ -17,10 +17,11 @@
|
|
|
17
17
|
*/
|
|
18
18
|
|
|
19
19
|
import { reactive } from 'vue';
|
|
20
|
-
import
|
|
20
|
+
import serialize from 'serialize-javascript';
|
|
21
21
|
|
|
22
22
|
import type { CodeBlockContent, DataSourceSchema, Id, MPage, MPageFragment } from '@tmagic/core';
|
|
23
23
|
import type { ChangeRecord } from '@tmagic/form';
|
|
24
|
+
import { guid } from '@tmagic/utils';
|
|
24
25
|
|
|
25
26
|
import type {
|
|
26
27
|
CodeBlockHistoryGroup,
|
|
@@ -28,173 +29,37 @@ import type {
|
|
|
28
29
|
DataSourceHistoryGroup,
|
|
29
30
|
DataSourceStepValue,
|
|
30
31
|
HistoryOpSource,
|
|
32
|
+
HistoryPersistOptions,
|
|
31
33
|
HistoryState,
|
|
32
34
|
PageHistoryGroup,
|
|
33
35
|
PageHistoryStepEntry,
|
|
36
|
+
PersistedHistoryState,
|
|
34
37
|
StepValue,
|
|
35
38
|
} from '@editor/type';
|
|
39
|
+
import { getEditorConfig } from '@editor/utils/config';
|
|
40
|
+
import {
|
|
41
|
+
createStackStep,
|
|
42
|
+
deserializeStacks,
|
|
43
|
+
getOrCreateStack,
|
|
44
|
+
markStackSaved,
|
|
45
|
+
mergePageSteps,
|
|
46
|
+
mergeStackSteps,
|
|
47
|
+
serializeStacks,
|
|
48
|
+
undoFloor,
|
|
49
|
+
} from '@editor/utils/history';
|
|
50
|
+
import { idbGet, idbSet } from '@editor/utils/indexed-db';
|
|
36
51
|
import { UndoRedo } from '@editor/utils/undo-redo';
|
|
37
52
|
|
|
38
53
|
import BaseService from './BaseService';
|
|
54
|
+
import editorService from './editor';
|
|
39
55
|
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
*/
|
|
46
|
-
private static mergeCodeBlockSteps(
|
|
47
|
-
codeBlockId: Id,
|
|
48
|
-
list: CodeBlockStepValue[],
|
|
49
|
-
cursor: number,
|
|
50
|
-
): CodeBlockHistoryGroup[] {
|
|
51
|
-
const groups: CodeBlockHistoryGroup[] = [];
|
|
52
|
-
let current: CodeBlockHistoryGroup | null = null;
|
|
53
|
-
const currentIndex = cursor - 1;
|
|
54
|
-
list.forEach((step, index) => {
|
|
55
|
-
const opType = History.detectOpType(step.oldContent, step.newContent);
|
|
56
|
-
const applied = index < cursor;
|
|
57
|
-
const isCurrent = index === currentIndex;
|
|
58
|
-
if (opType === 'update' && current?.opType === 'update') {
|
|
59
|
-
current.steps.push({ step, index, applied, isCurrent });
|
|
60
|
-
current.applied = applied;
|
|
61
|
-
if (isCurrent) current.isCurrent = true;
|
|
62
|
-
} else {
|
|
63
|
-
current = {
|
|
64
|
-
kind: 'code-block',
|
|
65
|
-
id: codeBlockId,
|
|
66
|
-
opType,
|
|
67
|
-
steps: [{ step, index, applied, isCurrent }],
|
|
68
|
-
applied,
|
|
69
|
-
isCurrent,
|
|
70
|
-
};
|
|
71
|
-
groups.push(current);
|
|
72
|
-
}
|
|
73
|
-
});
|
|
74
|
-
return groups;
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
private static mergeDataSourceSteps(
|
|
78
|
-
dataSourceId: Id,
|
|
79
|
-
list: DataSourceStepValue[],
|
|
80
|
-
cursor: number,
|
|
81
|
-
): DataSourceHistoryGroup[] {
|
|
82
|
-
const groups: DataSourceHistoryGroup[] = [];
|
|
83
|
-
let current: DataSourceHistoryGroup | null = null;
|
|
84
|
-
const currentIndex = cursor - 1;
|
|
85
|
-
list.forEach((step, index) => {
|
|
86
|
-
const opType = History.detectOpType(step.oldSchema, step.newSchema);
|
|
87
|
-
const applied = index < cursor;
|
|
88
|
-
const isCurrent = index === currentIndex;
|
|
89
|
-
if (opType === 'update' && current?.opType === 'update') {
|
|
90
|
-
current.steps.push({ step, index, applied, isCurrent });
|
|
91
|
-
current.applied = applied;
|
|
92
|
-
if (isCurrent) current.isCurrent = true;
|
|
93
|
-
} else {
|
|
94
|
-
current = {
|
|
95
|
-
kind: 'data-source',
|
|
96
|
-
id: dataSourceId,
|
|
97
|
-
opType,
|
|
98
|
-
steps: [{ step, index, applied, isCurrent }],
|
|
99
|
-
applied,
|
|
100
|
-
isCurrent,
|
|
101
|
-
};
|
|
102
|
-
groups.push(current);
|
|
103
|
-
}
|
|
104
|
-
});
|
|
105
|
-
return groups;
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
/**
|
|
109
|
-
* 根据 old/new 是否为 null 推断 opType(与 push 时的约定一致)。
|
|
110
|
-
*/
|
|
111
|
-
private static detectOpType(oldVal: unknown, newVal: unknown): 'add' | 'remove' | 'update' {
|
|
112
|
-
if (oldVal === null && newVal !== null) return 'add';
|
|
113
|
-
if (oldVal !== null && newVal === null) return 'remove';
|
|
114
|
-
return 'update';
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
/**
|
|
118
|
-
* 把页面栈拆成若干 group:
|
|
119
|
-
* - 单节点的 'update' 按 targetId 与相邻同 targetId 的 update 合并到一个 group;
|
|
120
|
-
* - 'add' / 'remove' 始终独立成组(语义上是结构变更,不应被收纳进单节点修改组);
|
|
121
|
-
* - 多节点 'update'(如批量改属性)也独立成组(无明确单一目标,避免误合并)。
|
|
122
|
-
*/
|
|
123
|
-
private static mergePageSteps(pageId: Id, list: StepValue[], cursor: number): PageHistoryGroup[] {
|
|
124
|
-
const groups: PageHistoryGroup[] = [];
|
|
125
|
-
let current: PageHistoryGroup | null = null;
|
|
126
|
-
const currentIndex = cursor - 1;
|
|
127
|
-
list.forEach((step, index) => {
|
|
128
|
-
const applied = index < cursor;
|
|
129
|
-
const isCurrent = index === currentIndex;
|
|
130
|
-
const targetId = History.detectPageTargetId(step);
|
|
131
|
-
const targetName = History.detectPageTargetName(step);
|
|
132
|
-
const entry: PageHistoryStepEntry = { step, index, applied, isCurrent };
|
|
133
|
-
|
|
134
|
-
// 仅"单节点 update"参与合并;其它情形(add/remove/多节点 update)始终独立成组。
|
|
135
|
-
const mergeable = step.opType === 'update' && targetId !== undefined;
|
|
136
|
-
if (mergeable && current?.opType === 'update' && current.targetId === targetId) {
|
|
137
|
-
current.steps.push(entry);
|
|
138
|
-
current.applied = applied;
|
|
139
|
-
if (isCurrent) current.isCurrent = true;
|
|
140
|
-
// 保持目标名为最近一次的(节点重命名时也能反映)
|
|
141
|
-
if (targetName) current.targetName = targetName;
|
|
142
|
-
} else {
|
|
143
|
-
current = {
|
|
144
|
-
kind: 'page',
|
|
145
|
-
pageId,
|
|
146
|
-
opType: step.opType,
|
|
147
|
-
targetId: mergeable ? targetId : undefined,
|
|
148
|
-
targetName,
|
|
149
|
-
steps: [entry],
|
|
150
|
-
applied,
|
|
151
|
-
isCurrent,
|
|
152
|
-
};
|
|
153
|
-
groups.push(current);
|
|
154
|
-
}
|
|
155
|
-
});
|
|
156
|
-
return groups;
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
/**
|
|
160
|
-
* 解析 StepValue 中的"目标节点 id"用于合并:
|
|
161
|
-
* - 单节点 update:取唯一一项 updatedItems 的节点 id;
|
|
162
|
-
* - 其它情形(多节点 update / add / remove):返回 undefined,表示不参与合并。
|
|
163
|
-
*/
|
|
164
|
-
private static detectPageTargetId(step: StepValue): Id | undefined {
|
|
165
|
-
if (step.opType !== 'update') return undefined;
|
|
166
|
-
const items = step.updatedItems;
|
|
167
|
-
if (items?.length !== 1) return undefined;
|
|
168
|
-
return items[0].newNode?.id ?? items[0].oldNode?.id;
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
/** 解析 StepValue 中的目标节点可读名(用于 UI 展示)。 */
|
|
172
|
-
private static detectPageTargetName(step: StepValue): string | undefined {
|
|
173
|
-
if (step.opType === 'update') {
|
|
174
|
-
const items = step.updatedItems;
|
|
175
|
-
if (items?.length === 1) {
|
|
176
|
-
const node = items[0].newNode || items[0].oldNode;
|
|
177
|
-
return (node?.name as string) || (node?.type as string) || (node?.id !== undefined ? `${node.id}` : undefined);
|
|
178
|
-
}
|
|
179
|
-
return items?.length ? `${items.length} 个节点` : undefined;
|
|
180
|
-
}
|
|
181
|
-
if (step.opType === 'add') {
|
|
182
|
-
if (step.nodes?.length === 1) {
|
|
183
|
-
const n = step.nodes[0];
|
|
184
|
-
return (n.name as string) || (n.type as string) || `${n.id}`;
|
|
185
|
-
}
|
|
186
|
-
return step.nodes?.length ? `${step.nodes.length} 个节点` : undefined;
|
|
187
|
-
}
|
|
188
|
-
if (step.opType === 'remove') {
|
|
189
|
-
if (step.removedItems?.length === 1) {
|
|
190
|
-
const n = step.removedItems[0].node;
|
|
191
|
-
return (n.name as string) || (n.type as string) || `${n.id}`;
|
|
192
|
-
}
|
|
193
|
-
return step.removedItems?.length ? `${step.removedItems.length} 个节点` : undefined;
|
|
194
|
-
}
|
|
195
|
-
return undefined;
|
|
196
|
-
}
|
|
56
|
+
/** 历史记录持久化快照的默认存储位置与结构版本。 */
|
|
57
|
+
const DEFAULT_DB_NAME = 'tmagic-editor';
|
|
58
|
+
const DEFAULT_STORE_NAME = 'history';
|
|
59
|
+
const DEFAULT_KEY: IDBValidKey = 'default';
|
|
60
|
+
const PERSIST_VERSION = 1;
|
|
197
61
|
|
|
62
|
+
class History extends BaseService {
|
|
198
63
|
public state = reactive<HistoryState>({
|
|
199
64
|
pageSteps: {},
|
|
200
65
|
pageId: undefined,
|
|
@@ -246,6 +111,59 @@ class History extends BaseService {
|
|
|
246
111
|
this.state.dataSourceState = {};
|
|
247
112
|
}
|
|
248
113
|
|
|
114
|
+
/**
|
|
115
|
+
* 为指定页面 / 页面片种入一条「初始基线」记录(如加载 DSL 时的「初始 / 加载」基线)。
|
|
116
|
+
*
|
|
117
|
+
* 该记录是一条 `opType: 'initial'` 的 {@link StepValue},作为页面历史栈 **index 0 的固定底线**:
|
|
118
|
+
* - 它是一条真实入栈的 step(随栈一起持久化),但被钉为撤销/回滚的下限——cursor 永不低于它,
|
|
119
|
+
* 因此不会被 undo / goto / revert 触达(详见 {@link undo} / {@link setCanUndoRedo});
|
|
120
|
+
* - 历史面板把它过滤出分组列表(见 {@link getPageHistoryGroups}),改由底部「初始」行展示。
|
|
121
|
+
*
|
|
122
|
+
* 仅当目标页面栈为空时种入(保证 initial 一定位于 index 0);已存在 initial 底线时默认不重复种入,
|
|
123
|
+
* 传 `force=true` 且栈为空时按新基线种入。
|
|
124
|
+
*/
|
|
125
|
+
public setPageMarker(
|
|
126
|
+
pageId: Id,
|
|
127
|
+
options: { name?: string; description?: string; source?: HistoryOpSource } = {},
|
|
128
|
+
): StepValue | null {
|
|
129
|
+
if (pageId === undefined || pageId === null || `${pageId}` === '') return null;
|
|
130
|
+
|
|
131
|
+
const existing = this.getPageMarker(pageId);
|
|
132
|
+
if (existing) return existing;
|
|
133
|
+
|
|
134
|
+
const stack = getOrCreateStack(this.state.pageSteps, pageId);
|
|
135
|
+
// initial 必须是 index 0;栈非空(已有真实记录、却无 initial,如旧数据)时不强行前插,优雅降级为无基线。
|
|
136
|
+
if (stack.getLength() > 0) return null;
|
|
137
|
+
|
|
138
|
+
const marker: StepValue = {
|
|
139
|
+
uuid: guid(),
|
|
140
|
+
opType: 'initial',
|
|
141
|
+
diff: [],
|
|
142
|
+
data: { name: options.name || '', id: pageId },
|
|
143
|
+
selectedBefore: [],
|
|
144
|
+
selectedAfter: [],
|
|
145
|
+
modifiedNodeIds: new Map(),
|
|
146
|
+
historyDescription: options.description || '未修改的初始状态',
|
|
147
|
+
timestamp: Date.now(),
|
|
148
|
+
...(options.source ? { source: options.source } : {}),
|
|
149
|
+
};
|
|
150
|
+
stack.pushElement(marker);
|
|
151
|
+
if (`${pageId}` === `${this.state.pageId}`) this.setCanUndoRedo();
|
|
152
|
+
this.emit('page-marker-change', marker);
|
|
153
|
+
return marker;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* 读取指定页面(缺省当前活动页)的初始基线 step(页面栈 index 0 且 `opType: 'initial'`);
|
|
158
|
+
* 不存在时返回 undefined。
|
|
159
|
+
*/
|
|
160
|
+
public getPageMarker(pageId?: Id): StepValue | undefined {
|
|
161
|
+
const targetPageId = pageId ?? this.state.pageId;
|
|
162
|
+
if (!targetPageId) return undefined;
|
|
163
|
+
const first = this.state.pageSteps[targetPageId]?.getElementList()[0];
|
|
164
|
+
return first?.opType === 'initial' ? first : undefined;
|
|
165
|
+
}
|
|
166
|
+
|
|
249
167
|
/**
|
|
250
168
|
* 把一条步骤推入指定页面的栈;不指定 pageId 时落到当前活动页。
|
|
251
169
|
*
|
|
@@ -255,6 +173,7 @@ class History extends BaseService {
|
|
|
255
173
|
public push(state: StepValue, pageId?: Id): StepValue | null {
|
|
256
174
|
const undoRedo = this.getUndoRedo(pageId);
|
|
257
175
|
if (!undoRedo) return null;
|
|
176
|
+
if (state.uuid === undefined) state.uuid = guid();
|
|
258
177
|
if (state.timestamp === undefined) state.timestamp = Date.now();
|
|
259
178
|
undoRedo.pushElement(state);
|
|
260
179
|
// 仅当推入的是当前活动页时才需要刷新 canUndo/canRedo —— 其它页栈对当前 UI 状态没影响。
|
|
@@ -264,6 +183,27 @@ class History extends BaseService {
|
|
|
264
183
|
return state;
|
|
265
184
|
}
|
|
266
185
|
|
|
186
|
+
/** 读取指定页面(缺省当前活动页)历史栈当前游标所在的 step(cursor - 1);无则返回 null。 */
|
|
187
|
+
public getCurrentPageStep(pageId?: Id): StepValue | null {
|
|
188
|
+
const targetPageId = pageId ?? this.state.pageId;
|
|
189
|
+
if (!targetPageId) return null;
|
|
190
|
+
return this.state.pageSteps[targetPageId]?.getCurrentElement() ?? null;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* 用 `state` 替换指定页面栈当前游标所在的 step(并丢弃其后的重做尾部),游标不变。
|
|
195
|
+
* 用于「连续 set root 记录合并」等就地替换最新一条的场景;替换成功后按需刷新 / 通知。
|
|
196
|
+
*/
|
|
197
|
+
public replaceCurrentPageStep(state: StepValue, pageId?: Id): StepValue | null {
|
|
198
|
+
const undoRedo = this.getUndoRedo(pageId);
|
|
199
|
+
if (!undoRedo) return null;
|
|
200
|
+
if (state.uuid === undefined) state.uuid = guid();
|
|
201
|
+
if (state.timestamp === undefined) state.timestamp = Date.now();
|
|
202
|
+
if (!undoRedo.replaceCurrentElement(state)) return null;
|
|
203
|
+
this.emit('change', state);
|
|
204
|
+
return state;
|
|
205
|
+
}
|
|
206
|
+
|
|
267
207
|
/**
|
|
268
208
|
* 推入一条代码块变更记录(与页面/节点完全无关),按 `codeBlockId` 维度独立一份 UndoRedo 栈。
|
|
269
209
|
*
|
|
@@ -285,19 +225,15 @@ class History extends BaseService {
|
|
|
285
225
|
source?: HistoryOpSource;
|
|
286
226
|
},
|
|
287
227
|
): CodeBlockStepValue | null {
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
oldContent: payload.oldContent ? cloneDeep(payload.oldContent) : null,
|
|
293
|
-
newContent: payload.newContent ? cloneDeep(payload.newContent) : null,
|
|
294
|
-
changeRecords: payload.changeRecords?.length ? cloneDeep(payload.changeRecords) : undefined,
|
|
228
|
+
const step = createStackStep<CodeBlockContent, CodeBlockStepValue>(codeBlockId, {
|
|
229
|
+
oldValue: payload.oldContent,
|
|
230
|
+
newValue: payload.newContent,
|
|
231
|
+
changeRecords: payload.changeRecords,
|
|
295
232
|
historyDescription: payload.historyDescription,
|
|
296
233
|
source: payload.source,
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
this.getCodeBlockUndoRedo(codeBlockId).pushElement(step);
|
|
234
|
+
});
|
|
235
|
+
if (!step) return null;
|
|
236
|
+
getOrCreateStack(this.state.codeBlockState, codeBlockId).pushElement(step);
|
|
301
237
|
this.emit('code-block-history-change', codeBlockId, step);
|
|
302
238
|
return step;
|
|
303
239
|
}
|
|
@@ -318,19 +254,15 @@ class History extends BaseService {
|
|
|
318
254
|
source?: HistoryOpSource;
|
|
319
255
|
},
|
|
320
256
|
): DataSourceStepValue | null {
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
oldSchema: payload.oldSchema ? cloneDeep(payload.oldSchema) : null,
|
|
326
|
-
newSchema: payload.newSchema ? cloneDeep(payload.newSchema) : null,
|
|
327
|
-
changeRecords: payload.changeRecords?.length ? cloneDeep(payload.changeRecords) : undefined,
|
|
257
|
+
const step = createStackStep<DataSourceSchema, DataSourceStepValue>(dataSourceId, {
|
|
258
|
+
oldValue: payload.oldSchema,
|
|
259
|
+
newValue: payload.newSchema,
|
|
260
|
+
changeRecords: payload.changeRecords,
|
|
328
261
|
historyDescription: payload.historyDescription,
|
|
329
262
|
source: payload.source,
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
this.getDataSourceUndoRedo(dataSourceId).pushElement(step);
|
|
263
|
+
});
|
|
264
|
+
if (!step) return null;
|
|
265
|
+
getOrCreateStack(this.state.dataSourceState, dataSourceId).pushElement(step);
|
|
334
266
|
this.emit('data-source-history-change', dataSourceId, step);
|
|
335
267
|
return step;
|
|
336
268
|
}
|
|
@@ -394,6 +326,8 @@ class History extends BaseService {
|
|
|
394
326
|
public undo(): StepValue | null {
|
|
395
327
|
const undoRedo = this.getUndoRedo();
|
|
396
328
|
if (!undoRedo) return null;
|
|
329
|
+
// 不允许撤销越过初始基线(index 0 的 initial step)。
|
|
330
|
+
if (undoRedo.getCursor() <= undoFloor(undoRedo)) return null;
|
|
397
331
|
const state = undoRedo.undo();
|
|
398
332
|
this.emit('change', state);
|
|
399
333
|
return state;
|
|
@@ -413,6 +347,146 @@ class History extends BaseService {
|
|
|
413
347
|
this.removeAllPlugins();
|
|
414
348
|
}
|
|
415
349
|
|
|
350
|
+
/**
|
|
351
|
+
* 清空指定页面(缺省当前活动页)的历史记录栈。
|
|
352
|
+
* 仅删除撤销/重做记录,不会改动当前 DSL;清空后该页将无法再撤销/重做之前的操作。
|
|
353
|
+
*/
|
|
354
|
+
public clearPage(pageId?: Id): void {
|
|
355
|
+
const targetPageId = pageId ?? this.state.pageId;
|
|
356
|
+
if (!targetPageId) return;
|
|
357
|
+
// 保留该页原 initial 基线的文案 / 来源(仅清空其后的真实操作记录),无基线时清空成空栈。
|
|
358
|
+
const marker = this.getPageMarker(targetPageId);
|
|
359
|
+
this.state.pageSteps[targetPageId] = new UndoRedo<StepValue>();
|
|
360
|
+
if (marker) {
|
|
361
|
+
this.setPageMarker(targetPageId, {
|
|
362
|
+
name: marker.data?.name,
|
|
363
|
+
description: marker.historyDescription,
|
|
364
|
+
source: marker.source,
|
|
365
|
+
});
|
|
366
|
+
}
|
|
367
|
+
if (`${targetPageId}` === `${this.state.pageId}`) {
|
|
368
|
+
this.setCanUndoRedo();
|
|
369
|
+
this.emit('clear-page', null);
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
/**
|
|
374
|
+
* 清空数据源历史记录栈:传入 `dataSourceId` 仅清空该数据源,缺省清空全部数据源。
|
|
375
|
+
* 仅删除撤销/重做记录,不会改动数据源本身。
|
|
376
|
+
*/
|
|
377
|
+
public clearDataSource(dataSourceId?: Id): void {
|
|
378
|
+
if (dataSourceId !== undefined) {
|
|
379
|
+
delete this.state.dataSourceState[dataSourceId];
|
|
380
|
+
} else {
|
|
381
|
+
this.state.dataSourceState = {};
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
/**
|
|
386
|
+
* 清空代码块历史记录栈:传入 `codeBlockId` 仅清空该代码块,缺省清空全部代码块。
|
|
387
|
+
* 仅删除撤销/重做记录,不会改动代码块本身。
|
|
388
|
+
*/
|
|
389
|
+
public clearCodeBlock(codeBlockId?: Id): void {
|
|
390
|
+
if (codeBlockId !== undefined) {
|
|
391
|
+
delete this.state.codeBlockState[codeBlockId];
|
|
392
|
+
} else {
|
|
393
|
+
this.state.codeBlockState = {};
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
/**
|
|
398
|
+
* 标记「整份 DSL 已保存」:把页面 / 代码块 / 数据源所有栈当前游标所在的记录都标为 `saved`。
|
|
399
|
+
* 适用于「整体落库」场景;若只保存了其中一类,请改用更细粒度的
|
|
400
|
+
* {@link markPageSaved} / {@link markCodeBlockSaved} / {@link markDataSourceSaved}。
|
|
401
|
+
*/
|
|
402
|
+
public markSaved(): void {
|
|
403
|
+
Object.values(this.state.pageSteps).forEach(markStackSaved);
|
|
404
|
+
Object.values(this.state.codeBlockState).forEach(markStackSaved);
|
|
405
|
+
Object.values(this.state.dataSourceState).forEach(markStackSaved);
|
|
406
|
+
this.emit('mark-saved', { kind: 'all' });
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
/**
|
|
410
|
+
* 标记指定页面(缺省为当前活动页)的历史栈当前记录为已保存。
|
|
411
|
+
* 仅影响该页面自己的栈,不波及代码块 / 数据源 / 其它页面。
|
|
412
|
+
*/
|
|
413
|
+
public markPageSaved(pageId?: Id): void {
|
|
414
|
+
const targetPageId = pageId ?? this.state.pageId;
|
|
415
|
+
if (!targetPageId) return;
|
|
416
|
+
markStackSaved(this.state.pageSteps[targetPageId]);
|
|
417
|
+
this.emit('mark-saved', { kind: 'page', id: targetPageId });
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
/** 标记指定代码块的历史栈当前记录为已保存,仅影响该代码块自己的栈。 */
|
|
421
|
+
public markCodeBlockSaved(codeBlockId: Id): void {
|
|
422
|
+
if (!codeBlockId) return;
|
|
423
|
+
markStackSaved(this.state.codeBlockState[codeBlockId]);
|
|
424
|
+
this.emit('mark-saved', { kind: 'code-block', id: codeBlockId });
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
/** 标记指定数据源的历史栈当前记录为已保存,仅影响该数据源自己的栈。 */
|
|
428
|
+
public markDataSourceSaved(dataSourceId: Id): void {
|
|
429
|
+
if (!dataSourceId) return;
|
|
430
|
+
markStackSaved(this.state.dataSourceState[dataSourceId]);
|
|
431
|
+
this.emit('mark-saved', { kind: 'data-source', id: dataSourceId });
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
/**
|
|
435
|
+
* 把当前内存中的全部历史栈(页面 / 代码块 / 数据源)序列化后写入本地 IndexedDB。
|
|
436
|
+
*
|
|
437
|
+
* - 每个 UndoRedo 栈连同其游标、容量一并保存,恢复后可继续 undo/redo;
|
|
438
|
+
* - `key` 用于区分不同活动页 / 项目(同一 store 下可保存多份快照),缺省为 `default`;
|
|
439
|
+
* - 返回写入成功的快照对象,便于调用方记录 savedAt 等信息;
|
|
440
|
+
* - 不支持 IndexedDB 的环境(如 SSR)会 reject。
|
|
441
|
+
*/
|
|
442
|
+
public async saveToIndexedDB(options: HistoryPersistOptions = {}): Promise<PersistedHistoryState> {
|
|
443
|
+
const { dbName = DEFAULT_DB_NAME, storeName = DEFAULT_STORE_NAME, key = DEFAULT_KEY, appId } = options;
|
|
444
|
+
|
|
445
|
+
const snapshot: PersistedHistoryState = {
|
|
446
|
+
version: PERSIST_VERSION,
|
|
447
|
+
pageId: this.state.pageId,
|
|
448
|
+
pageSteps: serializeStacks(this.state.pageSteps),
|
|
449
|
+
codeBlockState: serializeStacks(this.state.codeBlockState),
|
|
450
|
+
dataSourceState: serializeStacks(this.state.dataSourceState),
|
|
451
|
+
savedAt: Date.now(),
|
|
452
|
+
};
|
|
453
|
+
|
|
454
|
+
// 历史记录里可能包含函数(如代码块内容 / 节点事件 / 数据源方法),IndexedDB 的结构化克隆无法写入函数,
|
|
455
|
+
// 因此用 serialize-javascript 序列化成字符串后再写入(支持函数 / Map 等),读取时用 parseDSL 还原。
|
|
456
|
+
await idbSet(this.resolveDbName(dbName, appId), storeName, key, serialize(snapshot));
|
|
457
|
+
this.emit('save-to-indexed-db', snapshot);
|
|
458
|
+
return snapshot;
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
/**
|
|
462
|
+
* 从本地 IndexedDB 读取此前保存的历史快照并重建全部撤销/重做栈。
|
|
463
|
+
*
|
|
464
|
+
* - 读取到的每个栈都会经 {@link UndoRedo.fromSerialized} 还原(含游标),随后可直接 undo/redo;
|
|
465
|
+
* - 会整体覆盖当前内存中的历史状态,并把活动页恢复为快照中的 pageId;
|
|
466
|
+
* - 找不到对应记录时返回 null,且不改动当前状态;
|
|
467
|
+
* - 不支持 IndexedDB 的环境(如 SSR)会 reject。
|
|
468
|
+
*/
|
|
469
|
+
public async restoreFromIndexedDB(options: HistoryPersistOptions = {}): Promise<PersistedHistoryState | null> {
|
|
470
|
+
const { dbName = DEFAULT_DB_NAME, storeName = DEFAULT_STORE_NAME, key = DEFAULT_KEY, appId } = options;
|
|
471
|
+
|
|
472
|
+
const raw = await idbGet<string | PersistedHistoryState>(this.resolveDbName(dbName, appId), storeName, key);
|
|
473
|
+
if (!raw) return null;
|
|
474
|
+
|
|
475
|
+
// 新版以序列化字符串存储(含函数),用 parseDSL 还原;兼容历史上以对象形式存入的旧数据。
|
|
476
|
+
const snapshot = (typeof raw === 'string' ? getEditorConfig('parseDSL')(`(${raw})`) : raw) as PersistedHistoryState;
|
|
477
|
+
if (!snapshot) return null;
|
|
478
|
+
|
|
479
|
+
this.state.pageSteps = deserializeStacks(snapshot.pageSteps);
|
|
480
|
+
this.state.codeBlockState = deserializeStacks(snapshot.codeBlockState);
|
|
481
|
+
this.state.dataSourceState = deserializeStacks(snapshot.dataSourceState);
|
|
482
|
+
// initial 基线作为页面栈 index 0 的 step 随 pageSteps 一并还原,无需单独恢复。
|
|
483
|
+
this.state.pageId = snapshot.pageId;
|
|
484
|
+
|
|
485
|
+
this.setCanUndoRedo();
|
|
486
|
+
this.emit('restore-from-indexed-db', snapshot);
|
|
487
|
+
return snapshot;
|
|
488
|
+
}
|
|
489
|
+
|
|
416
490
|
/**
|
|
417
491
|
* 取出当前活动页的历史步骤平铺列表(包含已应用 + 已撤销)。
|
|
418
492
|
* 列表按时间正序,最早一步在最前面。
|
|
@@ -446,7 +520,9 @@ class History extends BaseService {
|
|
|
446
520
|
const list = undoRedo.getElementList();
|
|
447
521
|
if (!list.length) return [];
|
|
448
522
|
const cursor = undoRedo.getCursor();
|
|
449
|
-
|
|
523
|
+
// initial 基线(index 0)不作为普通操作组展示,过滤掉;其余真实 step 的 index 保持不变,
|
|
524
|
+
// 以便面板 goto(index+1) / revert(index) 仍直接对应栈内位置。底部「初始」行由 getPageMarker 驱动。
|
|
525
|
+
return mergePageSteps(targetPageId, list, cursor).filter((group) => group.opType !== 'initial');
|
|
450
526
|
}
|
|
451
527
|
|
|
452
528
|
/**
|
|
@@ -463,7 +539,7 @@ class History extends BaseService {
|
|
|
463
539
|
const list = undoRedo.getElementList();
|
|
464
540
|
if (!list.length) return;
|
|
465
541
|
const cursor = undoRedo.getCursor();
|
|
466
|
-
groups.push(...
|
|
542
|
+
groups.push(...mergeStackSteps('code-block', id, list, cursor));
|
|
467
543
|
});
|
|
468
544
|
return groups;
|
|
469
545
|
}
|
|
@@ -510,6 +586,41 @@ class History extends BaseService {
|
|
|
510
586
|
return list.map((step, index) => ({ step, index, applied: index < cursor }));
|
|
511
587
|
}
|
|
512
588
|
|
|
589
|
+
/**
|
|
590
|
+
* 按历史记录 uuid 在指定页面(默认当前活动页)的栈中查找其索引。
|
|
591
|
+
* 找不到时返回 -1。供「按 uuid 回滚」等需要把 uuid 映射回 index 的场景使用。
|
|
592
|
+
*/
|
|
593
|
+
public getPageStepIndexByUuid(uuid: string, pageId?: Id): number {
|
|
594
|
+
if (!uuid) return -1;
|
|
595
|
+
return this.getPageStepList(pageId).findIndex((entry) => entry.step.uuid === uuid);
|
|
596
|
+
}
|
|
597
|
+
|
|
598
|
+
/**
|
|
599
|
+
* 按历史记录 uuid 在全部代码块栈中查找其所属 codeBlockId 与索引。
|
|
600
|
+
* 找不到时返回 null。
|
|
601
|
+
*/
|
|
602
|
+
public findCodeBlockStepLocationByUuid(uuid: string): { id: Id; index: number } | null {
|
|
603
|
+
if (!uuid) return null;
|
|
604
|
+
for (const id of Object.keys(this.state.codeBlockState)) {
|
|
605
|
+
const index = this.getCodeBlockStepList(id).findIndex((entry) => entry.step.uuid === uuid);
|
|
606
|
+
if (index >= 0) return { id, index };
|
|
607
|
+
}
|
|
608
|
+
return null;
|
|
609
|
+
}
|
|
610
|
+
|
|
611
|
+
/**
|
|
612
|
+
* 按历史记录 uuid 在全部数据源栈中查找其所属 dataSourceId 与索引。
|
|
613
|
+
* 找不到时返回 null。
|
|
614
|
+
*/
|
|
615
|
+
public findDataSourceStepLocationByUuid(uuid: string): { id: Id; index: number } | null {
|
|
616
|
+
if (!uuid) return null;
|
|
617
|
+
for (const id of Object.keys(this.state.dataSourceState)) {
|
|
618
|
+
const index = this.getDataSourceStepList(id).findIndex((entry) => entry.step.uuid === uuid);
|
|
619
|
+
if (index >= 0) return { id, index };
|
|
620
|
+
}
|
|
621
|
+
return null;
|
|
622
|
+
}
|
|
623
|
+
|
|
513
624
|
/**
|
|
514
625
|
* 取出全部数据源的历史栈,按 dataSourceId 分组。同上。
|
|
515
626
|
*/
|
|
@@ -520,7 +631,7 @@ class History extends BaseService {
|
|
|
520
631
|
const list = undoRedo.getElementList();
|
|
521
632
|
if (!list.length) return;
|
|
522
633
|
const cursor = undoRedo.getCursor();
|
|
523
|
-
groups.push(...
|
|
634
|
+
groups.push(...mergeStackSteps('data-source', id, list, cursor));
|
|
524
635
|
});
|
|
525
636
|
return groups;
|
|
526
637
|
}
|
|
@@ -540,30 +651,21 @@ class History extends BaseService {
|
|
|
540
651
|
return this.state.pageSteps[targetPageId];
|
|
541
652
|
}
|
|
542
653
|
|
|
543
|
-
private setCanUndoRedo(): void {
|
|
544
|
-
const undoRedo = this.getUndoRedo();
|
|
545
|
-
this.state.canRedo = undoRedo?.canRedo() || false;
|
|
546
|
-
this.state.canUndo = undoRedo?.canUndo() || false;
|
|
547
|
-
}
|
|
548
|
-
|
|
549
654
|
/**
|
|
550
|
-
*
|
|
655
|
+
* 把基础 dbName 与当前 DSL(root app)的 id 拼成最终库名,实现不同应用历史隔离。
|
|
656
|
+
* 取不到 app id(如尚未加载 DSL)时退回基础 dbName。
|
|
551
657
|
*/
|
|
552
|
-
private
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
}
|
|
556
|
-
return this.state.codeBlockState[codeBlockId];
|
|
658
|
+
private resolveDbName(dbName: string, appId?: Id): string {
|
|
659
|
+
// 优先用显式传入的 appId(「先恢复再 set root」时 root 尚未就绪);否则回退到当前 root.id。
|
|
660
|
+
const resolvedAppId = appId ?? editorService.get('root')?.id;
|
|
661
|
+
return resolvedAppId ? `${dbName}-${resolvedAppId}` : dbName;
|
|
557
662
|
}
|
|
558
663
|
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
this.state.dataSourceState[dataSourceId] = new UndoRedo<DataSourceStepValue>();
|
|
565
|
-
}
|
|
566
|
-
return this.state.dataSourceState[dataSourceId];
|
|
664
|
+
private setCanUndoRedo(): void {
|
|
665
|
+
const undoRedo = this.getUndoRedo();
|
|
666
|
+
this.state.canRedo = undoRedo?.canRedo() || false;
|
|
667
|
+
// 初始基线之上才可撤销:cursor 必须高于底线(有 initial 时为 1)。
|
|
668
|
+
this.state.canUndo = undoRedo ? undoRedo.getCursor() > undoFloor(undoRedo) : false;
|
|
567
669
|
}
|
|
568
670
|
}
|
|
569
671
|
|
package/src/services/ui.ts
CHANGED
|
@@ -62,6 +62,7 @@ const state = shallowReactive<UiState>({
|
|
|
62
62
|
showPageListButton: true,
|
|
63
63
|
hideSlideBar: false,
|
|
64
64
|
sideBarItems: [],
|
|
65
|
+
sideBarActiveTabName: '',
|
|
65
66
|
navMenuRect: {
|
|
66
67
|
left: 0,
|
|
67
68
|
top: 0,
|
|
@@ -104,7 +105,13 @@ class Ui extends BaseService {
|
|
|
104
105
|
mask?.showRule(value as unknown as boolean);
|
|
105
106
|
}
|
|
106
107
|
|
|
108
|
+
const preValue = state[name];
|
|
109
|
+
|
|
107
110
|
state[name] = value;
|
|
111
|
+
|
|
112
|
+
if (preValue !== value) {
|
|
113
|
+
this.emit('state-change', name, value, preValue);
|
|
114
|
+
}
|
|
108
115
|
}
|
|
109
116
|
|
|
110
117
|
public get<K extends keyof UiState>(name: K) {
|