@tooee/commands 0.2.4 → 0.3.1
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 +5 -5
- package/dist/build-context.d.ts +32 -0
- package/dist/build-context.d.ts.map +1 -0
- package/dist/build-context.js +44 -0
- package/dist/build-context.js.map +1 -0
- package/dist/command-store.d.ts +19 -19
- package/dist/command-store.d.ts.map +1 -1
- package/dist/command-store.js +191 -148
- package/dist/command-store.js.map +1 -1
- package/dist/context.d.ts +16 -16
- package/dist/context.d.ts.map +1 -1
- package/dist/context.js +112 -107
- package/dist/context.js.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js.map +1 -1
- package/dist/match.d.ts +1 -1
- package/dist/match.d.ts.map +1 -1
- package/dist/match.js +14 -8
- package/dist/match.js.map +1 -1
- package/dist/mode.d.ts +4 -4
- package/dist/mode.d.ts.map +1 -1
- package/dist/mode.js +14 -14
- package/dist/mode.js.map +1 -1
- package/dist/parse.d.ts +1 -1
- package/dist/parse.d.ts.map +1 -1
- package/dist/parse.js +19 -18
- package/dist/parse.js.map +1 -1
- package/dist/sequence.d.ts +6 -5
- package/dist/sequence.d.ts.map +1 -1
- package/dist/sequence.js +31 -20
- package/dist/sequence.js.map +1 -1
- package/dist/types.d.ts +18 -10
- package/dist/types.d.ts.map +1 -1
- package/dist/use-actions.d.ts +1 -1
- package/dist/use-actions.d.ts.map +1 -1
- package/dist/use-actions.js +22 -12
- package/dist/use-actions.js.map +1 -1
- package/dist/use-command.d.ts +1 -1
- package/dist/use-command.d.ts.map +1 -1
- package/dist/use-command.js +21 -12
- package/dist/use-command.js.map +1 -1
- package/package.json +1 -1
- package/src/build-context.ts +65 -0
- package/src/command-store.ts +366 -290
- package/src/context.tsx +298 -284
- package/src/index.ts +17 -16
- package/src/match.ts +23 -11
- package/src/mode.tsx +37 -41
- package/src/parse.ts +48 -46
- package/src/sequence.ts +88 -66
- package/src/types.ts +69 -57
- package/src/use-actions.ts +46 -35
- package/src/use-command.ts +44 -33
package/README.md
CHANGED
|
@@ -13,11 +13,11 @@ for one keyed slice so the value is checked against the augmented key:
|
|
|
13
13
|
```tsx
|
|
14
14
|
declare module "@tooee/commands" {
|
|
15
15
|
interface CommandContext {
|
|
16
|
-
myApp: { selectedId: string | null }
|
|
16
|
+
myApp: { selectedId: string | null };
|
|
17
17
|
}
|
|
18
18
|
}
|
|
19
19
|
|
|
20
|
-
useProvideCommandContextKey("myApp", () => ({ selectedId }))
|
|
20
|
+
useProvideCommandContextKey("myApp", () => ({ selectedId }));
|
|
21
21
|
```
|
|
22
22
|
|
|
23
23
|
Key ownership is by convention: Tooee packages use short built-in keys such as
|
|
@@ -32,12 +32,12 @@ stand down while an overlay is open, or be ported to `useCommand` registrations
|
|
|
32
32
|
so the dispatcher arbitrates them:
|
|
33
33
|
|
|
34
34
|
```tsx
|
|
35
|
-
const hasOverlay = useHasOverlay() // from @tooee/overlays
|
|
35
|
+
const hasOverlay = useHasOverlay(); // from @tooee/overlays
|
|
36
36
|
|
|
37
37
|
useKeyboard((key) => {
|
|
38
|
-
if (hasOverlay) return
|
|
38
|
+
if (hasOverlay) return;
|
|
39
39
|
// ...
|
|
40
|
-
})
|
|
40
|
+
});
|
|
41
41
|
```
|
|
42
42
|
|
|
43
43
|
Why `key.preventDefault()` is **not** sufficient:
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import type { CommandCommands, CommandContext, CommandRegistry } from "./types.js";
|
|
2
|
+
import type { ContextGetter } from "./command-store.js";
|
|
3
|
+
import type { Mode } from "./mode.js";
|
|
4
|
+
export interface BuildCommandContextInput {
|
|
5
|
+
commands: CommandCommands;
|
|
6
|
+
mode: Mode;
|
|
7
|
+
setMode: (mode: Mode) => void;
|
|
8
|
+
/** Registered context sources, in registration order (later sources win). */
|
|
9
|
+
contributions?: Iterable<ContextGetter>;
|
|
10
|
+
}
|
|
11
|
+
/** The `commands` slice of a context, backed by a live surface registry. */
|
|
12
|
+
export declare const commandsFromRegistry: (registry: CommandRegistry) => CommandCommands;
|
|
13
|
+
/**
|
|
14
|
+
* The single place a `CommandContext` is assembled — shared by the root
|
|
15
|
+
* dispatcher, every command surface, and the pre-dispatch placeholder.
|
|
16
|
+
*
|
|
17
|
+
* The core fields are built as a concrete, typed `CommandContextBase`: no
|
|
18
|
+
* `Record<string, any>` staging object. Domain packages (overlay, toast, ask,
|
|
19
|
+
* choose, view) add their own required fields to `CommandContext` by
|
|
20
|
+
* declaration merging, and supply the values at runtime through a context
|
|
21
|
+
* source registered by that package's provider.
|
|
22
|
+
*
|
|
23
|
+
* Registration is the readiness contract: a domain's commands are registered by
|
|
24
|
+
* that domain's components, and those components mount the domain's context
|
|
25
|
+
* source. A handler therefore always observes the fields its own package
|
|
26
|
+
* declared. TypeScript cannot express that relationship (the merged interface
|
|
27
|
+
* demands every domain's fields everywhere, including in packages that cannot
|
|
28
|
+
* see the providers), so it is enforced by construction and narrowed exactly
|
|
29
|
+
* once here, rather than being asserted or `any`-typed at every consumer.
|
|
30
|
+
*/
|
|
31
|
+
export declare const buildCommandContext: (input: BuildCommandContextInput) => CommandContext;
|
|
32
|
+
//# sourceMappingURL=build-context.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"build-context.d.ts","sourceRoot":"","sources":["../src/build-context.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,eAAe,EACf,cAAc,EAEd,eAAe,EAChB,MAAM,YAAY,CAAC;AACpB,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAC;AACxD,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AAEtC,MAAM,WAAW,wBAAwB;IACvC,QAAQ,EAAE,eAAe,CAAC;IAC1B,IAAI,EAAE,IAAI,CAAC;IACX,OAAO,EAAE,CAAC,IAAI,EAAE,IAAI,KAAK,IAAI,CAAC;IAC9B,6EAA6E;IAC7E,aAAa,CAAC,EAAE,QAAQ,CAAC,aAAa,CAAC,CAAC;CACzC;AAED,4EAA4E;AAC5E,eAAO,MAAM,oBAAoB,GAC/B,UAAU,eAAe,KACxB,eAOF,CAAC;AAEF;;;;;;;;;;;;;;;;;GAiBG;AACH,eAAO,MAAM,mBAAmB,GAC9B,OAAO,wBAAwB,KAC9B,cAeF,CAAC"}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/** The `commands` slice of a context, backed by a live surface registry. */
|
|
2
|
+
export const commandsFromRegistry = function commandsFromRegistry(registry) {
|
|
3
|
+
return {
|
|
4
|
+
invoke: (id) => {
|
|
5
|
+
registry.invoke(id);
|
|
6
|
+
},
|
|
7
|
+
list: () => [...registry.commands.values()],
|
|
8
|
+
};
|
|
9
|
+
};
|
|
10
|
+
/**
|
|
11
|
+
* The single place a `CommandContext` is assembled — shared by the root
|
|
12
|
+
* dispatcher, every command surface, and the pre-dispatch placeholder.
|
|
13
|
+
*
|
|
14
|
+
* The core fields are built as a concrete, typed `CommandContextBase`: no
|
|
15
|
+
* `Record<string, any>` staging object. Domain packages (overlay, toast, ask,
|
|
16
|
+
* choose, view) add their own required fields to `CommandContext` by
|
|
17
|
+
* declaration merging, and supply the values at runtime through a context
|
|
18
|
+
* source registered by that package's provider.
|
|
19
|
+
*
|
|
20
|
+
* Registration is the readiness contract: a domain's commands are registered by
|
|
21
|
+
* that domain's components, and those components mount the domain's context
|
|
22
|
+
* source. A handler therefore always observes the fields its own package
|
|
23
|
+
* declared. TypeScript cannot express that relationship (the merged interface
|
|
24
|
+
* demands every domain's fields everywhere, including in packages that cannot
|
|
25
|
+
* see the providers), so it is enforced by construction and narrowed exactly
|
|
26
|
+
* once here, rather than being asserted or `any`-typed at every consumer.
|
|
27
|
+
*/
|
|
28
|
+
export const buildCommandContext = function buildCommandContext(input) {
|
|
29
|
+
const base = {
|
|
30
|
+
commands: input.commands,
|
|
31
|
+
exit: () => void 0,
|
|
32
|
+
mode: input.mode,
|
|
33
|
+
setMode: input.setMode,
|
|
34
|
+
};
|
|
35
|
+
for (const getter of input.contributions ?? []) {
|
|
36
|
+
Object.assign(base, getter());
|
|
37
|
+
}
|
|
38
|
+
// The assertion is required by the whole-program build (where every domain's
|
|
39
|
+
// augmentation is visible) and is redundant in the package-local program,
|
|
40
|
+
// hence both suppressions.
|
|
41
|
+
// oxlint-disable-next-line typescript/no-unnecessary-type-assertion, typescript/no-unsafe-type-assertion -- single documented augmentation boundary (see above)
|
|
42
|
+
return base;
|
|
43
|
+
};
|
|
44
|
+
//# sourceMappingURL=build-context.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"build-context.js","sourceRoot":"","sources":["../src/build-context.ts"],"names":[],"mappings":"AAiBA,4EAA4E;AAC5E,MAAM,CAAC,MAAM,oBAAoB,GAAG,SAAS,oBAAoB,CAC/D,QAAyB;IAEzB,OAAO;QACL,MAAM,EAAE,CAAC,EAAU,EAAE,EAAE;YACrB,QAAQ,CAAC,MAAM,CAAC,EAAE,CAAC,CAAC;QACtB,CAAC;QACD,IAAI,EAAE,GAAG,EAAE,CAAC,CAAC,GAAG,QAAQ,CAAC,QAAQ,CAAC,MAAM,EAAE,CAAC;KAC5C,CAAC;AACJ,CAAC,CAAC;AAEF;;;;;;;;;;;;;;;;;GAiBG;AACH,MAAM,CAAC,MAAM,mBAAmB,GAAG,SAAS,mBAAmB,CAC7D,KAA+B;IAE/B,MAAM,IAAI,GAAuB;QAC/B,QAAQ,EAAE,KAAK,CAAC,QAAQ;QACxB,IAAI,EAAE,GAAG,EAAE,CAAC,KAAK,CAAC;QAClB,IAAI,EAAE,KAAK,CAAC,IAAI;QAChB,OAAO,EAAE,KAAK,CAAC,OAAO;KACvB,CAAC;IACF,KAAK,MAAM,MAAM,IAAI,KAAK,CAAC,aAAa,IAAI,EAAE,EAAE,CAAC;QAC/C,MAAM,CAAC,MAAM,CAAC,IAAI,EAAE,MAAM,EAAE,CAAC,CAAC;IAChC,CAAC;IACD,6EAA6E;IAC7E,0EAA0E;IAC1E,2BAA2B;IAC3B,gKAAgK;IAChK,OAAO,IAAsB,CAAC;AAChC,CAAC,CAAC"}
|
package/dist/command-store.d.ts
CHANGED
|
@@ -40,19 +40,19 @@ export interface CommandStoreContext {
|
|
|
40
40
|
* The topmost modal surface: role === "modal", max depth, then max order.
|
|
41
41
|
* Passive surfaces and the root never win.
|
|
42
42
|
*/
|
|
43
|
-
export declare
|
|
43
|
+
export declare const selectActiveModalSurface: (ctx: CommandStoreContext) => SurfaceRecord | null;
|
|
44
44
|
/**
|
|
45
45
|
* Commands registered on a surface. Returns a fresh array; memoize against
|
|
46
46
|
* `selectSurfaceCommandMap` identity in render paths.
|
|
47
47
|
*/
|
|
48
|
-
export declare
|
|
48
|
+
export declare const selectSurfaceCommands: (ctx: CommandStoreContext, surfaceId: string) => readonly Command[];
|
|
49
49
|
/** Identity-stable per-surface command map (undefined when none registered). */
|
|
50
|
-
export declare
|
|
51
|
-
export declare
|
|
52
|
-
export declare
|
|
53
|
-
export declare
|
|
54
|
-
export declare
|
|
55
|
-
declare
|
|
50
|
+
export declare const selectSurfaceCommandMap: (ctx: CommandStoreContext, surfaceId: string) => ReadonlyMap<string, Command> | undefined;
|
|
51
|
+
export declare const selectSequence: (ctx: CommandStoreContext) => CommandSequenceState | null;
|
|
52
|
+
export declare const selectGroups: (ctx: CommandStoreContext) => ReadonlyMap<string, RegisteredCommandGroup>;
|
|
53
|
+
export declare const formatStepKey: (step: ParsedStep) => string;
|
|
54
|
+
export declare const stepsKey: (steps: readonly ParsedStep[]) => string;
|
|
55
|
+
declare const createBaseStore: (initialContext: CommandStoreContext) => import("@xstate/store").Store<CommandStoreContext, {
|
|
56
56
|
commandRegistered: {
|
|
57
57
|
surfaceId: string;
|
|
58
58
|
command: Command;
|
|
@@ -61,12 +61,6 @@ declare function createBaseStore(initialContext: CommandStoreContext): import("@
|
|
|
61
61
|
surfaceId: string;
|
|
62
62
|
command: Command;
|
|
63
63
|
};
|
|
64
|
-
groupRegistered: {
|
|
65
|
-
group: RegisteredCommandGroup;
|
|
66
|
-
};
|
|
67
|
-
groupUnregistered: {
|
|
68
|
-
group: RegisteredCommandGroup;
|
|
69
|
-
};
|
|
70
64
|
contextSourceRegistered: {
|
|
71
65
|
id: string;
|
|
72
66
|
getter: ContextGetter;
|
|
@@ -74,11 +68,11 @@ declare function createBaseStore(initialContext: CommandStoreContext): import("@
|
|
|
74
68
|
contextSourceUnregistered: {
|
|
75
69
|
id: string;
|
|
76
70
|
};
|
|
77
|
-
|
|
78
|
-
|
|
71
|
+
groupRegistered: {
|
|
72
|
+
group: RegisteredCommandGroup;
|
|
79
73
|
};
|
|
80
|
-
|
|
81
|
-
|
|
74
|
+
groupUnregistered: {
|
|
75
|
+
group: RegisteredCommandGroup;
|
|
82
76
|
};
|
|
83
77
|
modeChanged: {
|
|
84
78
|
surfaceId: string;
|
|
@@ -87,6 +81,12 @@ declare function createBaseStore(initialContext: CommandStoreContext): import("@
|
|
|
87
81
|
state: CommandSequenceState;
|
|
88
82
|
};
|
|
89
83
|
sequenceReset: unknown;
|
|
84
|
+
surfacePopped: {
|
|
85
|
+
surface: SurfaceRecord;
|
|
86
|
+
};
|
|
87
|
+
surfacePushed: {
|
|
88
|
+
surface: SurfaceRecord;
|
|
89
|
+
};
|
|
90
90
|
}, never>;
|
|
91
91
|
export type CommandStoreInstance = ReturnType<typeof createBaseStore>;
|
|
92
92
|
export interface KeyDispatchResult {
|
|
@@ -142,6 +142,6 @@ export interface CommandStore {
|
|
|
142
142
|
/** Update live config (leader/keymap/timeout are read per dispatch, as before). */
|
|
143
143
|
setConfig: (config: CommandStoreConfig) => void;
|
|
144
144
|
}
|
|
145
|
-
export declare
|
|
145
|
+
export declare const createCommandStore: (options: CreateCommandStoreOptions) => CommandStore;
|
|
146
146
|
export {};
|
|
147
147
|
//# sourceMappingURL=command-store.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"command-store.d.ts","sourceRoot":"","sources":["../src/command-store.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,eAAe,
|
|
1
|
+
{"version":3,"file":"command-store.d.ts","sourceRoot":"","sources":["../src/command-store.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AAC9C,OAAO,KAAK,EACV,OAAO,EACP,cAAc,EACd,eAAe,EACf,oBAAoB,EACpB,kBAAkB,EAElB,UAAU,EACV,sBAAsB,EACvB,MAAM,YAAY,CAAC;AACpB,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AAUtC,eAAO,MAAM,eAAe,WAAW,CAAC;AAIxC;;;;;;;;;GASG;AACH,MAAM,WAAW,aAAa;IAC5B,EAAE,EAAE,MAAM,CAAC;IACX,IAAI,EAAE,kBAAkB,GAAG,MAAM,CAAC;IAClC,gCAAgC;IAChC,KAAK,EAAE,MAAM,CAAC;IACd,iFAAiF;IACjF,KAAK,EAAE,MAAM,CAAC;IACd,wFAAwF;IACxF,OAAO,EAAE,MAAM,IAAI,CAAC;IACpB,oEAAoE;IACpE,QAAQ,EAAE,MAAM,cAAc,CAAC;CAChC;AAED,MAAM,MAAM,aAAa,GAAG,MAAM,OAAO,CAAC,cAAc,CAAC,CAAC;AAE1D,MAAM,WAAW,mBAAmB;IAClC,+EAA+E;IAC/E,QAAQ,EAAE,SAAS,aAAa,EAAE,CAAC;IACnC,8EAA8E;IAC9E,iBAAiB,EAAE,WAAW,CAAC,MAAM,EAAE,WAAW,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,CAAC;IACrE,yCAAyC;IACzC,MAAM,EAAE,WAAW,CAAC,MAAM,EAAE,sBAAsB,CAAC,CAAC;IACpD,cAAc,EAAE,WAAW,CAAC,MAAM,EAAE,aAAa,CAAC,CAAC;IACnD,mEAAmE;IACnE,QAAQ,EAAE,oBAAoB,GAAG,IAAI,CAAC;CACvC;AAID;;;GAGG;AACH,e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