@knotx/plugins-history 0.2.7 → 0.2.9

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/index.cjs CHANGED
@@ -83,6 +83,8 @@ class History extends (_a = core.BasePlugin, _canUndo_dec = [decorators.register
83
83
  __publicField(this, "isUndoRedo", false);
84
84
  __publicField(this, "maxHistory");
85
85
  __publicField(this, "tagMap", /* @__PURE__ */ new Map());
86
+ __publicField(this, "operationBuffer", {});
87
+ __publicField(this, "operations$", new rxjs.Subject());
86
88
  __publicField(this, "canUndo", __runInitializers(_init, 8, this, false)), __runInitializers(_init, 11, this);
87
89
  __publicField(this, "canRedo", __runInitializers(_init, 12, this, false)), __runInitializers(_init, 15, this);
88
90
  __publicField(this, "ref", __runInitializers(_init, 16, this, this)), __runInitializers(_init, 19, this);
@@ -99,14 +101,28 @@ class History extends (_a = core.BasePlugin, _canUndo_dec = [decorators.register
99
101
  __publicField(this, "edgesManager", __runInitializers(_init, 28, this)), __runInitializers(_init, 31, this);
100
102
  __publicField(this, "dataManagers", /* @__PURE__ */ new Map());
101
103
  }
102
- init(config) {
103
- var _a2;
104
+ init(config = {}) {
105
+ var _a2, _b;
104
106
  this.maxHistory = (_a2 = config.maxHistory) != null ? _a2 : 50;
107
+ const debounceTimeConfig = (_b = config.debounceTime) != null ? _b : 20;
105
108
  this.dataManagers.set(this.nodesManager.tag, this.nodesManager);
106
109
  this.dataManagers.set(this.edgesManager.tag, this.edgesManager);
107
110
  this.dataManagers.forEach((dataManager) => {
108
111
  this.addOperationPipe(dataManager);
109
112
  });
113
+ this.subscriptions.push(
114
+ this.operations$.pipe(
115
+ rxjs.tap(([tag, operation]) => {
116
+ if (!this.operationBuffer[tag]) {
117
+ this.operationBuffer[tag] = [];
118
+ }
119
+ this.operationBuffer[tag].push(operation);
120
+ }),
121
+ rxjs.debounceTime(debounceTimeConfig)
122
+ ).subscribe(() => {
123
+ this.flushOperationBuffer();
124
+ })
125
+ );
110
126
  }
111
127
  destroy() {
112
128
  this.dataManagers.clear();
@@ -121,17 +137,22 @@ class History extends (_a = core.BasePlugin, _canUndo_dec = [decorators.register
121
137
  }
122
138
  if (!this.isUndoRedo) {
123
139
  const enrichedOperation = this.enrichOperation(dataManager, operation);
124
- this.pushHistory({
125
- value: {
126
- [dataManager.tag]: [enrichedOperation]
127
- },
128
- timestamp: Date.now()
129
- });
140
+ this.operations$.next([dataManager.tag, enrichedOperation]);
130
141
  }
131
142
  })
132
143
  )
133
144
  });
134
145
  }
146
+ flushOperationBuffer() {
147
+ if (Object.keys(this.operationBuffer).length === 0) {
148
+ return;
149
+ }
150
+ this.pushHistory({
151
+ value: __spreadValues({}, this.operationBuffer),
152
+ timestamp: Date.now()
153
+ });
154
+ this.operationBuffer = {};
155
+ }
135
156
  enrichOperation(dataManager, operation) {
136
157
  switch (operation.type) {
137
158
  case "update": {
@@ -177,6 +198,7 @@ class History extends (_a = core.BasePlugin, _canUndo_dec = [decorators.register
177
198
  undo() {
178
199
  if (!this.canUndo)
179
200
  return;
201
+ this.flushOperationBufferImmediately();
180
202
  const state = this.history[this.currentIndex];
181
203
  this.isUndoRedo = true;
182
204
  try {
@@ -184,10 +206,13 @@ class History extends (_a = core.BasePlugin, _canUndo_dec = [decorators.register
184
206
  const dataManager = this.dataManagers.get(key);
185
207
  if (!dataManager)
186
208
  return;
187
- this.reverseOperation(dataManager, {
209
+ const reverseOperations = this.reverseOperation({
188
210
  type: "batch",
189
211
  operations: core.flattenOperations(operations)
190
212
  });
213
+ if (reverseOperations) {
214
+ dataManager.dispatch(reverseOperations);
215
+ }
191
216
  });
192
217
  this.currentIndex--;
193
218
  this.updateCanUndoRedo();
@@ -198,6 +223,7 @@ class History extends (_a = core.BasePlugin, _canUndo_dec = [decorators.register
198
223
  redo() {
199
224
  if (!this.canRedo)
200
225
  return;
226
+ this.flushOperationBufferImmediately();
201
227
  this.currentIndex++;
202
228
  const state = this.history[this.currentIndex];
203
229
  this.isUndoRedo = true;
@@ -217,12 +243,14 @@ class History extends (_a = core.BasePlugin, _canUndo_dec = [decorators.register
217
243
  }
218
244
  }
219
245
  addTag(tag) {
246
+ this.flushOperationBufferImmediately();
220
247
  this.tagMap.set(tag, this.history[this.currentIndex]);
221
248
  }
222
249
  removeTag(tag) {
223
250
  this.tagMap.delete(tag);
224
251
  }
225
252
  toTag(tag) {
253
+ this.flushOperationBufferImmediately();
226
254
  const tagIndex = this.ensureTagIndex(tag);
227
255
  if (tagIndex === false) {
228
256
  return;
@@ -259,30 +287,50 @@ class History extends (_a = core.BasePlugin, _canUndo_dec = [decorators.register
259
287
  }
260
288
  return stateIndex;
261
289
  }
262
- reverseOperation(dataManager, operation) {
290
+ reverseOperation(operation) {
263
291
  switch (operation.type) {
264
292
  case "add":
265
- dataManager.dispatch({ type: "remove", id: operation.data.id });
266
- break;
293
+ return { type: "remove", id: operation.data.id };
267
294
  case "remove":
268
295
  if (operation.removedNode) {
269
- dataManager.dispatch({ type: "add", data: operation.removedNode });
296
+ return { type: "add", data: operation.removedNode };
270
297
  }
271
298
  break;
272
299
  case "update":
273
300
  if (operation.originalData) {
274
- dataManager.dispatch({
301
+ return {
275
302
  type: "update",
276
303
  id: operation.id,
277
304
  data: operation.originalData
278
- });
305
+ };
279
306
  }
280
307
  break;
281
- case "batch":
282
- operation.operations.reverse().forEach((op) => this.reverseOperation(dataManager, op));
283
- break;
308
+ case "batch": {
309
+ const operations = [];
310
+ for (const op of operation.operations.reverse()) {
311
+ const reverseOperation = this.reverseOperation(op);
312
+ if (reverseOperation) {
313
+ operations.push(reverseOperation);
314
+ }
315
+ }
316
+ if (operations.length > 0) {
317
+ return {
318
+ type: "batch",
319
+ operations
320
+ };
321
+ }
322
+ return void 0;
323
+ }
324
+ default:
325
+ return void 0;
284
326
  }
285
327
  }
328
+ /**
329
+ * 立即处理操作缓冲区,不等待 debounce
330
+ */
331
+ flushOperationBufferImmediately() {
332
+ this.flushOperationBuffer();
333
+ }
286
334
  }
287
335
  _init = __decoratorStart(_a);
288
336
  __decorateElement(_init, 1, "init", _init_dec, History);
package/dist/index.d.cts CHANGED
@@ -18,16 +18,18 @@ interface IHistory {
18
18
  toTag: (tag: string) => void;
19
19
  clearTags: () => void;
20
20
  }
21
+ type HistoryOperation<T extends Record<string, IData>> = DataOperation<T[keyof T]> & {
22
+ originalData?: Partial<T[keyof T]>;
23
+ };
21
24
  interface HistoryState<T extends Record<string, IData> = Record<string, any>> {
22
25
  value: {
23
- [key: string]: Array<DataOperation<T[keyof T]> & {
24
- originalData?: Partial<T[keyof T]>;
25
- }>;
26
+ [key: string]: HistoryOperation<T>[];
26
27
  };
27
28
  timestamp: number;
28
29
  }
29
30
  interface HistoryConfig {
30
31
  maxHistory?: number;
32
+ debounceTime?: number;
31
33
  }
32
34
  declare class History<T extends Record<string, IData> = Record<string, any>> extends BasePlugin<'history', HistoryConfig> {
33
35
  name: "history";
@@ -36,6 +38,8 @@ declare class History<T extends Record<string, IData> = Record<string, any>> ext
36
38
  private isUndoRedo;
37
39
  private maxHistory;
38
40
  private tagMap;
41
+ private operationBuffer;
42
+ private operations$;
39
43
  canUndo: boolean;
40
44
  canRedo: boolean;
41
45
  ref: this;
@@ -43,9 +47,10 @@ declare class History<T extends Record<string, IData> = Record<string, any>> ext
43
47
  private nodesManager;
44
48
  private edgesManager;
45
49
  private dataManagers;
46
- init(config: HistoryConfig): void;
50
+ init(config?: HistoryConfig): void;
47
51
  destroy(): void;
48
52
  private addOperationPipe;
53
+ private flushOperationBuffer;
49
54
  private enrichOperation;
50
55
  private pushHistory;
51
56
  private updateCanUndoRedo;
@@ -57,6 +62,10 @@ declare class History<T extends Record<string, IData> = Record<string, any>> ext
57
62
  clearTags(): void;
58
63
  private ensureTagIndex;
59
64
  private reverseOperation;
65
+ /**
66
+ * 立即处理操作缓冲区,不等待 debounce
67
+ */
68
+ private flushOperationBufferImmediately;
60
69
  }
61
70
 
62
- export { History, type HistoryConfig, type HistoryState, type IHistory };
71
+ export { History, type HistoryConfig, type HistoryOperation, type HistoryState, type IHistory };
package/dist/index.d.mts CHANGED
@@ -18,16 +18,18 @@ interface IHistory {
18
18
  toTag: (tag: string) => void;
19
19
  clearTags: () => void;
20
20
  }
21
+ type HistoryOperation<T extends Record<string, IData>> = DataOperation<T[keyof T]> & {
22
+ originalData?: Partial<T[keyof T]>;
23
+ };
21
24
  interface HistoryState<T extends Record<string, IData> = Record<string, any>> {
22
25
  value: {
23
- [key: string]: Array<DataOperation<T[keyof T]> & {
24
- originalData?: Partial<T[keyof T]>;
25
- }>;
26
+ [key: string]: HistoryOperation<T>[];
26
27
  };
27
28
  timestamp: number;
28
29
  }
29
30
  interface HistoryConfig {
30
31
  maxHistory?: number;
32
+ debounceTime?: number;
31
33
  }
32
34
  declare class History<T extends Record<string, IData> = Record<string, any>> extends BasePlugin<'history', HistoryConfig> {
33
35
  name: "history";
@@ -36,6 +38,8 @@ declare class History<T extends Record<string, IData> = Record<string, any>> ext
36
38
  private isUndoRedo;
37
39
  private maxHistory;
38
40
  private tagMap;
41
+ private operationBuffer;
42
+ private operations$;
39
43
  canUndo: boolean;
40
44
  canRedo: boolean;
41
45
  ref: this;
@@ -43,9 +47,10 @@ declare class History<T extends Record<string, IData> = Record<string, any>> ext
43
47
  private nodesManager;
44
48
  private edgesManager;
45
49
  private dataManagers;
46
- init(config: HistoryConfig): void;
50
+ init(config?: HistoryConfig): void;
47
51
  destroy(): void;
48
52
  private addOperationPipe;
53
+ private flushOperationBuffer;
49
54
  private enrichOperation;
50
55
  private pushHistory;
51
56
  private updateCanUndoRedo;
@@ -57,6 +62,10 @@ declare class History<T extends Record<string, IData> = Record<string, any>> ext
57
62
  clearTags(): void;
58
63
  private ensureTagIndex;
59
64
  private reverseOperation;
65
+ /**
66
+ * 立即处理操作缓冲区,不等待 debounce
67
+ */
68
+ private flushOperationBufferImmediately;
60
69
  }
61
70
 
62
- export { History, type HistoryConfig, type HistoryState, type IHistory };
71
+ export { History, type HistoryConfig, type HistoryOperation, type HistoryState, type IHistory };
package/dist/index.d.ts CHANGED
@@ -18,16 +18,18 @@ interface IHistory {
18
18
  toTag: (tag: string) => void;
19
19
  clearTags: () => void;
20
20
  }
21
+ type HistoryOperation<T extends Record<string, IData>> = DataOperation<T[keyof T]> & {
22
+ originalData?: Partial<T[keyof T]>;
23
+ };
21
24
  interface HistoryState<T extends Record<string, IData> = Record<string, any>> {
22
25
  value: {
23
- [key: string]: Array<DataOperation<T[keyof T]> & {
24
- originalData?: Partial<T[keyof T]>;
25
- }>;
26
+ [key: string]: HistoryOperation<T>[];
26
27
  };
27
28
  timestamp: number;
28
29
  }
29
30
  interface HistoryConfig {
30
31
  maxHistory?: number;
32
+ debounceTime?: number;
31
33
  }
32
34
  declare class History<T extends Record<string, IData> = Record<string, any>> extends BasePlugin<'history', HistoryConfig> {
33
35
  name: "history";
@@ -36,6 +38,8 @@ declare class History<T extends Record<string, IData> = Record<string, any>> ext
36
38
  private isUndoRedo;
37
39
  private maxHistory;
38
40
  private tagMap;
41
+ private operationBuffer;
42
+ private operations$;
39
43
  canUndo: boolean;
40
44
  canRedo: boolean;
41
45
  ref: this;
@@ -43,9 +47,10 @@ declare class History<T extends Record<string, IData> = Record<string, any>> ext
43
47
  private nodesManager;
44
48
  private edgesManager;
45
49
  private dataManagers;
46
- init(config: HistoryConfig): void;
50
+ init(config?: HistoryConfig): void;
47
51
  destroy(): void;
48
52
  private addOperationPipe;
53
+ private flushOperationBuffer;
49
54
  private enrichOperation;
50
55
  private pushHistory;
51
56
  private updateCanUndoRedo;
@@ -57,6 +62,10 @@ declare class History<T extends Record<string, IData> = Record<string, any>> ext
57
62
  clearTags(): void;
58
63
  private ensureTagIndex;
59
64
  private reverseOperation;
65
+ /**
66
+ * 立即处理操作缓冲区,不等待 debounce
67
+ */
68
+ private flushOperationBufferImmediately;
60
69
  }
61
70
 
62
- export { History, type HistoryConfig, type HistoryState, type IHistory };
71
+ export { History, type HistoryConfig, type HistoryOperation, type HistoryState, type IHistory };
package/dist/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import { isDraftOperation, isInitOperation, flattenOperations, BasePlugin } from '@knotx/core';
2
2
  import { register, inject, OnInit, OnDestroy } from '@knotx/decorators';
3
- import { pipe, tap } from 'rxjs';
3
+ import { Subject, tap, debounceTime, pipe } from 'rxjs';
4
4
 
5
5
  var __create = Object.create;
6
6
  var __defProp = Object.defineProperty;
@@ -81,6 +81,8 @@ class History extends (_a = BasePlugin, _canUndo_dec = [register("canUndo")], _c
81
81
  __publicField(this, "isUndoRedo", false);
82
82
  __publicField(this, "maxHistory");
83
83
  __publicField(this, "tagMap", /* @__PURE__ */ new Map());
84
+ __publicField(this, "operationBuffer", {});
85
+ __publicField(this, "operations$", new Subject());
84
86
  __publicField(this, "canUndo", __runInitializers(_init, 8, this, false)), __runInitializers(_init, 11, this);
85
87
  __publicField(this, "canRedo", __runInitializers(_init, 12, this, false)), __runInitializers(_init, 15, this);
86
88
  __publicField(this, "ref", __runInitializers(_init, 16, this, this)), __runInitializers(_init, 19, this);
@@ -97,14 +99,28 @@ class History extends (_a = BasePlugin, _canUndo_dec = [register("canUndo")], _c
97
99
  __publicField(this, "edgesManager", __runInitializers(_init, 28, this)), __runInitializers(_init, 31, this);
98
100
  __publicField(this, "dataManagers", /* @__PURE__ */ new Map());
99
101
  }
100
- init(config) {
101
- var _a2;
102
+ init(config = {}) {
103
+ var _a2, _b;
102
104
  this.maxHistory = (_a2 = config.maxHistory) != null ? _a2 : 50;
105
+ const debounceTimeConfig = (_b = config.debounceTime) != null ? _b : 20;
103
106
  this.dataManagers.set(this.nodesManager.tag, this.nodesManager);
104
107
  this.dataManagers.set(this.edgesManager.tag, this.edgesManager);
105
108
  this.dataManagers.forEach((dataManager) => {
106
109
  this.addOperationPipe(dataManager);
107
110
  });
111
+ this.subscriptions.push(
112
+ this.operations$.pipe(
113
+ tap(([tag, operation]) => {
114
+ if (!this.operationBuffer[tag]) {
115
+ this.operationBuffer[tag] = [];
116
+ }
117
+ this.operationBuffer[tag].push(operation);
118
+ }),
119
+ debounceTime(debounceTimeConfig)
120
+ ).subscribe(() => {
121
+ this.flushOperationBuffer();
122
+ })
123
+ );
108
124
  }
109
125
  destroy() {
110
126
  this.dataManagers.clear();
@@ -119,17 +135,22 @@ class History extends (_a = BasePlugin, _canUndo_dec = [register("canUndo")], _c
119
135
  }
120
136
  if (!this.isUndoRedo) {
121
137
  const enrichedOperation = this.enrichOperation(dataManager, operation);
122
- this.pushHistory({
123
- value: {
124
- [dataManager.tag]: [enrichedOperation]
125
- },
126
- timestamp: Date.now()
127
- });
138
+ this.operations$.next([dataManager.tag, enrichedOperation]);
128
139
  }
129
140
  })
130
141
  )
131
142
  });
132
143
  }
144
+ flushOperationBuffer() {
145
+ if (Object.keys(this.operationBuffer).length === 0) {
146
+ return;
147
+ }
148
+ this.pushHistory({
149
+ value: __spreadValues({}, this.operationBuffer),
150
+ timestamp: Date.now()
151
+ });
152
+ this.operationBuffer = {};
153
+ }
133
154
  enrichOperation(dataManager, operation) {
134
155
  switch (operation.type) {
135
156
  case "update": {
@@ -175,6 +196,7 @@ class History extends (_a = BasePlugin, _canUndo_dec = [register("canUndo")], _c
175
196
  undo() {
176
197
  if (!this.canUndo)
177
198
  return;
199
+ this.flushOperationBufferImmediately();
178
200
  const state = this.history[this.currentIndex];
179
201
  this.isUndoRedo = true;
180
202
  try {
@@ -182,10 +204,13 @@ class History extends (_a = BasePlugin, _canUndo_dec = [register("canUndo")], _c
182
204
  const dataManager = this.dataManagers.get(key);
183
205
  if (!dataManager)
184
206
  return;
185
- this.reverseOperation(dataManager, {
207
+ const reverseOperations = this.reverseOperation({
186
208
  type: "batch",
187
209
  operations: flattenOperations(operations)
188
210
  });
211
+ if (reverseOperations) {
212
+ dataManager.dispatch(reverseOperations);
213
+ }
189
214
  });
190
215
  this.currentIndex--;
191
216
  this.updateCanUndoRedo();
@@ -196,6 +221,7 @@ class History extends (_a = BasePlugin, _canUndo_dec = [register("canUndo")], _c
196
221
  redo() {
197
222
  if (!this.canRedo)
198
223
  return;
224
+ this.flushOperationBufferImmediately();
199
225
  this.currentIndex++;
200
226
  const state = this.history[this.currentIndex];
201
227
  this.isUndoRedo = true;
@@ -215,12 +241,14 @@ class History extends (_a = BasePlugin, _canUndo_dec = [register("canUndo")], _c
215
241
  }
216
242
  }
217
243
  addTag(tag) {
244
+ this.flushOperationBufferImmediately();
218
245
  this.tagMap.set(tag, this.history[this.currentIndex]);
219
246
  }
220
247
  removeTag(tag) {
221
248
  this.tagMap.delete(tag);
222
249
  }
223
250
  toTag(tag) {
251
+ this.flushOperationBufferImmediately();
224
252
  const tagIndex = this.ensureTagIndex(tag);
225
253
  if (tagIndex === false) {
226
254
  return;
@@ -257,30 +285,50 @@ class History extends (_a = BasePlugin, _canUndo_dec = [register("canUndo")], _c
257
285
  }
258
286
  return stateIndex;
259
287
  }
260
- reverseOperation(dataManager, operation) {
288
+ reverseOperation(operation) {
261
289
  switch (operation.type) {
262
290
  case "add":
263
- dataManager.dispatch({ type: "remove", id: operation.data.id });
264
- break;
291
+ return { type: "remove", id: operation.data.id };
265
292
  case "remove":
266
293
  if (operation.removedNode) {
267
- dataManager.dispatch({ type: "add", data: operation.removedNode });
294
+ return { type: "add", data: operation.removedNode };
268
295
  }
269
296
  break;
270
297
  case "update":
271
298
  if (operation.originalData) {
272
- dataManager.dispatch({
299
+ return {
273
300
  type: "update",
274
301
  id: operation.id,
275
302
  data: operation.originalData
276
- });
303
+ };
277
304
  }
278
305
  break;
279
- case "batch":
280
- operation.operations.reverse().forEach((op) => this.reverseOperation(dataManager, op));
281
- break;
306
+ case "batch": {
307
+ const operations = [];
308
+ for (const op of operation.operations.reverse()) {
309
+ const reverseOperation = this.reverseOperation(op);
310
+ if (reverseOperation) {
311
+ operations.push(reverseOperation);
312
+ }
313
+ }
314
+ if (operations.length > 0) {
315
+ return {
316
+ type: "batch",
317
+ operations
318
+ };
319
+ }
320
+ return void 0;
321
+ }
322
+ default:
323
+ return void 0;
282
324
  }
283
325
  }
326
+ /**
327
+ * 立即处理操作缓冲区,不等待 debounce
328
+ */
329
+ flushOperationBufferImmediately() {
330
+ this.flushOperationBuffer();
331
+ }
284
332
  }
285
333
  _init = __decoratorStart(_a);
286
334
  __decorateElement(_init, 1, "init", _init_dec, History);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@knotx/plugins-history",
3
- "version": "0.2.7",
3
+ "version": "0.2.9",
4
4
  "description": "History Plugin for Knotx",
5
5
  "author": "boenfu",
6
6
  "license": "MIT",
@@ -29,13 +29,13 @@
29
29
  ],
30
30
  "dependencies": {
31
31
  "rxjs": "^7.8.1",
32
- "@knotx/core": "0.2.6",
33
- "@knotx/decorators": "0.2.6"
32
+ "@knotx/decorators": "0.2.7",
33
+ "@knotx/core": "0.2.7"
34
34
  },
35
35
  "devDependencies": {
36
- "@knotx/build-config": "0.2.6",
37
- "@knotx/eslint-config": "0.2.6",
38
- "@knotx/typescript-config": "0.2.6"
36
+ "@knotx/eslint-config": "0.2.7",
37
+ "@knotx/typescript-config": "0.2.7",
38
+ "@knotx/build-config": "0.2.7"
39
39
  },
40
40
  "scripts": {
41
41
  "build": "unbuild",