@tooee/commands 0.7.3 → 0.9.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/README.md +26 -1
- package/dist/build-context.d.ts +3 -12
- package/dist/build-context.d.ts.map +1 -1
- package/dist/build-context.js +6 -17
- package/dist/build-context.js.map +1 -1
- package/dist/command-store-wrapper.d.ts +28 -0
- package/dist/command-store-wrapper.d.ts.map +1 -0
- package/dist/command-store-wrapper.js +115 -0
- package/dist/command-store-wrapper.js.map +1 -0
- package/dist/command-store.d.ts +2 -65
- package/dist/command-store.d.ts.map +1 -1
- package/dist/command-store.js +1 -345
- package/dist/command-store.js.map +1 -1
- package/dist/context.d.ts +5 -4
- package/dist/context.d.ts.map +1 -1
- package/dist/context.js +55 -50
- package/dist/context.js.map +1 -1
- package/dist/hooks/use-latest.d.ts +4 -0
- package/dist/hooks/use-latest.d.ts.map +1 -0
- package/dist/hooks/use-latest.js +8 -0
- package/dist/hooks/use-latest.js.map +1 -0
- package/dist/hooks/use-lazy-ref.d.ts +4 -0
- package/dist/hooks/use-lazy-ref.d.ts.map +1 -0
- package/dist/hooks/use-lazy-ref.js +8 -0
- package/dist/hooks/use-lazy-ref.js.map +1 -0
- package/dist/index.d.ts +7 -7
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +5 -4
- package/dist/index.js.map +1 -1
- package/dist/key-dispatch.d.ts +24 -0
- package/dist/key-dispatch.d.ts.map +1 -0
- package/dist/key-dispatch.js +159 -0
- package/dist/key-dispatch.js.map +1 -0
- package/dist/parse.js +10 -10
- package/dist/parse.js.map +1 -1
- package/dist/public-contract.typecheck.d.ts +2 -0
- package/dist/public-contract.typecheck.d.ts.map +1 -0
- package/dist/public-contract.typecheck.js +7 -0
- package/dist/public-contract.typecheck.js.map +1 -0
- package/dist/store.d.ts +9 -0
- package/dist/store.d.ts.map +1 -0
- package/dist/store.js +5 -0
- package/dist/store.js.map +1 -0
- package/dist/types.d.ts +6 -5
- package/dist/types.d.ts.map +1 -1
- package/dist/use-actions.d.ts.map +1 -1
- package/dist/use-actions.js +3 -1
- package/dist/use-actions.js.map +1 -1
- package/dist/use-command.d.ts.map +1 -1
- package/dist/use-command.js +3 -1
- package/dist/use-command.js.map +1 -1
- package/package.json +8 -1
- package/src/build-context.ts +7 -18
- package/src/command-store-wrapper.ts +149 -0
- package/src/command-store.ts +1 -491
- package/src/context.tsx +73 -65
- package/src/hooks/use-latest.ts +9 -0
- package/src/hooks/use-lazy-ref.ts +9 -0
- package/src/index.ts +7 -22
- package/src/key-dispatch.ts +272 -0
- package/src/parse.ts +10 -10
- package/src/public-contract.typecheck.ts +7 -0
- package/src/store.ts +12 -0
- package/src/types.ts +6 -5
- package/src/use-actions.ts +3 -1
- package/src/use-command.ts +3 -1
package/README.md
CHANGED
|
@@ -13,7 +13,7 @@ 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
|
|
16
|
+
myApp?: { selectedId: string | null };
|
|
17
17
|
}
|
|
18
18
|
}
|
|
19
19
|
|
|
@@ -25,6 +25,21 @@ Key ownership is by convention: Tooee packages use short built-in keys such as
|
|
|
25
25
|
should use app/package-specific keys to avoid collisions. If multiple providers
|
|
26
26
|
return the same top-level key, the last registered provider wins.
|
|
27
27
|
|
|
28
|
+
Augmented fields must be optional because their providers are not mounted on
|
|
29
|
+
every command surface. A command handler must check that its domain field is
|
|
30
|
+
present before it reads the field:
|
|
31
|
+
|
|
32
|
+
```ts
|
|
33
|
+
handler: (ctx) => {
|
|
34
|
+
const selectedId = ctx.myApp?.selectedId;
|
|
35
|
+
if (selectedId === undefined) return;
|
|
36
|
+
openItem(selectedId);
|
|
37
|
+
};
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
Only the core `mode`, `setMode`, `commands`, and `exit` fields are always
|
|
41
|
+
available.
|
|
42
|
+
|
|
28
43
|
## Command surfaces & arbitration
|
|
29
44
|
|
|
30
45
|
Commands register on a **surface**. The root app is the implicit base surface;
|
|
@@ -72,6 +87,16 @@ invocation routed to the owning surface. The shell palette uses the active
|
|
|
72
87
|
panel's local mode; if opened programmatically during panel insert mode it omits
|
|
73
88
|
root commands, matching the editor-safe dispatch boundary.
|
|
74
89
|
|
|
90
|
+
Choose the command hook by the question the caller needs to answer:
|
|
91
|
+
|
|
92
|
+
- Use `useSurfaceCommands()` to render the commands registered on one surface.
|
|
93
|
+
- Use `useEffectiveCommands()` to build a palette that follows panel fall-through
|
|
94
|
+
and command shadowing.
|
|
95
|
+
- Use `useCommandRegistry()` for integration bridges that need the current
|
|
96
|
+
surface registry, groups, context sources, or leader key.
|
|
97
|
+
- Use `useSurfaceInvoke()` when a component needs the current surface's command
|
|
98
|
+
list and its `invoke` function.
|
|
99
|
+
|
|
75
100
|
## Raw `useKeyboard` policy
|
|
76
101
|
|
|
77
102
|
App-level `useKeyboard` handlers MUST guard against active overlays — either
|
package/dist/build-context.d.ts
CHANGED
|
@@ -15,18 +15,9 @@ export declare const commandsFromRegistry: (registry: CommandRegistry) => Comman
|
|
|
15
15
|
* dispatcher, every command surface, and the pre-dispatch placeholder.
|
|
16
16
|
*
|
|
17
17
|
* The core fields are built as a concrete, typed `CommandContextBase`: no
|
|
18
|
-
* `Record<string, any>` staging object. Domain packages
|
|
19
|
-
*
|
|
20
|
-
*
|
|
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.
|
|
18
|
+
* `Record<string, any>` staging object. Domain packages add optional fields to
|
|
19
|
+
* `CommandContext` by declaration merging, then supply values at runtime
|
|
20
|
+
* through context sources registered by their providers.
|
|
30
21
|
*/
|
|
31
22
|
export declare const buildCommandContext: (input: BuildCommandContextInput) => CommandContext;
|
|
32
23
|
//# sourceMappingURL=build-context.d.ts.map
|
|
@@ -1 +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,aACrB,eAAe,KACxB,eAOF,CAAC;AAEF
|
|
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,aACrB,eAAe,KACxB,eAOF,CAAC;AAEF;;;;;;;;GAQG;AACH,eAAO,MAAM,mBAAmB,UACvB,wBAAwB,KAC9B,cAaF,CAAC"}
|
package/dist/build-context.js
CHANGED
|
@@ -12,33 +12,22 @@ export const commandsFromRegistry = function commandsFromRegistry(registry) {
|
|
|
12
12
|
* dispatcher, every command surface, and the pre-dispatch placeholder.
|
|
13
13
|
*
|
|
14
14
|
* The core fields are built as a concrete, typed `CommandContextBase`: no
|
|
15
|
-
* `Record<string, any>` staging object. Domain packages
|
|
16
|
-
*
|
|
17
|
-
*
|
|
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.
|
|
15
|
+
* `Record<string, any>` staging object. Domain packages add optional fields to
|
|
16
|
+
* `CommandContext` by declaration merging, then supply values at runtime
|
|
17
|
+
* through context sources registered by their providers.
|
|
27
18
|
*/
|
|
28
19
|
export const buildCommandContext = function buildCommandContext(input) {
|
|
29
20
|
const base = {
|
|
30
21
|
commands: input.commands,
|
|
31
|
-
exit: () =>
|
|
22
|
+
exit: () => {
|
|
23
|
+
// Default until a registered context source supplies the real exit.
|
|
24
|
+
},
|
|
32
25
|
mode: input.mode,
|
|
33
26
|
setMode: input.setMode,
|
|
34
27
|
};
|
|
35
28
|
for (const getter of input.contributions ?? []) {
|
|
36
29
|
Object.assign(base, getter());
|
|
37
30
|
}
|
|
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
31
|
return base;
|
|
43
32
|
};
|
|
44
33
|
//# sourceMappingURL=build-context.js.map
|
|
@@ -1 +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
|
|
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;;;;;;;;GAQG;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;YACT,oEAAoE;QACtE,CAAC;QACD,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,OAAO,IAAI,CAAC;AACd,CAAC,CAAC"}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { KeyEvent } from "@opentui/core";
|
|
2
|
+
import type { Mode } from "./mode.js";
|
|
3
|
+
import type { CommandStoreInstance, SurfaceRecord } from "./command-store.js";
|
|
4
|
+
import type { KeyDispatchConfig, KeyDispatchResult } from "./key-dispatch.js";
|
|
5
|
+
import type { CommandContext, CommandRegistry } from "./types.js";
|
|
6
|
+
export type { KeyDispatchResult } from "./key-dispatch.js";
|
|
7
|
+
export type CommandStoreConfig = KeyDispatchConfig;
|
|
8
|
+
export interface CreateCommandStoreOptions extends CommandStoreConfig {
|
|
9
|
+
root: {
|
|
10
|
+
getMode: () => Mode;
|
|
11
|
+
buildCtx: () => CommandContext;
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
export interface CommandStore {
|
|
15
|
+
store: CommandStoreInstance;
|
|
16
|
+
rootRecord: SurfaceRecord;
|
|
17
|
+
key: (event: KeyEvent) => KeyDispatchResult;
|
|
18
|
+
reset: () => void;
|
|
19
|
+
dispose: () => void;
|
|
20
|
+
pushSurface: (surface: SurfaceRecord) => () => void;
|
|
21
|
+
activatePanel: (groupId: string, panelId: string) => void;
|
|
22
|
+
removePanelGroup: (groupId: string) => void;
|
|
23
|
+
modeChanged: (surfaceId: string) => void;
|
|
24
|
+
registryFor: (record: SurfaceRecord) => CommandRegistry;
|
|
25
|
+
setConfig: (config: CommandStoreConfig) => void;
|
|
26
|
+
}
|
|
27
|
+
export declare const createCommandStore: (options: CreateCommandStoreOptions) => CommandStore;
|
|
28
|
+
//# sourceMappingUR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AE,CAAC,MAAM,EAAE,aAAa,KAAK,eAAe,CAAC;IACxD,SAAS,EAAE,CAAC,MAAM,EAAE,kBAAkB,KAAK,IAAI,CAAC;CACjD;AAUD,eAAO,MAAM,kBAAkB,YACpB,yBAAyB,KACjC,YA4GF,CAAC"}
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import { ROOT_SURFACE_ID, createBaseStore, selectKeyboardOwnerSurface } from "./command-store.js";
|
|
2
|
+
import { createKeyDispatcher } from "./key-dispatch.js";
|
|
3
|
+
const reportCommandFailure = async function reportCommandFailure(result) {
|
|
4
|
+
try {
|
|
5
|
+
await result;
|
|
6
|
+
}
|
|
7
|
+
catch (error) {
|
|
8
|
+
console.error("Command handler failed", error);
|
|
9
|
+
}
|
|
10
|
+
};
|
|
11
|
+
export const createCommandStore = function createCommandStore(options) {
|
|
12
|
+
const rootRecord = {
|
|
13
|
+
buildCtx: options.root.buildCtx,
|
|
14
|
+
depth: 0,
|
|
15
|
+
getMode: options.root.getMode,
|
|
16
|
+
id: ROOT_SURFACE_ID,
|
|
17
|
+
order: 0,
|
|
18
|
+
role: "root",
|
|
19
|
+
};
|
|
20
|
+
const store = createBaseStore({
|
|
21
|
+
activePanels: new Map(),
|
|
22
|
+
commandsBySurface: new Map(),
|
|
23
|
+
contextSources: new Map(),
|
|
24
|
+
groups: new Map(),
|
|
25
|
+
sequence: null,
|
|
26
|
+
surfaces: [rootRecord],
|
|
27
|
+
});
|
|
28
|
+
let config = {
|
|
29
|
+
keymap: options.keymap,
|
|
30
|
+
leader: options.leader,
|
|
31
|
+
sequenceTimeoutMs: options.sequenceTimeoutMs,
|
|
32
|
+
};
|
|
33
|
+
const dispatcher = createKeyDispatcher({ getConfig: () => config, rootRecord, store });
|
|
34
|
+
const registries = new Map();
|
|
35
|
+
let orderCounter = 1;
|
|
36
|
+
const clearIfOwnerChanged = (before) => {
|
|
37
|
+
const after = selectKeyboardOwnerSurface(store.getSnapshot().context);
|
|
38
|
+
if (before !== after) {
|
|
39
|
+
dispatcher.dispose();
|
|
40
|
+
}
|
|
41
|
+
};
|
|
42
|
+
const pushSurface = (surface) => {
|
|
43
|
+
surface.order = orderCounter;
|
|
44
|
+
orderCounter += 1;
|
|
45
|
+
const before = selectKeyboardOwnerSurface(store.getSnapshot().context);
|
|
46
|
+
store.trigger.surfacePushed({ surface });
|
|
47
|
+
clearIfOwnerChanged(before);
|
|
48
|
+
return () => {
|
|
49
|
+
const beforePop = selectKeyboardOwnerSurface(store.getSnapshot().context);
|
|
50
|
+
store.trigger.surfacePopped({ surface });
|
|
51
|
+
clearIfOwnerChanged(beforePop);
|
|
52
|
+
};
|
|
53
|
+
};
|
|
54
|
+
const activatePanel = (groupId, panelId) => {
|
|
55
|
+
const before = selectKeyboardOwnerSurface(store.getSnapshot().context);
|
|
56
|
+
store.trigger.panelActivated({ groupId, panelId });
|
|
57
|
+
clearIfOwnerChanged(before);
|
|
58
|
+
};
|
|
59
|
+
const removePanelGroup = (groupId) => {
|
|
60
|
+
const before = selectKeyboardOwnerSurface(store.getSnapshot().context);
|
|
61
|
+
store.trigger.panelGroupRemoved({ groupId });
|
|
62
|
+
clearIfOwnerChanged(before);
|
|
63
|
+
};
|
|
64
|
+
const modeChanged = (surfaceId) => {
|
|
65
|
+
dispatcher.dispose();
|
|
66
|
+
store.trigger.modeChanged({ surfaceId });
|
|
67
|
+
};
|
|
68
|
+
const registryFor = (record) => {
|
|
69
|
+
let registry = registries.get(record);
|
|
70
|
+
if (!registry) {
|
|
71
|
+
registry = {
|
|
72
|
+
get commands() {
|
|
73
|
+
return store.getSnapshot().context.commandsBySurface.get(record.id) ?? new Map();
|
|
74
|
+
},
|
|
75
|
+
invoke(id) {
|
|
76
|
+
const command = store.getSnapshot().context.commandsBySurface.get(record.id)?.get(id);
|
|
77
|
+
if (!command) {
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
const commandContext = record.buildCtx();
|
|
81
|
+
if (!command.when || command.when(commandContext)) {
|
|
82
|
+
const result = command.handler(commandContext);
|
|
83
|
+
if (result) {
|
|
84
|
+
void reportCommandFailure(result);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
},
|
|
88
|
+
register(command) {
|
|
89
|
+
store.trigger.commandRegistered({ command, surfaceId: record.id });
|
|
90
|
+
return () => {
|
|
91
|
+
store.trigger.commandUnregistered({ command, surfaceId: record.id });
|
|
92
|
+
};
|
|
93
|
+
},
|
|
94
|
+
};
|
|
95
|
+
registries.set(record, registry);
|
|
96
|
+
}
|
|
97
|
+
return registry;
|
|
98
|
+
};
|
|
99
|
+
return {
|
|
100
|
+
activatePanel,
|
|
101
|
+
dispose: dispatcher.dispose,
|
|
102
|
+
key: dispatcher.key,
|
|
103
|
+
modeChanged,
|
|
104
|
+
pushSurface,
|
|
105
|
+
registryFor,
|
|
106
|
+
removePanelGroup,
|
|
107
|
+
reset: dispatcher.reset,
|
|
108
|
+
rootRecord,
|
|
109
|
+
setConfig: (next) => {
|
|
110
|
+
config = next;
|
|
111
|
+
},
|
|
112
|
+
store,
|
|
113
|
+
};
|
|
114
|
+
};
|
|
115
|
+
//# sourceMappingURL=command-store-wrapper.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"command-store-wrapper.js","sourceRoot":"","sources":["../src/command-store-wrapper.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,eAAe,EAAE,eAAe,EAAE,0BAA0B,EAAE,MAAM,oBAAoB,CAAC;AAElG,OAAO,EAAE,mBAAmB,EAAE,MAAM,mBAAmB,CAAC;AA0BxD,MAAM,oBAAoB,GAAG,KAAK,UAAU,oBAAoB,CAAC,MAAqB;IACpF,IAAI,CAAC;QACH,MAAM,MAAM,CAAC;IACf,CAAC;IAAC,OAAO,KAAc,EAAE,CAAC;QACxB,OAAO,CAAC,KAAK,CAAC,wBAAwB,EAAE,KAAK,CAAC,CAAC;IACjD,CAAC;AACH,CAAC,CAAC;AAEF,MAAM,CAAC,MAAM,kBAAkB,GAAG,S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G,CAAC,MAAM,EAAE,QAAQ,CAAC,CAAC;QACnC,CAAC;QACD,OAAO,QAAQ,CAAC;IAClB,CAAC,CAAC;IAEF,OAAO;QACL,aAAa;QACb,OAAO,EAAE,UAAU,CAAC,OAAO;QAC3B,GAAG,EAAE,UAAU,CAAC,GAAG;QACnB,WAAW;QACX,WAAW;QACX,WAAW;QACX,gBAAgB;QAChB,KAAK,EAAE,UAAU,CAAC,KAAK;QACvB,UAAU;QACV,SAAS,EAAE,CAAC,IAAI,EAAE,EAAE;YAClB,MAAM,GAAG,IAAI,CAAC;QAChB,CAAC;QACD,KAAK;KACN,CAAC;AACJ,CAAC,CAAC"}
|
package/dist/command-store.d.ts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
import type { Command, CommandContext, CommandRegistry, CommandSequenceState, CommandSurfaceRole, ParsedStep, RegisteredCommandGroup } from "./types.js";
|
|
1
|
+
import type { Command, CommandContext, CommandSequenceState, CommandSurfaceRole, ParsedStep, RegisteredCommandGroup } from "./types.js";
|
|
3
2
|
import type { Mode } from "./mode.js";
|
|
4
3
|
export declare const ROOT_SURFACE_ID = "__root";
|
|
5
4
|
/**
|
|
@@ -85,7 +84,7 @@ export declare const selectSequence: (ctx: CommandStoreContext) => CommandSequen
|
|
|
85
84
|
export declare const selectGroups: (ctx: CommandStoreContext) => ReadonlyMap<string, RegisteredCommandGroup>;
|
|
86
85
|
export declare const formatStepKey: (step: ParsedStep) => string;
|
|
87
86
|
export declare const stepsKey: (steps: readonly ParsedStep[]) => string;
|
|
88
|
-
declare const createBaseStore: (initialContext: CommandStoreContext) => import("@xstate/store").Store<CommandStoreContext, {
|
|
87
|
+
export declare const createBaseStore: (initialContext: CommandStoreContext) => import("@xstate/store").Store<CommandStoreContext, {
|
|
89
88
|
commandRegistered: {
|
|
90
89
|
surfaceId: string;
|
|
91
90
|
command: Command;
|
|
@@ -129,66 +128,4 @@ declare const createBaseStore: (initialContext: CommandStoreContext) => import("
|
|
|
129
128
|
};
|
|
130
129
|
}, never>;
|
|
131
130
|
export type CommandStoreInstance = ReturnType<typeof createBaseStore>;
|
|
132
|
-
export interface KeyDispatchResult {
|
|
133
|
-
handled: boolean;
|
|
134
|
-
/**
|
|
135
|
-
* Present on a full command match. The caller (the `useKeyboard` effect)
|
|
136
|
-
* calls `preventDefault()` and then `invoke()` synchronously, preserving
|
|
137
|
-
* today's dispatch order and `event.defaultPrevented` semantics.
|
|
138
|
-
*/
|
|
139
|
-
invoke?: () => void;
|
|
140
|
-
}
|
|
141
|
-
export interface CommandStoreConfig {
|
|
142
|
-
leader?: string;
|
|
143
|
-
keymap?: Record<string, string>;
|
|
144
|
-
sequenceTimeoutMs?: number;
|
|
145
|
-
}
|
|
146
|
-
export interface CreateCommandStoreOptions extends CommandStoreConfig {
|
|
147
|
-
/** Root surface accessors, provided by the command provider/dispatcher. */
|
|
148
|
-
root: {
|
|
149
|
-
getMode: () => Mode;
|
|
150
|
-
buildCtx: () => CommandContext;
|
|
151
|
-
};
|
|
152
|
-
}
|
|
153
|
-
/**
|
|
154
|
-
* The single writer around the store. Owns the two pieces of IO-adjacent,
|
|
155
|
-
* non-render state: the key buffer (not render state — putting it in store
|
|
156
|
-
* context would notify subscribers on every keystroke) and the sequence
|
|
157
|
-
* timeout timer. The renderable `sequence` display state lives in the store.
|
|
158
|
-
*/
|
|
159
|
-
export interface CommandStore {
|
|
160
|
-
/** The underlying @xstate/store instance (subscribe/getSnapshot/trigger). */
|
|
161
|
-
store: CommandStoreInstance;
|
|
162
|
-
/** The always-present root surface record (`surfaces[0]`). */
|
|
163
|
-
rootRecord: SurfaceRecord;
|
|
164
|
-
/**
|
|
165
|
-
* Feed a key event: arbitrates the active surface, filters candidates by
|
|
166
|
-
* mode/when, runs the pure sequence matchers against the internal buffer,
|
|
167
|
-
* triggers sequencePending/sequenceReset as needed, schedules/clears the
|
|
168
|
-
* timeout, and returns what happened so the caller can preventDefault and
|
|
169
|
-
* invoke synchronously.
|
|
170
|
-
*/
|
|
171
|
-
key: (event: KeyEvent) => KeyDispatchResult;
|
|
172
|
-
/** Clears buffer + timer and triggers sequenceReset. Idempotent. */
|
|
173
|
-
reset: () => void;
|
|
174
|
-
/** Clears buffer + timer permanently, without touching store state. Called on dispatcher unmount. */
|
|
175
|
-
dispose: () => void;
|
|
176
|
-
/** Push a surface record; assigns registration order. Returns the pop cleanup. */
|
|
177
|
-
pushSurface: (surface: SurfaceRecord) => () => void;
|
|
178
|
-
/**
|
|
179
|
-
* Publish a panel group's active panel. Clears the pending chord when it
|
|
180
|
-
* changes which surface owns keyboard input. Idempotent for an unchanged id.
|
|
181
|
-
*/
|
|
182
|
-
activatePanel: (groupId: string, panelId: string) => void;
|
|
183
|
-
/** Forget a panel group's activation (e.g. the group unmounted). */
|
|
184
|
-
removePanelGroup: (groupId: string) => void;
|
|
185
|
-
/** Notify a (root or surface-local) mode change: clears buffer + pending sequence. */
|
|
186
|
-
modeChanged: (surfaceId: string) => void;
|
|
187
|
-
/** Back-compat CommandRegistry facade for a surface record (stable per record). */
|
|
188
|
-
registryFor: (record: SurfaceRecord) => CommandRegistry;
|
|
189
|
-
/** Update live config (leader/keymap/timeout are read per dispatch, as before). */
|
|
190
|
-
setConfig: (config: CommandStoreConfig) => void;
|
|
191
|
-
}
|
|
192
|
-
export declare const createCommandStore: (options: CreateCommandStoreOptions) => CommandStore;
|
|
193
|
-
export {};
|
|
194
131
|
//# 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,
|
|
1
|
+
{"version":3,"file":"command-store.d.ts","sourceRoot":"","sources":["../src/command-store.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EACV,OAAO,EACP,cAAc,EACd,oBAAoB,EACpB,kBAAkB,EAClB,UAAU,EACV,sBAAsB,EACvB,MAAM,YAAY,CAAC;AACpB,OA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