use-good-hooks 1.0.32 → 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/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
- - `capacity`: Maximum number of history entries to keep (default: 10)
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
- - `history`: Array of all recorded states
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,11 +4,14 @@ export { DebounceSettings }
4
4
 
5
5
  declare type HistoryAction<T> = {
6
6
  type: 'CLEAR';
7
- initialPresent: T;
7
+ initialState: T;
8
8
  } | {
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
  } | {
@@ -36,9 +39,10 @@ declare type UseHistoryOptionsState<T> = {
36
39
  immutable?: boolean;
37
40
  maxCapacity?: number;
38
41
  onChange?: HistoryOnChange<T>;
42
+ paused?: boolean;
39
43
  };
40
44
 
41
- declare const useHistoryState: <T>(initialPresent: T, options?: UseHistoryOptionsState<T>) => {
45
+ declare const useHistoryState: <T>(initialState: T, options?: UseHistoryOptionsState<T>) => {
42
46
  canRedo: boolean;
43
47
  canUndo: boolean;
44
48
  clear: () => void;
@@ -47,6 +51,7 @@ declare const useHistoryState: <T>(initialPresent: T, options?: UseHistoryOption
47
51
  pause: () => void;
48
52
  paused: boolean;
49
53
  redo: () => void;
54
+ replace: (newPresent: T) => void;
50
55
  resume: () => void;
51
56
  set: (newPresent: T) => void;
52
57
  setDirect: (newPresent: T) => void;
@@ -1,146 +1,164 @@
1
- import { useRef as F, useReducer as H, useCallback as S } from "react";
1
+ import { useRef as F, useReducer as H, useCallback as d } from "react";
2
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 = {
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
- paused: !1,
10
9
  present: null
11
- }, i = (e, r) => r ? e : J(e), q = (e, r, o) => o ? e === r : JSON.stringify(e) === JSON.stringify(r), v = ({
10
+ }, l = (e, r) => r ? e : J(e), v = (e, r, y) => y ? e === r : JSON.stringify(e) === JSON.stringify(r), z = ({
12
11
  action: e,
13
12
  immutable: r,
14
- maxCapacity: o,
13
+ maxCapacity: y,
15
14
  onChange: t,
16
15
  state: f
17
16
  }) => {
18
- const { future: E, past: c, paused: l, present: p } = f;
19
- if (e.type === "PAUSE")
20
- return {
21
- ...f,
22
- paused: !0
23
- };
24
- if (e.type === "RESUME")
25
- return {
26
- ...f,
27
- paused: !1
28
- };
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)
37
- };
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") {
17
+ const { future: o, past: n, paused: S, present: p } = f;
18
+ if (e.type === "CLEAR") {
82
19
  const s = {
83
20
  future: [],
84
21
  past: [],
85
- paused: l,
86
- present: i(e.initialPresent, r)
22
+ paused: S,
23
+ present: l(e.initialState, r)
87
24
  };
88
25
  return t == null || t({
89
26
  action: e.type,
90
27
  state: s.present
91
28
  }), s;
92
- } else
93
- throw new Error("Unsupported action type");
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({
29
+ } else {
30
+ if (e.type === "PAUSE")
31
+ return {
32
+ ...f,
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)
43
+ };
44
+ return t == null || t({
45
+ action: e.type,
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");
106
+ }
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({
97
111
  action: T,
98
- immutable: c,
99
- maxCapacity: o,
100
- onChange: E,
101
- state: y
112
+ immutable: n,
113
+ maxCapacity: y,
114
+ onChange: o,
115
+ state: i
102
116
  }),
103
117
  {
104
- ...k,
105
- present: i(l.current, c)
118
+ ...q,
119
+ paused: (r == null ? void 0 : r.paused) ?? !1,
120
+ present: l(S.current, n)
106
121
  }
107
- ), u = d(p.future) !== 0, n = d(p.past) !== 0, w = S(() => s({
108
- initialPresent: l.current,
122
+ ), u = E(p.future) !== 0, c = E(p.past) !== 0, w = d(() => s({
123
+ initialState: S.current,
109
124
  type: "CLEAR"
110
- }), []), U = S(() => {
125
+ }), []), U = d(() => {
111
126
  s({ type: "PAUSE" });
112
- }, []), P = S(() => {
113
- s({ type: "RESUME" });
114
- }, []), O = S(() => {
127
+ }, []), P = d(() => {
115
128
  u && s({ type: "REDO" });
116
- }, [u]), D = M(
117
- (y) => s({ type: "SET", newPresent: y }),
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 }),
118
135
  t,
119
136
  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);
137
+ ), R = d((i) => s({ type: "SET", newPresent: i }), []), N = d(
138
+ (i) => {
139
+ t ? D(i) : R(i);
125
140
  },
126
141
  [t, D, R]
127
- );
142
+ ), L = d(() => {
143
+ c && s({ type: "UNDO" });
144
+ }, [c]);
128
145
  return {
129
146
  canRedo: u,
130
- canUndo: n,
147
+ canUndo: c,
131
148
  clear: w,
132
149
  future: p.future,
133
150
  past: p.past,
134
151
  pause: U,
135
152
  paused: p.paused,
136
- redo: O,
137
- resume: P,
138
- set: A,
153
+ redo: P,
154
+ replace: A,
155
+ resume: O,
156
+ set: N,
139
157
  setDirect: R,
140
158
  state: p.present,
141
- undo: N
159
+ undo: L
142
160
  };
143
161
  };
144
162
  export {
145
- B as default
163
+ G as default
146
164
  };
package/package.json CHANGED
@@ -50,5 +50,5 @@
50
50
  "test:coverage": "vitest --coverage --run",
51
51
  "test:watch": "vitest"
52
52
  },
53
- "version": "1.0.32"
53
+ "version": "1.0.34"
54
54
  }