use-good-hooks 1.0.30 → 1.0.31
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 +1 -1
- package/dist/use-history-state.d.ts +9 -0
- package/dist/use-history-state.js +124 -90
- package/package.json +5 -4
package/README.md
CHANGED
|
@@ -5,8 +5,12 @@ export { DebounceSettings }
|
|
|
5
5
|
declare type HistoryAction<T> = {
|
|
6
6
|
type: 'CLEAR';
|
|
7
7
|
initialPresent: T;
|
|
8
|
+
} | {
|
|
9
|
+
type: 'PAUSE';
|
|
8
10
|
} | {
|
|
9
11
|
type: 'REDO';
|
|
12
|
+
} | {
|
|
13
|
+
type: 'RESUME';
|
|
10
14
|
} | {
|
|
11
15
|
type: 'SET';
|
|
12
16
|
newPresent: T;
|
|
@@ -22,12 +26,14 @@ declare type HistoryOnChange<T> = ({ action, state }: {
|
|
|
22
26
|
declare type HistoryState<T> = {
|
|
23
27
|
future: T[];
|
|
24
28
|
past: T[];
|
|
29
|
+
paused: boolean;
|
|
25
30
|
present: T | null;
|
|
26
31
|
};
|
|
27
32
|
|
|
28
33
|
declare type UseHistoryOptionsState<T> = {
|
|
29
34
|
debounceSettings?: DebounceSettings;
|
|
30
35
|
debounceTime?: number;
|
|
36
|
+
immutable?: boolean;
|
|
31
37
|
maxCapacity?: number;
|
|
32
38
|
onChange?: HistoryOnChange<T>;
|
|
33
39
|
};
|
|
@@ -40,7 +46,10 @@ declare const useHistoryState: <T>(initialPresent: T, options?: UseHistoryOption
|
|
|
40
46
|
past: T[];
|
|
41
47
|
future: T[];
|
|
42
48
|
};
|
|
49
|
+
pause: () => void;
|
|
50
|
+
paused: boolean;
|
|
43
51
|
redo: () => void;
|
|
52
|
+
resume: () => void;
|
|
44
53
|
set: (newPresent: T) => void;
|
|
45
54
|
setDirect: (newPresent: T) => void;
|
|
46
55
|
state: T;
|
|
@@ -1,114 +1,148 @@
|
|
|
1
|
-
import { useRef as
|
|
2
|
-
import
|
|
3
|
-
import
|
|
4
|
-
import
|
|
5
|
-
import
|
|
6
|
-
const
|
|
1
|
+
import { useRef as F, useReducer as H, useCallback as S } from "react";
|
|
2
|
+
import J from "lodash/cloneDeep";
|
|
3
|
+
import L from "lodash/isNil";
|
|
4
|
+
import d from "lodash/size";
|
|
5
|
+
import M from "./use-debounce-fn.js";
|
|
6
|
+
const k = {
|
|
7
|
+
future: [],
|
|
7
8
|
past: [],
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
},
|
|
11
|
-
action:
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
9
|
+
paused: !1,
|
|
10
|
+
present: null
|
|
11
|
+
}, i = (e, r) => r ? e : J(e), q = (e, r, o) => o ? e === r : JSON.stringify(e) === JSON.stringify(r), v = ({
|
|
12
|
+
action: e,
|
|
13
|
+
immutable: r,
|
|
14
|
+
maxCapacity: o,
|
|
15
|
+
onChange: t,
|
|
16
|
+
state: f
|
|
15
17
|
}) => {
|
|
16
|
-
const {
|
|
17
|
-
if (
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
past: p.slice(0, f(p) - 1),
|
|
22
|
-
present: c(e),
|
|
23
|
-
future: [c(o), ...l]
|
|
18
|
+
const { future: E, past: c, paused: l, present: p } = f;
|
|
19
|
+
if (e.type === "PAUSE")
|
|
20
|
+
return {
|
|
21
|
+
...f,
|
|
22
|
+
paused: !0
|
|
24
23
|
};
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
} else if (r.type === "REDO") {
|
|
30
|
-
if (f(l) === 0)
|
|
31
|
-
return n;
|
|
32
|
-
const e = l[0], t = l.slice(1), u = {
|
|
33
|
-
past: [...p, c(o)],
|
|
34
|
-
present: c(e),
|
|
35
|
-
future: t
|
|
24
|
+
if (e.type === "RESUME")
|
|
25
|
+
return {
|
|
26
|
+
...f,
|
|
27
|
+
paused: !1
|
|
36
28
|
};
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
let t = [...p];
|
|
46
|
-
o !== null && (t = [...t, c(o)]), !N(y) && y > 0 && f(t) > y && (t = t.slice(f(t) - y));
|
|
47
|
-
const u = {
|
|
48
|
-
past: t,
|
|
49
|
-
present: c(e),
|
|
50
|
-
future: []
|
|
29
|
+
if (e.type === "UNDO") {
|
|
30
|
+
if (d(c) === 0)
|
|
31
|
+
return f;
|
|
32
|
+
const s = c[d(c) - 1], u = c.slice(0, d(c) - 1), n = {
|
|
33
|
+
future: [i(p, r), ...E],
|
|
34
|
+
past: u,
|
|
35
|
+
paused: l,
|
|
36
|
+
present: i(s, r)
|
|
51
37
|
};
|
|
52
|
-
return
|
|
53
|
-
action:
|
|
54
|
-
state:
|
|
55
|
-
}),
|
|
56
|
-
} else if (
|
|
57
|
-
|
|
38
|
+
return t == null || t({
|
|
39
|
+
action: e.type,
|
|
40
|
+
state: n.present
|
|
41
|
+
}), n;
|
|
42
|
+
} else if (e.type === "REDO") {
|
|
43
|
+
if (d(E) === 0)
|
|
44
|
+
return f;
|
|
45
|
+
const s = E[0], n = {
|
|
46
|
+
future: E.slice(1),
|
|
47
|
+
past: [...c, i(p, r)],
|
|
48
|
+
paused: l,
|
|
49
|
+
present: i(s, r)
|
|
50
|
+
};
|
|
51
|
+
return t == null || t({
|
|
52
|
+
action: e.type,
|
|
53
|
+
state: n.present
|
|
54
|
+
}), n;
|
|
55
|
+
} else if (e.type === "SET") {
|
|
56
|
+
const { newPresent: s } = e;
|
|
57
|
+
if (q(s, p, r))
|
|
58
|
+
return f;
|
|
59
|
+
if (l) {
|
|
60
|
+
const w = {
|
|
61
|
+
...f,
|
|
62
|
+
present: i(s, r)
|
|
63
|
+
};
|
|
64
|
+
return t == null || t({
|
|
65
|
+
action: e.type,
|
|
66
|
+
state: w.present
|
|
67
|
+
}), w;
|
|
68
|
+
}
|
|
69
|
+
let u = [...c];
|
|
70
|
+
p !== null && (u = [...u, i(p, r)]), !L(o) && o > 0 && d(u) > o && (u = u.slice(d(u) - o));
|
|
71
|
+
const n = {
|
|
72
|
+
future: [],
|
|
73
|
+
past: u,
|
|
74
|
+
paused: l,
|
|
75
|
+
present: i(s, r)
|
|
76
|
+
};
|
|
77
|
+
return t == null || t({
|
|
78
|
+
action: e.type,
|
|
79
|
+
state: n.present
|
|
80
|
+
}), n;
|
|
81
|
+
} else if (e.type === "CLEAR") {
|
|
82
|
+
const s = {
|
|
83
|
+
future: [],
|
|
58
84
|
past: [],
|
|
59
|
-
|
|
60
|
-
|
|
85
|
+
paused: l,
|
|
86
|
+
present: i(e.initialPresent, r)
|
|
61
87
|
};
|
|
62
|
-
return
|
|
63
|
-
action:
|
|
64
|
-
state:
|
|
65
|
-
}),
|
|
88
|
+
return t == null || t({
|
|
89
|
+
action: e.type,
|
|
90
|
+
state: s.present
|
|
91
|
+
}), s;
|
|
66
92
|
} else
|
|
67
93
|
throw new Error("Unsupported action type");
|
|
68
|
-
},
|
|
69
|
-
const { maxCapacity:
|
|
70
|
-
(
|
|
71
|
-
action:
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
94
|
+
}, B = (e, r) => {
|
|
95
|
+
const { maxCapacity: o, debounceTime: t, debounceSettings: f, onChange: E, immutable: c } = r || {}, l = F(e), [p, s] = H(
|
|
96
|
+
(y, T) => v({
|
|
97
|
+
action: T,
|
|
98
|
+
immutable: c,
|
|
99
|
+
maxCapacity: o,
|
|
100
|
+
onChange: E,
|
|
101
|
+
state: y
|
|
75
102
|
}),
|
|
76
103
|
{
|
|
77
|
-
...
|
|
78
|
-
present:
|
|
104
|
+
...k,
|
|
105
|
+
present: i(l.current, c)
|
|
79
106
|
}
|
|
80
|
-
), u =
|
|
107
|
+
), u = d(p.future) !== 0, n = d(p.past) !== 0, w = S(() => s({
|
|
81
108
|
initialPresent: l.current,
|
|
82
109
|
type: "CLEAR"
|
|
83
|
-
}), []),
|
|
84
|
-
|
|
85
|
-
}, [
|
|
86
|
-
(
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
110
|
+
}), []), U = S(() => {
|
|
111
|
+
s({ type: "PAUSE" });
|
|
112
|
+
}, []), P = S(() => {
|
|
113
|
+
s({ type: "RESUME" });
|
|
114
|
+
}, []), O = S(() => {
|
|
115
|
+
u && s({ type: "REDO" });
|
|
116
|
+
}, [u]), D = M(
|
|
117
|
+
(y) => s({ type: "SET", newPresent: y }),
|
|
118
|
+
t,
|
|
119
|
+
f
|
|
120
|
+
), R = S((y) => s({ type: "SET", newPresent: y }), []), N = S(() => {
|
|
121
|
+
n && s({ type: "UNDO" });
|
|
122
|
+
}, [n]), A = S(
|
|
123
|
+
(y) => {
|
|
124
|
+
t ? D(y) : R(y);
|
|
94
125
|
},
|
|
95
|
-
[
|
|
126
|
+
[t, D, R]
|
|
96
127
|
);
|
|
97
128
|
return {
|
|
98
129
|
canRedo: u,
|
|
99
|
-
canUndo:
|
|
100
|
-
clear:
|
|
130
|
+
canUndo: n,
|
|
131
|
+
clear: w,
|
|
101
132
|
history: {
|
|
102
|
-
past:
|
|
103
|
-
future:
|
|
133
|
+
past: p.past,
|
|
134
|
+
future: p.future
|
|
104
135
|
},
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
136
|
+
pause: U,
|
|
137
|
+
paused: p.paused,
|
|
138
|
+
redo: O,
|
|
139
|
+
resume: P,
|
|
140
|
+
set: A,
|
|
141
|
+
setDirect: R,
|
|
142
|
+
state: p.present,
|
|
143
|
+
undo: N
|
|
110
144
|
};
|
|
111
145
|
};
|
|
112
146
|
export {
|
|
113
|
-
|
|
147
|
+
B as default
|
|
114
148
|
};
|
package/package.json
CHANGED
|
@@ -45,9 +45,10 @@
|
|
|
45
45
|
"scripts": {
|
|
46
46
|
"build": "yarn lint && vite build --mode export",
|
|
47
47
|
"lint": "prettier --write . && eslint .",
|
|
48
|
-
"npm:publish": "yarn test
|
|
49
|
-
"test": "vitest",
|
|
50
|
-
"test:coverage": "vitest --coverage"
|
|
48
|
+
"npm:publish": "yarn test && yarn build && yarn version --patch --no-git-tag-version && yarn publish --non-interactive",
|
|
49
|
+
"test": "vitest --run",
|
|
50
|
+
"test:coverage": "vitest --coverage --run",
|
|
51
|
+
"test:watch": "vitest"
|
|
51
52
|
},
|
|
52
|
-
"version": "1.0.
|
|
53
|
+
"version": "1.0.31"
|
|
53
54
|
}
|