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