@knotx/plugins-history 0.2.5 → 0.2.7

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
@@ -82,6 +82,7 @@ class History extends (_a = core.BasePlugin, _canUndo_dec = [decorators.register
82
82
  __publicField(this, "currentIndex", -1);
83
83
  __publicField(this, "isUndoRedo", false);
84
84
  __publicField(this, "maxHistory");
85
+ __publicField(this, "tagMap", /* @__PURE__ */ new Map());
85
86
  __publicField(this, "canUndo", __runInitializers(_init, 8, this, false)), __runInitializers(_init, 11, this);
86
87
  __publicField(this, "canRedo", __runInitializers(_init, 12, this, false)), __runInitializers(_init, 15, this);
87
88
  __publicField(this, "ref", __runInitializers(_init, 16, this, this)), __runInitializers(_init, 19, this);
@@ -109,6 +110,7 @@ class History extends (_a = core.BasePlugin, _canUndo_dec = [decorators.register
109
110
  }
110
111
  destroy() {
111
112
  this.dataManagers.clear();
113
+ this.tagMap.clear();
112
114
  }
113
115
  addOperationPipe(dataManager) {
114
116
  dataManager.addDataOperationPipe({
@@ -214,6 +216,49 @@ class History extends (_a = core.BasePlugin, _canUndo_dec = [decorators.register
214
216
  this.isUndoRedo = false;
215
217
  }
216
218
  }
219
+ addTag(tag) {
220
+ this.tagMap.set(tag, this.history[this.currentIndex]);
221
+ }
222
+ removeTag(tag) {
223
+ this.tagMap.delete(tag);
224
+ }
225
+ toTag(tag) {
226
+ const tagIndex = this.ensureTagIndex(tag);
227
+ if (tagIndex === false) {
228
+ return;
229
+ }
230
+ if (this.currentIndex > tagIndex) {
231
+ let offset = this.currentIndex - tagIndex;
232
+ while (offset > 0) {
233
+ this.undo();
234
+ offset--;
235
+ }
236
+ } else if (this.currentIndex < tagIndex) {
237
+ let offset = tagIndex - this.currentIndex;
238
+ while (offset > 0) {
239
+ this.redo();
240
+ offset--;
241
+ }
242
+ }
243
+ }
244
+ clearTags() {
245
+ this.tagMap.clear();
246
+ }
247
+ ensureTagIndex(tag) {
248
+ if (!this.tagMap.has(tag)) {
249
+ return false;
250
+ }
251
+ const state = this.tagMap.get(tag);
252
+ if (typeof state === "undefined") {
253
+ return -1;
254
+ }
255
+ const stateIndex = this.history.indexOf(state);
256
+ if (stateIndex === -1) {
257
+ this.removeTag(tag);
258
+ return false;
259
+ }
260
+ return stateIndex;
261
+ }
217
262
  reverseOperation(dataManager, operation) {
218
263
  switch (operation.type) {
219
264
  case "add":
package/dist/index.d.cts CHANGED
@@ -13,6 +13,10 @@ declare module '@knotx/core' {
13
13
  interface IHistory {
14
14
  undo: () => void;
15
15
  redo: () => void;
16
+ addTag: (tag: string) => void;
17
+ removeTag: (tag: string) => void;
18
+ toTag: (tag: string) => void;
19
+ clearTags: () => void;
16
20
  }
17
21
  interface HistoryState<T extends Record<string, IData> = Record<string, any>> {
18
22
  value: {
@@ -31,6 +35,7 @@ declare class History<T extends Record<string, IData> = Record<string, any>> ext
31
35
  private currentIndex;
32
36
  private isUndoRedo;
33
37
  private maxHistory;
38
+ private tagMap;
34
39
  canUndo: boolean;
35
40
  canRedo: boolean;
36
41
  ref: this;
@@ -46,6 +51,11 @@ declare class History<T extends Record<string, IData> = Record<string, any>> ext
46
51
  private updateCanUndoRedo;
47
52
  undo(): void;
48
53
  redo(): void;
54
+ addTag(tag: string): void;
55
+ removeTag(tag: string): void;
56
+ toTag(tag: string): void;
57
+ clearTags(): void;
58
+ private ensureTagIndex;
49
59
  private reverseOperation;
50
60
  }
51
61
 
package/dist/index.d.mts CHANGED
@@ -13,6 +13,10 @@ declare module '@knotx/core' {
13
13
  interface IHistory {
14
14
  undo: () => void;
15
15
  redo: () => void;
16
+ addTag: (tag: string) => void;
17
+ removeTag: (tag: string) => void;
18
+ toTag: (tag: string) => void;
19
+ clearTags: () => void;
16
20
  }
17
21
  interface HistoryState<T extends Record<string, IData> = Record<string, any>> {
18
22
  value: {
@@ -31,6 +35,7 @@ declare class History<T extends Record<string, IData> = Record<string, any>> ext
31
35
  private currentIndex;
32
36
  private isUndoRedo;
33
37
  private maxHistory;
38
+ private tagMap;
34
39
  canUndo: boolean;
35
40
  canRedo: boolean;
36
41
  ref: this;
@@ -46,6 +51,11 @@ declare class History<T extends Record<string, IData> = Record<string, any>> ext
46
51
  private updateCanUndoRedo;
47
52
  undo(): void;
48
53
  redo(): void;
54
+ addTag(tag: string): void;
55
+ removeTag(tag: string): void;
56
+ toTag(tag: string): void;
57
+ clearTags(): void;
58
+ private ensureTagIndex;
49
59
  private reverseOperation;
50
60
  }
51
61
 
package/dist/index.d.ts CHANGED
@@ -13,6 +13,10 @@ declare module '@knotx/core' {
13
13
  interface IHistory {
14
14
  undo: () => void;
15
15
  redo: () => void;
16
+ addTag: (tag: string) => void;
17
+ removeTag: (tag: string) => void;
18
+ toTag: (tag: string) => void;
19
+ clearTags: () => void;
16
20
  }
17
21
  interface HistoryState<T extends Record<string, IData> = Record<string, any>> {
18
22
  value: {
@@ -31,6 +35,7 @@ declare class History<T extends Record<string, IData> = Record<string, any>> ext
31
35
  private currentIndex;
32
36
  private isUndoRedo;
33
37
  private maxHistory;
38
+ private tagMap;
34
39
  canUndo: boolean;
35
40
  canRedo: boolean;
36
41
  ref: this;
@@ -46,6 +51,11 @@ declare class History<T extends Record<string, IData> = Record<string, any>> ext
46
51
  private updateCanUndoRedo;
47
52
  undo(): void;
48
53
  redo(): void;
54
+ addTag(tag: string): void;
55
+ removeTag(tag: string): void;
56
+ toTag(tag: string): void;
57
+ clearTags(): void;
58
+ private ensureTagIndex;
49
59
  private reverseOperation;
50
60
  }
51
61
 
package/dist/index.js CHANGED
@@ -80,6 +80,7 @@ class History extends (_a = BasePlugin, _canUndo_dec = [register("canUndo")], _c
80
80
  __publicField(this, "currentIndex", -1);
81
81
  __publicField(this, "isUndoRedo", false);
82
82
  __publicField(this, "maxHistory");
83
+ __publicField(this, "tagMap", /* @__PURE__ */ new Map());
83
84
  __publicField(this, "canUndo", __runInitializers(_init, 8, this, false)), __runInitializers(_init, 11, this);
84
85
  __publicField(this, "canRedo", __runInitializers(_init, 12, this, false)), __runInitializers(_init, 15, this);
85
86
  __publicField(this, "ref", __runInitializers(_init, 16, this, this)), __runInitializers(_init, 19, this);
@@ -107,6 +108,7 @@ class History extends (_a = BasePlugin, _canUndo_dec = [register("canUndo")], _c
107
108
  }
108
109
  destroy() {
109
110
  this.dataManagers.clear();
111
+ this.tagMap.clear();
110
112
  }
111
113
  addOperationPipe(dataManager) {
112
114
  dataManager.addDataOperationPipe({
@@ -212,6 +214,49 @@ class History extends (_a = BasePlugin, _canUndo_dec = [register("canUndo")], _c
212
214
  this.isUndoRedo = false;
213
215
  }
214
216
  }
217
+ addTag(tag) {
218
+ this.tagMap.set(tag, this.history[this.currentIndex]);
219
+ }
220
+ removeTag(tag) {
221
+ this.tagMap.delete(tag);
222
+ }
223
+ toTag(tag) {
224
+ const tagIndex = this.ensureTagIndex(tag);
225
+ if (tagIndex === false) {
226
+ return;
227
+ }
228
+ if (this.currentIndex > tagIndex) {
229
+ let offset = this.currentIndex - tagIndex;
230
+ while (offset > 0) {
231
+ this.undo();
232
+ offset--;
233
+ }
234
+ } else if (this.currentIndex < tagIndex) {
235
+ let offset = tagIndex - this.currentIndex;
236
+ while (offset > 0) {
237
+ this.redo();
238
+ offset--;
239
+ }
240
+ }
241
+ }
242
+ clearTags() {
243
+ this.tagMap.clear();
244
+ }
245
+ ensureTagIndex(tag) {
246
+ if (!this.tagMap.has(tag)) {
247
+ return false;
248
+ }
249
+ const state = this.tagMap.get(tag);
250
+ if (typeof state === "undefined") {
251
+ return -1;
252
+ }
253
+ const stateIndex = this.history.indexOf(state);
254
+ if (stateIndex === -1) {
255
+ this.removeTag(tag);
256
+ return false;
257
+ }
258
+ return stateIndex;
259
+ }
215
260
  reverseOperation(dataManager, operation) {
216
261
  switch (operation.type) {
217
262
  case "add":
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@knotx/plugins-history",
3
- "version": "0.2.5",
3
+ "version": "0.2.7",
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.5",
33
- "@knotx/decorators": "0.2.5"
32
+ "@knotx/core": "0.2.6",
33
+ "@knotx/decorators": "0.2.6"
34
34
  },
35
35
  "devDependencies": {
36
- "@knotx/build-config": "0.2.5",
37
- "@knotx/eslint-config": "0.2.5",
38
- "@knotx/typescript-config": "0.2.5"
36
+ "@knotx/build-config": "0.2.6",
37
+ "@knotx/eslint-config": "0.2.6",
38
+ "@knotx/typescript-config": "0.2.6"
39
39
  },
40
40
  "scripts": {
41
41
  "build": "unbuild",