@tmagic/editor 1.8.0-beta.4 → 1.8.0-beta.6
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/fields/CodeSelectCol.vue_vue_type_script_setup_true_lang.js +4 -1
- package/dist/es/fields/DataSourceFieldSelect/FieldSelect.vue_vue_type_script_setup_true_lang.js +8 -2
- package/dist/es/fields/DataSourceFields.vue_vue_type_script_setup_true_lang.js +25 -8
- package/dist/es/fields/DataSourceMethodSelect.vue_vue_type_script_setup_true_name_true_lang.js +6 -4
- package/dist/es/fields/DataSourceMethods.vue_vue_type_script_setup_true_lang.js +25 -12
- package/dist/es/fields/StyleSetter/components/Border.vue_vue_type_script_setup_true_lang.js +27 -5
- 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 +335 -213
- package/dist/es/layouts/history-list/InitialRow.vue_vue_type_script_setup_true_lang.js +36 -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/layouts/sidebar/data-source/DataSourceConfigPanel.vue_vue_type_script_setup_true_lang.js +16 -3
- package/dist/es/layouts/sidebar/data-source/DataSourceListPanel.vue_vue_type_script_setup_true_lang.js +23 -1
- package/dist/es/services/codeBlock.js +96 -37
- package/dist/es/services/dataSource.js +70 -26
- package/dist/es/services/editor.js +266 -104
- package/dist/es/services/history.js +271 -212
- package/dist/es/services/ui.js +3 -0
- package/dist/es/style.css +53 -7
- package/dist/es/utils/data-source/index.js +2 -0
- package/dist/es/utils/editor.js +103 -49
- package/dist/es/utils/history.js +232 -0
- package/dist/es/utils/indexed-db.js +86 -0
- package/dist/es/utils/undo-redo.js +60 -1
- package/dist/style.css +53 -7
- package/dist/tmagic-editor.umd.cjs +2064 -1000
- package/package.json +7 -7
- package/src/components/CompareForm.vue +3 -1
- package/src/components/ToolButton.vue +2 -2
- package/src/fields/CodeSelectCol.vue +7 -1
- package/src/fields/DataSourceFieldSelect/FieldSelect.vue +16 -2
- package/src/fields/DataSourceFields.vue +37 -8
- package/src/fields/DataSourceMethodSelect.vue +9 -4
- package/src/fields/DataSourceMethods.vue +42 -21
- package/src/fields/StyleSetter/components/Border.vue +15 -6
- 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 +298 -115
- package/src/layouts/history-list/InitialRow.vue +28 -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/layouts/sidebar/data-source/DataSourceConfigPanel.vue +30 -2
- package/src/layouts/sidebar/data-source/DataSourceListPanel.vue +26 -1
- package/src/services/codeBlock.ts +113 -37
- package/src/services/dataSource.ts +94 -40
- package/src/services/editor.ts +376 -136
- package/src/services/history.ts +306 -209
- 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/theme/style-setter/border.scss +4 -1
- package/src/type.ts +183 -64
- package/src/utils/data-source/index.ts +2 -0
- package/src/utils/editor.ts +166 -60
- package/src/utils/history.ts +308 -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 +813 -302
package/src/services/history.ts
CHANGED
|
@@ -17,10 +17,10 @@
|
|
|
17
17
|
*/
|
|
18
18
|
|
|
19
19
|
import { reactive } from 'vue';
|
|
20
|
-
import { cloneDeep } from 'lodash-es';
|
|
21
20
|
|
|
22
21
|
import type { CodeBlockContent, DataSourceSchema, Id, MPage, MPageFragment } from '@tmagic/core';
|
|
23
22
|
import type { ChangeRecord } from '@tmagic/form';
|
|
23
|
+
import { guid } from '@tmagic/utils';
|
|
24
24
|
|
|
25
25
|
import type {
|
|
26
26
|
CodeBlockHistoryGroup,
|
|
@@ -28,173 +28,38 @@ import type {
|
|
|
28
28
|
DataSourceHistoryGroup,
|
|
29
29
|
DataSourceStepValue,
|
|
30
30
|
HistoryOpSource,
|
|
31
|
+
HistoryPersistOptions,
|
|
31
32
|
HistoryState,
|
|
32
33
|
PageHistoryGroup,
|
|
33
34
|
PageHistoryStepEntry,
|
|
35
|
+
PersistedHistoryState,
|
|
34
36
|
StepValue,
|
|
35
37
|
} from '@editor/type';
|
|
38
|
+
import { getEditorConfig } from '@editor/utils/config';
|
|
39
|
+
import {
|
|
40
|
+
createStackStep,
|
|
41
|
+
deserializeStacks,
|
|
42
|
+
getOrCreateStack,
|
|
43
|
+
markStackSaved,
|
|
44
|
+
mergePageSteps,
|
|
45
|
+
mergeStackSteps,
|
|
46
|
+
serializeStacks,
|
|
47
|
+
undoFloor,
|
|
48
|
+
} from '@editor/utils/history';
|
|
49
|
+
import { idbGet, idbSet } from '@editor/utils/indexed-db';
|
|
36
50
|
import { UndoRedo } from '@editor/utils/undo-redo';
|
|
37
51
|
|
|
38
52
|
import BaseService from './BaseService';
|
|
53
|
+
import editorService from './editor';
|
|
39
54
|
|
|
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
|
-
}
|
|
55
|
+
/** 历史记录持久化快照的默认存储位置与结构版本。 */
|
|
56
|
+
const DEFAULT_DB_NAME = 'tmagic-editor';
|
|
57
|
+
const DEFAULT_STORE_NAME = 'history';
|
|
58
|
+
const DEFAULT_KEY: IDBValidKey = 'default';
|
|
59
|
+
// v2:仅把每条 step 的 diff 序列化成字符串,其余字段交给 IndexedDB 结构化克隆原生存储(见 saveToIndexedDB)。
|
|
60
|
+
const PERSIST_VERSION = 2;
|
|
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,144 @@ 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
|
+
// serializeStacks 会在序列化各栈时只把每条 step 的 diff(可能含函数)序列化成字符串,其余字段原样保留,
|
|
446
|
+
// 因此整份快照可直接按对象写入 IndexedDB(结构化克隆),避免序列化整份快照的开销;读取时再用 parseDSL 还原 diff。
|
|
447
|
+
const snapshot: PersistedHistoryState = {
|
|
448
|
+
version: PERSIST_VERSION,
|
|
449
|
+
pageId: this.state.pageId,
|
|
450
|
+
pageSteps: serializeStacks(this.state.pageSteps),
|
|
451
|
+
codeBlockState: serializeStacks(this.state.codeBlockState),
|
|
452
|
+
dataSourceState: serializeStacks(this.state.dataSourceState),
|
|
453
|
+
savedAt: Date.now(),
|
|
454
|
+
};
|
|
455
|
+
|
|
456
|
+
await idbSet(this.resolveDbName(dbName, appId), storeName, key, 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 snapshot = await idbGet<PersistedHistoryState>(this.resolveDbName(dbName, appId), storeName, key);
|
|
473
|
+
if (!snapshot) return null;
|
|
474
|
+
|
|
475
|
+
// 各 step 的 diff 以序列化字符串存储(含函数),由 deserializeStacks 逐条用 parseDSL 还原。
|
|
476
|
+
const parseDSL = getEditorConfig('parseDSL');
|
|
477
|
+
this.state.pageSteps = deserializeStacks(snapshot.pageSteps, parseDSL);
|
|
478
|
+
this.state.codeBlockState = deserializeStacks(snapshot.codeBlockState, parseDSL);
|
|
479
|
+
this.state.dataSourceState = deserializeStacks(snapshot.dataSourceState, parseDSL);
|
|
480
|
+
// initial 基线作为页面栈 index 0 的 step 随 pageSteps 一并还原,无需单独恢复。
|
|
481
|
+
this.state.pageId = snapshot.pageId;
|
|
482
|
+
|
|
483
|
+
this.setCanUndoRedo();
|
|
484
|
+
this.emit('restore-from-indexed-db', snapshot);
|
|
485
|
+
return snapshot;
|
|
486
|
+
}
|
|
487
|
+
|
|
416
488
|
/**
|
|
417
489
|
* 取出当前活动页的历史步骤平铺列表(包含已应用 + 已撤销)。
|
|
418
490
|
* 列表按时间正序,最早一步在最前面。
|
|
@@ -446,15 +518,14 @@ class History extends BaseService {
|
|
|
446
518
|
const list = undoRedo.getElementList();
|
|
447
519
|
if (!list.length) return [];
|
|
448
520
|
const cursor = undoRedo.getCursor();
|
|
449
|
-
|
|
521
|
+
// initial 基线(index 0)不作为普通操作组展示,过滤掉;其余真实 step 的 index 保持不变,
|
|
522
|
+
// 以便面板 goto(index+1) / revert(index) 仍直接对应栈内位置。底部「初始」行由 getPageMarker 驱动。
|
|
523
|
+
return mergePageSteps(targetPageId, list, cursor).filter((group) => group.opType !== 'initial');
|
|
450
524
|
}
|
|
451
525
|
|
|
452
526
|
/**
|
|
453
|
-
* 取出全部代码块的历史栈,按 codeBlockId
|
|
454
|
-
*
|
|
455
|
-
* - 这正是"代码块/数据源各自按 id 分栈"的天然表现,再叠加"连续修改同目标的相邻步骤合并展示"。
|
|
456
|
-
* - 合并后 group 暴露子步骤数组,UI 可展开查看每一步的 changeRecords。
|
|
457
|
-
* - applied 字段:组内最后一步是否处于已应用段。
|
|
527
|
+
* 取出全部代码块的历史栈,按 codeBlockId 分桶展示。
|
|
528
|
+
* 同一栈内每条操作记录独立成组,不做相邻 update 合并。
|
|
458
529
|
*/
|
|
459
530
|
public getCodeBlockHistoryGroups(): CodeBlockHistoryGroup[] {
|
|
460
531
|
const groups: CodeBlockHistoryGroup[] = [];
|
|
@@ -463,7 +534,7 @@ class History extends BaseService {
|
|
|
463
534
|
const list = undoRedo.getElementList();
|
|
464
535
|
if (!list.length) return;
|
|
465
536
|
const cursor = undoRedo.getCursor();
|
|
466
|
-
groups.push(...
|
|
537
|
+
groups.push(...mergeStackSteps('code-block', id, list, cursor));
|
|
467
538
|
});
|
|
468
539
|
return groups;
|
|
469
540
|
}
|
|
@@ -511,7 +582,42 @@ class History extends BaseService {
|
|
|
511
582
|
}
|
|
512
583
|
|
|
513
584
|
/**
|
|
514
|
-
*
|
|
585
|
+
* 按历史记录 uuid 在指定页面(默认当前活动页)的栈中查找其索引。
|
|
586
|
+
* 找不到时返回 -1。供「按 uuid 回滚」等需要把 uuid 映射回 index 的场景使用。
|
|
587
|
+
*/
|
|
588
|
+
public getPageStepIndexByUuid(uuid: string, pageId?: Id): number {
|
|
589
|
+
if (!uuid) return -1;
|
|
590
|
+
return this.getPageStepList(pageId).findIndex((entry) => entry.step.uuid === uuid);
|
|
591
|
+
}
|
|
592
|
+
|
|
593
|
+
/**
|
|
594
|
+
* 按历史记录 uuid 在全部代码块栈中查找其所属 codeBlockId 与索引。
|
|
595
|
+
* 找不到时返回 null。
|
|
596
|
+
*/
|
|
597
|
+
public findCodeBlockStepLocationByUuid(uuid: string): { id: Id; index: number } | null {
|
|
598
|
+
if (!uuid) return null;
|
|
599
|
+
for (const id of Object.keys(this.state.codeBlockState)) {
|
|
600
|
+
const index = this.getCodeBlockStepList(id).findIndex((entry) => entry.step.uuid === uuid);
|
|
601
|
+
if (index >= 0) return { id, index };
|
|
602
|
+
}
|
|
603
|
+
return null;
|
|
604
|
+
}
|
|
605
|
+
|
|
606
|
+
/**
|
|
607
|
+
* 按历史记录 uuid 在全部数据源栈中查找其所属 dataSourceId 与索引。
|
|
608
|
+
* 找不到时返回 null。
|
|
609
|
+
*/
|
|
610
|
+
public findDataSourceStepLocationByUuid(uuid: string): { id: Id; index: number } | null {
|
|
611
|
+
if (!uuid) return null;
|
|
612
|
+
for (const id of Object.keys(this.state.dataSourceState)) {
|
|
613
|
+
const index = this.getDataSourceStepList(id).findIndex((entry) => entry.step.uuid === uuid);
|
|
614
|
+
if (index >= 0) return { id, index };
|
|
615
|
+
}
|
|
616
|
+
return null;
|
|
617
|
+
}
|
|
618
|
+
|
|
619
|
+
/**
|
|
620
|
+
* 取出全部数据源的历史栈,按 dataSourceId 分桶展示。同上,每条操作独立成组。
|
|
515
621
|
*/
|
|
516
622
|
public getDataSourceHistoryGroups(): DataSourceHistoryGroup[] {
|
|
517
623
|
const groups: DataSourceHistoryGroup[] = [];
|
|
@@ -520,7 +626,7 @@ class History extends BaseService {
|
|
|
520
626
|
const list = undoRedo.getElementList();
|
|
521
627
|
if (!list.length) return;
|
|
522
628
|
const cursor = undoRedo.getCursor();
|
|
523
|
-
groups.push(...
|
|
629
|
+
groups.push(...mergeStackSteps('data-source', id, list, cursor));
|
|
524
630
|
});
|
|
525
631
|
return groups;
|
|
526
632
|
}
|
|
@@ -540,30 +646,21 @@ class History extends BaseService {
|
|
|
540
646
|
return this.state.pageSteps[targetPageId];
|
|
541
647
|
}
|
|
542
648
|
|
|
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
649
|
/**
|
|
550
|
-
*
|
|
650
|
+
* 把基础 dbName 与当前 DSL(root app)的 id 拼成最终库名,实现不同应用历史隔离。
|
|
651
|
+
* 取不到 app id(如尚未加载 DSL)时退回基础 dbName。
|
|
551
652
|
*/
|
|
552
|
-
private
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
}
|
|
556
|
-
return this.state.codeBlockState[codeBlockId];
|
|
653
|
+
private resolveDbName(dbName: string, appId?: Id): string {
|
|
654
|
+
// 优先用显式传入的 appId(「先恢复再 set root」时 root 尚未就绪);否则回退到当前 root.id。
|
|
655
|
+
const resolvedAppId = appId ?? editorService.get('root')?.id;
|
|
656
|
+
return resolvedAppId ? `${dbName}-${resolvedAppId}` : dbName;
|
|
557
657
|
}
|
|
558
658
|
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
this.state.dataSourceState[dataSourceId] = new UndoRedo<DataSourceStepValue>();
|
|
565
|
-
}
|
|
566
|
-
return this.state.dataSourceState[dataSourceId];
|
|
659
|
+
private setCanUndoRedo(): void {
|
|
660
|
+
const undoRedo = this.getUndoRedo();
|
|
661
|
+
this.state.canRedo = undoRedo?.canRedo() || false;
|
|
662
|
+
// 初始基线之上才可撤销:cursor 必须高于底线(有 initial 时为 1)。
|
|
663
|
+
this.state.canUndo = undoRedo ? undoRedo.getCursor() > undoFloor(undoRedo) : false;
|
|
567
664
|
}
|
|
568
665
|
}
|
|
569
666
|
|
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) {
|