use-good-hooks 1.0.32 → 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 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,7 +4,7 @@ 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
  } | {
@@ -36,9 +36,10 @@ 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>(initialPresent: T, options?: UseHistoryOptionsState<T>) => {
42
+ declare const useHistoryState: <T>(initialState: T, options?: UseHistoryOptionsState<T>) => {
42
43
  canRedo: boolean;
43
44
  canUndo: boolean;
44
45
  clear: () => void;
@@ -1,21 +1,20 @@
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";
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 M from "./use-debounce-fn.js";
6
- const k = {
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, r) => r ? e : J(e), q = (e, r, o) => o ? e === r : JSON.stringify(e) === JSON.stringify(r), v = ({
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: r,
14
- maxCapacity: o,
15
- onChange: t,
12
+ immutable: t,
13
+ maxCapacity: l,
14
+ onChange: s,
16
15
  state: f
17
16
  }) => {
18
- const { future: E, past: c, paused: l, present: p } = f;
17
+ const { future: E, past: n, paused: y, present: p } = f;
19
18
  if (e.type === "PAUSE")
20
19
  return {
21
20
  ...f,
@@ -27,118 +26,119 @@ const k = {
27
26
  paused: !1
28
27
  };
29
28
  if (e.type === "UNDO") {
30
- if (d(c) === 0)
29
+ if (d(n) === 0)
31
30
  return f;
32
- const s = c[d(c) - 1], u = c.slice(0, d(c) - 1), n = {
33
- future: [i(p, r), ...E],
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: l,
36
- present: i(s, r)
34
+ paused: y,
35
+ present: i(r, t)
37
36
  };
38
- return t == null || t({
37
+ return s == null || s({
39
38
  action: e.type,
40
- state: n.present
41
- }), n;
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 s = E[0], n = {
44
+ const r = E[0], c = {
46
45
  future: E.slice(1),
47
- past: [...c, i(p, r)],
48
- paused: l,
49
- present: i(s, r)
46
+ past: [...n, i(p, t)],
47
+ paused: y,
48
+ present: i(r, t)
50
49
  };
51
- return t == null || t({
50
+ return s == null || s({
52
51
  action: e.type,
53
- state: n.present
54
- }), n;
52
+ state: c.present
53
+ }), c;
55
54
  } else if (e.type === "SET") {
56
- const { newPresent: s } = e;
57
- if (q(s, p, r))
55
+ const { newPresent: r } = e;
56
+ if (k(r, p, t))
58
57
  return f;
59
- if (l) {
58
+ if (y) {
60
59
  const w = {
61
60
  ...f,
62
- present: i(s, r)
61
+ present: i(r, t)
63
62
  };
64
- return t == null || t({
63
+ return s == null || s({
65
64
  action: e.type,
66
65
  state: w.present
67
66
  }), w;
68
67
  }
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 = {
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: l,
75
- present: i(s, r)
73
+ paused: y,
74
+ present: i(r, t)
76
75
  };
77
- return t == null || t({
76
+ return s == null || s({
78
77
  action: e.type,
79
- state: n.present
80
- }), n;
78
+ state: c.present
79
+ }), c;
81
80
  } else if (e.type === "CLEAR") {
82
- const s = {
81
+ const r = {
83
82
  future: [],
84
83
  past: [],
85
- paused: l,
86
- present: i(e.initialPresent, r)
84
+ paused: y,
85
+ present: i(e.initialState, t)
87
86
  };
88
- return t == null || t({
87
+ return s == null || s({
89
88
  action: e.type,
90
- state: s.present
91
- }), s;
89
+ state: r.present
90
+ }), r;
92
91
  } else
93
92
  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({
97
- action: T,
98
- immutable: c,
99
- maxCapacity: o,
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: y
100
+ state: S
102
101
  }),
103
102
  {
104
- ...k,
105
- present: i(l.current, c)
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, n = d(p.past) !== 0, w = S(() => s({
108
- initialPresent: l.current,
107
+ ), u = d(p.future) !== 0, c = d(p.past) !== 0, w = o(() => r({
108
+ initialState: y.current,
109
109
  type: "CLEAR"
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,
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 = 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);
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
- [t, D, R]
126
+ [s, D, R]
127
127
  );
128
128
  return {
129
129
  canRedo: u,
130
- canUndo: n,
130
+ canUndo: c,
131
131
  clear: w,
132
132
  future: p.future,
133
133
  past: p.past,
134
134
  pause: U,
135
135
  paused: p.paused,
136
- redo: O,
137
- resume: P,
136
+ redo: N,
137
+ resume: O,
138
138
  set: A,
139
139
  setDirect: R,
140
140
  state: p.present,
141
- undo: N
141
+ undo: a
142
142
  };
143
143
  };
144
144
  export {
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.33"
54
54
  }