@flowgram-vue/history 0.2.0
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/LICENSE +22 -0
- package/dist/index.cjs +851 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.ts +683 -0
- package/dist/index.js +843 -0
- package/dist/index.js.map +1 -0
- package/package.json +54 -0
- package/src/create-history-plugin.ts +24 -0
- package/src/history/history-manager.ts +129 -0
- package/src/history/history-service.ts +244 -0
- package/src/history/history-stack.ts +166 -0
- package/src/history/index.ts +12 -0
- package/src/history/stack-operation.ts +112 -0
- package/src/history/types.ts +325 -0
- package/src/history/undo-redo-service.ts +189 -0
- package/src/history-config.ts +14 -0
- package/src/history-container-module.ts +47 -0
- package/src/history-context.ts +19 -0
- package/src/index.ts +10 -0
- package/src/operation/index.ts +9 -0
- package/src/operation/operation-contribution.ts +12 -0
- package/src/operation/operation-registry.ts +58 -0
- package/src/operation/operation-service.ts +105 -0
- package/src/operation/types.ts +109 -0
package/dist/index.js
ADDED
|
@@ -0,0 +1,843 @@
|
|
|
1
|
+
import { multiInject, optional, postConstruct, injectable, inject, ContainerModule } from 'inversify';
|
|
2
|
+
import { Disposable, DisposableCollection, Emitter } from '@flowgram-vue/utils';
|
|
3
|
+
import { nanoid } from 'nanoid';
|
|
4
|
+
import { cloneDeep, pick } from 'lodash-es';
|
|
5
|
+
import { definePluginCreator } from '@flowgram-vue/core';
|
|
6
|
+
|
|
7
|
+
var __defProp = Object.defineProperty;
|
|
8
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
9
|
+
var __decorateClass = (decorators, target, key, kind) => {
|
|
10
|
+
var result = kind > 1 ? void 0 : kind ? __getOwnPropDesc(target, key) : target;
|
|
11
|
+
for (var i = decorators.length - 1, decorator; i >= 0; i--)
|
|
12
|
+
if (decorator = decorators[i])
|
|
13
|
+
result = (kind ? decorator(target, key, result) : decorator(result)) || result;
|
|
14
|
+
if (kind && result) __defProp(target, key, result);
|
|
15
|
+
return result;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
// src/operation/operation-contribution.ts
|
|
19
|
+
var OperationContribution = /* @__PURE__ */ Symbol("OperationContribution");
|
|
20
|
+
var OperationRegistry = class {
|
|
21
|
+
constructor() {
|
|
22
|
+
this._operationMetas = /* @__PURE__ */ new Map();
|
|
23
|
+
this.contributions = [];
|
|
24
|
+
}
|
|
25
|
+
init() {
|
|
26
|
+
for (const contrib of this.contributions) {
|
|
27
|
+
contrib.registerOperationMeta?.(this);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* 注册操作的元数据
|
|
32
|
+
* @param operationMeta 操作的元数据
|
|
33
|
+
* @returns 销毁函数
|
|
34
|
+
*/
|
|
35
|
+
registerOperationMeta(operationMeta) {
|
|
36
|
+
if (this._operationMetas.has(operationMeta.type)) {
|
|
37
|
+
console.warn(`A operation meta ${operationMeta.type} is already registered.`);
|
|
38
|
+
return Disposable.NULL;
|
|
39
|
+
}
|
|
40
|
+
const toDispose = new DisposableCollection(this._doRegisterOperationMetaMeta(operationMeta));
|
|
41
|
+
return toDispose;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* 获取操作的元数据
|
|
45
|
+
* @param type 操作类型
|
|
46
|
+
* @returns 操作的元数据
|
|
47
|
+
*/
|
|
48
|
+
getOperationMeta(type) {
|
|
49
|
+
return this._operationMetas.get(type);
|
|
50
|
+
}
|
|
51
|
+
_doRegisterOperationMetaMeta(operationMeta) {
|
|
52
|
+
this._operationMetas.set(operationMeta.type, operationMeta);
|
|
53
|
+
return {
|
|
54
|
+
dispose: () => {
|
|
55
|
+
this._operationMetas.delete(operationMeta.type);
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
};
|
|
60
|
+
__decorateClass([
|
|
61
|
+
multiInject(OperationContribution),
|
|
62
|
+
optional()
|
|
63
|
+
], OperationRegistry.prototype, "contributions", 2);
|
|
64
|
+
__decorateClass([
|
|
65
|
+
postConstruct()
|
|
66
|
+
], OperationRegistry.prototype, "init", 1);
|
|
67
|
+
OperationRegistry = __decorateClass([
|
|
68
|
+
injectable()
|
|
69
|
+
], OperationRegistry);
|
|
70
|
+
var HistoryContext = class {
|
|
71
|
+
};
|
|
72
|
+
HistoryContext = __decorateClass([
|
|
73
|
+
injectable()
|
|
74
|
+
], HistoryContext);
|
|
75
|
+
var HistoryConfig = class {
|
|
76
|
+
constructor() {
|
|
77
|
+
this.generateId = () => nanoid();
|
|
78
|
+
this.getSnapshot = () => "";
|
|
79
|
+
}
|
|
80
|
+
};
|
|
81
|
+
HistoryConfig = __decorateClass([
|
|
82
|
+
injectable()
|
|
83
|
+
], HistoryConfig);
|
|
84
|
+
|
|
85
|
+
// src/operation/operation-service.ts
|
|
86
|
+
var OperationService = class {
|
|
87
|
+
constructor() {
|
|
88
|
+
this.applyEmitter = new Emitter();
|
|
89
|
+
this.onApply = this.applyEmitter.event;
|
|
90
|
+
this._toDispose = new DisposableCollection();
|
|
91
|
+
}
|
|
92
|
+
init() {
|
|
93
|
+
this._toDispose.push(this.applyEmitter);
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* 执行操作
|
|
97
|
+
* @param op
|
|
98
|
+
* @returns
|
|
99
|
+
*/
|
|
100
|
+
applyOperation(op, options) {
|
|
101
|
+
const meta = this.operationRegistry.getOperationMeta(op.type);
|
|
102
|
+
if (!meta) {
|
|
103
|
+
throw new Error(`Operation meta ${op.type} has not registered.`);
|
|
104
|
+
}
|
|
105
|
+
let res;
|
|
106
|
+
if (!options?.noApply) {
|
|
107
|
+
res = meta.apply(op, this.context.source);
|
|
108
|
+
}
|
|
109
|
+
this.applyEmitter.fire(op);
|
|
110
|
+
return res;
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* 根据操作类型获取操作的label
|
|
114
|
+
* @param operation 操作
|
|
115
|
+
* @returns
|
|
116
|
+
*/
|
|
117
|
+
getOperationLabel(operation) {
|
|
118
|
+
const operationMeta = this.operationRegistry.getOperationMeta(operation.type);
|
|
119
|
+
if (operationMeta && operationMeta.getLabel) {
|
|
120
|
+
return operationMeta.getLabel(operation, this.context.source);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* 根据操作类型获取操作的description
|
|
125
|
+
* @param operation 操作
|
|
126
|
+
* @returns
|
|
127
|
+
*/
|
|
128
|
+
getOperationDescription(operation) {
|
|
129
|
+
const operationMeta = this.operationRegistry.getOperationMeta(operation.type);
|
|
130
|
+
if (operationMeta && operationMeta.getDescription) {
|
|
131
|
+
return operationMeta.getDescription(operation, this.context.source);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* 操作取反
|
|
136
|
+
* @param operations
|
|
137
|
+
* @returns
|
|
138
|
+
*/
|
|
139
|
+
inverseOperations(operations) {
|
|
140
|
+
return operations.map((op) => this.inverseOperation(op)).reverse();
|
|
141
|
+
}
|
|
142
|
+
inverseOperation(op) {
|
|
143
|
+
const meta = this.operationRegistry.getOperationMeta(op.type);
|
|
144
|
+
if (!meta) {
|
|
145
|
+
throw new Error(`Operation meta ${op.type} has not registered.`);
|
|
146
|
+
}
|
|
147
|
+
return meta.inverse(op);
|
|
148
|
+
}
|
|
149
|
+
dispose() {
|
|
150
|
+
this._toDispose.dispose();
|
|
151
|
+
}
|
|
152
|
+
};
|
|
153
|
+
__decorateClass([
|
|
154
|
+
inject(OperationRegistry)
|
|
155
|
+
], OperationService.prototype, "operationRegistry", 2);
|
|
156
|
+
__decorateClass([
|
|
157
|
+
inject(HistoryContext)
|
|
158
|
+
], OperationService.prototype, "context", 2);
|
|
159
|
+
__decorateClass([
|
|
160
|
+
inject(HistoryConfig)
|
|
161
|
+
], OperationService.prototype, "config", 2);
|
|
162
|
+
__decorateClass([
|
|
163
|
+
postConstruct()
|
|
164
|
+
], OperationService.prototype, "init", 1);
|
|
165
|
+
OperationService = __decorateClass([
|
|
166
|
+
injectable()
|
|
167
|
+
], OperationService);
|
|
168
|
+
|
|
169
|
+
// src/history/types.ts
|
|
170
|
+
var UndoRedoChangeType = /* @__PURE__ */ ((UndoRedoChangeType2) => {
|
|
171
|
+
UndoRedoChangeType2["UNDO"] = "undo";
|
|
172
|
+
UndoRedoChangeType2["REDO"] = "redo";
|
|
173
|
+
UndoRedoChangeType2["PUSH"] = "push";
|
|
174
|
+
UndoRedoChangeType2["CLEAR"] = "clear";
|
|
175
|
+
return UndoRedoChangeType2;
|
|
176
|
+
})(UndoRedoChangeType || {});
|
|
177
|
+
var HistoryStackChangeType = /* @__PURE__ */ ((HistoryStackChangeType2) => {
|
|
178
|
+
HistoryStackChangeType2["ADD"] = "add";
|
|
179
|
+
HistoryStackChangeType2["UPDATE"] = "update";
|
|
180
|
+
HistoryStackChangeType2["CLEAR"] = "clear";
|
|
181
|
+
HistoryStackChangeType2["ADD_OPERATION"] = "add_operation";
|
|
182
|
+
HistoryStackChangeType2["UPDATE_OPERATION"] = "update_operation";
|
|
183
|
+
return HistoryStackChangeType2;
|
|
184
|
+
})(HistoryStackChangeType || {});
|
|
185
|
+
var HistoryMergeEventType = /* @__PURE__ */ ((HistoryMergeEventType2) => {
|
|
186
|
+
HistoryMergeEventType2["ADD"] = "ADD";
|
|
187
|
+
HistoryMergeEventType2["UPDATE"] = "UPDATE";
|
|
188
|
+
return HistoryMergeEventType2;
|
|
189
|
+
})(HistoryMergeEventType || {});
|
|
190
|
+
|
|
191
|
+
// src/history/undo-redo-service.ts
|
|
192
|
+
var UndoRedoService = class {
|
|
193
|
+
constructor() {
|
|
194
|
+
this._undoing = false;
|
|
195
|
+
this._redoing = false;
|
|
196
|
+
this._limit = 100;
|
|
197
|
+
this.onChangeEmitter = new Emitter();
|
|
198
|
+
this.onChange = this.onChangeEmitter.event;
|
|
199
|
+
this._toDispose = new DisposableCollection();
|
|
200
|
+
this._undoStack = [];
|
|
201
|
+
this._redoStack = [];
|
|
202
|
+
this._toDispose.push(this.onChangeEmitter);
|
|
203
|
+
}
|
|
204
|
+
setLimit(limit) {
|
|
205
|
+
this._limit = limit;
|
|
206
|
+
}
|
|
207
|
+
pushElement(element) {
|
|
208
|
+
this._redoStack = [];
|
|
209
|
+
this._stackPush(this._undoStack, element);
|
|
210
|
+
this._toDispose.push(element);
|
|
211
|
+
this._emitChange("push" /* PUSH */, element);
|
|
212
|
+
}
|
|
213
|
+
getUndoStack() {
|
|
214
|
+
return this._undoStack;
|
|
215
|
+
}
|
|
216
|
+
getRedoStack() {
|
|
217
|
+
return this._redoStack;
|
|
218
|
+
}
|
|
219
|
+
getLastElement() {
|
|
220
|
+
return this._undoStack[this._undoStack.length - 1];
|
|
221
|
+
}
|
|
222
|
+
/**
|
|
223
|
+
* 执行undo
|
|
224
|
+
* @returns void
|
|
225
|
+
*/
|
|
226
|
+
async undo() {
|
|
227
|
+
if (!this.canUndo()) {
|
|
228
|
+
return;
|
|
229
|
+
}
|
|
230
|
+
if (this._undoing) {
|
|
231
|
+
return;
|
|
232
|
+
}
|
|
233
|
+
this._undoing = true;
|
|
234
|
+
const item = this._undoStack.pop();
|
|
235
|
+
try {
|
|
236
|
+
await item.undo();
|
|
237
|
+
} finally {
|
|
238
|
+
this._stackPush(this._redoStack, item);
|
|
239
|
+
this._emitChange("undo" /* UNDO */, item);
|
|
240
|
+
this._undoing = false;
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
/**
|
|
244
|
+
* 执行redo
|
|
245
|
+
* @returns void
|
|
246
|
+
*/
|
|
247
|
+
async redo() {
|
|
248
|
+
if (!this.canRedo()) {
|
|
249
|
+
return;
|
|
250
|
+
}
|
|
251
|
+
if (this._redoing) {
|
|
252
|
+
return;
|
|
253
|
+
}
|
|
254
|
+
this._redoing = true;
|
|
255
|
+
const item = this._redoStack.pop();
|
|
256
|
+
try {
|
|
257
|
+
await item.redo();
|
|
258
|
+
} finally {
|
|
259
|
+
this._stackPush(this._undoStack, item);
|
|
260
|
+
this._emitChange("redo" /* REDO */, item);
|
|
261
|
+
this._redoing = false;
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
/**
|
|
265
|
+
* 是否可undo
|
|
266
|
+
* @returns true代表可以,false代表不可以
|
|
267
|
+
*/
|
|
268
|
+
canUndo() {
|
|
269
|
+
return this._undoStack.length > 0;
|
|
270
|
+
}
|
|
271
|
+
/**
|
|
272
|
+
* 是否可redo
|
|
273
|
+
* @returns true代表可以,false代表不可以
|
|
274
|
+
*/
|
|
275
|
+
canRedo() {
|
|
276
|
+
return this._redoStack.length > 0;
|
|
277
|
+
}
|
|
278
|
+
/**
|
|
279
|
+
* 是否可以push
|
|
280
|
+
* @returns true代表可以,false代表不可以
|
|
281
|
+
*/
|
|
282
|
+
canPush() {
|
|
283
|
+
return !this._redoing && !this._undoing;
|
|
284
|
+
}
|
|
285
|
+
/**
|
|
286
|
+
* 清空
|
|
287
|
+
*/
|
|
288
|
+
clear() {
|
|
289
|
+
this.clearRedoStack();
|
|
290
|
+
this.clearUndoStack();
|
|
291
|
+
this._emitChange("clear" /* CLEAR */);
|
|
292
|
+
}
|
|
293
|
+
/**
|
|
294
|
+
* 清空redo栈
|
|
295
|
+
*/
|
|
296
|
+
clearRedoStack() {
|
|
297
|
+
this._redoStack.forEach((element) => {
|
|
298
|
+
element.dispose();
|
|
299
|
+
});
|
|
300
|
+
this._redoStack = [];
|
|
301
|
+
}
|
|
302
|
+
/**
|
|
303
|
+
* 清空undo栈
|
|
304
|
+
*/
|
|
305
|
+
clearUndoStack() {
|
|
306
|
+
this._undoStack.forEach((element) => {
|
|
307
|
+
element.dispose();
|
|
308
|
+
});
|
|
309
|
+
this._undoStack = [];
|
|
310
|
+
}
|
|
311
|
+
/**
|
|
312
|
+
* 销毁
|
|
313
|
+
*/
|
|
314
|
+
dispose() {
|
|
315
|
+
this.clear();
|
|
316
|
+
this._toDispose.dispose();
|
|
317
|
+
}
|
|
318
|
+
_stackPush(stack, element) {
|
|
319
|
+
stack.push(element);
|
|
320
|
+
if (stack.length > this._limit) {
|
|
321
|
+
stack.shift();
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
_emitChange(type, element) {
|
|
325
|
+
if (element) {
|
|
326
|
+
this.onChangeEmitter.fire({ type, element });
|
|
327
|
+
} else {
|
|
328
|
+
this.onChangeEmitter.fire({ type });
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
};
|
|
332
|
+
UndoRedoService = __decorateClass([
|
|
333
|
+
injectable()
|
|
334
|
+
], UndoRedoService);
|
|
335
|
+
var StackOperation = class {
|
|
336
|
+
constructor(operationService, operations = []) {
|
|
337
|
+
this._toDispose = new DisposableCollection();
|
|
338
|
+
this._timestamp = Date.now();
|
|
339
|
+
this._operationService = operationService;
|
|
340
|
+
this._operations = operations.map((op) => this._operation(op));
|
|
341
|
+
this._id = operationService.config.generateId();
|
|
342
|
+
}
|
|
343
|
+
get id() {
|
|
344
|
+
return this._id;
|
|
345
|
+
}
|
|
346
|
+
getTimestamp() {
|
|
347
|
+
return this._timestamp;
|
|
348
|
+
}
|
|
349
|
+
pushOperation(operation) {
|
|
350
|
+
const op = this._operation(operation);
|
|
351
|
+
this._operations.push(op);
|
|
352
|
+
return op;
|
|
353
|
+
}
|
|
354
|
+
getOperations() {
|
|
355
|
+
return this._operations;
|
|
356
|
+
}
|
|
357
|
+
getChangeOperations(type) {
|
|
358
|
+
if (type === "undo" /* UNDO */) {
|
|
359
|
+
return this._operationService.inverseOperations(this._operations);
|
|
360
|
+
}
|
|
361
|
+
return this._operations;
|
|
362
|
+
}
|
|
363
|
+
getFirstOperation() {
|
|
364
|
+
return this._operations[0];
|
|
365
|
+
}
|
|
366
|
+
getLastOperation() {
|
|
367
|
+
return this._operations[this._operations.length - 1];
|
|
368
|
+
}
|
|
369
|
+
async undo() {
|
|
370
|
+
const inverseOps = this._operationService.inverseOperations(this._operations);
|
|
371
|
+
for (const op of inverseOps) {
|
|
372
|
+
await this._apply(op);
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
async redo() {
|
|
376
|
+
for (const op of this._operations) {
|
|
377
|
+
await this._apply(op);
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
revert(type) {
|
|
381
|
+
let operations = this._operations;
|
|
382
|
+
if (type !== "undo" /* UNDO */) {
|
|
383
|
+
operations = this._operations.map((op) => this._inverse(op)).reverse();
|
|
384
|
+
}
|
|
385
|
+
for (const op of operations) {
|
|
386
|
+
this._apply(op);
|
|
387
|
+
}
|
|
388
|
+
}
|
|
389
|
+
_inverse(op) {
|
|
390
|
+
return this._operationService.inverseOperation(op);
|
|
391
|
+
}
|
|
392
|
+
async _apply(op) {
|
|
393
|
+
await this._operationService.applyOperation(op);
|
|
394
|
+
}
|
|
395
|
+
_operation(op) {
|
|
396
|
+
return {
|
|
397
|
+
...op,
|
|
398
|
+
value: cloneDeep(op.value),
|
|
399
|
+
id: this._operationService.config.generateId()
|
|
400
|
+
};
|
|
401
|
+
}
|
|
402
|
+
dispose() {
|
|
403
|
+
this._toDispose.dispose();
|
|
404
|
+
}
|
|
405
|
+
};
|
|
406
|
+
var HistoryStack = class {
|
|
407
|
+
constructor() {
|
|
408
|
+
this._items = [];
|
|
409
|
+
this.onChangeEmitter = new Emitter();
|
|
410
|
+
this.onChange = this.onChangeEmitter.event;
|
|
411
|
+
this._toDispose = new DisposableCollection();
|
|
412
|
+
this.limit = 100;
|
|
413
|
+
this._toDispose.push(this.onChangeEmitter);
|
|
414
|
+
}
|
|
415
|
+
get items() {
|
|
416
|
+
return this._items;
|
|
417
|
+
}
|
|
418
|
+
add(service, item) {
|
|
419
|
+
const historyItem = this._getHistoryItem(service, item);
|
|
420
|
+
this._items.unshift(historyItem);
|
|
421
|
+
if (this._items.length > this.limit) {
|
|
422
|
+
this._items.pop();
|
|
423
|
+
}
|
|
424
|
+
this.onChangeEmitter.fire({
|
|
425
|
+
type: "add" /* ADD */,
|
|
426
|
+
value: historyItem,
|
|
427
|
+
service
|
|
428
|
+
});
|
|
429
|
+
return historyItem;
|
|
430
|
+
}
|
|
431
|
+
findById(id) {
|
|
432
|
+
return this._items.find((item) => item.id === id);
|
|
433
|
+
}
|
|
434
|
+
changeByIndex(index, service, item) {
|
|
435
|
+
const historyItem = this._getHistoryItem(service, item);
|
|
436
|
+
this._items[index] = historyItem;
|
|
437
|
+
this.onChangeEmitter.fire({
|
|
438
|
+
type: "update" /* UPDATE */,
|
|
439
|
+
value: historyItem,
|
|
440
|
+
service
|
|
441
|
+
});
|
|
442
|
+
}
|
|
443
|
+
addOperation(service, id, op) {
|
|
444
|
+
const historyItem = this._items.find((item) => item.id === id);
|
|
445
|
+
if (!historyItem) {
|
|
446
|
+
console.warn("no history item found");
|
|
447
|
+
return;
|
|
448
|
+
}
|
|
449
|
+
const newOperatopn = this._getHistoryOperation(service, op);
|
|
450
|
+
historyItem.operations.push(newOperatopn);
|
|
451
|
+
this.onChangeEmitter.fire({
|
|
452
|
+
type: "add_operation" /* ADD_OPERATION */,
|
|
453
|
+
value: {
|
|
454
|
+
historyItem,
|
|
455
|
+
operation: newOperatopn
|
|
456
|
+
},
|
|
457
|
+
service
|
|
458
|
+
});
|
|
459
|
+
}
|
|
460
|
+
updateOperation(service, id, op) {
|
|
461
|
+
const historyItem = this._items.find((item) => item.id === id);
|
|
462
|
+
if (!historyItem) {
|
|
463
|
+
console.warn("no history item found");
|
|
464
|
+
return;
|
|
465
|
+
}
|
|
466
|
+
const index = historyItem.operations.findIndex((op2) => op2.id === op2.id);
|
|
467
|
+
if (index < 0) {
|
|
468
|
+
console.warn("no operation found");
|
|
469
|
+
return;
|
|
470
|
+
}
|
|
471
|
+
const newOperatopn = this._getHistoryOperation(service, op);
|
|
472
|
+
historyItem.operations.splice(index, 1, newOperatopn);
|
|
473
|
+
this.onChangeEmitter.fire({
|
|
474
|
+
type: "update_operation" /* UPDATE_OPERATION */,
|
|
475
|
+
value: {
|
|
476
|
+
historyItem,
|
|
477
|
+
operation: newOperatopn
|
|
478
|
+
},
|
|
479
|
+
service
|
|
480
|
+
});
|
|
481
|
+
}
|
|
482
|
+
clear() {
|
|
483
|
+
this._items = [];
|
|
484
|
+
}
|
|
485
|
+
dispose() {
|
|
486
|
+
this._items = [];
|
|
487
|
+
this._toDispose.dispose();
|
|
488
|
+
}
|
|
489
|
+
_getHistoryItem(service, item) {
|
|
490
|
+
return {
|
|
491
|
+
...item,
|
|
492
|
+
uri: service.context.uri,
|
|
493
|
+
time: HistoryStack.dateFormat(item.timestamp),
|
|
494
|
+
operations: item.operations.map(
|
|
495
|
+
(op) => this._getHistoryOperation(service, op, item.type !== "push" /* PUSH */)
|
|
496
|
+
)
|
|
497
|
+
};
|
|
498
|
+
}
|
|
499
|
+
_getHistoryOperation(service, op, generateId = false) {
|
|
500
|
+
let id;
|
|
501
|
+
if (generateId) {
|
|
502
|
+
id = this.historyConfig.generateId();
|
|
503
|
+
} else {
|
|
504
|
+
const oldId = op.id;
|
|
505
|
+
if (!oldId) {
|
|
506
|
+
throw new Error("no operation id found");
|
|
507
|
+
}
|
|
508
|
+
id = oldId;
|
|
509
|
+
}
|
|
510
|
+
return {
|
|
511
|
+
...cloneDeep(op),
|
|
512
|
+
id,
|
|
513
|
+
label: service.operationService.getOperationLabel(op),
|
|
514
|
+
description: service.operationService.getOperationDescription(op),
|
|
515
|
+
timestamp: Date.now()
|
|
516
|
+
};
|
|
517
|
+
}
|
|
518
|
+
static dateFormat(timestamp) {
|
|
519
|
+
return new Date(timestamp).toLocaleString();
|
|
520
|
+
}
|
|
521
|
+
};
|
|
522
|
+
__decorateClass([
|
|
523
|
+
inject(HistoryConfig)
|
|
524
|
+
], HistoryStack.prototype, "historyConfig", 2);
|
|
525
|
+
HistoryStack = __decorateClass([
|
|
526
|
+
injectable()
|
|
527
|
+
], HistoryStack);
|
|
528
|
+
|
|
529
|
+
// src/history/history-manager.ts
|
|
530
|
+
var HistoryManager = class {
|
|
531
|
+
constructor() {
|
|
532
|
+
this._historyServices = /* @__PURE__ */ new Map();
|
|
533
|
+
this._toDispose = new DisposableCollection();
|
|
534
|
+
}
|
|
535
|
+
registerHistoryService(service) {
|
|
536
|
+
const toDispose = new DisposableCollection();
|
|
537
|
+
toDispose.pushAll([
|
|
538
|
+
service.undoRedoService.onChange((event) => {
|
|
539
|
+
if (event.type === "clear" /* CLEAR */) {
|
|
540
|
+
return;
|
|
541
|
+
}
|
|
542
|
+
const { type, element } = event;
|
|
543
|
+
const operations = element.getChangeOperations(type);
|
|
544
|
+
const historyStackItem = {
|
|
545
|
+
id: type === "push" /* PUSH */ ? element.id : this.historyConfig.generateId(),
|
|
546
|
+
type,
|
|
547
|
+
uri: service.context.uri,
|
|
548
|
+
operations,
|
|
549
|
+
timestamp: Date.now()
|
|
550
|
+
};
|
|
551
|
+
this.historyStack.add(service, historyStackItem);
|
|
552
|
+
}),
|
|
553
|
+
service.onMerge((event) => {
|
|
554
|
+
this._handleMerge(service, event);
|
|
555
|
+
})
|
|
556
|
+
]);
|
|
557
|
+
this._historyServices.set(service, toDispose);
|
|
558
|
+
this._toDispose.push(
|
|
559
|
+
service.onWillDispose(() => {
|
|
560
|
+
this.unregisterHistoryService(service);
|
|
561
|
+
})
|
|
562
|
+
);
|
|
563
|
+
}
|
|
564
|
+
unregisterHistoryService(service) {
|
|
565
|
+
const disposable = this._historyServices.get(service);
|
|
566
|
+
if (!disposable) {
|
|
567
|
+
return;
|
|
568
|
+
}
|
|
569
|
+
disposable.dispose();
|
|
570
|
+
this._historyServices.delete(service);
|
|
571
|
+
}
|
|
572
|
+
getHistoryServiceByURI(uri) {
|
|
573
|
+
for (const service of this._historyServices.keys()) {
|
|
574
|
+
if (service.context.uri === uri) {
|
|
575
|
+
return service;
|
|
576
|
+
}
|
|
577
|
+
}
|
|
578
|
+
}
|
|
579
|
+
getFirstHistoryService() {
|
|
580
|
+
for (const service of this._historyServices.keys()) {
|
|
581
|
+
return service;
|
|
582
|
+
}
|
|
583
|
+
}
|
|
584
|
+
dispose() {
|
|
585
|
+
this._toDispose.dispose();
|
|
586
|
+
this.historyStack.dispose();
|
|
587
|
+
this._historyServices.forEach((service) => service.dispose());
|
|
588
|
+
this._historyServices.clear();
|
|
589
|
+
}
|
|
590
|
+
_handleMerge(service, event) {
|
|
591
|
+
const { element, operation } = event.value;
|
|
592
|
+
const find = this.historyStack.findById(element.id);
|
|
593
|
+
if (!find) {
|
|
594
|
+
return;
|
|
595
|
+
}
|
|
596
|
+
if (!operation.id) {
|
|
597
|
+
console.warn("no operation id found");
|
|
598
|
+
return;
|
|
599
|
+
}
|
|
600
|
+
if (event.type === "UPDATE" /* UPDATE */) {
|
|
601
|
+
this.historyStack.updateOperation(
|
|
602
|
+
service,
|
|
603
|
+
element.id,
|
|
604
|
+
operation
|
|
605
|
+
);
|
|
606
|
+
}
|
|
607
|
+
if (event.type === "ADD" /* ADD */) {
|
|
608
|
+
this.historyStack.addOperation(
|
|
609
|
+
service,
|
|
610
|
+
element.id,
|
|
611
|
+
operation
|
|
612
|
+
);
|
|
613
|
+
}
|
|
614
|
+
}
|
|
615
|
+
};
|
|
616
|
+
__decorateClass([
|
|
617
|
+
inject(HistoryStack)
|
|
618
|
+
], HistoryManager.prototype, "historyStack", 2);
|
|
619
|
+
__decorateClass([
|
|
620
|
+
inject(HistoryConfig)
|
|
621
|
+
], HistoryManager.prototype, "historyConfig", 2);
|
|
622
|
+
HistoryManager = __decorateClass([
|
|
623
|
+
injectable()
|
|
624
|
+
], HistoryManager);
|
|
625
|
+
|
|
626
|
+
// src/history/history-service.ts
|
|
627
|
+
var HistoryService = class {
|
|
628
|
+
constructor() {
|
|
629
|
+
this._toDispose = new DisposableCollection();
|
|
630
|
+
this._transacting = false;
|
|
631
|
+
this._transactOperation = null;
|
|
632
|
+
this._locked = false;
|
|
633
|
+
this._willDisposeEmitter = new Emitter();
|
|
634
|
+
this._mergeEmitter = new Emitter();
|
|
635
|
+
this.onWillDispose = this._willDisposeEmitter.event;
|
|
636
|
+
this.onMerge = this._mergeEmitter.event;
|
|
637
|
+
}
|
|
638
|
+
get onApply() {
|
|
639
|
+
return this.operationService.onApply;
|
|
640
|
+
}
|
|
641
|
+
init() {
|
|
642
|
+
this._toDispose.push(this._willDisposeEmitter);
|
|
643
|
+
this._toDispose.push(this._mergeEmitter);
|
|
644
|
+
}
|
|
645
|
+
start() {
|
|
646
|
+
this._locked = false;
|
|
647
|
+
}
|
|
648
|
+
stop() {
|
|
649
|
+
this._locked = true;
|
|
650
|
+
}
|
|
651
|
+
limit(num) {
|
|
652
|
+
this.undoRedoService.setLimit(num);
|
|
653
|
+
}
|
|
654
|
+
startTransaction() {
|
|
655
|
+
if (this._transacting) {
|
|
656
|
+
return;
|
|
657
|
+
}
|
|
658
|
+
this._transacting = true;
|
|
659
|
+
const stackOperation = new StackOperation(this.operationService, []);
|
|
660
|
+
this._transactOperation = stackOperation;
|
|
661
|
+
}
|
|
662
|
+
endTransaction() {
|
|
663
|
+
const stackOperation = this._transactOperation;
|
|
664
|
+
if (!stackOperation) {
|
|
665
|
+
return;
|
|
666
|
+
}
|
|
667
|
+
if (stackOperation.getOperations().length !== 0) {
|
|
668
|
+
this._pushStackOperation(stackOperation);
|
|
669
|
+
}
|
|
670
|
+
this._transactOperation = null;
|
|
671
|
+
this._transacting = false;
|
|
672
|
+
}
|
|
673
|
+
transact(transaction) {
|
|
674
|
+
if (this._transacting) {
|
|
675
|
+
return;
|
|
676
|
+
}
|
|
677
|
+
this.startTransaction();
|
|
678
|
+
transaction();
|
|
679
|
+
this.endTransaction();
|
|
680
|
+
}
|
|
681
|
+
pushOperation(operation, options) {
|
|
682
|
+
if (!this._canPush()) {
|
|
683
|
+
return;
|
|
684
|
+
}
|
|
685
|
+
const prev = this._transactOperation || this.undoRedoService.getLastElement();
|
|
686
|
+
const operationMeta = this.operationRegistry.getOperationMeta(operation.type);
|
|
687
|
+
if (!operationMeta) {
|
|
688
|
+
throw new Error(`Operation meta ${operation.type} has not registered.`);
|
|
689
|
+
}
|
|
690
|
+
if (operationMeta.shouldSave && !operationMeta.shouldSave(operation)) {
|
|
691
|
+
return operationMeta.apply(operation, this.context.source);
|
|
692
|
+
}
|
|
693
|
+
const res = this.operationService.applyOperation(operation, { noApply: options?.noApply });
|
|
694
|
+
if (operationMeta.getURI && !operation.uri) {
|
|
695
|
+
operation.uri = operationMeta.getURI(operation, this.context.source);
|
|
696
|
+
}
|
|
697
|
+
const shouldMerge = this._shouldMerge(operation, prev, operationMeta);
|
|
698
|
+
if (shouldMerge) {
|
|
699
|
+
if (typeof shouldMerge === "object") {
|
|
700
|
+
const operation2 = prev.getLastOperation();
|
|
701
|
+
operation2.value = shouldMerge.value;
|
|
702
|
+
this._mergeEmitter.fire({
|
|
703
|
+
type: "UPDATE" /* UPDATE */,
|
|
704
|
+
value: {
|
|
705
|
+
element: prev,
|
|
706
|
+
operation: operation2,
|
|
707
|
+
value: shouldMerge.value
|
|
708
|
+
}
|
|
709
|
+
});
|
|
710
|
+
} else {
|
|
711
|
+
const op = prev.pushOperation(operation);
|
|
712
|
+
this._mergeEmitter.fire({
|
|
713
|
+
type: "ADD" /* ADD */,
|
|
714
|
+
value: {
|
|
715
|
+
element: prev,
|
|
716
|
+
operation: op
|
|
717
|
+
}
|
|
718
|
+
});
|
|
719
|
+
}
|
|
720
|
+
} else {
|
|
721
|
+
const stackOperation = new StackOperation(this.operationService, [operation]);
|
|
722
|
+
this._pushStackOperation(stackOperation);
|
|
723
|
+
}
|
|
724
|
+
return res;
|
|
725
|
+
}
|
|
726
|
+
getHistoryOperations() {
|
|
727
|
+
return this.historyManager.historyStack.items.reverse().map(
|
|
728
|
+
(item) => item.operations.map((o) => ({
|
|
729
|
+
...pick(o, ["type", "value"]),
|
|
730
|
+
label: o.label || o.type
|
|
731
|
+
}))
|
|
732
|
+
).flat();
|
|
733
|
+
}
|
|
734
|
+
async undo() {
|
|
735
|
+
await this.undoRedoService.undo();
|
|
736
|
+
}
|
|
737
|
+
async redo() {
|
|
738
|
+
await this.undoRedoService.redo();
|
|
739
|
+
}
|
|
740
|
+
canUndo() {
|
|
741
|
+
return this.undoRedoService.canUndo();
|
|
742
|
+
}
|
|
743
|
+
canRedo() {
|
|
744
|
+
return this.undoRedoService.canRedo();
|
|
745
|
+
}
|
|
746
|
+
getSnapshot() {
|
|
747
|
+
return this.config.getSnapshot();
|
|
748
|
+
}
|
|
749
|
+
getRecords() {
|
|
750
|
+
throw new Error("Method not implemented.");
|
|
751
|
+
}
|
|
752
|
+
restore(historyRecord) {
|
|
753
|
+
throw new Error("Method not implemented.");
|
|
754
|
+
}
|
|
755
|
+
clear() {
|
|
756
|
+
this.undoRedoService.clear();
|
|
757
|
+
}
|
|
758
|
+
dispose() {
|
|
759
|
+
this._willDisposeEmitter.fire(this);
|
|
760
|
+
this._toDispose.dispose();
|
|
761
|
+
}
|
|
762
|
+
_canPush() {
|
|
763
|
+
if (this._locked) {
|
|
764
|
+
return false;
|
|
765
|
+
}
|
|
766
|
+
return this.undoRedoService.canPush();
|
|
767
|
+
}
|
|
768
|
+
_pushStackOperation(stackOperation) {
|
|
769
|
+
this.undoRedoService.pushElement(stackOperation);
|
|
770
|
+
this.undoRedoService.clearRedoStack();
|
|
771
|
+
}
|
|
772
|
+
_shouldMerge(operation, prev, operationMeta) {
|
|
773
|
+
if (!prev) {
|
|
774
|
+
return false;
|
|
775
|
+
}
|
|
776
|
+
if (this._transacting) {
|
|
777
|
+
return true;
|
|
778
|
+
}
|
|
779
|
+
return operationMeta.shouldMerge && operationMeta.shouldMerge(operation, prev.getLastOperation(), prev);
|
|
780
|
+
}
|
|
781
|
+
};
|
|
782
|
+
__decorateClass([
|
|
783
|
+
inject(UndoRedoService)
|
|
784
|
+
], HistoryService.prototype, "undoRedoService", 2);
|
|
785
|
+
__decorateClass([
|
|
786
|
+
inject(OperationRegistry)
|
|
787
|
+
], HistoryService.prototype, "operationRegistry", 2);
|
|
788
|
+
__decorateClass([
|
|
789
|
+
inject(OperationService)
|
|
790
|
+
], HistoryService.prototype, "operationService", 2);
|
|
791
|
+
__decorateClass([
|
|
792
|
+
inject(HistoryContext)
|
|
793
|
+
], HistoryService.prototype, "context", 2);
|
|
794
|
+
__decorateClass([
|
|
795
|
+
inject(HistoryConfig)
|
|
796
|
+
], HistoryService.prototype, "config", 2);
|
|
797
|
+
__decorateClass([
|
|
798
|
+
inject(HistoryManager)
|
|
799
|
+
], HistoryService.prototype, "historyManager", 2);
|
|
800
|
+
__decorateClass([
|
|
801
|
+
postConstruct()
|
|
802
|
+
], HistoryService.prototype, "init", 1);
|
|
803
|
+
HistoryService = __decorateClass([
|
|
804
|
+
injectable()
|
|
805
|
+
], HistoryService);
|
|
806
|
+
var HistoryContainerModule = new ContainerModule(
|
|
807
|
+
(bind, _unbind, _isBound, _rebind, _unbindAsync, onActivation, _onDeactivation) => {
|
|
808
|
+
bind(OperationRegistry).toSelf().inSingletonScope();
|
|
809
|
+
bind(OperationService).toSelf().inSingletonScope();
|
|
810
|
+
bind(UndoRedoService).toSelf().inSingletonScope();
|
|
811
|
+
bind(HistoryService).toSelf().inSingletonScope();
|
|
812
|
+
bind(HistoryContext).toSelf().inSingletonScope();
|
|
813
|
+
bind(HistoryManager).toSelf().inSingletonScope();
|
|
814
|
+
bind(HistoryStack).toSelf().inSingletonScope();
|
|
815
|
+
bind(HistoryConfig).toSelf().inSingletonScope();
|
|
816
|
+
onActivation(HistoryService, (ctx, historyService) => {
|
|
817
|
+
let historyManager;
|
|
818
|
+
if (ctx.container?.parent?.isBound(HistoryManager)) {
|
|
819
|
+
historyManager = ctx.container?.parent?.get(HistoryManager);
|
|
820
|
+
} else {
|
|
821
|
+
historyManager = ctx.container.get(HistoryManager);
|
|
822
|
+
}
|
|
823
|
+
if (!historyManager) {
|
|
824
|
+
return historyService;
|
|
825
|
+
}
|
|
826
|
+
historyService.historyManager = historyManager;
|
|
827
|
+
historyManager.registerHistoryService(historyService);
|
|
828
|
+
return historyService;
|
|
829
|
+
});
|
|
830
|
+
}
|
|
831
|
+
);
|
|
832
|
+
var createHistoryPlugin = definePluginCreator({
|
|
833
|
+
onInit: (ctx, opts) => {
|
|
834
|
+
if (opts.onApply) {
|
|
835
|
+
ctx.get(OperationService).onApply(opts.onApply.bind(null, ctx));
|
|
836
|
+
}
|
|
837
|
+
},
|
|
838
|
+
containerModules: [HistoryContainerModule]
|
|
839
|
+
});
|
|
840
|
+
|
|
841
|
+
export { HistoryConfig, HistoryContainerModule, HistoryContext, HistoryManager, HistoryMergeEventType, HistoryService, HistoryStack, HistoryStackChangeType, OperationContribution, OperationRegistry, OperationService, StackOperation, UndoRedoChangeType, UndoRedoService, createHistoryPlugin };
|
|
842
|
+
//# sourceMappingURL=index.js.map
|
|
843
|
+
//# sourceMappingURL=index.js.map
|