@voidhash/mimic-react 0.0.1 → 0.0.3
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/.turbo/turbo-build.log +60 -26
- package/dist/_virtual/_@oxc-project_runtime@0.103.0/helpers/defineProperty.cjs +14 -0
- package/dist/_virtual/_@oxc-project_runtime@0.103.0/helpers/defineProperty.mjs +14 -0
- package/dist/_virtual/_@oxc-project_runtime@0.103.0/helpers/objectSpread2.cjs +27 -0
- package/dist/_virtual/_@oxc-project_runtime@0.103.0/helpers/objectSpread2.mjs +27 -0
- package/dist/_virtual/_@oxc-project_runtime@0.103.0/helpers/toPrimitive.cjs +16 -0
- package/dist/_virtual/_@oxc-project_runtime@0.103.0/helpers/toPrimitive.mjs +16 -0
- package/dist/_virtual/_@oxc-project_runtime@0.103.0/helpers/toPropertyKey.cjs +11 -0
- package/dist/_virtual/_@oxc-project_runtime@0.103.0/helpers/toPropertyKey.mjs +11 -0
- package/dist/_virtual/_@oxc-project_runtime@0.103.0/helpers/typeof.cjs +18 -0
- package/dist/_virtual/_@oxc-project_runtime@0.103.0/helpers/typeof.mjs +12 -0
- package/dist/zustand/index.cjs +2 -94
- package/dist/zustand/index.d.cts +3 -115
- package/dist/zustand/index.d.mts +3 -115
- package/dist/zustand/index.mjs +2 -95
- package/dist/zustand/middleware.cjs +95 -0
- package/dist/zustand/middleware.d.cts +47 -0
- package/dist/zustand/middleware.d.cts.map +1 -0
- package/dist/zustand/middleware.d.mts +47 -0
- package/dist/zustand/middleware.d.mts.map +1 -0
- package/dist/zustand/middleware.mjs +96 -0
- package/dist/zustand/middleware.mjs.map +1 -0
- package/dist/zustand/types.d.cts +75 -0
- package/dist/zustand/types.d.cts.map +1 -0
- package/dist/zustand/types.d.mts +75 -0
- package/dist/zustand/types.d.mts.map +1 -0
- package/dist/zustand-commander/commander.cjs +187 -0
- package/dist/zustand-commander/commander.d.cts +52 -0
- package/dist/zustand-commander/commander.d.cts.map +1 -0
- package/dist/zustand-commander/commander.d.mts +52 -0
- package/dist/zustand-commander/commander.d.mts.map +1 -0
- package/dist/zustand-commander/commander.mjs +185 -0
- package/dist/zustand-commander/commander.mjs.map +1 -0
- package/dist/zustand-commander/hooks.cjs +145 -0
- package/dist/zustand-commander/hooks.d.cts +78 -0
- package/dist/zustand-commander/hooks.d.cts.map +1 -0
- package/dist/zustand-commander/hooks.d.mts +78 -0
- package/dist/zustand-commander/hooks.d.mts.map +1 -0
- package/dist/zustand-commander/hooks.mjs +144 -0
- package/dist/zustand-commander/hooks.mjs.map +1 -0
- package/dist/zustand-commander/index.cjs +15 -354
- package/dist/zustand-commander/index.d.cts +4 -313
- package/dist/zustand-commander/index.d.mts +4 -313
- package/dist/zustand-commander/index.mjs +4 -344
- package/dist/zustand-commander/types.cjs +28 -0
- package/dist/zustand-commander/types.d.cts +195 -0
- package/dist/zustand-commander/types.d.cts.map +1 -0
- package/dist/zustand-commander/types.d.mts +195 -0
- package/dist/zustand-commander/types.d.mts.map +1 -0
- package/dist/zustand-commander/types.mjs +25 -0
- package/dist/zustand-commander/types.mjs.map +1 -0
- package/package.json +3 -3
- package/tsdown.config.ts +1 -1
- package/dist/objectSpread2-CIP_6jda.cjs +0 -73
- package/dist/objectSpread2-CxTyNSYl.mjs +0 -67
- package/dist/zustand/index.d.cts.map +0 -1
- package/dist/zustand/index.d.mts.map +0 -1
- package/dist/zustand/index.mjs.map +0 -1
- package/dist/zustand-commander/index.d.cts.map +0 -1
- package/dist/zustand-commander/index.d.mts.map +0 -1
- package/dist/zustand-commander/index.mjs.map +0 -1
|
@@ -1,313 +1,4 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Symbol used to identify Command objects at runtime.
|
|
7
|
-
*/
|
|
8
|
-
declare const COMMAND_SYMBOL: unique symbol;
|
|
9
|
-
/**
|
|
10
|
-
* Symbol used to identify UndoableCommand objects at runtime.
|
|
11
|
-
*/
|
|
12
|
-
declare const UNDOABLE_COMMAND_SYMBOL: unique symbol;
|
|
13
|
-
/**
|
|
14
|
-
* Context provided to command functions.
|
|
15
|
-
* Gives access to store state and dispatch capabilities.
|
|
16
|
-
*/
|
|
17
|
-
interface CommandContext<TStore> {
|
|
18
|
-
/**
|
|
19
|
-
* Get the current store state.
|
|
20
|
-
*/
|
|
21
|
-
readonly getState: () => TStore;
|
|
22
|
-
/**
|
|
23
|
-
* Set partial store state (for local/browser state updates).
|
|
24
|
-
*/
|
|
25
|
-
readonly setState: (partial: Partial<TStore>) => void;
|
|
26
|
-
/**
|
|
27
|
-
* Dispatch another command.
|
|
28
|
-
* Returns the result of the dispatched command.
|
|
29
|
-
*
|
|
30
|
-
* @example
|
|
31
|
-
* dispatch(otherCommand)({ param: "value" });
|
|
32
|
-
*/
|
|
33
|
-
readonly dispatch: CommandDispatch<TStore>;
|
|
34
|
-
}
|
|
35
|
-
/**
|
|
36
|
-
* The function signature for a command handler.
|
|
37
|
-
*/
|
|
38
|
-
type CommandFn<TStore, TParams, TReturn> = (ctx: CommandContext<TStore>, params: TParams) => TReturn;
|
|
39
|
-
/**
|
|
40
|
-
* The function signature for an undoable command's revert handler.
|
|
41
|
-
* Receives the original params and the result from the forward execution.
|
|
42
|
-
*/
|
|
43
|
-
type RevertFn<TStore, TParams, TReturn> = (ctx: CommandContext<TStore>, params: TParams, result: TReturn) => void;
|
|
44
|
-
/**
|
|
45
|
-
* A command that can be dispatched to modify store state.
|
|
46
|
-
* Regular commands do not support undo/redo.
|
|
47
|
-
*/
|
|
48
|
-
interface Command<TStore, TParams, TReturn> {
|
|
49
|
-
readonly [COMMAND_SYMBOL]: true;
|
|
50
|
-
readonly fn: CommandFn<TStore, TParams, TReturn>;
|
|
51
|
-
}
|
|
52
|
-
/**
|
|
53
|
-
* An undoable command that supports undo/redo.
|
|
54
|
-
* Must provide a revert function that knows how to undo the change.
|
|
55
|
-
*/
|
|
56
|
-
interface UndoableCommand<TStore, TParams, TReturn> extends Command<TStore, TParams, TReturn> {
|
|
57
|
-
readonly [UNDOABLE_COMMAND_SYMBOL]: true;
|
|
58
|
-
readonly revert: RevertFn<TStore, TParams, TReturn>;
|
|
59
|
-
}
|
|
60
|
-
/**
|
|
61
|
-
* Any command type (regular or undoable).
|
|
62
|
-
*/
|
|
63
|
-
type AnyCommand = Command<any, any, any>;
|
|
64
|
-
/**
|
|
65
|
-
* Any undoable command type.
|
|
66
|
-
*/
|
|
67
|
-
type AnyUndoableCommand = UndoableCommand<any, any, any>;
|
|
68
|
-
/**
|
|
69
|
-
* Dispatch function that accepts commands and returns a function to call with params.
|
|
70
|
-
* Returns the result of the command execution.
|
|
71
|
-
*
|
|
72
|
-
* @example
|
|
73
|
-
* const result = dispatch(myCommand)({ param: "value" });
|
|
74
|
-
*/
|
|
75
|
-
type CommandDispatch<TStore> = <TParams, TReturn>(command: Command<TStore, TParams, TReturn>) => (params: TParams) => TReturn;
|
|
76
|
-
/**
|
|
77
|
-
* An entry in the undo/redo stack.
|
|
78
|
-
* Contains all information needed to revert or redo a command.
|
|
79
|
-
*/
|
|
80
|
-
interface UndoEntry<TParams = unknown, TReturn = unknown> {
|
|
81
|
-
/** The undoable command that was executed */
|
|
82
|
-
readonly command: AnyUndoableCommand;
|
|
83
|
-
/** The parameters that were passed to the command */
|
|
84
|
-
readonly params: TParams;
|
|
85
|
-
/** The result returned by the command (passed to revert) */
|
|
86
|
-
readonly result: TReturn;
|
|
87
|
-
/** Timestamp when the command was executed */
|
|
88
|
-
readonly timestamp: number;
|
|
89
|
-
}
|
|
90
|
-
/**
|
|
91
|
-
* State slice for undo/redo functionality.
|
|
92
|
-
*/
|
|
93
|
-
interface CommanderSlice {
|
|
94
|
-
readonly _commander: {
|
|
95
|
-
/** Stack of commands that can be undone */
|
|
96
|
-
readonly undoStack: ReadonlyArray<UndoEntry>;
|
|
97
|
-
/** Stack of commands that can be redone */
|
|
98
|
-
readonly redoStack: ReadonlyArray<UndoEntry>;
|
|
99
|
-
};
|
|
100
|
-
}
|
|
101
|
-
/**
|
|
102
|
-
* Options for creating a commander.
|
|
103
|
-
*/
|
|
104
|
-
interface CommanderOptions {
|
|
105
|
-
/**
|
|
106
|
-
* Maximum number of undo entries to keep.
|
|
107
|
-
* @default 100
|
|
108
|
-
*/
|
|
109
|
-
readonly maxUndoStackSize?: number;
|
|
110
|
-
}
|
|
111
|
-
/**
|
|
112
|
-
* A commander instance bound to a specific store type.
|
|
113
|
-
* Used to create commands and the middleware.
|
|
114
|
-
*/
|
|
115
|
-
interface Commander<TStore> {
|
|
116
|
-
/**
|
|
117
|
-
* Create a regular command (no undo support).
|
|
118
|
-
*
|
|
119
|
-
* @example
|
|
120
|
-
* // With params
|
|
121
|
-
* const addItem = commander.action<{ name: string }>(
|
|
122
|
-
* (ctx, params) => {
|
|
123
|
-
* // modify state using params.name
|
|
124
|
-
* }
|
|
125
|
-
* );
|
|
126
|
-
*
|
|
127
|
-
* // Without params
|
|
128
|
-
* const clearAll = commander.action((ctx) => {
|
|
129
|
-
* // modify state
|
|
130
|
-
* });
|
|
131
|
-
*/
|
|
132
|
-
readonly action: {
|
|
133
|
-
<TParams, TReturn = void>(fn: CommandFn<TStore, TParams, TReturn>): Command<TStore, TParams, TReturn>;
|
|
134
|
-
<TReturn = void>(fn: CommandFn<TStore, void, TReturn>): Command<TStore, void, TReturn>;
|
|
135
|
-
};
|
|
136
|
-
/**
|
|
137
|
-
* Create an undoable command with undo/redo support.
|
|
138
|
-
* The revert function is called when undoing the command.
|
|
139
|
-
*
|
|
140
|
-
* @example
|
|
141
|
-
* const moveItem = commander.undoableAction<{ id: string; toIndex: number }, { fromIndex: number }>(
|
|
142
|
-
* (ctx, params) => {
|
|
143
|
-
* const fromIndex = // get current index
|
|
144
|
-
* // perform move
|
|
145
|
-
* return { fromIndex }; // return data needed for revert
|
|
146
|
-
* },
|
|
147
|
-
* (ctx, params, result) => {
|
|
148
|
-
* // revert: move back to original position
|
|
149
|
-
* ctx.dispatch(moveItem)({ id: params.id, toIndex: result.fromIndex });
|
|
150
|
-
* }
|
|
151
|
-
* );
|
|
152
|
-
*/
|
|
153
|
-
readonly undoableAction: {
|
|
154
|
-
<TParams, TReturn>(fn: CommandFn<TStore, TParams, TReturn>, revert: RevertFn<TStore, TParams, TReturn>): UndoableCommand<TStore, TParams, TReturn>;
|
|
155
|
-
<TReturn>(fn: CommandFn<TStore, void, TReturn>, revert: RevertFn<TStore, void, TReturn>): UndoableCommand<TStore, void, TReturn>;
|
|
156
|
-
};
|
|
157
|
-
/**
|
|
158
|
-
* Zustand middleware that adds commander functionality.
|
|
159
|
-
* Adds undo/redo stacks to the store state.
|
|
160
|
-
*/
|
|
161
|
-
readonly middleware: CommanderMiddleware<TStore>;
|
|
162
|
-
}
|
|
163
|
-
/**
|
|
164
|
-
* Type for the commander middleware.
|
|
165
|
-
* Note: TStore is intentionally unused here to match the Commander interface signature.
|
|
166
|
-
* The middleware is generic over T (the inner store type) and adds CommanderSlice.
|
|
167
|
-
*/
|
|
168
|
-
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;
|
|
169
|
-
/**
|
|
170
|
-
* Extract the params type from a command.
|
|
171
|
-
*/
|
|
172
|
-
type CommandParams<T> = T extends Command<any, infer P, any> ? P : never;
|
|
173
|
-
/**
|
|
174
|
-
* Extract the return type from a command.
|
|
175
|
-
*/
|
|
176
|
-
type CommandReturn<T> = T extends Command<any, any, infer R> ? R : undefined;
|
|
177
|
-
/**
|
|
178
|
-
* Extract the store type from a command.
|
|
179
|
-
*/
|
|
180
|
-
type CommandStore<T> = T extends Command<infer S, any, any> ? S : never;
|
|
181
|
-
/**
|
|
182
|
-
* Type guard to check if a value is a Command.
|
|
183
|
-
*/
|
|
184
|
-
declare function isCommand(value: unknown): value is AnyCommand;
|
|
185
|
-
/**
|
|
186
|
-
* Type guard to check if a command is undoable.
|
|
187
|
-
*/
|
|
188
|
-
declare function isUndoableCommand(value: unknown): value is AnyUndoableCommand;
|
|
189
|
-
/**
|
|
190
|
-
* Helper type to extract the state type from a zustand store.
|
|
191
|
-
*/
|
|
192
|
-
type ExtractState<TStore> = TStore extends UseBoundStore<StoreApi<infer S>> ? S : TStore extends StoreApi<infer S> ? S : never;
|
|
193
|
-
//#endregion
|
|
194
|
-
//#region src/zustand-commander/commander.d.ts
|
|
195
|
-
/**
|
|
196
|
-
* Creates a commander instance bound to a specific store type.
|
|
197
|
-
*
|
|
198
|
-
* @example
|
|
199
|
-
* ```ts
|
|
200
|
-
* // Create commander for your store type
|
|
201
|
-
* const commander = createCommander<StoreState>();
|
|
202
|
-
*
|
|
203
|
-
* // Define commands
|
|
204
|
-
* const addItem = commander.action(
|
|
205
|
-
* Schema.Struct({ name: Schema.String }),
|
|
206
|
-
* (ctx, params) => {
|
|
207
|
-
* const { mimic } = ctx.getState();
|
|
208
|
-
* mimic.document.transaction(root => {
|
|
209
|
-
* // add item
|
|
210
|
-
* });
|
|
211
|
-
* }
|
|
212
|
-
* );
|
|
213
|
-
*
|
|
214
|
-
* // Create store with middleware
|
|
215
|
-
* const useStore = create(
|
|
216
|
-
* commander.middleware(
|
|
217
|
-
* mimic(document, (set, get) => ({
|
|
218
|
-
* // your state
|
|
219
|
-
* }))
|
|
220
|
-
* )
|
|
221
|
-
* );
|
|
222
|
-
* ```
|
|
223
|
-
*/
|
|
224
|
-
declare function createCommander<TStore extends object>(options?: CommanderOptions): Commander<TStore & CommanderSlice>;
|
|
225
|
-
/**
|
|
226
|
-
* Perform an undo operation on the store.
|
|
227
|
-
* Returns true if an undo was performed, false if undo stack was empty.
|
|
228
|
-
*/
|
|
229
|
-
declare function performUndo<TStore extends CommanderSlice>(storeApi: StoreApi<TStore>): boolean;
|
|
230
|
-
/**
|
|
231
|
-
* Perform a redo operation on the store.
|
|
232
|
-
* Returns true if a redo was performed, false if redo stack was empty.
|
|
233
|
-
*/
|
|
234
|
-
declare function performRedo<TStore extends CommanderSlice>(storeApi: StoreApi<TStore>): boolean;
|
|
235
|
-
/**
|
|
236
|
-
* Clear the undo and redo stacks.
|
|
237
|
-
*/
|
|
238
|
-
declare function clearUndoHistory<TStore extends CommanderSlice>(storeApi: StoreApi<TStore>): void;
|
|
239
|
-
//#endregion
|
|
240
|
-
//#region src/zustand-commander/hooks.d.ts
|
|
241
|
-
/**
|
|
242
|
-
* React hook to get a dispatch function for commands.
|
|
243
|
-
* The dispatch function executes commands and manages undo/redo state.
|
|
244
|
-
*
|
|
245
|
-
* @example
|
|
246
|
-
* ```tsx
|
|
247
|
-
* const dispatch = useCommander(useStore);
|
|
248
|
-
*
|
|
249
|
-
* const handleClick = () => {
|
|
250
|
-
* dispatch(addCard)({ columnId: "col-1", title: "New Card" });
|
|
251
|
-
* };
|
|
252
|
-
* ```
|
|
253
|
-
*/
|
|
254
|
-
declare function useCommander<TStore extends CommanderSlice>(store: UseBoundStore<StoreApi<TStore>>): CommandDispatch<TStore>;
|
|
255
|
-
/**
|
|
256
|
-
* State and actions for undo/redo functionality.
|
|
257
|
-
*/
|
|
258
|
-
interface UndoRedoState {
|
|
259
|
-
/** Whether there are actions that can be undone */
|
|
260
|
-
readonly canUndo: boolean;
|
|
261
|
-
/** Whether there are actions that can be redone */
|
|
262
|
-
readonly canRedo: boolean;
|
|
263
|
-
/** Number of items in the undo stack */
|
|
264
|
-
readonly undoCount: number;
|
|
265
|
-
/** Number of items in the redo stack */
|
|
266
|
-
readonly redoCount: number;
|
|
267
|
-
/** Undo the last action */
|
|
268
|
-
readonly undo: () => boolean;
|
|
269
|
-
/** Redo the last undone action */
|
|
270
|
-
readonly redo: () => boolean;
|
|
271
|
-
/** Clear the undo/redo history */
|
|
272
|
-
readonly clear: () => void;
|
|
273
|
-
}
|
|
274
|
-
/**
|
|
275
|
-
* React hook for undo/redo functionality.
|
|
276
|
-
* Provides state (canUndo, canRedo) and actions (undo, redo, clear).
|
|
277
|
-
*
|
|
278
|
-
* @example
|
|
279
|
-
* ```tsx
|
|
280
|
-
* const { canUndo, canRedo, undo, redo } = useUndoRedo(useStore);
|
|
281
|
-
*
|
|
282
|
-
* return (
|
|
283
|
-
* <>
|
|
284
|
-
* <button onClick={undo} disabled={!canUndo}>Undo</button>
|
|
285
|
-
* <button onClick={redo} disabled={!canRedo}>Redo</button>
|
|
286
|
-
* </>
|
|
287
|
-
* );
|
|
288
|
-
* ```
|
|
289
|
-
*/
|
|
290
|
-
declare function useUndoRedo<TStore extends CommanderSlice>(store: UseBoundStore<StoreApi<TStore>>): UndoRedoState;
|
|
291
|
-
/**
|
|
292
|
-
* Options for the keyboard shortcut hook.
|
|
293
|
-
*/
|
|
294
|
-
interface UseUndoRedoKeyboardOptions {
|
|
295
|
-
/** Enable Ctrl/Cmd+Z for undo (default: true) */
|
|
296
|
-
readonly enableUndo?: boolean;
|
|
297
|
-
/** Enable Ctrl/Cmd+Shift+Z or Ctrl+Y for redo (default: true) */
|
|
298
|
-
readonly enableRedo?: boolean;
|
|
299
|
-
}
|
|
300
|
-
/**
|
|
301
|
-
* React hook that adds keyboard shortcuts for undo/redo.
|
|
302
|
-
* Listens for Ctrl/Cmd+Z (undo) and Ctrl/Cmd+Shift+Z or Ctrl+Y (redo).
|
|
303
|
-
*
|
|
304
|
-
* @example
|
|
305
|
-
* ```tsx
|
|
306
|
-
* // In your app component
|
|
307
|
-
* useUndoRedoKeyboard(useStore);
|
|
308
|
-
* ```
|
|
309
|
-
*/
|
|
310
|
-
declare function useUndoRedoKeyboard<TStore extends CommanderSlice>(store: UseBoundStore<StoreApi<TStore>>, options?: UseUndoRedoKeyboardOptions): void;
|
|
311
|
-
//#endregion
|
|
312
|
-
export { type AnyCommand, 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 RevertFn, UNDOABLE_COMMAND_SYMBOL, type UndoEntry, type UndoRedoState, type UndoableCommand, type UseUndoRedoKeyboardOptions, clearUndoHistory, createCommander, isCommand, isUndoableCommand, performRedo, performUndo, useCommander, useUndoRedo, useUndoRedoKeyboard };
|
|
313
|
-
//# sourceMappingURL=index.d.cts.map
|
|
1
|
+
import { AnyCommand, AnyUndoableCommand, COMMAND_SYMBOL, Command, CommandContext, CommandDispatch, CommandFn, CommandParams, CommandReturn, CommandStore, Commander, CommanderMiddleware, CommanderOptions, CommanderSlice, ExtractState, RevertFn, UNDOABLE_COMMAND_SYMBOL, UndoEntry, UndoableCommand, isCommand, isUndoableCommand } from "./types.cjs";
|
|
2
|
+
import { clearUndoHistory, createCommander, performRedo, performUndo } from "./commander.cjs";
|
|
3
|
+
import { UndoRedoState, UseUndoRedoKeyboardOptions, useCommander, useUndoRedo, useUndoRedoKeyboard } from "./hooks.cjs";
|
|
4
|
+
export { type AnyCommand, 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 RevertFn, UNDOABLE_COMMAND_SYMBOL, type UndoEntry, type UndoRedoState, type UndoableCommand, type UseUndoRedoKeyboardOptions, clearUndoHistory, createCommander, isCommand, isUndoableCommand, performRedo, performUndo, useCommander, useUndoRedo, useUndoRedoKeyboard };
|
|
@@ -1,313 +1,4 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Symbol used to identify Command objects at runtime.
|
|
7
|
-
*/
|
|
8
|
-
declare const COMMAND_SYMBOL: unique symbol;
|
|
9
|
-
/**
|
|
10
|
-
* Symbol used to identify UndoableCommand objects at runtime.
|
|
11
|
-
*/
|
|
12
|
-
declare const UNDOABLE_COMMAND_SYMBOL: unique symbol;
|
|
13
|
-
/**
|
|
14
|
-
* Context provided to command functions.
|
|
15
|
-
* Gives access to store state and dispatch capabilities.
|
|
16
|
-
*/
|
|
17
|
-
interface CommandContext<TStore> {
|
|
18
|
-
/**
|
|
19
|
-
* Get the current store state.
|
|
20
|
-
*/
|
|
21
|
-
readonly getState: () => TStore;
|
|
22
|
-
/**
|
|
23
|
-
* Set partial store state (for local/browser state updates).
|
|
24
|
-
*/
|
|
25
|
-
readonly setState: (partial: Partial<TStore>) => void;
|
|
26
|
-
/**
|
|
27
|
-
* Dispatch another command.
|
|
28
|
-
* Returns the result of the dispatched command.
|
|
29
|
-
*
|
|
30
|
-
* @example
|
|
31
|
-
* dispatch(otherCommand)({ param: "value" });
|
|
32
|
-
*/
|
|
33
|
-
readonly dispatch: CommandDispatch<TStore>;
|
|
34
|
-
}
|
|
35
|
-
/**
|
|
36
|
-
* The function signature for a command handler.
|
|
37
|
-
*/
|
|
38
|
-
type CommandFn<TStore, TParams, TReturn> = (ctx: CommandContext<TStore>, params: TParams) => TReturn;
|
|
39
|
-
/**
|
|
40
|
-
* The function signature for an undoable command's revert handler.
|
|
41
|
-
* Receives the original params and the result from the forward execution.
|
|
42
|
-
*/
|
|
43
|
-
type RevertFn<TStore, TParams, TReturn> = (ctx: CommandContext<TStore>, params: TParams, result: TReturn) => void;
|
|
44
|
-
/**
|
|
45
|
-
* A command that can be dispatched to modify store state.
|
|
46
|
-
* Regular commands do not support undo/redo.
|
|
47
|
-
*/
|
|
48
|
-
interface Command<TStore, TParams, TReturn> {
|
|
49
|
-
readonly [COMMAND_SYMBOL]: true;
|
|
50
|
-
readonly fn: CommandFn<TStore, TParams, TReturn>;
|
|
51
|
-
}
|
|
52
|
-
/**
|
|
53
|
-
* An undoable command that supports undo/redo.
|
|
54
|
-
* Must provide a revert function that knows how to undo the change.
|
|
55
|
-
*/
|
|
56
|
-
interface UndoableCommand<TStore, TParams, TReturn> extends Command<TStore, TParams, TReturn> {
|
|
57
|
-
readonly [UNDOABLE_COMMAND_SYMBOL]: true;
|
|
58
|
-
readonly revert: RevertFn<TStore, TParams, TReturn>;
|
|
59
|
-
}
|
|
60
|
-
/**
|
|
61
|
-
* Any command type (regular or undoable).
|
|
62
|
-
*/
|
|
63
|
-
type AnyCommand = Command<any, any, any>;
|
|
64
|
-
/**
|
|
65
|
-
* Any undoable command type.
|
|
66
|
-
*/
|
|
67
|
-
type AnyUndoableCommand = UndoableCommand<any, any, any>;
|
|
68
|
-
/**
|
|
69
|
-
* Dispatch function that accepts commands and returns a function to call with params.
|
|
70
|
-
* Returns the result of the command execution.
|
|
71
|
-
*
|
|
72
|
-
* @example
|
|
73
|
-
* const result = dispatch(myCommand)({ param: "value" });
|
|
74
|
-
*/
|
|
75
|
-
type CommandDispatch<TStore> = <TParams, TReturn>(command: Command<TStore, TParams, TReturn>) => (params: TParams) => TReturn;
|
|
76
|
-
/**
|
|
77
|
-
* An entry in the undo/redo stack.
|
|
78
|
-
* Contains all information needed to revert or redo a command.
|
|
79
|
-
*/
|
|
80
|
-
interface UndoEntry<TParams = unknown, TReturn = unknown> {
|
|
81
|
-
/** The undoable command that was executed */
|
|
82
|
-
readonly command: AnyUndoableCommand;
|
|
83
|
-
/** The parameters that were passed to the command */
|
|
84
|
-
readonly params: TParams;
|
|
85
|
-
/** The result returned by the command (passed to revert) */
|
|
86
|
-
readonly result: TReturn;
|
|
87
|
-
/** Timestamp when the command was executed */
|
|
88
|
-
readonly timestamp: number;
|
|
89
|
-
}
|
|
90
|
-
/**
|
|
91
|
-
* State slice for undo/redo functionality.
|
|
92
|
-
*/
|
|
93
|
-
interface CommanderSlice {
|
|
94
|
-
readonly _commander: {
|
|
95
|
-
/** Stack of commands that can be undone */
|
|
96
|
-
readonly undoStack: ReadonlyArray<UndoEntry>;
|
|
97
|
-
/** Stack of commands that can be redone */
|
|
98
|
-
readonly redoStack: ReadonlyArray<UndoEntry>;
|
|
99
|
-
};
|
|
100
|
-
}
|
|
101
|
-
/**
|
|
102
|
-
* Options for creating a commander.
|
|
103
|
-
*/
|
|
104
|
-
interface CommanderOptions {
|
|
105
|
-
/**
|
|
106
|
-
* Maximum number of undo entries to keep.
|
|
107
|
-
* @default 100
|
|
108
|
-
*/
|
|
109
|
-
readonly maxUndoStackSize?: number;
|
|
110
|
-
}
|
|
111
|
-
/**
|
|
112
|
-
* A commander instance bound to a specific store type.
|
|
113
|
-
* Used to create commands and the middleware.
|
|
114
|
-
*/
|
|
115
|
-
interface Commander<TStore> {
|
|
116
|
-
/**
|
|
117
|
-
* Create a regular command (no undo support).
|
|
118
|
-
*
|
|
119
|
-
* @example
|
|
120
|
-
* // With params
|
|
121
|
-
* const addItem = commander.action<{ name: string }>(
|
|
122
|
-
* (ctx, params) => {
|
|
123
|
-
* // modify state using params.name
|
|
124
|
-
* }
|
|
125
|
-
* );
|
|
126
|
-
*
|
|
127
|
-
* // Without params
|
|
128
|
-
* const clearAll = commander.action((ctx) => {
|
|
129
|
-
* // modify state
|
|
130
|
-
* });
|
|
131
|
-
*/
|
|
132
|
-
readonly action: {
|
|
133
|
-
<TParams, TReturn = void>(fn: CommandFn<TStore, TParams, TReturn>): Command<TStore, TParams, TReturn>;
|
|
134
|
-
<TReturn = void>(fn: CommandFn<TStore, void, TReturn>): Command<TStore, void, TReturn>;
|
|
135
|
-
};
|
|
136
|
-
/**
|
|
137
|
-
* Create an undoable command with undo/redo support.
|
|
138
|
-
* The revert function is called when undoing the command.
|
|
139
|
-
*
|
|
140
|
-
* @example
|
|
141
|
-
* const moveItem = commander.undoableAction<{ id: string; toIndex: number }, { fromIndex: number }>(
|
|
142
|
-
* (ctx, params) => {
|
|
143
|
-
* const fromIndex = // get current index
|
|
144
|
-
* // perform move
|
|
145
|
-
* return { fromIndex }; // return data needed for revert
|
|
146
|
-
* },
|
|
147
|
-
* (ctx, params, result) => {
|
|
148
|
-
* // revert: move back to original position
|
|
149
|
-
* ctx.dispatch(moveItem)({ id: params.id, toIndex: result.fromIndex });
|
|
150
|
-
* }
|
|
151
|
-
* );
|
|
152
|
-
*/
|
|
153
|
-
readonly undoableAction: {
|
|
154
|
-
<TParams, TReturn>(fn: CommandFn<TStore, TParams, TReturn>, revert: RevertFn<TStore, TParams, TReturn>): UndoableCommand<TStore, TParams, TReturn>;
|
|
155
|
-
<TReturn>(fn: CommandFn<TStore, void, TReturn>, revert: RevertFn<TStore, void, TReturn>): UndoableCommand<TStore, void, TReturn>;
|
|
156
|
-
};
|
|
157
|
-
/**
|
|
158
|
-
* Zustand middleware that adds commander functionality.
|
|
159
|
-
* Adds undo/redo stacks to the store state.
|
|
160
|
-
*/
|
|
161
|
-
readonly middleware: CommanderMiddleware<TStore>;
|
|
162
|
-
}
|
|
163
|
-
/**
|
|
164
|
-
* Type for the commander middleware.
|
|
165
|
-
* Note: TStore is intentionally unused here to match the Commander interface signature.
|
|
166
|
-
* The middleware is generic over T (the inner store type) and adds CommanderSlice.
|
|
167
|
-
*/
|
|
168
|
-
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;
|
|
169
|
-
/**
|
|
170
|
-
* Extract the params type from a command.
|
|
171
|
-
*/
|
|
172
|
-
type CommandParams<T> = T extends Command<any, infer P, any> ? P : never;
|
|
173
|
-
/**
|
|
174
|
-
* Extract the return type from a command.
|
|
175
|
-
*/
|
|
176
|
-
type CommandReturn<T> = T extends Command<any, any, infer R> ? R : undefined;
|
|
177
|
-
/**
|
|
178
|
-
* Extract the store type from a command.
|
|
179
|
-
*/
|
|
180
|
-
type CommandStore<T> = T extends Command<infer S, any, any> ? S : never;
|
|
181
|
-
/**
|
|
182
|
-
* Type guard to check if a value is a Command.
|
|
183
|
-
*/
|
|
184
|
-
declare function isCommand(value: unknown): value is AnyCommand;
|
|
185
|
-
/**
|
|
186
|
-
* Type guard to check if a command is undoable.
|
|
187
|
-
*/
|
|
188
|
-
declare function isUndoableCommand(value: unknown): value is AnyUndoableCommand;
|
|
189
|
-
/**
|
|
190
|
-
* Helper type to extract the state type from a zustand store.
|
|
191
|
-
*/
|
|
192
|
-
type ExtractState<TStore> = TStore extends UseBoundStore<StoreApi<infer S>> ? S : TStore extends StoreApi<infer S> ? S : never;
|
|
193
|
-
//#endregion
|
|
194
|
-
//#region src/zustand-commander/commander.d.ts
|
|
195
|
-
/**
|
|
196
|
-
* Creates a commander instance bound to a specific store type.
|
|
197
|
-
*
|
|
198
|
-
* @example
|
|
199
|
-
* ```ts
|
|
200
|
-
* // Create commander for your store type
|
|
201
|
-
* const commander = createCommander<StoreState>();
|
|
202
|
-
*
|
|
203
|
-
* // Define commands
|
|
204
|
-
* const addItem = commander.action(
|
|
205
|
-
* Schema.Struct({ name: Schema.String }),
|
|
206
|
-
* (ctx, params) => {
|
|
207
|
-
* const { mimic } = ctx.getState();
|
|
208
|
-
* mimic.document.transaction(root => {
|
|
209
|
-
* // add item
|
|
210
|
-
* });
|
|
211
|
-
* }
|
|
212
|
-
* );
|
|
213
|
-
*
|
|
214
|
-
* // Create store with middleware
|
|
215
|
-
* const useStore = create(
|
|
216
|
-
* commander.middleware(
|
|
217
|
-
* mimic(document, (set, get) => ({
|
|
218
|
-
* // your state
|
|
219
|
-
* }))
|
|
220
|
-
* )
|
|
221
|
-
* );
|
|
222
|
-
* ```
|
|
223
|
-
*/
|
|
224
|
-
declare function createCommander<TStore extends object>(options?: CommanderOptions): Commander<TStore & CommanderSlice>;
|
|
225
|
-
/**
|
|
226
|
-
* Perform an undo operation on the store.
|
|
227
|
-
* Returns true if an undo was performed, false if undo stack was empty.
|
|
228
|
-
*/
|
|
229
|
-
declare function performUndo<TStore extends CommanderSlice>(storeApi: StoreApi<TStore>): boolean;
|
|
230
|
-
/**
|
|
231
|
-
* Perform a redo operation on the store.
|
|
232
|
-
* Returns true if a redo was performed, false if redo stack was empty.
|
|
233
|
-
*/
|
|
234
|
-
declare function performRedo<TStore extends CommanderSlice>(storeApi: StoreApi<TStore>): boolean;
|
|
235
|
-
/**
|
|
236
|
-
* Clear the undo and redo stacks.
|
|
237
|
-
*/
|
|
238
|
-
declare function clearUndoHistory<TStore extends CommanderSlice>(storeApi: StoreApi<TStore>): void;
|
|
239
|
-
//#endregion
|
|
240
|
-
//#region src/zustand-commander/hooks.d.ts
|
|
241
|
-
/**
|
|
242
|
-
* React hook to get a dispatch function for commands.
|
|
243
|
-
* The dispatch function executes commands and manages undo/redo state.
|
|
244
|
-
*
|
|
245
|
-
* @example
|
|
246
|
-
* ```tsx
|
|
247
|
-
* const dispatch = useCommander(useStore);
|
|
248
|
-
*
|
|
249
|
-
* const handleClick = () => {
|
|
250
|
-
* dispatch(addCard)({ columnId: "col-1", title: "New Card" });
|
|
251
|
-
* };
|
|
252
|
-
* ```
|
|
253
|
-
*/
|
|
254
|
-
declare function useCommander<TStore extends CommanderSlice>(store: UseBoundStore<StoreApi<TStore>>): CommandDispatch<TStore>;
|
|
255
|
-
/**
|
|
256
|
-
* State and actions for undo/redo functionality.
|
|
257
|
-
*/
|
|
258
|
-
interface UndoRedoState {
|
|
259
|
-
/** Whether there are actions that can be undone */
|
|
260
|
-
readonly canUndo: boolean;
|
|
261
|
-
/** Whether there are actions that can be redone */
|
|
262
|
-
readonly canRedo: boolean;
|
|
263
|
-
/** Number of items in the undo stack */
|
|
264
|
-
readonly undoCount: number;
|
|
265
|
-
/** Number of items in the redo stack */
|
|
266
|
-
readonly redoCount: number;
|
|
267
|
-
/** Undo the last action */
|
|
268
|
-
readonly undo: () => boolean;
|
|
269
|
-
/** Redo the last undone action */
|
|
270
|
-
readonly redo: () => boolean;
|
|
271
|
-
/** Clear the undo/redo history */
|
|
272
|
-
readonly clear: () => void;
|
|
273
|
-
}
|
|
274
|
-
/**
|
|
275
|
-
* React hook for undo/redo functionality.
|
|
276
|
-
* Provides state (canUndo, canRedo) and actions (undo, redo, clear).
|
|
277
|
-
*
|
|
278
|
-
* @example
|
|
279
|
-
* ```tsx
|
|
280
|
-
* const { canUndo, canRedo, undo, redo } = useUndoRedo(useStore);
|
|
281
|
-
*
|
|
282
|
-
* return (
|
|
283
|
-
* <>
|
|
284
|
-
* <button onClick={undo} disabled={!canUndo}>Undo</button>
|
|
285
|
-
* <button onClick={redo} disabled={!canRedo}>Redo</button>
|
|
286
|
-
* </>
|
|
287
|
-
* );
|
|
288
|
-
* ```
|
|
289
|
-
*/
|
|
290
|
-
declare function useUndoRedo<TStore extends CommanderSlice>(store: UseBoundStore<StoreApi<TStore>>): UndoRedoState;
|
|
291
|
-
/**
|
|
292
|
-
* Options for the keyboard shortcut hook.
|
|
293
|
-
*/
|
|
294
|
-
interface UseUndoRedoKeyboardOptions {
|
|
295
|
-
/** Enable Ctrl/Cmd+Z for undo (default: true) */
|
|
296
|
-
readonly enableUndo?: boolean;
|
|
297
|
-
/** Enable Ctrl/Cmd+Shift+Z or Ctrl+Y for redo (default: true) */
|
|
298
|
-
readonly enableRedo?: boolean;
|
|
299
|
-
}
|
|
300
|
-
/**
|
|
301
|
-
* React hook that adds keyboard shortcuts for undo/redo.
|
|
302
|
-
* Listens for Ctrl/Cmd+Z (undo) and Ctrl/Cmd+Shift+Z or Ctrl+Y (redo).
|
|
303
|
-
*
|
|
304
|
-
* @example
|
|
305
|
-
* ```tsx
|
|
306
|
-
* // In your app component
|
|
307
|
-
* useUndoRedoKeyboard(useStore);
|
|
308
|
-
* ```
|
|
309
|
-
*/
|
|
310
|
-
declare function useUndoRedoKeyboard<TStore extends CommanderSlice>(store: UseBoundStore<StoreApi<TStore>>, options?: UseUndoRedoKeyboardOptions): void;
|
|
311
|
-
//#endregion
|
|
312
|
-
export { type AnyCommand, 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 RevertFn, UNDOABLE_COMMAND_SYMBOL, type UndoEntry, type UndoRedoState, type UndoableCommand, type UseUndoRedoKeyboardOptions, clearUndoHistory, createCommander, isCommand, isUndoableCommand, performRedo, performUndo, useCommander, useUndoRedo, useUndoRedoKeyboard };
|
|
313
|
-
//# sourceMappingURL=index.d.mts.map
|
|
1
|
+
import { AnyCommand, AnyUndoableCommand, COMMAND_SYMBOL, Command, CommandContext, CommandDispatch, CommandFn, CommandParams, CommandReturn, CommandStore, Commander, CommanderMiddleware, CommanderOptions, CommanderSlice, ExtractState, RevertFn, UNDOABLE_COMMAND_SYMBOL, UndoEntry, UndoableCommand, isCommand, isUndoableCommand } from "./types.mjs";
|
|
2
|
+
import { clearUndoHistory, createCommander, performRedo, performUndo } from "./commander.mjs";
|
|
3
|
+
import { UndoRedoState, UseUndoRedoKeyboardOptions, useCommander, useUndoRedo, useUndoRedoKeyboard } from "./hooks.mjs";
|
|
4
|
+
export { type AnyCommand, 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 RevertFn, UNDOABLE_COMMAND_SYMBOL, type UndoEntry, type UndoRedoState, type UndoableCommand, type UseUndoRedoKeyboardOptions, clearUndoHistory, createCommander, isCommand, isUndoableCommand, performRedo, performUndo, useCommander, useUndoRedo, useUndoRedoKeyboard };
|