@tooee/commands 0.12.0 → 0.14.0
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/dist/build-context.d.ts +5 -0
- package/dist/build-context.d.ts.map +1 -1
- package/dist/build-context.js +4 -3
- package/dist/build-context.js.map +1 -1
- package/dist/command-store-wrapper.d.ts.map +1 -1
- package/dist/command-store-wrapper.js.map +1 -1
- package/dist/command-store.d.ts.map +1 -1
- package/dist/command-store.js.map +1 -1
- package/dist/context.d.ts +20 -3
- package/dist/context.d.ts.map +1 -1
- package/dist/context.js +68 -34
- package/dist/context.js.map +1 -1
- package/dist/hooks/use-latest.d.ts.map +1 -1
- package/dist/hooks/use-latest.js.map +1 -1
- package/dist/hooks/use-lazy-ref.d.ts.map +1 -1
- package/dist/hooks/use-lazy-ref.js.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/key-dispatch.d.ts.map +1 -1
- package/dist/key-dispatch.js.map +1 -1
- package/dist/match.d.ts.map +1 -1
- package/dist/match.js.map +1 -1
- package/dist/mode.d.ts.map +1 -1
- package/dist/mode.js.map +1 -1
- package/dist/parse.d.ts.map +1 -1
- package/dist/parse.js +17 -2
- package/dist/parse.js.map +1 -1
- package/dist/screen-focus.d.ts.map +1 -1
- package/dist/screen-focus.js.map +1 -1
- package/dist/sequence.d.ts.map +1 -1
- package/dist/sequence.js.map +1 -1
- package/dist/store.d.ts.map +1 -1
- package/dist/store.js.map +1 -1
- package/dist/types.d.ts +1 -0
- package/dist/types.d.ts.map +1 -1
- package/dist/use-actions.d.ts.map +1 -1
- package/dist/use-actions.js.map +1 -1
- package/dist/use-command.d.ts.map +1 -1
- package/dist/use-command.js.map +1 -1
- package/package.json +1 -1
- package/src/build-context.ts +12 -3
- package/src/command-store-wrapper.ts +14 -0
- package/src/command-store.ts +37 -0
- package/src/context.tsx +138 -36
- package/src/hooks/use-latest.ts +1 -0
- package/src/hooks/use-lazy-ref.ts +1 -0
- package/src/index.ts +18 -0
- package/src/key-dispatch.ts +35 -0
- package/src/match.ts +6 -0
- package/src/mode.tsx +5 -0
- package/src/parse.ts +36 -2
- package/src/screen-focus.tsx +2 -0
- package/src/sequence.ts +11 -0
- package/src/store.ts +7 -0
- package/src/types.ts +2 -0
- package/src/use-actions.ts +4 -0
- package/src/use-command.ts +3 -0
package/src/context.tsx
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { useKeyboard } from "@opentui/react";
|
|
1
|
+
import { useKeyboard, useRenderer } from "@opentui/react";
|
|
2
2
|
import { useSelector } from "@xstate/store-react";
|
|
3
3
|
import { createContext, useContext, useCallback, useEffect, useMemo } from "react";
|
|
4
4
|
import type { ReactNode } from "react";
|
|
@@ -44,10 +44,14 @@ interface CommandStoreContextValue {
|
|
|
44
44
|
/** The surface record `useCommand` registrations under this subtree target. */
|
|
45
45
|
surface: SurfaceRecord;
|
|
46
46
|
leaderKey?: string;
|
|
47
|
+
/** User key remaps by command id, from the provider's `keymap` prop. */
|
|
48
|
+
keymap?: Record<string, string>;
|
|
47
49
|
}
|
|
48
50
|
|
|
49
51
|
const CommandStoreReactContext = createContext<CommandStoreContextValue | null>(null);
|
|
52
|
+
|
|
50
53
|
const CommandSequenceContext = createContext<CommandSequenceState | null>(null);
|
|
54
|
+
|
|
51
55
|
/** Nesting depth of the nearest command surface (0 at the root app). */
|
|
52
56
|
const CommandSurfaceDepthContext = createContext(0);
|
|
53
57
|
|
|
@@ -123,6 +127,7 @@ export const CommandProvider = function CommandProvider({
|
|
|
123
127
|
sequenceTimeoutMs,
|
|
124
128
|
})
|
|
125
129
|
);
|
|
130
|
+
|
|
126
131
|
const commandStore = storeRef.current;
|
|
127
132
|
|
|
128
133
|
// Root mode changes are transitions: reset any pending chord synchronously
|
|
@@ -149,7 +154,6 @@ export const CommandProvider = function CommandProvider({
|
|
|
149
154
|
};
|
|
150
155
|
|
|
151
156
|
// preserve the top-down provider/dispatcher organization.
|
|
152
|
-
// oxlint-disable-next-line no-use-before-define -- provider deliberately renders its dispatcher below
|
|
153
157
|
const CommandDispatcher = function CommandDispatcher({
|
|
154
158
|
children,
|
|
155
159
|
commandStore,
|
|
@@ -168,16 +172,20 @@ const CommandDispatcher = function CommandDispatcher({
|
|
|
168
172
|
const mode = useMode();
|
|
169
173
|
const modeRef = useLatest(mode);
|
|
170
174
|
const setMode = useSetMode();
|
|
175
|
+
const renderer = useRenderer();
|
|
171
176
|
|
|
172
177
|
const buildCtx = useCallback(
|
|
173
178
|
(): CommandContext =>
|
|
174
179
|
buildCommandContext({
|
|
175
180
|
commands: commandsFromRegistry(commandStore.registryFor(commandStore.rootRecord)),
|
|
176
181
|
contributions: commandStore.store.getSnapshot().context.contextSources.values(),
|
|
182
|
+
exit: () => {
|
|
183
|
+
renderer.destroy();
|
|
184
|
+
},
|
|
177
185
|
mode: modeRef.current,
|
|
178
186
|
setMode,
|
|
179
187
|
}),
|
|
180
|
-
[commandStore, modeRef, setMode]
|
|
188
|
+
[commandStore, modeRef, renderer, setMode]
|
|
181
189
|
);
|
|
182
190
|
|
|
183
191
|
const buildCtxRef = useLatest(buildCtx);
|
|
@@ -192,7 +200,9 @@ const CommandDispatcher = function CommandDispatcher({
|
|
|
192
200
|
if (event.defaultPrevented) {
|
|
193
201
|
return;
|
|
194
202
|
}
|
|
203
|
+
|
|
195
204
|
const result = commandStore.key(event);
|
|
205
|
+
|
|
196
206
|
if (result.handled) {
|
|
197
207
|
event.preventDefault();
|
|
198
208
|
result.invoke?.();
|
|
@@ -213,10 +223,11 @@ const CommandDispatcher = function CommandDispatcher({
|
|
|
213
223
|
const ctxValue = useMemo<CommandStoreContextValue>(
|
|
214
224
|
() => ({
|
|
215
225
|
commandStore,
|
|
226
|
+
keymap,
|
|
216
227
|
leaderKey: leader,
|
|
217
228
|
surface: commandStore.rootRecord,
|
|
218
229
|
}),
|
|
219
|
-
[commandStore, leader]
|
|
230
|
+
[commandStore, keymap, leader]
|
|
220
231
|
);
|
|
221
232
|
|
|
222
233
|
return (
|
|
@@ -258,9 +269,11 @@ export const CommandSurfaceProvider = function CommandSurfaceProvider({
|
|
|
258
269
|
initialMode = "cursor",
|
|
259
270
|
}: CommandSurfaceProviderProps): ReactNode {
|
|
260
271
|
const parent = useContext(CommandStoreReactContext);
|
|
272
|
+
|
|
261
273
|
if (!parent) {
|
|
262
274
|
throw new Error("CommandSurfaceProvider must be used within a CommandProvider");
|
|
263
275
|
}
|
|
276
|
+
|
|
264
277
|
const { commandStore } = parent;
|
|
265
278
|
|
|
266
279
|
// Surface-local mode changes are transitions too (F-08): a mid-chord mode
|
|
@@ -293,9 +306,11 @@ const CommandSurfaceInner = function CommandSurfaceInner({
|
|
|
293
306
|
groupId?: string;
|
|
294
307
|
}): ReactNode {
|
|
295
308
|
const parent = useContext(CommandStoreReactContext);
|
|
309
|
+
|
|
296
310
|
if (!parent) {
|
|
297
311
|
throw new Error("CommandSurfaceProvider must be used within a CommandProvider");
|
|
298
312
|
}
|
|
313
|
+
|
|
299
314
|
const { commandStore } = parent;
|
|
300
315
|
|
|
301
316
|
const parentDepth = useContext(CommandSurfaceDepthContext);
|
|
@@ -304,6 +319,7 @@ const CommandSurfaceInner = function CommandSurfaceInner({
|
|
|
304
319
|
const mode = useMode();
|
|
305
320
|
const setMode = useSetMode();
|
|
306
321
|
const modeRef = useLatest(mode);
|
|
322
|
+
const renderer = useRenderer();
|
|
307
323
|
|
|
308
324
|
const recordRef = useLazyRef<SurfaceRecord>(() => ({
|
|
309
325
|
// oxlint-disable-next-line no-use-before-define -- buildCtxRef is initialized before this callback can run
|
|
@@ -318,9 +334,13 @@ const CommandSurfaceInner = function CommandSurfaceInner({
|
|
|
318
334
|
|
|
319
335
|
const buildCtx = (): CommandContext => {
|
|
320
336
|
const registry = commandStore.registryFor(recordRef.current);
|
|
337
|
+
|
|
321
338
|
return buildCommandContext({
|
|
322
339
|
commands: commandsFromRegistry(registry),
|
|
323
340
|
contributions: commandStore.store.getSnapshot().context.contextSources.values(),
|
|
341
|
+
exit: () => {
|
|
342
|
+
renderer.destroy();
|
|
343
|
+
},
|
|
324
344
|
mode: modeRef.current,
|
|
325
345
|
setMode,
|
|
326
346
|
});
|
|
@@ -344,6 +364,7 @@ const CommandSurfaceInner = function CommandSurfaceInner({
|
|
|
344
364
|
role,
|
|
345
365
|
};
|
|
346
366
|
}
|
|
367
|
+
|
|
347
368
|
const record = recordRef.current;
|
|
348
369
|
|
|
349
370
|
// Mount/unmount registration is a keep-effect: it synchronizes the React
|
|
@@ -353,10 +374,11 @@ const CommandSurfaceInner = function CommandSurfaceInner({
|
|
|
353
374
|
const ctxValue = useMemo<CommandStoreContextValue>(
|
|
354
375
|
() => ({
|
|
355
376
|
commandStore,
|
|
377
|
+
keymap: parent.keymap,
|
|
356
378
|
leaderKey: parent.leaderKey,
|
|
357
379
|
surface: record,
|
|
358
380
|
}),
|
|
359
|
-
[commandStore, record, parent.leaderKey]
|
|
381
|
+
[commandStore, record, parent.keymap, parent.leaderKey]
|
|
360
382
|
);
|
|
361
383
|
|
|
362
384
|
return (
|
|
@@ -373,9 +395,11 @@ export const useSurfaceInvoke = function useSurfaceInvoke(): {
|
|
|
373
395
|
invoke: (id: string) => void;
|
|
374
396
|
} {
|
|
375
397
|
const ctx = useContext(CommandStoreReactContext);
|
|
398
|
+
|
|
376
399
|
if (!ctx) {
|
|
377
400
|
throw new Error("useSurfaceInvoke must be used within a CommandProvider");
|
|
378
401
|
}
|
|
402
|
+
|
|
379
403
|
const { commandStore, surface } = ctx;
|
|
380
404
|
|
|
381
405
|
// Reactive: consumers re-render when a command registers or unregisters on
|
|
@@ -383,6 +407,7 @@ export const useSurfaceInvoke = function useSurfaceInvoke(): {
|
|
|
383
407
|
const commandMap = useSelector(commandStore.store, (s) =>
|
|
384
408
|
selectSurfaceCommandMap(s.context, surface.id)
|
|
385
409
|
);
|
|
410
|
+
|
|
386
411
|
const registry = commandStore.registryFor(surface);
|
|
387
412
|
|
|
388
413
|
return useMemo(
|
|
@@ -394,6 +419,15 @@ export const useSurfaceInvoke = function useSurfaceInvoke(): {
|
|
|
394
419
|
);
|
|
395
420
|
};
|
|
396
421
|
|
|
422
|
+
/**
|
|
423
|
+
* Whether a `CommandProvider` is above this component. Packages that work with
|
|
424
|
+
* or without commands (for example the router) use this to decide whether to
|
|
425
|
+
* mount a component that registers commands.
|
|
426
|
+
*/
|
|
427
|
+
export const useHasCommandProvider = function useHasCommandProvider(): boolean {
|
|
428
|
+
return useContext(CommandStoreReactContext) !== null;
|
|
429
|
+
};
|
|
430
|
+
|
|
397
431
|
/**
|
|
398
432
|
* Builds the live command context of the nearest surface — the same value
|
|
399
433
|
* command handlers receive. For callers that must hand a context to something
|
|
@@ -401,10 +435,13 @@ export const useSurfaceInvoke = function useSurfaceInvoke(): {
|
|
|
401
435
|
*/
|
|
402
436
|
export const useBuildCommandContext = function useBuildCommandContext(): () => CommandContext {
|
|
403
437
|
const ctx = useContext(CommandStoreReactContext);
|
|
438
|
+
|
|
404
439
|
if (!ctx) {
|
|
405
440
|
throw new Error("useBuildCommandContext must be used within a CommandProvider");
|
|
406
441
|
}
|
|
442
|
+
|
|
407
443
|
const { surface } = ctx;
|
|
444
|
+
|
|
408
445
|
return useCallback(() => surface.buildCtx(), [surface]);
|
|
409
446
|
};
|
|
410
447
|
|
|
@@ -416,17 +453,21 @@ export const useBuildCommandContext = function useBuildCommandContext(): () => C
|
|
|
416
453
|
*/
|
|
417
454
|
export const useSurfaceRegistry = function useSurfaceRegistry(): CommandRegistry {
|
|
418
455
|
const ctx = useContext(CommandStoreReactContext);
|
|
456
|
+
|
|
419
457
|
if (!ctx) {
|
|
420
458
|
throw new Error("useSurfaceRegistry must be used within a CommandProvider");
|
|
421
459
|
}
|
|
460
|
+
|
|
422
461
|
return ctx.commandStore.registryFor(ctx.surface);
|
|
423
462
|
};
|
|
424
463
|
|
|
425
464
|
export const useCommandRegistry = function useCommandRegistry(): CommandContextValue {
|
|
426
465
|
const ctx = useContext(CommandStoreReactContext);
|
|
466
|
+
|
|
427
467
|
if (!ctx) {
|
|
428
468
|
throw new Error("useCommandRegistry must be used within a CommandProvider");
|
|
429
469
|
}
|
|
470
|
+
|
|
430
471
|
const { commandStore, surface, leaderKey } = ctx;
|
|
431
472
|
|
|
432
473
|
// Subscribe to the slices so captured maps stay current across renders (the
|
|
@@ -459,9 +500,11 @@ export const useCommandSequenceState =
|
|
|
459
500
|
*/
|
|
460
501
|
export const useCommandSurfaceId = function useCommandSurfaceId(): string {
|
|
461
502
|
const ctx = useContext(CommandStoreReactContext);
|
|
503
|
+
|
|
462
504
|
if (!ctx) {
|
|
463
505
|
throw new Error("useCommandSurfaceId must be used within a CommandProvider");
|
|
464
506
|
}
|
|
507
|
+
|
|
465
508
|
return ctx.surface.id;
|
|
466
509
|
};
|
|
467
510
|
|
|
@@ -478,8 +521,10 @@ export const useActiveCommandSurface =
|
|
|
478
521
|
const { store } = ctx?.commandStore ?? FALLBACK_COMMAND_STORE;
|
|
479
522
|
|
|
480
523
|
const record = useSelector(store, (s) => selectKeyboardOwnerSurface(s.context));
|
|
524
|
+
|
|
481
525
|
const commandMap = useSelector(store, (s) => {
|
|
482
526
|
const active = selectKeyboardOwnerSurface(s.context);
|
|
527
|
+
|
|
483
528
|
return active ? selectSurfaceCommandMap(s.context, active.id) : undefined;
|
|
484
529
|
});
|
|
485
530
|
|
|
@@ -490,6 +535,7 @@ export const useActiveCommandSurface =
|
|
|
490
535
|
if (!record || record.role === "root") {
|
|
491
536
|
return null;
|
|
492
537
|
}
|
|
538
|
+
|
|
493
539
|
return {
|
|
494
540
|
commands: commandMap ? [...commandMap.values()] : [],
|
|
495
541
|
id: record.id,
|
|
@@ -511,6 +557,7 @@ export const useSurfaceCommands = function useSurfaceCommands(
|
|
|
511
557
|
|
|
512
558
|
const commandMap = useSelector(store, (s) => {
|
|
513
559
|
const id = surfaceId ?? selectKeyboardOwnerSurface(s.context)?.id ?? ROOT_SURFACE_ID;
|
|
560
|
+
|
|
514
561
|
return selectSurfaceCommandMap(s.context, id);
|
|
515
562
|
});
|
|
516
563
|
|
|
@@ -522,6 +569,17 @@ export interface EffectiveCommands {
|
|
|
522
569
|
commands: Command[];
|
|
523
570
|
/** Invoke by id, routed to the surface that owns the command (panel, else root). */
|
|
524
571
|
invoke: (id: string) => void;
|
|
572
|
+
/**
|
|
573
|
+
* The key that runs the command now: the `keymap` remap, else
|
|
574
|
+
* `defaultHotkey`. Undefined when the command has no key, or when an active
|
|
575
|
+
* panel command shadows its root key.
|
|
576
|
+
*/
|
|
577
|
+
hotkeyFor: (id: string) => string | undefined;
|
|
578
|
+
/**
|
|
579
|
+
* Whether the command's `when` passes, checked against the context of the
|
|
580
|
+
* surface that owns it. Evaluated on each call.
|
|
581
|
+
*/
|
|
582
|
+
isAvailable: (id: string) => boolean;
|
|
525
583
|
}
|
|
526
584
|
|
|
527
585
|
/**
|
|
@@ -531,62 +589,98 @@ export interface EffectiveCommands {
|
|
|
531
589
|
* invocable there) but drops the now-misleading hotkey hint. With no active
|
|
532
590
|
* panel this is exactly the root command set, so non-panel apps are unchanged.
|
|
533
591
|
*
|
|
534
|
-
* Shadowing is computed on
|
|
535
|
-
*
|
|
536
|
-
*
|
|
592
|
+
* Shadowing is computed on the effective key (the `keymap` remap, else
|
|
593
|
+
* `defaultHotkey`), the same key dispatch uses. `hotkeyFor` gives that key for
|
|
594
|
+
* display, and `isAvailable` applies each command's `when`.
|
|
537
595
|
*/
|
|
538
596
|
export const useEffectiveCommands = function useEffectiveCommands(): EffectiveCommands {
|
|
539
597
|
const ctx = useContext(CommandStoreReactContext);
|
|
598
|
+
|
|
540
599
|
if (!ctx) {
|
|
541
600
|
throw new Error("useEffectiveCommands must be used within a CommandProvider");
|
|
542
601
|
}
|
|
543
|
-
|
|
602
|
+
|
|
603
|
+
const { commandStore, keymap } = ctx;
|
|
544
604
|
const { store } = commandStore;
|
|
545
605
|
|
|
546
606
|
const panelMap = useSelector(store, (s) => {
|
|
547
607
|
const panel = selectActivePanelSurface(s.context);
|
|
608
|
+
|
|
548
609
|
return panel ? selectSurfaceCommandMap(s.context, panel.id) : undefined;
|
|
549
610
|
});
|
|
611
|
+
|
|
550
612
|
const rootMap = useSelector(store, (s) => selectSurfaceCommandMap(s.context, ROOT_SURFACE_ID));
|
|
551
613
|
const hasPanel = useSelector(store, (s) => selectActivePanelSurface(s.context) !== null);
|
|
552
614
|
|
|
553
615
|
return useMemo(() => {
|
|
554
616
|
const rootCommands = rootMap ? [...rootMap.values()] : [];
|
|
555
|
-
const
|
|
556
|
-
|
|
617
|
+
const panelCommands = hasPanel && panelMap ? [...panelMap.values()] : [];
|
|
618
|
+
const panelIds = new Set(panelCommands.map((command) => command.id));
|
|
619
|
+
|
|
620
|
+
const resolveHotkey = (command: Command): string | undefined => {
|
|
621
|
+
const hotkey = keymap?.[command.id] ?? command.defaultHotkey;
|
|
622
|
+
|
|
623
|
+
return hotkey === "" ? undefined : hotkey;
|
|
557
624
|
};
|
|
558
|
-
if (!hasPanel) {
|
|
559
|
-
return { commands: rootCommands, invoke: rootInvoke };
|
|
560
|
-
}
|
|
561
625
|
|
|
562
|
-
const panelCommands = panelMap ? [...panelMap.values()] : [];
|
|
563
|
-
const panelIds = new Set(panelCommands.map((command) => command.id));
|
|
564
626
|
const shadowed = new Set(
|
|
565
|
-
panelCommands
|
|
566
|
-
.map((command) => command.defaultHotkey)
|
|
567
|
-
.filter((hotkey): hotkey is string => hotkey !== undefined && hotkey !== "")
|
|
627
|
+
panelCommands.map(resolveHotkey).filter((hotkey): hotkey is string => hotkey !== undefined)
|
|
568
628
|
);
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
629
|
+
|
|
630
|
+
const isShadowed = (command: Command): boolean => {
|
|
631
|
+
const hotkey = resolveHotkey(command);
|
|
632
|
+
|
|
633
|
+
return hotkey !== undefined && shadowed.has(hotkey);
|
|
634
|
+
};
|
|
635
|
+
|
|
636
|
+
const ownerOf = (id: string): { command: Command; record: SurfaceRecord } | undefined => {
|
|
637
|
+
const panel = selectActivePanelSurface(store.getSnapshot().context);
|
|
638
|
+
const panelCommand = panel ? panelMap?.get(id) : undefined;
|
|
639
|
+
|
|
640
|
+
if (panel && panelCommand) {
|
|
641
|
+
return { command: panelCommand, record: panel };
|
|
642
|
+
}
|
|
643
|
+
|
|
644
|
+
const rootCommand = rootMap?.get(id);
|
|
645
|
+
|
|
646
|
+
return rootCommand ? { command: rootCommand, record: commandStore.rootRecord } : undefined;
|
|
647
|
+
};
|
|
576
648
|
|
|
577
649
|
const invoke = (id: string): void => {
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
650
|
+
const owner = ownerOf(id);
|
|
651
|
+
commandStore.registryFor(owner?.record ?? commandStore.rootRecord).invoke(id);
|
|
652
|
+
};
|
|
653
|
+
|
|
654
|
+
const hotkeyFor = (id: string): string | undefined => {
|
|
655
|
+
const owner = ownerOf(id);
|
|
656
|
+
|
|
657
|
+
if (!owner) {
|
|
658
|
+
return undefined;
|
|
584
659
|
}
|
|
585
|
-
|
|
660
|
+
|
|
661
|
+
if (owner.record === commandStore.rootRecord && isShadowed(owner.command)) {
|
|
662
|
+
return undefined;
|
|
663
|
+
}
|
|
664
|
+
|
|
665
|
+
return resolveHotkey(owner.command);
|
|
666
|
+
};
|
|
667
|
+
|
|
668
|
+
const isAvailable = (id: string): boolean => {
|
|
669
|
+
const owner = ownerOf(id);
|
|
670
|
+
|
|
671
|
+
if (!owner) {
|
|
672
|
+
return false;
|
|
673
|
+
}
|
|
674
|
+
|
|
675
|
+
return !owner.command.when || owner.command.when(owner.record.buildCtx());
|
|
586
676
|
};
|
|
587
677
|
|
|
588
|
-
|
|
589
|
-
|
|
678
|
+
const effectiveRoot = rootCommands
|
|
679
|
+
.filter((command) => !panelIds.has(command.id))
|
|
680
|
+
.map((command) => (isShadowed(command) ? { ...command, defaultHotkey: undefined } : command));
|
|
681
|
+
|
|
682
|
+
return { commands: [...panelCommands, ...effectiveRoot], hotkeyFor, invoke, isAvailable };
|
|
683
|
+
}, [panelMap, rootMap, hasPanel, commandStore, store, keymap]);
|
|
590
684
|
};
|
|
591
685
|
|
|
592
686
|
/**
|
|
@@ -596,14 +690,17 @@ export const useEffectiveCommands = function useEffectiveCommands(): EffectiveCo
|
|
|
596
690
|
*/
|
|
597
691
|
export const useCommandStore = function useCommandStore(): CommandStore {
|
|
598
692
|
const ctx = useContext(CommandStoreReactContext);
|
|
693
|
+
|
|
599
694
|
if (!ctx) {
|
|
600
695
|
throw new Error("useCommandStore must be used within a CommandProvider");
|
|
601
696
|
}
|
|
697
|
+
|
|
602
698
|
return ctx.commandStore;
|
|
603
699
|
};
|
|
604
700
|
|
|
605
701
|
export const useCommandGroup = function useCommandGroup(group: CommandGroup): void {
|
|
606
702
|
const ctx = useContext(CommandStoreReactContext);
|
|
703
|
+
|
|
607
704
|
if (!ctx) {
|
|
608
705
|
throw new Error("useCommandGroup must be used within a CommandProvider");
|
|
609
706
|
}
|
|
@@ -613,11 +710,14 @@ export const useCommandGroup = function useCommandGroup(group: CommandGroup): vo
|
|
|
613
710
|
|
|
614
711
|
useEffect(() => {
|
|
615
712
|
const parsed = parseHotkey(groupRef.current.prefix, leaderKey);
|
|
713
|
+
|
|
616
714
|
const registered: RegisteredCommandGroup = {
|
|
617
715
|
...groupRef.current,
|
|
618
716
|
prefixKey: stepsKey(parsed.steps),
|
|
619
717
|
};
|
|
718
|
+
|
|
620
719
|
commandStore.store.trigger.groupRegistered({ group: registered });
|
|
720
|
+
|
|
621
721
|
return () => {
|
|
622
722
|
// Unregistration is identity-guarded in the store transition (R-05).
|
|
623
723
|
commandStore.store.trigger.groupUnregistered({ group: registered });
|
|
@@ -641,6 +741,7 @@ export const useProvideCommandContext = function useProvideCommandContext(
|
|
|
641
741
|
getter: () => Partial<CommandContext>
|
|
642
742
|
): void {
|
|
643
743
|
const ctx = useContext(CommandStoreReactContext);
|
|
744
|
+
|
|
644
745
|
if (!ctx) {
|
|
645
746
|
throw new Error("useProvideCommandContext must be used within a CommandProvider");
|
|
646
747
|
}
|
|
@@ -648,6 +749,7 @@ export const useProvideCommandContext = function useProvideCommandContext(
|
|
|
648
749
|
const idRef = useLazyRef(() => {
|
|
649
750
|
const id = `ctx-${nextContextSourceId}`;
|
|
650
751
|
nextContextSourceId += 1;
|
|
752
|
+
|
|
651
753
|
return id;
|
|
652
754
|
});
|
|
653
755
|
|
|
@@ -661,6 +763,7 @@ export const useProvideCommandContext = function useProvideCommandContext(
|
|
|
661
763
|
getter: () => getterRef.current(),
|
|
662
764
|
id,
|
|
663
765
|
});
|
|
766
|
+
|
|
664
767
|
return () => {
|
|
665
768
|
commandStore.store.trigger.contextSourceUnregistered({ id });
|
|
666
769
|
};
|
|
@@ -671,6 +774,5 @@ export const useProvideCommandContextKey = function useProvideCommandContextKey<
|
|
|
671
774
|
K extends keyof CommandContext,
|
|
672
775
|
>(key: K, getter: () => CommandContext[K]): void {
|
|
673
776
|
// provide a typed augmentation builder instead of asserting computed keys.
|
|
674
|
-
// oxlint-disable-next-line typescript/no-unsafe-type-assertion -- computed key is constrained by K
|
|
675
777
|
useProvideCommandContext(() => ({ [key]: getter() }));
|
|
676
778
|
};
|
package/src/hooks/use-latest.ts
CHANGED
package/src/index.ts
CHANGED
|
@@ -15,12 +15,19 @@ export type {
|
|
|
15
15
|
ParsedStep,
|
|
16
16
|
RegisteredCommandGroup,
|
|
17
17
|
} from "./types.js";
|
|
18
|
+
|
|
18
19
|
export type { Mode } from "./mode.js";
|
|
20
|
+
|
|
19
21
|
export { useMode, useSetMode } from "./mode.js";
|
|
22
|
+
|
|
20
23
|
export { parseHotkey } from "./parse.js";
|
|
24
|
+
|
|
21
25
|
export { matchStep } from "./match.js";
|
|
26
|
+
|
|
22
27
|
export { ScreenScopeProvider, useScreenScope } from "./screen-focus.js";
|
|
28
|
+
|
|
23
29
|
export type { ScreenScopeState } from "./screen-focus.js";
|
|
30
|
+
|
|
24
31
|
export {
|
|
25
32
|
CommandProvider,
|
|
26
33
|
CommandSurfaceProvider,
|
|
@@ -32,11 +39,13 @@ export {
|
|
|
32
39
|
useCommandSequenceState,
|
|
33
40
|
useCommandStore,
|
|
34
41
|
useCommandSurfaceId,
|
|
42
|
+
useHasCommandProvider,
|
|
35
43
|
useProvideCommandContext,
|
|
36
44
|
useProvideCommandContextKey,
|
|
37
45
|
useSurfaceCommands,
|
|
38
46
|
useSurfaceInvoke,
|
|
39
47
|
} from "./context.js";
|
|
48
|
+
|
|
40
49
|
export {
|
|
41
50
|
ROOT_SURFACE_ID,
|
|
42
51
|
formatStepKey,
|
|
@@ -46,16 +55,25 @@ export {
|
|
|
46
55
|
selectSequence,
|
|
47
56
|
selectSurfaceCommands,
|
|
48
57
|
} from "./command-store.js";
|
|
58
|
+
|
|
49
59
|
export type { CommandStoreContext, SurfaceRecord } from "./command-store.js";
|
|
60
|
+
|
|
50
61
|
export type { CommandStore, CommandStoreConfig } from "./command-store-wrapper.js";
|
|
62
|
+
|
|
51
63
|
export type {
|
|
52
64
|
CommandProviderProps,
|
|
53
65
|
CommandSurfaceProviderProps,
|
|
54
66
|
EffectiveCommands,
|
|
55
67
|
} from "./context.js";
|
|
68
|
+
|
|
56
69
|
export { useCommand } from "./use-command.js";
|
|
70
|
+
|
|
57
71
|
export type { UseCommandOptions } from "./use-command.js";
|
|
72
|
+
|
|
58
73
|
export { useActions } from "./use-actions.js";
|
|
74
|
+
|
|
59
75
|
export type { ActionDefinition } from "./use-actions.js";
|
|
76
|
+
|
|
60
77
|
export { useLatest } from "./hooks/use-latest.js";
|
|
78
|
+
|
|
61
79
|
export { useLazyRef } from "./hooks/use-lazy-ref.js";
|