@etsoo/shared 1.1.29 → 1.1.32

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,17 +1,7 @@
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();
@@ -37,7 +27,7 @@ test('Tests for history', () => {
37
27
 
38
28
  test('Tests for events', () => {
39
29
  const navigatorFn = jest.fn();
40
- const navigatorStopFn = jest.fn((event: LHistoryEvent) => {
30
+ const navigatorStopFn = jest.fn((event: EHistoryEvent) => {
41
31
  event.stopImmediatePropagation();
42
32
  });
43
33
  const clearFn = jest.fn();
@@ -1,5 +1,7 @@
1
1
  export * from './types/DelayedExecutorType';
2
2
  export * from './types/EColor';
3
+ export * from './types/EHistory';
4
+ export * from './types/EventClass';
3
5
  export * from './types/FormData';
4
6
  export * from './storage/IStorage';
5
7
  export * from './storage/WindowStorage';
package/lib/cjs/index.js CHANGED
@@ -16,6 +16,8 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
17
  __exportStar(require("./types/DelayedExecutorType"), exports);
18
18
  __exportStar(require("./types/EColor"), exports);
19
+ __exportStar(require("./types/EHistory"), exports);
20
+ __exportStar(require("./types/EventClass"), exports);
19
21
  __exportStar(require("./types/FormData"), exports);
20
22
  __exportStar(require("./storage/IStorage"), exports);
21
23
  __exportStar(require("./storage/WindowStorage"), exports);
@@ -12,10 +12,15 @@ 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> extends EventClass<EHistoryEventType, D> {
23
+ export declare abstract class EHistory<T> extends EventClass<EHistoryEventType, EHistoryEventData> {
19
24
  private _index;
20
25
  /**
21
26
  * States
@@ -46,11 +51,15 @@ export declare abstract class EHistory<T, D extends EHistoryEventData> extends E
46
51
  * @param type Type
47
52
  * @param index Current index
48
53
  */
49
- protected abstract createEvent(type: EHistoryEventType, index: number): EventBase<EHistoryEventType, D>;
54
+ protected createEvent(type: EHistoryEventType, index: number): EHistoryEvent;
50
55
  /**
51
56
  * Forward to the next state
52
57
  */
53
58
  forward(): void;
59
+ /**
60
+ * Get [undo, redo] status
61
+ */
62
+ getStatus(): [boolean, boolean];
54
63
  /**
55
64
  * Go to the specific state
56
65
  * @param delta A negative value moves backwards, a positive value moves forwards
@@ -1,7 +1,13 @@
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
  */
@@ -50,12 +56,32 @@ class EHistory extends EventClass_1.EventClass {
50
56
  this._index = -1;
51
57
  this.trigger(this.createEvent('clear', this._index));
52
58
  }
59
+ /**
60
+ * Create event
61
+ * @param type Type
62
+ * @param index Current index
63
+ */
64
+ createEvent(type, index) {
65
+ return new EHistoryEvent(this, type, { index });
66
+ }
53
67
  /**
54
68
  * Forward to the next state
55
69
  */
56
70
  forward() {
57
71
  this.go(1);
58
72
  }
73
+ /**
74
+ * Get [undo, redo] status
75
+ */
76
+ getStatus() {
77
+ if (this.length < 2)
78
+ return [false, false];
79
+ if (this._index <= 0)
80
+ return [false, true];
81
+ if (this._index + 1 >= this.length)
82
+ return [true, false];
83
+ return [true, true];
84
+ }
59
85
  /**
60
86
  * Go to the specific state
61
87
  * @param delta A negative value moves backwards, a positive value moves forwards
@@ -1,5 +1,7 @@
1
1
  export * from './types/DelayedExecutorType';
2
2
  export * from './types/EColor';
3
+ export * from './types/EHistory';
4
+ export * from './types/EventClass';
3
5
  export * from './types/FormData';
4
6
  export * from './storage/IStorage';
5
7
  export * from './storage/WindowStorage';
package/lib/mjs/index.js CHANGED
@@ -1,5 +1,7 @@
1
1
  export * from './types/DelayedExecutorType';
2
2
  export * from './types/EColor';
3
+ export * from './types/EHistory';
4
+ export * from './types/EventClass';
3
5
  export * from './types/FormData';
4
6
  export * from './storage/IStorage';
5
7
  export * from './storage/WindowStorage';
@@ -12,10 +12,15 @@ 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> extends EventClass<EHistoryEventType, D> {
23
+ export declare abstract class EHistory<T> extends EventClass<EHistoryEventType, EHistoryEventData> {
19
24
  private _index;
20
25
  /**
21
26
  * States
@@ -46,11 +51,15 @@ export declare abstract class EHistory<T, D extends EHistoryEventData> extends E
46
51
  * @param type Type
47
52
  * @param index Current index
48
53
  */
49
- protected abstract createEvent(type: EHistoryEventType, index: number): EventBase<EHistoryEventType, D>;
54
+ protected createEvent(type: EHistoryEventType, index: number): EHistoryEvent;
50
55
  /**
51
56
  * Forward to the next state
52
57
  */
53
58
  forward(): void;
59
+ /**
60
+ * Get [undo, redo] status
61
+ */
62
+ getStatus(): [boolean, boolean];
54
63
  /**
55
64
  * Go to the specific state
56
65
  * @param delta A negative value moves backwards, a positive value moves forwards
@@ -1,4 +1,9 @@
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
  */
@@ -47,12 +52,32 @@ export class EHistory extends EventClass {
47
52
  this._index = -1;
48
53
  this.trigger(this.createEvent('clear', this._index));
49
54
  }
55
+ /**
56
+ * Create event
57
+ * @param type Type
58
+ * @param index Current index
59
+ */
60
+ createEvent(type, index) {
61
+ return new EHistoryEvent(this, type, { index });
62
+ }
50
63
  /**
51
64
  * Forward to the next state
52
65
  */
53
66
  forward() {
54
67
  this.go(1);
55
68
  }
69
+ /**
70
+ * Get [undo, redo] status
71
+ */
72
+ getStatus() {
73
+ if (this.length < 2)
74
+ return [false, false];
75
+ if (this._index <= 0)
76
+ return [false, true];
77
+ if (this._index + 1 >= this.length)
78
+ return [true, false];
79
+ return [true, true];
80
+ }
56
81
  /**
57
82
  * Go to the specific state
58
83
  * @param delta A negative value moves backwards, a positive value moves forwards
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@etsoo/shared",
3
- "version": "1.1.29",
3
+ "version": "1.1.32",
4
4
  "description": "TypeScript shared utilities and functions",
5
5
  "main": "lib/cjs/index.js",
6
6
  "module": "lib/mjs/index.js",
package/src/index.ts CHANGED
@@ -1,5 +1,7 @@
1
1
  export * from './types/DelayedExecutorType';
2
2
  export * from './types/EColor';
3
+ export * from './types/EHistory';
4
+ export * from './types/EventClass';
3
5
  export * from './types/FormData';
4
6
 
5
7
  export * from './storage/IStorage';
@@ -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
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
 
@@ -74,10 +82,9 @@ export abstract class EHistory<
74
82
  * @param type Type
75
83
  * @param index Current index
76
84
  */
77
- protected abstract createEvent(
78
- type: EHistoryEventType,
79
- index: number
80
- ): EventBase<EHistoryEventType, D>;
85
+ protected createEvent(type: EHistoryEventType, index: number) {
86
+ return new EHistoryEvent(this, type, { index });
87
+ }
81
88
 
82
89
  /**
83
90
  * Forward to the next state
@@ -86,6 +93,16 @@ export abstract class EHistory<
86
93
  this.go(1);
87
94
  }
88
95
 
96
+ /**
97
+ * Get [undo, redo] status
98
+ */
99
+ getStatus(): [boolean, boolean] {
100
+ if (this.length < 2) return [false, false];
101
+ if (this._index <= 0) return [false, true];
102
+ if (this._index + 1 >= this.length) return [true, false];
103
+ return [true, true];
104
+ }
105
+
89
106
  /**
90
107
  * Go to the specific state
91
108
  * @param delta A negative value moves backwards, a positive value moves forwards