react-usectx 1.2.3 → 1.2.5
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 +10 -1
- package/context.js +2 -1
- package/create-context.js +15 -6
- package/index.js +1 -1
- package/package.json +1 -1
- package/src/context.ts +4 -3
- package/src/create-context.ts +18 -6
- package/src/index.ts +1 -1
package/README.md
CHANGED
|
@@ -19,8 +19,17 @@ import {useGlobalState} from "react-usectx";
|
|
|
19
19
|
```js
|
|
20
20
|
// get both current state and set function
|
|
21
21
|
const [myState, setMyState] = useGlobalState("stateName");
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
// use with undo and redo methods
|
|
25
|
+
const [myState, setMyState, undo, redo] = useGlobalState("stateName");
|
|
26
|
+
|
|
22
27
|
```
|
|
23
28
|
|
|
29
|
+
Undo and redo functionality is essential for any interface that allows users to make edits. It enhances usability by giving users confidence that their changes aren’t permanent and can be easily adjusted.
|
|
30
|
+
|
|
31
|
+
By implementing simple Undo and Redo actions—typically as two buttons—you allow users to navigate backward to a previous state or forward to a more recent state of the component.
|
|
32
|
+
|
|
24
33
|
## Other methods:
|
|
25
34
|
|
|
26
35
|
### getGlobalState
|
|
@@ -80,7 +89,7 @@ useGlobalState(myContextID, { name: "John" });
|
|
|
80
89
|
The value you want the state to be initially. It can be a value of any type.
|
|
81
90
|
|
|
82
91
|
|
|
83
|
-
|
|
92
|
+
## API Methods
|
|
84
93
|
|
|
85
94
|
```js
|
|
86
95
|
const api = stateApi(myContextID);
|
package/context.js
CHANGED
|
@@ -22,7 +22,8 @@ export function getCtx(name) {
|
|
|
22
22
|
}
|
|
23
23
|
export function useCtx(name, initialState) {
|
|
24
24
|
const initializeFirst = updateCtx(name, initialState);
|
|
25
|
-
|
|
25
|
+
const api = getInstance(name);
|
|
26
|
+
return [getCtx(name), initializeFirst, api.undo.bind(api), api.redo.bind(api), api];
|
|
26
27
|
}
|
|
27
28
|
const reducerCache = {};
|
|
28
29
|
export function useReducer(name, modiFier, useCache = false) {
|
package/create-context.js
CHANGED
|
@@ -33,26 +33,35 @@ export class CreateContext {
|
|
|
33
33
|
undo() {
|
|
34
34
|
const prev = this.#history[this.#activeVersion - 1];
|
|
35
35
|
if (prev) {
|
|
36
|
-
this.setState(prev, this.#history.indexOf(prev));
|
|
36
|
+
this.setState(prev, this.#history.indexOf(prev), true);
|
|
37
37
|
this.#needsReset = true;
|
|
38
38
|
this.#trigger();
|
|
39
39
|
}
|
|
40
40
|
}
|
|
41
|
+
hasUndo() {
|
|
42
|
+
return this.#activeVersion > 0;
|
|
43
|
+
}
|
|
41
44
|
redo() {
|
|
42
|
-
const
|
|
45
|
+
const nextIndex = this.#activeVersion + 1;
|
|
46
|
+
const next = this.#history[nextIndex];
|
|
43
47
|
if (next) {
|
|
44
|
-
this.setState(next,
|
|
48
|
+
this.setState(next, nextIndex, true);
|
|
45
49
|
this.#needsReset = true;
|
|
46
50
|
this.#trigger();
|
|
47
51
|
}
|
|
48
52
|
}
|
|
49
|
-
|
|
53
|
+
hasRedo() {
|
|
54
|
+
return !!this.#history[this.#activeVersion + 1];
|
|
55
|
+
}
|
|
56
|
+
setState(state, _av, _skipPush) {
|
|
50
57
|
if (typeof _av === 'undefined') {
|
|
51
|
-
if ((this.#history.length - 1) > this.#activeVersion && this.#needsReset) {
|
|
58
|
+
if (this.#history.length > 0 && (this.#history.length - 1) > this.#activeVersion && this.#needsReset) {
|
|
52
59
|
this.#history.splice(this.#activeVersion + 1, this.#history.length);
|
|
53
60
|
}
|
|
54
61
|
}
|
|
55
|
-
|
|
62
|
+
if (!_skipPush) {
|
|
63
|
+
this.#history.push(state);
|
|
64
|
+
}
|
|
56
65
|
if (this.#history.length > this.settings.historySize) {
|
|
57
66
|
this.#history.shift();
|
|
58
67
|
}
|
package/index.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
export { useCtx as default, useCtx as useGlobalState } from "./context";
|
|
2
|
-
export { getCtx, useReducer, updateCtx, getCtx as getGlobalState, useReducer as useGlobalReducer, updateCtx as updateGlobalState } from "./context";
|
|
2
|
+
export { getCtx, useReducer, updateCtx, getCtx as getGlobalState, useReducer as useGlobalReducer, updateCtx as updateGlobalState, stateApi } from "./context";
|
package/package.json
CHANGED
package/src/context.ts
CHANGED
|
@@ -39,9 +39,10 @@ export function getCtx<Snapshot>(name: string): Snapshot {
|
|
|
39
39
|
export function useCtx(
|
|
40
40
|
name: string,
|
|
41
41
|
initialState?: state
|
|
42
|
-
): [ReactNode, Function] {
|
|
43
|
-
const initializeFirst = updateCtx(name, initialState)
|
|
44
|
-
|
|
42
|
+
): [ReactNode, Function, Function, Function, CreateContext] {
|
|
43
|
+
const initializeFirst = updateCtx(name, initialState);
|
|
44
|
+
const api = getInstance(name);
|
|
45
|
+
return [getCtx(name), initializeFirst, api.undo.bind(api), api.redo.bind(api), api];
|
|
45
46
|
}
|
|
46
47
|
|
|
47
48
|
const reducerCache: { [key: string]: WeakMap<any, any> } = {};
|
package/src/create-context.ts
CHANGED
|
@@ -49,28 +49,40 @@ export class CreateContext {
|
|
|
49
49
|
undo() {
|
|
50
50
|
const prev = this.#history[this.#activeVersion - 1];
|
|
51
51
|
if (prev) {
|
|
52
|
-
this.setState(prev, this.#history.indexOf(prev));
|
|
52
|
+
this.setState(prev, this.#history.indexOf(prev), true);
|
|
53
53
|
this.#needsReset = true;
|
|
54
54
|
this.#trigger()
|
|
55
55
|
}
|
|
56
56
|
|
|
57
57
|
}
|
|
58
|
+
|
|
59
|
+
hasUndo() {
|
|
60
|
+
return this.#activeVersion > 0;
|
|
61
|
+
}
|
|
62
|
+
|
|
58
63
|
redo() {
|
|
59
|
-
const
|
|
64
|
+
const nextIndex = this.#activeVersion + 1;
|
|
65
|
+
const next = this.#history[nextIndex];
|
|
60
66
|
if (next) {
|
|
61
|
-
this.setState(next,
|
|
67
|
+
this.setState(next, nextIndex, true);
|
|
62
68
|
this.#needsReset = true;
|
|
63
69
|
this.#trigger()
|
|
64
70
|
}
|
|
65
71
|
}
|
|
66
72
|
|
|
67
|
-
|
|
73
|
+
hasRedo() {
|
|
74
|
+
return !!this.#history[this.#activeVersion + 1]
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
setState(state: state, _av?: number, _skipPush?: boolean): void {
|
|
68
78
|
if (typeof _av === 'undefined') {
|
|
69
|
-
if((this.#history.length - 1) > this.#activeVersion && this.#needsReset) {
|
|
79
|
+
if(this.#history.length > 0 && (this.#history.length - 1) > this.#activeVersion && this.#needsReset) {
|
|
70
80
|
this.#history.splice(this.#activeVersion + 1, this.#history.length)
|
|
71
81
|
}
|
|
72
82
|
}
|
|
73
|
-
|
|
83
|
+
if(!_skipPush) {
|
|
84
|
+
this.#history.push(state);
|
|
85
|
+
}
|
|
74
86
|
if (this.#history.length > this.settings.historySize!) {
|
|
75
87
|
this.#history.shift();
|
|
76
88
|
}
|
package/src/index.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
export { useCtx as default, useCtx as useGlobalState } from "./context";
|
|
2
|
-
export { getCtx, useReducer, updateCtx, getCtx as getGlobalState, useReducer as useGlobalReducer, updateCtx as updateGlobalState } from "./context";
|
|
2
|
+
export { getCtx, useReducer, updateCtx, getCtx as getGlobalState, useReducer as useGlobalReducer, updateCtx as updateGlobalState, stateApi } from "./context";
|