@etsoo/shared 1.1.30 → 1.1.33

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/README.md CHANGED
@@ -68,6 +68,7 @@ ETSOO Extended abstract history class
68
68
  |back|Back to the previous state|
69
69
  |clear|Clear all states but keep event listeners|
70
70
  |forward|Forward to the next state|
71
+ |getStatus|Get [undo, redo] status|
71
72
  |go|Go to the specific state|
72
73
  |pushState|Adds an entry to the history stack|
73
74
  |replaceState|Modifies the current history entry|
@@ -1,27 +1,20 @@
1
- import {
2
- EHistory,
3
- EHistoryEventData,
4
- EHistoryEventType
5
- } from '../src/types/EHistory';
6
- import { EventBase } from '../src/types/EventClass';
1
+ import { EHistory, EHistoryEvent } from '../src/types/EHistory';
7
2
 
8
3
  // Extended for tests
9
- class LHistoryEvent extends EventBase<EHistoryEventType, EHistoryEventData> {}
10
- class LHistory extends EHistory<number, EHistoryEventData> {
11
- protected createEvent(type: EHistoryEventType, index: number) {
12
- return new LHistoryEvent(this, type, { index });
13
- }
14
- }
4
+ class LHistory extends EHistory<number> {}
15
5
 
16
6
  test('Tests for history', () => {
17
7
  const history = new LHistory();
18
8
  expect(history.index).toBe(-1);
19
9
  history.pushState(1);
10
+ expect(history.getStatus()).toStrictEqual([false, false]);
20
11
  history.pushState(2);
21
12
  history.pushState(3);
13
+ expect(history.getStatus()).toStrictEqual([true, false]);
22
14
  expect(history.states).toStrictEqual([1, 2, 3]);
23
15
  expect(history.index).toBe(2);
24
16
  history.back();
17
+ expect(history.getStatus()).toStrictEqual([true, true]);
25
18
  history.pushState(4);
26
19
  expect(history.index).toBe(2);
27
20
  expect(history.states).toStrictEqual([1, 2, 4]);
@@ -37,14 +30,14 @@ test('Tests for history', () => {
37
30
 
38
31
  test('Tests for events', () => {
39
32
  const navigatorFn = jest.fn();
40
- const navigatorStopFn = jest.fn((event: LHistoryEvent) => {
33
+ const navigatorStopFn = jest.fn((event: EHistoryEvent) => {
41
34
  event.stopImmediatePropagation();
42
35
  });
43
36
  const clearFn = jest.fn();
44
37
  const pushFn = jest.fn();
45
38
  const replaceFn = jest.fn();
46
39
 
47
- const history = new LHistory();
40
+ const history = new LHistory(3);
48
41
 
49
42
  history.on({
50
43
  clear: clearFn,
@@ -78,4 +71,9 @@ test('Tests for events', () => {
78
71
 
79
72
  // Previous handler stopped propagation
80
73
  expect(navigatorFn).toBeCalledTimes(5);
74
+
75
+ history.pushState(3);
76
+ history.pushState(4);
77
+ history.pushState(5);
78
+ expect(history.length).toBe(3);
81
79
  });
@@ -12,10 +12,16 @@ export interface EHistoryEventData {
12
12
  */
13
13
  index: number;
14
14
  }
15
+ /**
16
+ * ETSOO Extended history event
17
+ */
18
+ export declare class EHistoryEvent extends EventBase<EHistoryEventType, EHistoryEventData> {
19
+ }
15
20
  /**
16
21
  * ETSOO Extended abstract history class
17
22
  */
18
- export declare abstract class EHistory<T, D extends EHistoryEventData = EHistoryEventData> extends EventClass<EHistoryEventType, D> {
23
+ export declare abstract class EHistory<T> extends EventClass<EHistoryEventType, EHistoryEventData> {
24
+ readonly maxDepth: number;
19
25
  private _index;
20
26
  /**
21
27
  * States
@@ -33,6 +39,11 @@ export declare abstract class EHistory<T, D extends EHistoryEventData = EHistory
33
39
  * Get current state
34
40
  */
35
41
  get state(): T | undefined;
42
+ /**
43
+ * Constructor
44
+ * @param maxDepth Max depth of the history
45
+ */
46
+ constructor(maxDepth?: number);
36
47
  /**
37
48
  * Back to the previous state
38
49
  */
@@ -46,11 +57,15 @@ export declare abstract class EHistory<T, D extends EHistoryEventData = EHistory
46
57
  * @param type Type
47
58
  * @param index Current index
48
59
  */
49
- protected abstract createEvent(type: EHistoryEventType, index: number): EventBase<EHistoryEventType, D>;
60
+ protected createEvent(type: EHistoryEventType, index: number): EHistoryEvent;
50
61
  /**
51
62
  * Forward to the next state
52
63
  */
53
64
  forward(): void;
65
+ /**
66
+ * Get [undo, redo] status
67
+ */
68
+ getStatus(): [boolean, boolean];
54
69
  /**
55
70
  * Go to the specific state
56
71
  * @param delta A negative value moves backwards, a positive value moves forwards
@@ -1,13 +1,24 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.EHistory = void 0;
3
+ exports.EHistory = exports.EHistoryEvent = void 0;
4
4
  const EventClass_1 = require("./EventClass");
5
+ /**
6
+ * ETSOO Extended history event
7
+ */
8
+ class EHistoryEvent extends EventClass_1.EventBase {
9
+ }
10
+ exports.EHistoryEvent = EHistoryEvent;
5
11
  /**
6
12
  * ETSOO Extended abstract history class
7
13
  */
8
14
  class EHistory extends EventClass_1.EventClass {
9
- constructor() {
10
- super(...arguments);
15
+ /**
16
+ * Constructor
17
+ * @param maxDepth Max depth of the history
18
+ */
19
+ constructor(maxDepth = 20) {
20
+ super();
21
+ this.maxDepth = maxDepth;
11
22
  // Index
12
23
  this._index = -1;
13
24
  /**
@@ -50,12 +61,32 @@ class EHistory extends EventClass_1.EventClass {
50
61
  this._index = -1;
51
62
  this.trigger(this.createEvent('clear', this._index));
52
63
  }
64
+ /**
65
+ * Create event
66
+ * @param type Type
67
+ * @param index Current index
68
+ */
69
+ createEvent(type, index) {
70
+ return new EHistoryEvent(this, type, { index });
71
+ }
53
72
  /**
54
73
  * Forward to the next state
55
74
  */
56
75
  forward() {
57
76
  this.go(1);
58
77
  }
78
+ /**
79
+ * Get [undo, redo] status
80
+ */
81
+ getStatus() {
82
+ if (this.length < 2)
83
+ return [false, false];
84
+ if (this._index <= 0)
85
+ return [false, true];
86
+ if (this._index + 1 >= this.length)
87
+ return [true, false];
88
+ return [true, true];
89
+ }
59
90
  /**
60
91
  * Go to the specific state
61
92
  * @param delta A negative value moves backwards, a positive value moves forwards
@@ -85,6 +116,9 @@ class EHistory extends EventClass_1.EventClass {
85
116
  }
86
117
  this.states.push(state);
87
118
  this._index++;
119
+ if (this.length > this.maxDepth) {
120
+ this.states.shift();
121
+ }
88
122
  this.trigger(this.createEvent('push', this._index));
89
123
  }
90
124
  /**
@@ -12,10 +12,16 @@ export interface EHistoryEventData {
12
12
  */
13
13
  index: number;
14
14
  }
15
+ /**
16
+ * ETSOO Extended history event
17
+ */
18
+ export declare class EHistoryEvent extends EventBase<EHistoryEventType, EHistoryEventData> {
19
+ }
15
20
  /**
16
21
  * ETSOO Extended abstract history class
17
22
  */
18
- export declare abstract class EHistory<T, D extends EHistoryEventData = EHistoryEventData> extends EventClass<EHistoryEventType, D> {
23
+ export declare abstract class EHistory<T> extends EventClass<EHistoryEventType, EHistoryEventData> {
24
+ readonly maxDepth: number;
19
25
  private _index;
20
26
  /**
21
27
  * States
@@ -33,6 +39,11 @@ export declare abstract class EHistory<T, D extends EHistoryEventData = EHistory
33
39
  * Get current state
34
40
  */
35
41
  get state(): T | undefined;
42
+ /**
43
+ * Constructor
44
+ * @param maxDepth Max depth of the history
45
+ */
46
+ constructor(maxDepth?: number);
36
47
  /**
37
48
  * Back to the previous state
38
49
  */
@@ -46,11 +57,15 @@ export declare abstract class EHistory<T, D extends EHistoryEventData = EHistory
46
57
  * @param type Type
47
58
  * @param index Current index
48
59
  */
49
- protected abstract createEvent(type: EHistoryEventType, index: number): EventBase<EHistoryEventType, D>;
60
+ protected createEvent(type: EHistoryEventType, index: number): EHistoryEvent;
50
61
  /**
51
62
  * Forward to the next state
52
63
  */
53
64
  forward(): void;
65
+ /**
66
+ * Get [undo, redo] status
67
+ */
68
+ getStatus(): [boolean, boolean];
54
69
  /**
55
70
  * Go to the specific state
56
71
  * @param delta A negative value moves backwards, a positive value moves forwards
@@ -1,10 +1,20 @@
1
- import { EventClass } from './EventClass';
1
+ import { EventBase, EventClass } from './EventClass';
2
+ /**
3
+ * ETSOO Extended history event
4
+ */
5
+ export class EHistoryEvent extends EventBase {
6
+ }
2
7
  /**
3
8
  * ETSOO Extended abstract history class
4
9
  */
5
10
  export class EHistory extends EventClass {
6
- constructor() {
7
- super(...arguments);
11
+ /**
12
+ * Constructor
13
+ * @param maxDepth Max depth of the history
14
+ */
15
+ constructor(maxDepth = 20) {
16
+ super();
17
+ this.maxDepth = maxDepth;
8
18
  // Index
9
19
  this._index = -1;
10
20
  /**
@@ -47,12 +57,32 @@ export class EHistory extends EventClass {
47
57
  this._index = -1;
48
58
  this.trigger(this.createEvent('clear', this._index));
49
59
  }
60
+ /**
61
+ * Create event
62
+ * @param type Type
63
+ * @param index Current index
64
+ */
65
+ createEvent(type, index) {
66
+ return new EHistoryEvent(this, type, { index });
67
+ }
50
68
  /**
51
69
  * Forward to the next state
52
70
  */
53
71
  forward() {
54
72
  this.go(1);
55
73
  }
74
+ /**
75
+ * Get [undo, redo] status
76
+ */
77
+ getStatus() {
78
+ if (this.length < 2)
79
+ return [false, false];
80
+ if (this._index <= 0)
81
+ return [false, true];
82
+ if (this._index + 1 >= this.length)
83
+ return [true, false];
84
+ return [true, true];
85
+ }
56
86
  /**
57
87
  * Go to the specific state
58
88
  * @param delta A negative value moves backwards, a positive value moves forwards
@@ -82,6 +112,9 @@ export class EHistory extends EventClass {
82
112
  }
83
113
  this.states.push(state);
84
114
  this._index++;
115
+ if (this.length > this.maxDepth) {
116
+ this.states.shift();
117
+ }
85
118
  this.trigger(this.createEvent('push', this._index));
86
119
  }
87
120
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@etsoo/shared",
3
- "version": "1.1.30",
3
+ "version": "1.1.33",
4
4
  "description": "TypeScript shared utilities and functions",
5
5
  "main": "lib/cjs/index.js",
6
6
  "module": "lib/mjs/index.js",
@@ -15,13 +15,21 @@ export interface EHistoryEventData {
15
15
  index: number;
16
16
  }
17
17
 
18
+ /**
19
+ * ETSOO Extended history event
20
+ */
21
+ export class EHistoryEvent extends EventBase<
22
+ EHistoryEventType,
23
+ EHistoryEventData
24
+ > {}
25
+
18
26
  /**
19
27
  * ETSOO Extended abstract history class
20
28
  */
21
- export abstract class EHistory<
22
- T,
23
- D extends EHistoryEventData = EHistoryEventData
24
- > extends EventClass<EHistoryEventType, D> {
29
+ export abstract class EHistory<T> extends EventClass<
30
+ EHistoryEventType,
31
+ EHistoryEventData
32
+ > {
25
33
  // Index
26
34
  private _index: number = -1;
27
35
 
@@ -52,6 +60,14 @@ export abstract class EHistory<
52
60
  return this.states[this._index];
53
61
  }
54
62
 
63
+ /**
64
+ * Constructor
65
+ * @param maxDepth Max depth of the history
66
+ */
67
+ constructor(public readonly maxDepth: number = 20) {
68
+ super();
69
+ }
70
+
55
71
  /**
56
72
  * Back to the previous state
57
73
  */
@@ -74,10 +90,9 @@ export abstract class EHistory<
74
90
  * @param type Type
75
91
  * @param index Current index
76
92
  */
77
- protected abstract createEvent(
78
- type: EHistoryEventType,
79
- index: number
80
- ): EventBase<EHistoryEventType, D>;
93
+ protected createEvent(type: EHistoryEventType, index: number) {
94
+ return new EHistoryEvent(this, type, { index });
95
+ }
81
96
 
82
97
  /**
83
98
  * Forward to the next state
@@ -86,6 +101,16 @@ export abstract class EHistory<
86
101
  this.go(1);
87
102
  }
88
103
 
104
+ /**
105
+ * Get [undo, redo] status
106
+ */
107
+ getStatus(): [boolean, boolean] {
108
+ if (this.length < 2) return [false, false];
109
+ if (this._index <= 0) return [false, true];
110
+ if (this._index + 1 >= this.length) return [true, false];
111
+ return [true, true];
112
+ }
113
+
89
114
  /**
90
115
  * Go to the specific state
91
116
  * @param delta A negative value moves backwards, a positive value moves forwards
@@ -119,6 +144,11 @@ export abstract class EHistory<
119
144
 
120
145
  this.states.push(state);
121
146
  this._index++;
147
+
148
+ if (this.length > this.maxDepth) {
149
+ this.states.shift();
150
+ }
151
+
122
152
  this.trigger(this.createEvent('push', this._index));
123
153
  }
124
154