use-good-hooks 1.0.31 → 1.0.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 +12 -2
- package/dist/use-history-state.d.ts +5 -6
- package/dist/use-history-state.js +77 -79
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -243,16 +243,26 @@ const TextEditor = () => {
|
|
|
243
243
|
|
|
244
244
|
- `initialState`: The initial state value
|
|
245
245
|
- `options`: (Optional) Configuration options:
|
|
246
|
-
- `
|
|
246
|
+
- `debounceSettings`: Debounce settings object (default: { leading: true, trailing: false })
|
|
247
|
+
- `debounceTime`: Time in milliseconds to debounce the state changes (default: 0)
|
|
248
|
+
- `immutable`: Boolean indicating if the state should be treated as immutable (default: false)
|
|
249
|
+
- `maxCapacity`: Maximum number of history entries to keep (default: 10)
|
|
250
|
+
- `onChange`: Function to call when the state changes
|
|
251
|
+
- `paused`: Boolean indicating if the history is paused (default: false)
|
|
247
252
|
|
|
248
253
|
#### Returns
|
|
249
254
|
|
|
250
255
|
- Object with:
|
|
251
256
|
- `canRedo`: Boolean indicating if redo is possible
|
|
252
257
|
- `canUndo`: Boolean indicating if undo is possible
|
|
253
|
-
- `
|
|
258
|
+
- `clear`: Function to clear the history
|
|
259
|
+
- `future`: Array of future states
|
|
260
|
+
- `past`: Array of past states
|
|
261
|
+
- `pause`: Function to pause the history
|
|
262
|
+
- `paused`: Boolean indicating if the history is paused
|
|
254
263
|
- `redo`: Function to move to the next state (redo)
|
|
255
264
|
- `set`: Function to update the state and record history
|
|
265
|
+
- `setDirect`: Function to update the state without recording history
|
|
256
266
|
- `state`: The current state value
|
|
257
267
|
- `undo`: Function to move to the previous state (undo)
|
|
258
268
|
|
|
@@ -4,7 +4,7 @@ export { DebounceSettings }
|
|
|
4
4
|
|
|
5
5
|
declare type HistoryAction<T> = {
|
|
6
6
|
type: 'CLEAR';
|
|
7
|
-
|
|
7
|
+
initialState: T;
|
|
8
8
|
} | {
|
|
9
9
|
type: 'PAUSE';
|
|
10
10
|
} | {
|
|
@@ -36,16 +36,15 @@ declare type UseHistoryOptionsState<T> = {
|
|
|
36
36
|
immutable?: boolean;
|
|
37
37
|
maxCapacity?: number;
|
|
38
38
|
onChange?: HistoryOnChange<T>;
|
|
39
|
+
paused?: boolean;
|
|
39
40
|
};
|
|
40
41
|
|
|
41
|
-
declare const useHistoryState: <T>(
|
|
42
|
+
declare const useHistoryState: <T>(initialState: T, options?: UseHistoryOptionsState<T>) => {
|
|
42
43
|
canRedo: boolean;
|
|
43
44
|
canUndo: boolean;
|
|
44
45
|
clear: () => void;
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
future: T[];
|
|
48
|
-
};
|
|
46
|
+
future: T[];
|
|
47
|
+
past: T[];
|
|
49
48
|
pause: () => void;
|
|
50
49
|
paused: boolean;
|
|
51
50
|
redo: () => void;
|
|
@@ -1,21 +1,20 @@
|
|
|
1
|
-
import { useRef as
|
|
2
|
-
import
|
|
3
|
-
import
|
|
1
|
+
import { useRef as T, useReducer as F, useCallback as o } from "react";
|
|
2
|
+
import H from "lodash/cloneDeep";
|
|
3
|
+
import J from "lodash/isNil";
|
|
4
4
|
import d from "lodash/size";
|
|
5
|
-
import
|
|
6
|
-
const
|
|
5
|
+
import L from "./use-debounce-fn.js";
|
|
6
|
+
const M = {
|
|
7
7
|
future: [],
|
|
8
8
|
past: [],
|
|
9
|
-
paused: !1,
|
|
10
9
|
present: null
|
|
11
|
-
}, i = (e,
|
|
10
|
+
}, i = (e, t) => t ? e : H(e), k = (e, t, l) => l ? e === t : JSON.stringify(e) === JSON.stringify(t), q = ({
|
|
12
11
|
action: e,
|
|
13
|
-
immutable:
|
|
14
|
-
maxCapacity:
|
|
15
|
-
onChange:
|
|
12
|
+
immutable: t,
|
|
13
|
+
maxCapacity: l,
|
|
14
|
+
onChange: s,
|
|
16
15
|
state: f
|
|
17
16
|
}) => {
|
|
18
|
-
const { future: E, past:
|
|
17
|
+
const { future: E, past: n, paused: y, present: p } = f;
|
|
19
18
|
if (e.type === "PAUSE")
|
|
20
19
|
return {
|
|
21
20
|
...f,
|
|
@@ -27,120 +26,119 @@ const k = {
|
|
|
27
26
|
paused: !1
|
|
28
27
|
};
|
|
29
28
|
if (e.type === "UNDO") {
|
|
30
|
-
if (d(
|
|
29
|
+
if (d(n) === 0)
|
|
31
30
|
return f;
|
|
32
|
-
const
|
|
33
|
-
future: [i(p,
|
|
31
|
+
const r = n[d(n) - 1], u = n.slice(0, d(n) - 1), c = {
|
|
32
|
+
future: [i(p, t), ...E],
|
|
34
33
|
past: u,
|
|
35
|
-
paused:
|
|
36
|
-
present: i(
|
|
34
|
+
paused: y,
|
|
35
|
+
present: i(r, t)
|
|
37
36
|
};
|
|
38
|
-
return
|
|
37
|
+
return s == null || s({
|
|
39
38
|
action: e.type,
|
|
40
|
-
state:
|
|
41
|
-
}),
|
|
39
|
+
state: c.present
|
|
40
|
+
}), c;
|
|
42
41
|
} else if (e.type === "REDO") {
|
|
43
42
|
if (d(E) === 0)
|
|
44
43
|
return f;
|
|
45
|
-
const
|
|
44
|
+
const r = E[0], c = {
|
|
46
45
|
future: E.slice(1),
|
|
47
|
-
past: [...
|
|
48
|
-
paused:
|
|
49
|
-
present: i(
|
|
46
|
+
past: [...n, i(p, t)],
|
|
47
|
+
paused: y,
|
|
48
|
+
present: i(r, t)
|
|
50
49
|
};
|
|
51
|
-
return
|
|
50
|
+
return s == null || s({
|
|
52
51
|
action: e.type,
|
|
53
|
-
state:
|
|
54
|
-
}),
|
|
52
|
+
state: c.present
|
|
53
|
+
}), c;
|
|
55
54
|
} else if (e.type === "SET") {
|
|
56
|
-
const { newPresent:
|
|
57
|
-
if (
|
|
55
|
+
const { newPresent: r } = e;
|
|
56
|
+
if (k(r, p, t))
|
|
58
57
|
return f;
|
|
59
|
-
if (
|
|
58
|
+
if (y) {
|
|
60
59
|
const w = {
|
|
61
60
|
...f,
|
|
62
|
-
present: i(
|
|
61
|
+
present: i(r, t)
|
|
63
62
|
};
|
|
64
|
-
return
|
|
63
|
+
return s == null || s({
|
|
65
64
|
action: e.type,
|
|
66
65
|
state: w.present
|
|
67
66
|
}), w;
|
|
68
67
|
}
|
|
69
|
-
let u = [...
|
|
70
|
-
p !== null && (u = [...u, i(p,
|
|
71
|
-
const
|
|
68
|
+
let u = [...n];
|
|
69
|
+
p !== null && (u = [...u, i(p, t)]), !J(l) && l > 0 && d(u) > l && (u = u.slice(d(u) - l));
|
|
70
|
+
const c = {
|
|
72
71
|
future: [],
|
|
73
72
|
past: u,
|
|
74
|
-
paused:
|
|
75
|
-
present: i(
|
|
73
|
+
paused: y,
|
|
74
|
+
present: i(r, t)
|
|
76
75
|
};
|
|
77
|
-
return
|
|
76
|
+
return s == null || s({
|
|
78
77
|
action: e.type,
|
|
79
|
-
state:
|
|
80
|
-
}),
|
|
78
|
+
state: c.present
|
|
79
|
+
}), c;
|
|
81
80
|
} else if (e.type === "CLEAR") {
|
|
82
|
-
const
|
|
81
|
+
const r = {
|
|
83
82
|
future: [],
|
|
84
83
|
past: [],
|
|
85
|
-
paused:
|
|
86
|
-
present: i(e.
|
|
84
|
+
paused: y,
|
|
85
|
+
present: i(e.initialState, t)
|
|
87
86
|
};
|
|
88
|
-
return
|
|
87
|
+
return s == null || s({
|
|
89
88
|
action: e.type,
|
|
90
|
-
state:
|
|
91
|
-
}),
|
|
89
|
+
state: r.present
|
|
90
|
+
}), r;
|
|
92
91
|
} else
|
|
93
92
|
throw new Error("Unsupported action type");
|
|
94
|
-
}, B = (e,
|
|
95
|
-
const { maxCapacity:
|
|
96
|
-
(
|
|
97
|
-
action:
|
|
98
|
-
immutable:
|
|
99
|
-
maxCapacity:
|
|
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,
|
|
97
|
+
immutable: n,
|
|
98
|
+
maxCapacity: l,
|
|
100
99
|
onChange: E,
|
|
101
|
-
state:
|
|
100
|
+
state: S
|
|
102
101
|
}),
|
|
103
102
|
{
|
|
104
|
-
...
|
|
105
|
-
|
|
103
|
+
...M,
|
|
104
|
+
paused: (t == null ? void 0 : t.paused) ?? !1,
|
|
105
|
+
present: i(y.current, n)
|
|
106
106
|
}
|
|
107
|
-
), u = d(p.future) !== 0,
|
|
108
|
-
|
|
107
|
+
), u = d(p.future) !== 0, c = d(p.past) !== 0, w = o(() => r({
|
|
108
|
+
initialState: y.current,
|
|
109
109
|
type: "CLEAR"
|
|
110
|
-
}), []), U =
|
|
111
|
-
|
|
112
|
-
}, []),
|
|
113
|
-
|
|
114
|
-
}, []),
|
|
115
|
-
u &&
|
|
116
|
-
}, [u]), D =
|
|
117
|
-
(
|
|
118
|
-
|
|
110
|
+
}), []), U = o(() => {
|
|
111
|
+
r({ type: "PAUSE" });
|
|
112
|
+
}, []), O = o(() => {
|
|
113
|
+
r({ type: "RESUME" });
|
|
114
|
+
}, []), N = o(() => {
|
|
115
|
+
u && r({ type: "REDO" });
|
|
116
|
+
}, [u]), D = L(
|
|
117
|
+
(S) => r({ type: "SET", newPresent: S }),
|
|
118
|
+
s,
|
|
119
119
|
f
|
|
120
|
-
), R =
|
|
121
|
-
|
|
122
|
-
}, [
|
|
123
|
-
(
|
|
124
|
-
|
|
120
|
+
), R = o((S) => r({ type: "SET", newPresent: S }), []), a = o(() => {
|
|
121
|
+
c && r({ type: "UNDO" });
|
|
122
|
+
}, [c]), A = o(
|
|
123
|
+
(S) => {
|
|
124
|
+
s ? D(S) : R(S);
|
|
125
125
|
},
|
|
126
|
-
[
|
|
126
|
+
[s, D, R]
|
|
127
127
|
);
|
|
128
128
|
return {
|
|
129
129
|
canRedo: u,
|
|
130
|
-
canUndo:
|
|
130
|
+
canUndo: c,
|
|
131
131
|
clear: w,
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
future: p.future
|
|
135
|
-
},
|
|
132
|
+
future: p.future,
|
|
133
|
+
past: p.past,
|
|
136
134
|
pause: U,
|
|
137
135
|
paused: p.paused,
|
|
138
|
-
redo:
|
|
139
|
-
resume:
|
|
136
|
+
redo: N,
|
|
137
|
+
resume: O,
|
|
140
138
|
set: A,
|
|
141
139
|
setDirect: R,
|
|
142
140
|
state: p.present,
|
|
143
|
-
undo:
|
|
141
|
+
undo: a
|
|
144
142
|
};
|
|
145
143
|
};
|
|
146
144
|
export {
|
package/package.json
CHANGED