@tooee/commands 0.2.3 → 0.3.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 +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/dist/use-command.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { useEffect, useRef } from "react";
|
|
2
2
|
import { useSurfaceRegistry } from "./context.js";
|
|
3
|
-
export function useCommand(options) {
|
|
3
|
+
export const useCommand = function useCommand(options) {
|
|
4
4
|
const registry = useSurfaceRegistry();
|
|
5
5
|
const optionsRef = useRef(options);
|
|
6
6
|
optionsRef.current = options;
|
|
@@ -11,21 +11,30 @@ export function useCommand(options) {
|
|
|
11
11
|
// register and observe commands, e.g. the command palette provider).
|
|
12
12
|
const modesKey = options.modes?.join("|") ?? "";
|
|
13
13
|
useEffect(() => {
|
|
14
|
-
if (options.enabled === false)
|
|
15
|
-
return;
|
|
14
|
+
if (options.enabled === false) {
|
|
15
|
+
return () => void 0;
|
|
16
|
+
}
|
|
16
17
|
const command = {
|
|
17
|
-
|
|
18
|
-
title: options.title,
|
|
19
|
-
handler: (...args) => optionsRef.current.handler(...args),
|
|
18
|
+
category: options.category,
|
|
20
19
|
defaultHotkey: options.hotkey,
|
|
20
|
+
group: options.group,
|
|
21
|
+
handler: (...args) => optionsRef.current.handler(...args),
|
|
22
|
+
hidden: options.hidden,
|
|
23
|
+
icon: options.icon,
|
|
24
|
+
id: options.id,
|
|
21
25
|
// Read through the ref: the effect is keyed on modesKey (content), and
|
|
22
26
|
// the ref holds the same-render options when the effect runs.
|
|
23
27
|
modes: optionsRef.current.modes,
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
28
|
+
title: options.title,
|
|
29
|
+
when: optionsRef.current.when
|
|
30
|
+
? (ctx) => {
|
|
31
|
+
const { when } = optionsRef.current;
|
|
32
|
+
if (!when) {
|
|
33
|
+
return false;
|
|
34
|
+
}
|
|
35
|
+
return when(ctx);
|
|
36
|
+
}
|
|
37
|
+
: undefined,
|
|
29
38
|
};
|
|
30
39
|
return registry.register(command);
|
|
31
40
|
}, [
|
|
@@ -40,5 +49,5 @@ export function useCommand(options) {
|
|
|
40
49
|
options.enabled,
|
|
41
50
|
registry,
|
|
42
51
|
]);
|
|
43
|
-
}
|
|
52
|
+
};
|
|
44
53
|
//# sourceMappingURL=use-command.js.map
|
package/dist/use-command.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-command.js","sourceRoot":"","sources":["../src/use-command.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,EAAE,MAAM,OAAO,
|
|
1
|
+
{"version":3,"file":"use-command.js","sourceRoot":"","sources":["../src/use-command.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,EAAE,MAAM,OAAO,CAAC;AAG1C,OAAO,EAAE,kBAAkB,EAAE,MAAM,cAAc,CAAC;AAqBlD,MAAM,CAAC,MAAM,UAAU,GAAG,SAAS,UAAU,CAAC,OAA0B;IACtE,MAAM,QAAQ,GAAG,kBAAkB,EAAE,CAAC;IACtC,MAAM,UAAU,GAAG,MAAM,CAAC,OAAO,CAAC,CAAC;IACnC,UAAU,CAAC,OAAO,GAAG,OAAO,CAAC;IAE7B,wEAAwE;IACxE,4EAA4E;IAC5E,uEAAuE;IACvE,sEAAsE;IACtE,qEAAqE;IACrE,MAAM,QAAQ,GAAG,OAAO,CAAC,KAAK,EAAE,IAAI,CAAC,GAAG,CAAC,IAAI,EAAE,CAAC;IAEhD,SAAS,CAAC,GAAG,EAAE;QACb,IAAI,OAAO,CAAC,OAAO,KAAK,KAAK,EAAE,CAAC;YAC9B,OAAO,GAAG,EAAE,CAAC,KAAK,CAAC,CAAC;QACtB,CAAC;QAED,MAAM,OAAO,GAAY;YACvB,QAAQ,EAAE,OAAO,CAAC,QAAQ;YAC1B,aAAa,EAAE,OAAO,CAAC,MAAM;YAC7B,KAAK,EAAE,OAAO,CAAC,KAAK;YACpB,OAAO,EAAE,CAAC,GAAG,IAAoC,EAAwB,EAAE,CACzE,UAAU,CAAC,OAAO,CAAC,OAAO,CAAC,GAAG,IAAI,CAAC;YACrC,MAAM,EAAE,OAAO,CAAC,MAAM;YACtB,IAAI,EAAE,OAAO,CAAC,IAAI;YAClB,EAAE,EAAE,OAAO,CAAC,EAAE;YACd,uEAAuE;YACvE,8DAA8D;YAC9D,KAAK,EAAE,UAAU,CAAC,OAAO,CAAC,KAAK;YAC/B,KAAK,EAAE,OAAO,CAAC,KAAK;YACpB,IAAI,EAAE,UAAU,CAAC,OAAO,CAAC,IAAI;gBAC3B,CAAC,CAAC,CAAC,GAAG,EAAE,EAAE;oBACN,MAAM,EAAE,IAAI,EAAE,GAAG,UAAU,CAAC,OAAO,CAAC;oBACpC,IAAI,CAAC,IAAI,EAAE,CAAC;wBACV,OAAO,KAAK,CAAC;oBACf,CAAC;oBACD,OAAO,IAAI,CAAC,GAAG,CAAC,CAAC;gBACnB,CAAC;gBACH,CAAC,CAAC,SAAS;SACd,CAAC;QACF,OAAO,QAAQ,CAAC,QAAQ,CAAC,OAAO,CAAC,CAAC;IACpC,CAAC,EAAE;QACD,OAAO,CAAC,EAAE;QACV,OAAO,CAAC,KAAK;QACb,OAAO,CAAC,MAAM;QACd,QAAQ;QACR,OAAO,CAAC,QAAQ;QAChB,OAAO,CAAC,KAAK;QACb,OAAO,CAAC,IAAI;QACZ,OAAO,CAAC,MAAM;QACd,OAAO,CAAC,OAAO;QACf,QAAQ;KACT,CAAC,CAAC;AACL,CAAC,CAAC"}
|
package/package.json
CHANGED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
CommandCommands,
|
|
3
|
+
CommandContext,
|
|
4
|
+
CommandContextBase,
|
|
5
|
+
CommandRegistry,
|
|
6
|
+
} from "./types.js";
|
|
7
|
+
import type { ContextGetter } from "./command-store.js";
|
|
8
|
+
import type { Mode } from "./mode.js";
|
|
9
|
+
|
|
10
|
+
export interface BuildCommandContextInput {
|
|
11
|
+
commands: CommandCommands;
|
|
12
|
+
mode: Mode;
|
|
13
|
+
setMode: (mode: Mode) => void;
|
|
14
|
+
/** Registered context sources, in registration order (later sources win). */
|
|
15
|
+
contributions?: Iterable<ContextGetter>;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/** The `commands` slice of a context, backed by a live surface registry. */
|
|
19
|
+
export const commandsFromRegistry = function commandsFromRegistry(
|
|
20
|
+
registry: CommandRegistry,
|
|
21
|
+
): CommandCommands {
|
|
22
|
+
return {
|
|
23
|
+
invoke: (id: string) => {
|
|
24
|
+
registry.invoke(id);
|
|
25
|
+
},
|
|
26
|
+
list: () => [...registry.commands.values()],
|
|
27
|
+
};
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* The single place a `CommandContext` is assembled — shared by the root
|
|
32
|
+
* dispatcher, every command surface, and the pre-dispatch placeholder.
|
|
33
|
+
*
|
|
34
|
+
* The core fields are built as a concrete, typed `CommandContextBase`: no
|
|
35
|
+
* `Record<string, any>` staging object. Domain packages (overlay, toast, ask,
|
|
36
|
+
* choose, view) add their own required fields to `CommandContext` by
|
|
37
|
+
* declaration merging, and supply the values at runtime through a context
|
|
38
|
+
* source registered by that package's provider.
|
|
39
|
+
*
|
|
40
|
+
* Registration is the readiness contract: a domain's commands are registered by
|
|
41
|
+
* that domain's components, and those components mount the domain's context
|
|
42
|
+
* source. A handler therefore always observes the fields its own package
|
|
43
|
+
* declared. TypeScript cannot express that relationship (the merged interface
|
|
44
|
+
* demands every domain's fields everywhere, including in packages that cannot
|
|
45
|
+
* see the providers), so it is enforced by construction and narrowed exactly
|
|
46
|
+
* once here, rather than being asserted or `any`-typed at every consumer.
|
|
47
|
+
*/
|
|
48
|
+
export const buildCommandContext = function buildCommandContext(
|
|
49
|
+
input: BuildCommandContextInput,
|
|
50
|
+
): CommandContext {
|
|
51
|
+
const base: CommandContextBase = {
|
|
52
|
+
commands: input.commands,
|
|
53
|
+
exit: () => void 0,
|
|
54
|
+
mode: input.mode,
|
|
55
|
+
setMode: input.setMode,
|
|
56
|
+
};
|
|
57
|
+
for (const getter of input.contributions ?? []) {
|
|
58
|
+
Object.assign(base, getter());
|
|
59
|
+
}
|
|
60
|
+
// The assertion is required by the whole-program build (where every domain's
|
|
61
|
+
// augmentation is visible) and is redundant in the package-local program,
|
|
62
|
+
// hence both suppressions.
|
|
63
|
+
// oxlint-disable-next-line typescript/no-unnecessary-type-assertion, typescript/no-unsafe-type-assertion -- single documented augmentation boundary (see above)
|
|
64
|
+
return base as CommandContext;
|
|
65
|
+
};
|