@knotx/plugins-history 0.2.7 → 0.2.8

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 {
@@ -198,6 +220,7 @@ class History extends (_a = core.BasePlugin, _canUndo_dec = [decorators.register
198
220
  redo() {
199
221
  if (!this.canRedo)
200
222
  return;
223
+ this.flushOperationBufferImmediately();
201
224
  this.currentIndex++;
202
225
  const state = this.history[this.currentIndex];
203
226
  this.isUndoRedo = true;
@@ -217,12 +240,14 @@ class History extends (_a = core.BasePlugin, _canUndo_dec = [decorators.register
217
240
  }
218
241
  }
219
242
  addTag(tag) {
243
+ this.flushOperationBufferImmediately();
220
244
  this.tagMap.set(tag, this.history[this.currentIndex]);
221
245
  }
222
246
  removeTag(tag) {
223
247
  this.tagMap.delete(tag);
224
248
  }
225
249
  toTag(tag) {
250
+ this.flushOperationBufferImmediately();
226
251
  const tagIndex = this.ensureTagIndex(tag);
227
252
  if (tagIndex === false) {
228
253
  return;
@@ -283,6 +308,12 @@ class History extends (_a = core.BasePlugin, _canUndo_dec = [decorators.register
283
308
  break;
284
309
  }
285
310
  }
311
+ /**
312
+ * 立即处理操作缓冲区,不等待 debounce
313
+ */
314
+ flushOperationBufferImmediately() {
315
+ this.flushOperationBuffer();
316
+ }
286
317
  }
287
318
  _init = __decoratorStart(_a);
288
319
  __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 {
@@ -196,6 +218,7 @@ class History extends (_a = BasePlugin, _canUndo_dec = [register("canUndo")], _c
196
218
  redo() {
197
219
  if (!this.canRedo)
198
220
  return;
221
+ this.flushOperationBufferImmediately();
199
222
  this.currentIndex++;
200
223
  const state = this.history[this.currentIndex];
201
224
  this.isUndoRedo = true;
@@ -215,12 +238,14 @@ class History extends (_a = BasePlugin, _canUndo_dec = [register("canUndo")], _c
215
238
  }
216
239
  }
217
240
  addTag(tag) {
241
+ this.flushOperationBufferImmediately();
218
242
  this.tagMap.set(tag, this.history[this.currentIndex]);
219
243
  }
220
244
  removeTag(tag) {
221
245
  this.tagMap.delete(tag);
222
246
  }
223
247
  toTag(tag) {
248
+ this.flushOperationBufferImmediately();
224
249
  const tagIndex = this.ensureTagIndex(tag);
225
250
  if (tagIndex === false) {
226
251
  return;
@@ -281,6 +306,12 @@ class History extends (_a = BasePlugin, _canUndo_dec = [register("canUndo")], _c
281
306
  break;
282
307
  }
283
308
  }
309
+ /**
310
+ * 立即处理操作缓冲区,不等待 debounce
311
+ */
312
+ flushOperationBufferImmediately() {
313
+ this.flushOperationBuffer();
314
+ }
284
315
  }
285
316
  _init = __decoratorStart(_a);
286
317
  __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.8",
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/core": "0.2.7",
33
+ "@knotx/decorators": "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/build-config": "0.2.7",
37
+ "@knotx/eslint-config": "0.2.7",
38
+ "@knotx/typescript-config": "0.2.7"
39
39
  },
40
40
  "scripts": {
41
41
  "build": "unbuild",