@voidhash/mimic-react 0.0.1-alpha.6 → 0.0.1-alpha.8

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.
@@ -0,0 +1,325 @@
1
+ import { StoreApi, UseBoundStore } from "zustand";
2
+ import { Schema } from "effect";
3
+
4
+ //#region src/zustand-commander/types.d.ts
5
+
6
+ /**
7
+ * Any Effect Schema type (used for type constraints).
8
+ */
9
+ type AnyEffectSchema = Schema.Schema<any, any, any>;
10
+ /**
11
+ * Infer the Type from an Effect Schema.
12
+ */
13
+ type InferSchemaType<T> = T extends Schema.Schema<infer A, any, any> ? A : never;
14
+ /**
15
+ * Symbol used to identify Command objects at runtime.
16
+ */
17
+ declare const COMMAND_SYMBOL: unique symbol;
18
+ /**
19
+ * Symbol used to identify UndoableCommand objects at runtime.
20
+ */
21
+ declare const UNDOABLE_COMMAND_SYMBOL: unique symbol;
22
+ /**
23
+ * Context provided to command functions.
24
+ * Gives access to store state and dispatch capabilities.
25
+ */
26
+ interface CommandContext<TStore> {
27
+ /**
28
+ * Get the current store state.
29
+ */
30
+ readonly getState: () => TStore;
31
+ /**
32
+ * Set partial store state (for local/browser state updates).
33
+ */
34
+ readonly setState: (partial: Partial<TStore>) => void;
35
+ /**
36
+ * Dispatch another command.
37
+ * Returns the result of the dispatched command.
38
+ *
39
+ * @example
40
+ * dispatch(otherCommand)({ param: "value" });
41
+ */
42
+ readonly dispatch: CommandDispatch<TStore>;
43
+ }
44
+ /**
45
+ * The function signature for a command handler.
46
+ */
47
+ type CommandFn<TStore, TParams, TReturn> = (ctx: CommandContext<TStore>, params: TParams) => TReturn;
48
+ /**
49
+ * The function signature for an undoable command's revert handler.
50
+ * Receives the original params and the result from the forward execution.
51
+ */
52
+ type RevertFn<TStore, TParams, TReturn> = (ctx: CommandContext<TStore>, params: TParams, result: TReturn) => void;
53
+ /**
54
+ * A command that can be dispatched to modify store state.
55
+ * Regular commands do not support undo/redo.
56
+ */
57
+ interface Command<TStore, TParams, TReturn> {
58
+ readonly [COMMAND_SYMBOL]: true;
59
+ readonly fn: CommandFn<TStore, TParams, TReturn>;
60
+ readonly paramsSchema: AnyEffectSchema | null;
61
+ }
62
+ /**
63
+ * An undoable command that supports undo/redo.
64
+ * Must provide a revert function that knows how to undo the change.
65
+ */
66
+ interface UndoableCommand<TStore, TParams, TReturn> extends Command<TStore, TParams, TReturn> {
67
+ readonly [UNDOABLE_COMMAND_SYMBOL]: true;
68
+ readonly revert: RevertFn<TStore, TParams, TReturn>;
69
+ }
70
+ /**
71
+ * Any command type (regular or undoable).
72
+ */
73
+ type AnyCommand = Command<any, any, any>;
74
+ /**
75
+ * Any undoable command type.
76
+ */
77
+ type AnyUndoableCommand = UndoableCommand<any, any, any>;
78
+ /**
79
+ * Dispatch function that accepts commands and returns a function to call with params.
80
+ * Returns the result of the command execution.
81
+ *
82
+ * @example
83
+ * const result = dispatch(myCommand)({ param: "value" });
84
+ */
85
+ type CommandDispatch<TStore> = <TParams, TReturn>(command: Command<TStore, TParams, TReturn>) => (params: TParams) => TReturn;
86
+ /**
87
+ * An entry in the undo/redo stack.
88
+ * Contains all information needed to revert or redo a command.
89
+ */
90
+ interface UndoEntry<TParams = unknown, TReturn = unknown> {
91
+ /** The undoable command that was executed */
92
+ readonly command: AnyUndoableCommand;
93
+ /** The parameters that were passed to the command */
94
+ readonly params: TParams;
95
+ /** The result returned by the command (passed to revert) */
96
+ readonly result: TReturn;
97
+ /** Timestamp when the command was executed */
98
+ readonly timestamp: number;
99
+ }
100
+ /**
101
+ * State slice for undo/redo functionality.
102
+ */
103
+ interface CommanderSlice {
104
+ readonly _commander: {
105
+ /** Stack of commands that can be undone */
106
+ readonly undoStack: ReadonlyArray<UndoEntry>;
107
+ /** Stack of commands that can be redone */
108
+ readonly redoStack: ReadonlyArray<UndoEntry>;
109
+ };
110
+ }
111
+ /**
112
+ * Options for creating a commander.
113
+ */
114
+ interface CommanderOptions {
115
+ /**
116
+ * Maximum number of undo entries to keep.
117
+ * @default 100
118
+ */
119
+ readonly maxUndoStackSize?: number;
120
+ }
121
+ /**
122
+ * A commander instance bound to a specific store type.
123
+ * Used to create commands and the middleware.
124
+ */
125
+ interface Commander<TStore> {
126
+ /**
127
+ * Create a regular command (no undo support).
128
+ *
129
+ * @example
130
+ * // With params schema
131
+ * const addItem = commander.action(
132
+ * Schema.Struct({ name: Schema.String }),
133
+ * (ctx, params) => {
134
+ * // modify state
135
+ * }
136
+ * );
137
+ *
138
+ * // Without params
139
+ * const clearAll = commander.action((ctx) => {
140
+ * // modify state
141
+ * });
142
+ */
143
+ readonly action: {
144
+ <TParamsSchema extends AnyEffectSchema, TReturn = void>(paramsSchema: TParamsSchema, fn: CommandFn<TStore, InferSchemaType<TParamsSchema>, TReturn>): Command<TStore, InferSchemaType<TParamsSchema>, TReturn>;
145
+ <TReturn = void>(fn: CommandFn<TStore, void, TReturn>): Command<TStore, void, TReturn>;
146
+ };
147
+ /**
148
+ * Create an undoable command with undo/redo support.
149
+ * The revert function is called when undoing the command.
150
+ *
151
+ * @example
152
+ * const moveItem = commander.undoableAction(
153
+ * Schema.Struct({ id: Schema.String, toIndex: Schema.Number }),
154
+ * (ctx, params) => {
155
+ * const fromIndex = // get current index
156
+ * // perform move
157
+ * return { fromIndex }; // return data needed for revert
158
+ * },
159
+ * (ctx, params, result) => {
160
+ * // revert: move back to original position
161
+ * ctx.dispatch(moveItem)({ id: params.id, toIndex: result.fromIndex });
162
+ * }
163
+ * );
164
+ */
165
+ readonly undoableAction: {
166
+ <TParamsSchema extends AnyEffectSchema, TReturn>(paramsSchema: TParamsSchema, fn: CommandFn<TStore, InferSchemaType<TParamsSchema>, TReturn>, revert: RevertFn<TStore, InferSchemaType<TParamsSchema>, TReturn>): UndoableCommand<TStore, InferSchemaType<TParamsSchema>, TReturn>;
167
+ <TReturn>(fn: CommandFn<TStore, void, TReturn>, revert: RevertFn<TStore, void, TReturn>): UndoableCommand<TStore, void, TReturn>;
168
+ };
169
+ /**
170
+ * Zustand middleware that adds commander functionality.
171
+ * Adds undo/redo stacks to the store state.
172
+ */
173
+ readonly middleware: CommanderMiddleware<TStore>;
174
+ }
175
+ /**
176
+ * Type for the commander middleware.
177
+ * Note: TStore is intentionally unused here to match the Commander interface signature.
178
+ * The middleware is generic over T (the inner store type) and adds CommanderSlice.
179
+ */
180
+ type CommanderMiddleware<_TStore> = <T extends object>(config: (set: StoreApi<T & CommanderSlice>["setState"], get: StoreApi<T & CommanderSlice>["getState"], api: StoreApi<T & CommanderSlice>) => T) => (set: StoreApi<T & CommanderSlice>["setState"], get: StoreApi<T & CommanderSlice>["getState"], api: StoreApi<T & CommanderSlice>) => T & CommanderSlice;
181
+ /**
182
+ * Extract the params type from a command.
183
+ */
184
+ type CommandParams<T> = T extends Command<any, infer P, any> ? P : never;
185
+ /**
186
+ * Extract the return type from a command.
187
+ */
188
+ type CommandReturn<T> = T extends Command<any, any, infer R> ? R : undefined;
189
+ /**
190
+ * Extract the store type from a command.
191
+ */
192
+ type CommandStore<T> = T extends Command<infer S, any, any> ? S : never;
193
+ /**
194
+ * Type guard to check if a value is a Command.
195
+ */
196
+ declare function isCommand(value: unknown): value is AnyCommand;
197
+ /**
198
+ * Type guard to check if a command is undoable.
199
+ */
200
+ declare function isUndoableCommand(value: unknown): value is AnyUndoableCommand;
201
+ /**
202
+ * Helper type to extract the state type from a zustand store.
203
+ */
204
+ type ExtractState<TStore> = TStore extends UseBoundStore<StoreApi<infer S>> ? S : TStore extends StoreApi<infer S> ? S : never;
205
+ //#endregion
206
+ //#region src/zustand-commander/commander.d.ts
207
+ /**
208
+ * Creates a commander instance bound to a specific store type.
209
+ *
210
+ * @example
211
+ * ```ts
212
+ * // Create commander for your store type
213
+ * const commander = createCommander<StoreState>();
214
+ *
215
+ * // Define commands
216
+ * const addItem = commander.action(
217
+ * Schema.Struct({ name: Schema.String }),
218
+ * (ctx, params) => {
219
+ * const { mimic } = ctx.getState();
220
+ * mimic.document.transaction(root => {
221
+ * // add item
222
+ * });
223
+ * }
224
+ * );
225
+ *
226
+ * // Create store with middleware
227
+ * const useStore = create(
228
+ * commander.middleware(
229
+ * mimic(document, (set, get) => ({
230
+ * // your state
231
+ * }))
232
+ * )
233
+ * );
234
+ * ```
235
+ */
236
+ declare function createCommander<TStore extends object>(options?: CommanderOptions): Commander<TStore & CommanderSlice>;
237
+ /**
238
+ * Perform an undo operation on the store.
239
+ * Returns true if an undo was performed, false if undo stack was empty.
240
+ */
241
+ declare function performUndo<TStore extends CommanderSlice>(storeApi: StoreApi<TStore>): boolean;
242
+ /**
243
+ * Perform a redo operation on the store.
244
+ * Returns true if a redo was performed, false if redo stack was empty.
245
+ */
246
+ declare function performRedo<TStore extends CommanderSlice>(storeApi: StoreApi<TStore>): boolean;
247
+ /**
248
+ * Clear the undo and redo stacks.
249
+ */
250
+ declare function clearUndoHistory<TStore extends CommanderSlice>(storeApi: StoreApi<TStore>): void;
251
+ //#endregion
252
+ //#region src/zustand-commander/hooks.d.ts
253
+ /**
254
+ * React hook to get a dispatch function for commands.
255
+ * The dispatch function executes commands and manages undo/redo state.
256
+ *
257
+ * @example
258
+ * ```tsx
259
+ * const dispatch = useCommander(useStore);
260
+ *
261
+ * const handleClick = () => {
262
+ * dispatch(addCard)({ columnId: "col-1", title: "New Card" });
263
+ * };
264
+ * ```
265
+ */
266
+ declare function useCommander<TStore extends CommanderSlice>(store: UseBoundStore<StoreApi<TStore>>): CommandDispatch<TStore>;
267
+ /**
268
+ * State and actions for undo/redo functionality.
269
+ */
270
+ interface UndoRedoState {
271
+ /** Whether there are actions that can be undone */
272
+ readonly canUndo: boolean;
273
+ /** Whether there are actions that can be redone */
274
+ readonly canRedo: boolean;
275
+ /** Number of items in the undo stack */
276
+ readonly undoCount: number;
277
+ /** Number of items in the redo stack */
278
+ readonly redoCount: number;
279
+ /** Undo the last action */
280
+ readonly undo: () => boolean;
281
+ /** Redo the last undone action */
282
+ readonly redo: () => boolean;
283
+ /** Clear the undo/redo history */
284
+ readonly clear: () => void;
285
+ }
286
+ /**
287
+ * React hook for undo/redo functionality.
288
+ * Provides state (canUndo, canRedo) and actions (undo, redo, clear).
289
+ *
290
+ * @example
291
+ * ```tsx
292
+ * const { canUndo, canRedo, undo, redo } = useUndoRedo(useStore);
293
+ *
294
+ * return (
295
+ * <>
296
+ * <button onClick={undo} disabled={!canUndo}>Undo</button>
297
+ * <button onClick={redo} disabled={!canRedo}>Redo</button>
298
+ * </>
299
+ * );
300
+ * ```
301
+ */
302
+ declare function useUndoRedo<TStore extends CommanderSlice>(store: UseBoundStore<StoreApi<TStore>>): UndoRedoState;
303
+ /**
304
+ * Options for the keyboard shortcut hook.
305
+ */
306
+ interface UseUndoRedoKeyboardOptions {
307
+ /** Enable Ctrl/Cmd+Z for undo (default: true) */
308
+ readonly enableUndo?: boolean;
309
+ /** Enable Ctrl/Cmd+Shift+Z or Ctrl+Y for redo (default: true) */
310
+ readonly enableRedo?: boolean;
311
+ }
312
+ /**
313
+ * React hook that adds keyboard shortcuts for undo/redo.
314
+ * Listens for Ctrl/Cmd+Z (undo) and Ctrl/Cmd+Shift+Z or Ctrl+Y (redo).
315
+ *
316
+ * @example
317
+ * ```tsx
318
+ * // In your app component
319
+ * useUndoRedoKeyboard(useStore);
320
+ * ```
321
+ */
322
+ declare function useUndoRedoKeyboard<TStore extends CommanderSlice>(store: UseBoundStore<StoreApi<TStore>>, options?: UseUndoRedoKeyboardOptions): void;
323
+ //#endregion
324
+ export { type AnyCommand, type AnyEffectSchema, type AnyUndoableCommand, COMMAND_SYMBOL, type Command, type CommandContext, type CommandDispatch, type CommandFn, type CommandParams, type CommandReturn, type CommandStore, type Commander, type CommanderMiddleware, type CommanderOptions, type CommanderSlice, type ExtractState, type InferSchemaType, type RevertFn, UNDOABLE_COMMAND_SYMBOL, type UndoEntry, type UndoRedoState, type UndoableCommand, type UseUndoRedoKeyboardOptions, clearUndoHistory, createCommander, isCommand, isUndoableCommand, performRedo, performUndo, useCommander, useUndoRedo, useUndoRedoKeyboard };
325
+ //# sourceMappingURL=index.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.mts","names":[],"sources":["../../src/zustand-commander/types.ts","../../src/zustand-commander/commander.ts","../../src/zustand-commander/hooks.ts"],"sourcesContent":[],"mappings":";;;;;;;;AAqEqC,KAnDzB,eAAA,GAAkB,MAAA,CAAO,MAmDA,CAAA,GAAA,EAAA,GAAA,EAAA,GAAA,CAAA;;;AAUrC;AACsB,KAzDV,eAyDU,CAAA,CAAA,CAAA,GAzDW,CAyDX,SAzDqB,MAAA,CAAO,MAyD5B,CAAA,KAAA,EAAA,EAAA,GAAA,EAAA,GAAA,CAAA,GAAA,CAAA,GAAA,KAAA;;;;AAEV,cAhDC,cAgDD,EAAA,OAAA,MAAA;AAMZ;;;AAEU,cAnDG,uBAmDH,EAAA,OAAA,MAAA;;;AAYV;;AAEyB,UArDR,cAqDQ,CAAA,MAAA,CAAA,CAAA;EAAQ;;;EACR,SAAA,QAAA,EAAA,GAAA,GAlDE,MAkDF;EAAe;AAOxC;;EAC0B,SAAA,QAAA,EAAA,CAAA,OAAA,EArDK,OAqDL,CArDa,MAqDb,CAAA,EAAA,GAAA,IAAA;EAAS;;;;;;;EAAlB,SAAA,QAAA,EA5CI,eA4CJ,CA5CoB,MA4CpB,CAAA;AAQjB;AAKA;AAaA;;AAC2B,KA7Df,SA6De,CAAA,MAAA,EAAA,OAAA,EAAA,OAAA,CAAA,GAAA,CAAA,GAAA,EA5DpB,cA4DoB,CA5DL,MA4DK,CAAA,EAAA,MAAA,EA3DjB,OA2DiB,EAAA,GA1DtB,OA0DsB;;;;;AACM,KArDrB,QAqDqB,CAAA,MAAA,EAAA,OAAA,EAAA,OAAA,CAAA,GAAA,CAAA,GAAA,EApD1B,cAoD0B,CApDX,MAoDW,CAAA,EAAA,MAAA,EAnDvB,OAmDuB,EAAA,MAAA,EAlDvB,OAkDuB,EAAA,GAAA,IAAA;AAUjC;;;;AAM0B,UAvDT,OAu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AAA,GAAA,GAAA,OAAA;EACvB;EACgB,SAAA,KAAA,EAAA,GAAA,GAAA,IAAA;;;;;;AAM5B;AAKA;AAaA;;;;;;;;AAYA;;AAImB,iBEJH,WFIG,CAAA,eEJwB,cFIxB,CAAA,CAAA,KAAA,EEHV,aFGU,CEHI,QFGJ,CEHa,MFGb,CAAA,CAAA,CAAA,EEFhB,aFEgB;;;AAUnB;AAGsC,UEgCrB,0BAAA,CFhCqB;EAAd;EAEc,SAAA,UAAA,CAAA,EAAA,OAAA;EAAd;EAAa,SAAA,UAAA,CAAA,EAAA,OAAA;AAWrC;AAYA;;;;;;;;;;AAuBuB,iBECP,mBFDO,CAAA,eEC4B,cFD5B,CAAA,CAAA,KAAA,EEEd,aFFc,CEEA,QFFA,CEES,MFFT,CAAA,CAAA,EAAA,OAAA,CAAA,EEGZ,0BFHY,CAAA,EAAA,IAAA"}
@@ -0,0 +1,355 @@
1
+ import { t as _objectSpread2 } from "../objectSpread2-CxTyNSYl.mjs";
2
+ import { useCallback, useEffect, useMemo } from "react";
3
+ import { useStore } from "zustand";
4
+
5
+ //#region src/zustand-commander/types.ts
6
+ /**
7
+ * Symbol used to identify Command objects at runtime.
8
+ */
9
+ const COMMAND_SYMBOL = Symbol.for("zustand-commander/command");
10
+ /**
11
+ * Symbol used to identify UndoableCommand objects at runtime.
12
+ */
13
+ const UNDOABLE_COMMAND_SYMBOL = Symbol.for("zustand-commander/undoable-command");
14
+ /**
15
+ * Type guard to check if a value is a Command.
16
+ */
17
+ function isCommand(value) {
18
+ return typeof value === "object" && value !== null && COMMAND_SYMBOL in value && value[COMMAND_SYMBOL] === true;
19
+ }
20
+ /**
21
+ * Type guard to check if a command is undoable.
22
+ */
23
+ function isUndoableCommand(value) {
24
+ return isCommand(value) && UNDOABLE_COMMAND_SYMBOL in value && value[UNDOABLE_COMMAND_SYMBOL] === true;
25
+ }
26
+
27
+ //#endregion
28
+ //#region src/zustand-commander/commander.ts
29
+ const DEFAULT_OPTIONS = { maxUndoStackSize: 100 };
30
+ /**
31
+ * Creates a commander instance bound to a specific store type.
32
+ *
33
+ * @example
34
+ * ```ts
35
+ * // Create commander for your store type
36
+ * const commander = createCommander<StoreState>();
37
+ *
38
+ * // Define commands
39
+ * const addItem = commander.action(
40
+ * Schema.Struct({ name: Schema.String }),
41
+ * (ctx, params) => {
42
+ * const { mimic } = ctx.getState();
43
+ * mimic.document.transaction(root => {
44
+ * // add item
45
+ * });
46
+ * }
47
+ * );
48
+ *
49
+ * // Create store with middleware
50
+ * const useStore = create(
51
+ * commander.middleware(
52
+ * mimic(document, (set, get) => ({
53
+ * // your state
54
+ * }))
55
+ * )
56
+ * );
57
+ * ```
58
+ */
59
+ function createCommander(options = {}) {
60
+ const { maxUndoStackSize } = _objectSpread2(_objectSpread2({}, DEFAULT_OPTIONS), options);
61
+ let _storeApi = null;
62
+ /**
63
+ * Creates the dispatch function for use within command handlers.
64
+ */
65
+ const createDispatch = () => {
66
+ return (command) => {
67
+ return (params) => {
68
+ if (!_storeApi) throw new Error("Commander: Store not initialized. Make sure to use the commander middleware.");
69
+ const ctx = {
70
+ getState: () => _storeApi.getState(),
71
+ setState: (partial) => _storeApi.setState(partial),
72
+ dispatch: createDispatch()
73
+ };
74
+ const result = command.fn(ctx, params);
75
+ if (isUndoableCommand(command)) {
76
+ const entry = {
77
+ command,
78
+ params,
79
+ result,
80
+ timestamp: Date.now()
81
+ };
82
+ _storeApi.setState((state) => {
83
+ const { undoStack, redoStack } = state._commander;
84
+ const newUndoStack = [...undoStack, entry].slice(-maxUndoStackSize);
85
+ return _objectSpread2(_objectSpread2({}, state), {}, { _commander: {
86
+ undoStack: newUndoStack,
87
+ redoStack: []
88
+ } });
89
+ });
90
+ }
91
+ return result;
92
+ };
93
+ };
94
+ };
95
+ function action(paramsSchemaOrFn, maybeFn) {
96
+ if (maybeFn !== void 0) return {
97
+ [COMMAND_SYMBOL]: true,
98
+ fn: maybeFn,
99
+ paramsSchema: paramsSchemaOrFn
100
+ };
101
+ if (typeof paramsSchemaOrFn !== "function") throw new Error("Commander: action requires a function");
102
+ return {
103
+ [COMMAND_SYMBOL]: true,
104
+ fn: paramsSchemaOrFn,
105
+ paramsSchema: null
106
+ };
107
+ }
108
+ function undoableAction(paramsSchemaOrFn, fnOrRevert, maybeRevert) {
109
+ if (maybeRevert !== void 0) return {
110
+ [COMMAND_SYMBOL]: true,
111
+ [UNDOABLE_COMMAND_SYMBOL]: true,
112
+ fn: fnOrRevert,
113
+ paramsSchema: paramsSchemaOrFn,
114
+ revert: maybeRevert
115
+ };
116
+ if (typeof paramsSchemaOrFn !== "function") throw new Error("Commander: undoableAction requires a function");
117
+ return {
118
+ [COMMAND_SYMBOL]: true,
119
+ [UNDOABLE_COMMAND_SYMBOL]: true,
120
+ fn: paramsSchemaOrFn,
121
+ paramsSchema: null,
122
+ revert: fnOrRevert
123
+ };
124
+ }
125
+ /**
126
+ * Zustand middleware that adds commander functionality.
127
+ */
128
+ const middleware = (config) => {
129
+ return (set, get, api) => {
130
+ _storeApi = api;
131
+ return _objectSpread2(_objectSpread2({}, config(set, get, api)), {}, { _commander: {
132
+ undoStack: [],
133
+ redoStack: []
134
+ } });
135
+ };
136
+ };
137
+ return {
138
+ action,
139
+ undoableAction,
140
+ middleware
141
+ };
142
+ }
143
+ /**
144
+ * Perform an undo operation on the store.
145
+ * Returns true if an undo was performed, false if undo stack was empty.
146
+ */
147
+ function performUndo(storeApi) {
148
+ const { undoStack, redoStack } = storeApi.getState()._commander;
149
+ const entry = undoStack[undoStack.length - 1];
150
+ if (!entry) return false;
151
+ const newUndoStack = undoStack.slice(0, -1);
152
+ const ctx = {
153
+ getState: () => storeApi.getState(),
154
+ setState: (partial) => storeApi.setState(partial),
155
+ dispatch: createDispatchForUndo(storeApi)
156
+ };
157
+ entry.command.revert(ctx, entry.params, entry.result);
158
+ storeApi.setState((state) => _objectSpread2(_objectSpread2({}, state), {}, { _commander: {
159
+ undoStack: newUndoStack,
160
+ redoStack: [...redoStack, entry]
161
+ } }));
162
+ return true;
163
+ }
164
+ /**
165
+ * Perform a redo operation on the store.
166
+ * Returns true if a redo was performed, false if redo stack was empty.
167
+ */
168
+ function performRedo(storeApi) {
169
+ const { undoStack, redoStack } = storeApi.getState()._commander;
170
+ const entry = redoStack[redoStack.length - 1];
171
+ if (!entry) return false;
172
+ const newRedoStack = redoStack.slice(0, -1);
173
+ const ctx = {
174
+ getState: () => storeApi.getState(),
175
+ setState: (partial) => storeApi.setState(partial),
176
+ dispatch: createDispatchForUndo(storeApi)
177
+ };
178
+ const result = entry.command.fn(ctx, entry.params);
179
+ const newEntry = {
180
+ command: entry.command,
181
+ params: entry.params,
182
+ result,
183
+ timestamp: Date.now()
184
+ };
185
+ storeApi.setState((state) => _objectSpread2(_objectSpread2({}, state), {}, { _commander: {
186
+ undoStack: [...undoStack, newEntry],
187
+ redoStack: newRedoStack
188
+ } }));
189
+ return true;
190
+ }
191
+ /**
192
+ * Creates a dispatch function for use during undo/redo operations.
193
+ * This dispatch does NOT add to undo stack (to avoid infinite loops).
194
+ */
195
+ function createDispatchForUndo(storeApi) {
196
+ return (command) => {
197
+ return (params) => {
198
+ const ctx = {
199
+ getState: () => storeApi.getState(),
200
+ setState: (partial) => storeApi.setState(partial),
201
+ dispatch: createDispatchForUndo(storeApi)
202
+ };
203
+ return command.fn(ctx, params);
204
+ };
205
+ };
206
+ }
207
+ /**
208
+ * Clear the undo and redo stacks.
209
+ */
210
+ function clearUndoHistory(storeApi) {
211
+ storeApi.setState((state) => _objectSpread2(_objectSpread2({}, state), {}, { _commander: {
212
+ undoStack: [],
213
+ redoStack: []
214
+ } }));
215
+ }
216
+
217
+ //#endregion
218
+ //#region src/zustand-commander/hooks.ts
219
+ /**
220
+ * @voidhash/mimic-react/zustand-commander
221
+ *
222
+ * React hooks for zustand-commander.
223
+ *
224
+ * @since 0.0.1
225
+ */
226
+ /**
227
+ * Creates a dispatch function for commands.
228
+ * This is for use outside of React components (e.g., in command handlers).
229
+ */
230
+ function createDispatchFromApi(storeApi, maxUndoStackSize = 100) {
231
+ const dispatch = (command) => {
232
+ return (params) => {
233
+ const ctx = {
234
+ getState: () => storeApi.getState(),
235
+ setState: (partial) => storeApi.setState(partial),
236
+ dispatch
237
+ };
238
+ const result = command.fn(ctx, params);
239
+ if (isUndoableCommand(command)) storeApi.setState((state) => {
240
+ const { undoStack } = state._commander;
241
+ const newUndoStack = [...undoStack, {
242
+ command,
243
+ params,
244
+ result,
245
+ timestamp: Date.now()
246
+ }].slice(-maxUndoStackSize);
247
+ return _objectSpread2(_objectSpread2({}, state), {}, { _commander: {
248
+ undoStack: newUndoStack,
249
+ redoStack: []
250
+ } });
251
+ });
252
+ return result;
253
+ };
254
+ };
255
+ return dispatch;
256
+ }
257
+ /**
258
+ * React hook to get a dispatch function for commands.
259
+ * The dispatch function executes commands and manages undo/redo state.
260
+ *
261
+ * @example
262
+ * ```tsx
263
+ * const dispatch = useCommander(useStore);
264
+ *
265
+ * const handleClick = () => {
266
+ * dispatch(addCard)({ columnId: "col-1", title: "New Card" });
267
+ * };
268
+ * ```
269
+ */
270
+ function useCommander(store) {
271
+ const storeApi = useMemo(() => {
272
+ return store;
273
+ }, [store]);
274
+ return useMemo(() => createDispatchFromApi(storeApi), [storeApi]);
275
+ }
276
+ /**
277
+ * React hook for undo/redo functionality.
278
+ * Provides state (canUndo, canRedo) and actions (undo, redo, clear).
279
+ *
280
+ * @example
281
+ * ```tsx
282
+ * const { canUndo, canRedo, undo, redo } = useUndoRedo(useStore);
283
+ *
284
+ * return (
285
+ * <>
286
+ * <button onClick={undo} disabled={!canUndo}>Undo</button>
287
+ * <button onClick={redo} disabled={!canRedo}>Redo</button>
288
+ * </>
289
+ * );
290
+ * ```
291
+ */
292
+ function useUndoRedo(store) {
293
+ const storeApi = useMemo(() => {
294
+ return store;
295
+ }, [store]);
296
+ const commanderState = useStore(store, (state) => state._commander);
297
+ return {
298
+ canUndo: commanderState.undoStack.length > 0,
299
+ canRedo: commanderState.redoStack.length > 0,
300
+ undoCount: commanderState.undoStack.length,
301
+ redoCount: commanderState.redoStack.length,
302
+ undo: useCallback(() => {
303
+ return performUndo(storeApi);
304
+ }, [storeApi]),
305
+ redo: useCallback(() => {
306
+ return performRedo(storeApi);
307
+ }, [storeApi]),
308
+ clear: useCallback(() => {
309
+ clearUndoHistory(storeApi);
310
+ }, [storeApi])
311
+ };
312
+ }
313
+ /**
314
+ * React hook that adds keyboard shortcuts for undo/redo.
315
+ * Listens for Ctrl/Cmd+Z (undo) and Ctrl/Cmd+Shift+Z or Ctrl+Y (redo).
316
+ *
317
+ * @example
318
+ * ```tsx
319
+ * // In your app component
320
+ * useUndoRedoKeyboard(useStore);
321
+ * ```
322
+ */
323
+ function useUndoRedoKeyboard(store, options = {}) {
324
+ const { enableUndo = true, enableRedo = true } = options;
325
+ const storeApi = useMemo(() => {
326
+ return store;
327
+ }, [store]);
328
+ useEffect(() => {
329
+ if (typeof window === "undefined") return;
330
+ const handleKeyDown = (event) => {
331
+ if (!(navigator.platform.toUpperCase().indexOf("MAC") >= 0 ? event.metaKey : event.ctrlKey)) return;
332
+ if (enableUndo && event.key === "z" && !event.shiftKey) {
333
+ event.preventDefault();
334
+ performUndo(storeApi);
335
+ return;
336
+ }
337
+ if (enableRedo) {
338
+ if (event.key === "z" && event.shiftKey || event.key === "y") {
339
+ event.preventDefault();
340
+ performRedo(storeApi);
341
+ }
342
+ }
343
+ };
344
+ window.addEventListener("keydown", handleKeyDown);
345
+ return () => window.removeEventListener("keydown", handleKeyDown);
346
+ }, [
347
+ storeApi,
348
+ enableUndo,
349
+ enableRedo
350
+ ]);
351
+ }
352
+
353
+ //#endregion
354
+ export { COMMAND_SYMBOL, UNDOABLE_COMMAND_SYMBOL, clearUndoHistory, createCommander, isCommand, isUndoableCommand, performRedo, performUndo, useCommander, useUndoRedo, useUndoRedoKeyboard };
355
+ //# sourceMappingURL=index.mjs.map