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