@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/src/index.ts
CHANGED
|
@@ -2,6 +2,7 @@ export type {
|
|
|
2
2
|
ActiveCommandSurface,
|
|
3
3
|
Command,
|
|
4
4
|
CommandContext,
|
|
5
|
+
CommandCommands,
|
|
5
6
|
CommandContextBase,
|
|
6
7
|
CommandGroup,
|
|
7
8
|
CommandHandler,
|
|
@@ -13,18 +14,18 @@ export type {
|
|
|
13
14
|
ParsedHotkey,
|
|
14
15
|
ParsedStep,
|
|
15
16
|
RegisteredCommandGroup,
|
|
16
|
-
} from "./types.js"
|
|
17
|
-
export type { Mode } from "./mode.js"
|
|
18
|
-
export { ModeProvider, useMode, useSetMode } from "./mode.js"
|
|
19
|
-
export type { ModeProviderProps } from "./mode.js"
|
|
20
|
-
export { parseHotkey } from "./parse.js"
|
|
21
|
-
export { matchStep } from "./match.js"
|
|
22
|
-
export { DEFAULT_SEQUENCE_TIMEOUT_MS, SequenceTracker } from "./sequence.js"
|
|
17
|
+
} from "./types.js";
|
|
18
|
+
export type { Mode } from "./mode.js";
|
|
19
|
+
export { ModeProvider, useMode, useSetMode } from "./mode.js";
|
|
20
|
+
export type { ModeProviderProps } from "./mode.js";
|
|
21
|
+
export { parseHotkey } from "./parse.js";
|
|
22
|
+
export { matchStep } from "./match.js";
|
|
23
|
+
export { DEFAULT_SEQUENCE_TIMEOUT_MS, SequenceTracker } from "./sequence.js";
|
|
23
24
|
export type {
|
|
24
25
|
SequenceFeedResult,
|
|
25
26
|
SequencePendingMatch,
|
|
26
27
|
SequenceTrackerOptions,
|
|
27
|
-
} from "./sequence.js"
|
|
28
|
+
} from "./sequence.js";
|
|
28
29
|
export {
|
|
29
30
|
CommandProvider,
|
|
30
31
|
CommandSurfaceProvider,
|
|
@@ -39,7 +40,7 @@ export {
|
|
|
39
40
|
useProvideCommandContext,
|
|
40
41
|
useProvideCommandContextKey,
|
|
41
42
|
useSurfaceCommands,
|
|
42
|
-
} from "./context.js"
|
|
43
|
+
} from "./context.js";
|
|
43
44
|
export {
|
|
44
45
|
ROOT_SURFACE_ID,
|
|
45
46
|
createCommandStore,
|
|
@@ -48,7 +49,7 @@ export {
|
|
|
48
49
|
selectSequence,
|
|
49
50
|
selectSurfaceCommandMap,
|
|
50
51
|
selectSurfaceCommands,
|
|
51
|
-
} from "./command-store.js"
|
|
52
|
+
} from "./command-store.js";
|
|
52
53
|
export type {
|
|
53
54
|
CommandStore,
|
|
54
55
|
CommandStoreConfig,
|
|
@@ -58,9 +59,9 @@ export type {
|
|
|
58
59
|
CreateCommandStoreOptions,
|
|
59
60
|
KeyDispatchResult,
|
|
60
61
|
SurfaceRecord,
|
|
61
|
-
} from "./command-store.js"
|
|
62
|
-
export type { CommandProviderProps, CommandSurfaceProviderProps } from "./context.js"
|
|
63
|
-
export { useCommand } from "./use-command.js"
|
|
64
|
-
export type { UseCommandOptions } from "./use-command.js"
|
|
65
|
-
export { useActions } from "./use-actions.js"
|
|
66
|
-
export type { ActionDefinition } from "./use-actions.js"
|
|
62
|
+
} from "./command-store.js";
|
|
63
|
+
export type { CommandProviderProps, CommandSurfaceProviderProps } from "./context.js";
|
|
64
|
+
export { useCommand } from "./use-command.js";
|
|
65
|
+
export type { UseCommandOptions } from "./use-command.js";
|
|
66
|
+
export { useActions } from "./use-actions.js";
|
|
67
|
+
export type { ActionDefinition } from "./use-actions.js";
|
package/src/match.ts
CHANGED
|
@@ -1,15 +1,27 @@
|
|
|
1
|
-
import type { KeyEvent } from "@opentui/core"
|
|
2
|
-
import type { ParsedStep } from "./types.js"
|
|
1
|
+
import type { KeyEvent } from "@opentui/core";
|
|
2
|
+
import type { ParsedStep } from "./types.js";
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
5
|
* Check if a KeyEvent matches a ParsedStep.
|
|
6
6
|
*/
|
|
7
|
-
export function matchStep(event: KeyEvent, step: ParsedStep): boolean {
|
|
8
|
-
if (event.name !== step.key)
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
if (event.
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
7
|
+
export const matchStep = function matchStep(event: KeyEvent, step: ParsedStep): boolean {
|
|
8
|
+
if (event.name !== step.key) {
|
|
9
|
+
return false;
|
|
10
|
+
}
|
|
11
|
+
if (event.ctrl !== step.ctrl) {
|
|
12
|
+
return false;
|
|
13
|
+
}
|
|
14
|
+
if (event.meta !== step.meta) {
|
|
15
|
+
return false;
|
|
16
|
+
}
|
|
17
|
+
if (event.shift !== step.shift) {
|
|
18
|
+
return false;
|
|
19
|
+
}
|
|
20
|
+
if (event.option !== step.option) {
|
|
21
|
+
return false;
|
|
22
|
+
}
|
|
23
|
+
if ((event.super ?? false) !== (step.super ?? false)) {
|
|
24
|
+
return false;
|
|
25
|
+
}
|
|
26
|
+
return true;
|
|
27
|
+
};
|
package/src/mode.tsx
CHANGED
|
@@ -1,68 +1,64 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
useContext,
|
|
4
|
-
useState,
|
|
5
|
-
useCallback,
|
|
6
|
-
useMemo,
|
|
7
|
-
useRef,
|
|
8
|
-
type ReactNode,
|
|
9
|
-
} from "react"
|
|
1
|
+
import { createContext, useContext, useState, useCallback, useMemo, useRef } from "react";
|
|
2
|
+
import type { ReactNode } from "react";
|
|
10
3
|
|
|
11
|
-
export type Mode = "cursor" | "insert" | "select"
|
|
4
|
+
export type Mode = "cursor" | "insert" | "select";
|
|
12
5
|
|
|
13
6
|
interface ModeContextValue {
|
|
14
|
-
mode: Mode
|
|
15
|
-
setMode: (mode: Mode) => void
|
|
7
|
+
mode: Mode;
|
|
8
|
+
setMode: (mode: Mode) => void;
|
|
16
9
|
}
|
|
17
10
|
|
|
18
|
-
const ModeContext = createContext<ModeContextValue | null>(null)
|
|
11
|
+
const ModeContext = createContext<ModeContextValue | null>(null);
|
|
19
12
|
|
|
20
13
|
export interface ModeProviderProps {
|
|
21
|
-
children: ReactNode
|
|
22
|
-
initialMode?: Mode
|
|
14
|
+
children: ReactNode;
|
|
15
|
+
initialMode?: Mode;
|
|
23
16
|
/**
|
|
24
17
|
* Internal: called synchronously whenever `setMode` changes the mode (not
|
|
25
18
|
* for same-value calls). Used by the command dispatcher to treat mode
|
|
26
19
|
* changes as transitions (sequence reset) instead of post-render repairs.
|
|
27
20
|
*/
|
|
28
|
-
onModeChange?: (mode: Mode) => void
|
|
21
|
+
onModeChange?: (mode: Mode) => void;
|
|
29
22
|
}
|
|
30
23
|
|
|
31
|
-
export function ModeProvider({
|
|
24
|
+
export const ModeProvider = function ModeProvider({
|
|
32
25
|
children,
|
|
33
26
|
initialMode = "cursor",
|
|
34
27
|
onModeChange,
|
|
35
|
-
}: ModeProviderProps) {
|
|
36
|
-
const [mode,
|
|
37
|
-
const modeRef = useRef(mode)
|
|
38
|
-
const onModeChangeRef = useRef(onModeChange)
|
|
39
|
-
onModeChangeRef.current = onModeChange
|
|
28
|
+
}: ModeProviderProps): ReactNode {
|
|
29
|
+
const [mode, setMode] = useState<Mode>(initialMode);
|
|
30
|
+
const modeRef = useRef(mode);
|
|
31
|
+
const onModeChangeRef = useRef(onModeChange);
|
|
32
|
+
onModeChangeRef.current = onModeChange;
|
|
40
33
|
|
|
41
|
-
const
|
|
42
|
-
if (
|
|
43
|
-
modeRef.current =
|
|
44
|
-
onModeChangeRef.current?.(
|
|
34
|
+
const changeMode = useCallback((nextMode: Mode) => {
|
|
35
|
+
if (nextMode !== modeRef.current) {
|
|
36
|
+
modeRef.current = nextMode;
|
|
37
|
+
onModeChangeRef.current?.(nextMode);
|
|
45
38
|
}
|
|
46
|
-
|
|
47
|
-
}, [])
|
|
39
|
+
setMode(nextMode);
|
|
40
|
+
}, []);
|
|
48
41
|
|
|
49
|
-
const value = useMemo<ModeContextValue>(
|
|
42
|
+
const value = useMemo<ModeContextValue>(
|
|
43
|
+
() => ({ mode, setMode: changeMode }),
|
|
44
|
+
[mode, changeMode],
|
|
45
|
+
);
|
|
50
46
|
|
|
51
|
-
return <ModeContext.Provider value={value}>{children}</ModeContext.Provider
|
|
52
|
-
}
|
|
47
|
+
return <ModeContext.Provider value={value}>{children}</ModeContext.Provider>;
|
|
48
|
+
};
|
|
53
49
|
|
|
54
|
-
export function useMode(): Mode {
|
|
55
|
-
const ctx = useContext(ModeContext)
|
|
50
|
+
export const useMode = function useMode(): Mode {
|
|
51
|
+
const ctx = useContext(ModeContext);
|
|
56
52
|
if (!ctx) {
|
|
57
|
-
throw new Error("useMode must be used within a ModeProvider")
|
|
53
|
+
throw new Error("useMode must be used within a ModeProvider");
|
|
58
54
|
}
|
|
59
|
-
return ctx.mode
|
|
60
|
-
}
|
|
55
|
+
return ctx.mode;
|
|
56
|
+
};
|
|
61
57
|
|
|
62
|
-
export function useSetMode(): (mode: Mode) => void {
|
|
63
|
-
const ctx = useContext(ModeContext)
|
|
58
|
+
export const useSetMode = function useSetMode(): (mode: Mode) => void {
|
|
59
|
+
const ctx = useContext(ModeContext);
|
|
64
60
|
if (!ctx) {
|
|
65
|
-
throw new Error("useSetMode must be used within a ModeProvider")
|
|
61
|
+
throw new Error("useSetMode must be used within a ModeProvider");
|
|
66
62
|
}
|
|
67
|
-
return ctx.setMode
|
|
68
|
-
}
|
|
63
|
+
return ctx.setMode;
|
|
64
|
+
};
|
package/src/parse.ts
CHANGED
|
@@ -1,56 +1,58 @@
|
|
|
1
|
-
import type { ParsedHotkey, ParsedStep } from "./types.js"
|
|
1
|
+
import type { ParsedHotkey, ParsedStep } from "./types.js";
|
|
2
2
|
|
|
3
3
|
const KEY_ALIASES: Record<string, string> = {
|
|
4
|
-
|
|
5
|
-
enter: "return",
|
|
4
|
+
backspace: "backspace",
|
|
6
5
|
cr: "return",
|
|
7
6
|
del: "delete",
|
|
8
|
-
|
|
9
|
-
|
|
7
|
+
enter: "return",
|
|
8
|
+
esc: "escape",
|
|
10
9
|
plus: "+",
|
|
11
|
-
|
|
10
|
+
tab: "tab",
|
|
11
|
+
};
|
|
12
12
|
|
|
13
|
-
function normalizeKey(key: string): string {
|
|
14
|
-
const lower = key.toLowerCase()
|
|
15
|
-
return KEY_ALIASES[lower] ?? lower
|
|
16
|
-
}
|
|
13
|
+
const normalizeKey = function normalizeKey(key: string): string {
|
|
14
|
+
const lower = key.toLowerCase();
|
|
15
|
+
return KEY_ALIASES[lower] ?? lower;
|
|
16
|
+
};
|
|
17
17
|
|
|
18
|
-
const warned = new Set<string>()
|
|
18
|
+
const warned = new Set<string>();
|
|
19
19
|
|
|
20
|
-
function warnOnce(message: string): void {
|
|
21
|
-
if (warned.has(message))
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
20
|
+
const warnOnce = function warnOnce(message: string): void {
|
|
21
|
+
if (warned.has(message)) {
|
|
22
|
+
return;
|
|
23
|
+
}
|
|
24
|
+
warned.add(message);
|
|
25
|
+
console.warn(message);
|
|
26
|
+
};
|
|
25
27
|
|
|
26
|
-
function parseStep(step: string): ParsedStep {
|
|
27
|
-
const parts = step.toLowerCase().split("+")
|
|
28
|
-
let key = ""
|
|
29
|
-
let ctrl = false
|
|
30
|
-
let meta = false
|
|
31
|
-
let shift = false
|
|
32
|
-
let option = false
|
|
33
|
-
let superKey = false
|
|
28
|
+
const parseStep = function parseStep(step: string): ParsedStep {
|
|
29
|
+
const parts = step.toLowerCase().split("+");
|
|
30
|
+
let key = "";
|
|
31
|
+
let ctrl = false;
|
|
32
|
+
let meta = false;
|
|
33
|
+
let shift = false;
|
|
34
|
+
let option = false;
|
|
35
|
+
let superKey = false;
|
|
34
36
|
|
|
35
37
|
for (const part of parts) {
|
|
36
|
-
const trimmed = part.trim()
|
|
38
|
+
const trimmed = part.trim();
|
|
37
39
|
if (trimmed === "ctrl" || trimmed === "control") {
|
|
38
|
-
ctrl = true
|
|
40
|
+
ctrl = true;
|
|
39
41
|
} else if (trimmed === "meta" || trimmed === "alt") {
|
|
40
|
-
meta = true
|
|
42
|
+
meta = true;
|
|
41
43
|
} else if (trimmed === "shift") {
|
|
42
|
-
shift = true
|
|
44
|
+
shift = true;
|
|
43
45
|
} else if (trimmed === "option") {
|
|
44
|
-
option = true
|
|
46
|
+
option = true;
|
|
45
47
|
} else if (trimmed === "super") {
|
|
46
|
-
superKey = true
|
|
48
|
+
superKey = true;
|
|
47
49
|
} else {
|
|
48
|
-
key = normalizeKey(trimmed)
|
|
50
|
+
key = normalizeKey(trimmed);
|
|
49
51
|
}
|
|
50
52
|
}
|
|
51
53
|
|
|
52
|
-
return {
|
|
53
|
-
}
|
|
54
|
+
return { ctrl, key, meta, option, shift, super: superKey };
|
|
55
|
+
};
|
|
54
56
|
|
|
55
57
|
/**
|
|
56
58
|
* Parse a hotkey string into a ParsedHotkey.
|
|
@@ -60,27 +62,27 @@ function parseStep(step: string): ParsedStep {
|
|
|
60
62
|
* - Sequences (space-separated): "g g", "d d"
|
|
61
63
|
* - Leader prefix: "<leader>n" expands to leaderKey + "n"
|
|
62
64
|
*/
|
|
63
|
-
export function parseHotkey(hotkey: string, leaderKey?: string): ParsedHotkey {
|
|
64
|
-
const trimmed = hotkey.trim()
|
|
65
|
+
export const parseHotkey = function parseHotkey(hotkey: string, leaderKey?: string): ParsedHotkey {
|
|
66
|
+
const trimmed = hotkey.trim();
|
|
65
67
|
|
|
66
68
|
// Handle leader prefix
|
|
67
|
-
const leaderMatch =
|
|
69
|
+
const leaderMatch = /^<leader>(?<followingKey>.+)$/u.exec(trimmed);
|
|
68
70
|
if (leaderMatch) {
|
|
69
|
-
if (
|
|
71
|
+
if (leaderKey === undefined || leaderKey === "") {
|
|
70
72
|
// No leader configured: the hotkey must not spring to life on some
|
|
71
73
|
// invented default. Zero steps = unmatchable; the dispatcher skips it.
|
|
72
74
|
warnOnce(
|
|
73
75
|
`[tooee/commands] Hotkey "${trimmed}" uses <leader> but no leader key is configured; the hotkey is disabled.`,
|
|
74
|
-
)
|
|
75
|
-
return { steps: [] }
|
|
76
|
+
);
|
|
77
|
+
return { steps: [] };
|
|
76
78
|
}
|
|
77
|
-
const leaderStep = parseStep(leaderKey)
|
|
78
|
-
const followStep = parseStep(leaderMatch
|
|
79
|
-
return { steps: [leaderStep, followStep] }
|
|
79
|
+
const leaderStep = parseStep(leaderKey);
|
|
80
|
+
const followStep = parseStep(leaderMatch.groups?.followingKey ?? "");
|
|
81
|
+
return { steps: [leaderStep, followStep] };
|
|
80
82
|
}
|
|
81
83
|
|
|
82
84
|
// Space-separated = sequence
|
|
83
|
-
const parts = trimmed.split(/\s+/)
|
|
84
|
-
const steps = parts.map(parseStep)
|
|
85
|
-
return { steps }
|
|
86
|
-
}
|
|
85
|
+
const parts = trimmed.split(/\s+/u);
|
|
86
|
+
const steps = parts.map(parseStep);
|
|
87
|
+
return { steps };
|
|
88
|
+
};
|
package/src/sequence.ts
CHANGED
|
@@ -1,22 +1,23 @@
|
|
|
1
|
-
import type { KeyEvent } from "@opentui/core"
|
|
2
|
-
import type { ParsedHotkey } from "./types.js"
|
|
3
|
-
import { matchStep } from "./match.js"
|
|
1
|
+
import type { KeyEvent } from "@opentui/core";
|
|
2
|
+
import type { ParsedHotkey } from "./types.js";
|
|
3
|
+
import { matchStep } from "./match.js";
|
|
4
4
|
|
|
5
|
-
export const DEFAULT_SEQUENCE_TIMEOUT_MS = 1500
|
|
5
|
+
export const DEFAULT_SEQUENCE_TIMEOUT_MS = 1500;
|
|
6
6
|
|
|
7
7
|
export interface SequenceTrackerOptions {
|
|
8
|
-
|
|
9
|
-
|
|
8
|
+
/** Timeout in milliseconds; defaults to DEFAULT_SEQUENCE_TIMEOUT_MS. */
|
|
9
|
+
timeout?: number;
|
|
10
|
+
onReset?: () => void;
|
|
10
11
|
}
|
|
11
12
|
|
|
12
13
|
export interface SequencePendingMatch {
|
|
13
|
-
prefixLength: number
|
|
14
|
-
indexes: number[]
|
|
14
|
+
prefixLength: number;
|
|
15
|
+
indexes: number[];
|
|
15
16
|
}
|
|
16
17
|
|
|
17
18
|
export interface SequenceFeedResult {
|
|
18
|
-
matchedIndex: number
|
|
19
|
-
pending: SequencePendingMatch | null
|
|
19
|
+
matchedIndex: number;
|
|
20
|
+
pending: SequencePendingMatch | null;
|
|
20
21
|
}
|
|
21
22
|
|
|
22
23
|
// --- Pure matching helpers ---------------------------------------------------
|
|
@@ -29,81 +30,98 @@ export interface SequenceFeedResult {
|
|
|
29
30
|
* Zero-step hotkeys (e.g. a disabled leaderless `<leader>` binding) never
|
|
30
31
|
* match anything.
|
|
31
32
|
*/
|
|
32
|
-
export
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
if (!matchStep(buffer[start + i]!, steps[i]!)) return false
|
|
33
|
+
export const matchesBuffer = function matchesBuffer(
|
|
34
|
+
buffer: readonly KeyEvent[],
|
|
35
|
+
hotkey: ParsedHotkey,
|
|
36
|
+
): boolean {
|
|
37
|
+
const { steps } = hotkey;
|
|
38
|
+
if (steps.length === 0) {
|
|
39
|
+
return false;
|
|
40
40
|
}
|
|
41
|
-
|
|
42
|
-
|
|
41
|
+
if (buffer.length < steps.length) {
|
|
42
|
+
return false;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const start = buffer.length - steps.length;
|
|
46
|
+
for (let i = 0; i < steps.length; i += 1) {
|
|
47
|
+
const event = buffer[start + i];
|
|
48
|
+
const step = steps[i];
|
|
49
|
+
if (event === undefined || step === undefined || !matchStep(event, step)) {
|
|
50
|
+
return false;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
return true;
|
|
54
|
+
};
|
|
43
55
|
|
|
44
56
|
/**
|
|
45
57
|
* Find the longest buffer tail that is a proper prefix of at least one hotkey.
|
|
46
58
|
* Returns the prefix length and the indexes of the hotkeys it could still
|
|
47
59
|
* complete, or null when nothing is pending.
|
|
48
60
|
*/
|
|
49
|
-
export function findPendingMatch(
|
|
61
|
+
export const findPendingMatch = function findPendingMatch(
|
|
50
62
|
buffer: readonly KeyEvent[],
|
|
51
63
|
hotkeys: readonly ParsedHotkey[],
|
|
52
64
|
): SequencePendingMatch | null {
|
|
53
65
|
const maxPrefixLength = Math.min(
|
|
54
66
|
buffer.length,
|
|
55
67
|
Math.max(0, ...hotkeys.map((h) => h.steps.length - 1)),
|
|
56
|
-
)
|
|
68
|
+
);
|
|
57
69
|
|
|
58
|
-
for (let prefixLength = maxPrefixLength; prefixLength > 0; prefixLength
|
|
59
|
-
const start = buffer.length - prefixLength
|
|
60
|
-
const indexes: number[] = []
|
|
70
|
+
for (let prefixLength = maxPrefixLength; prefixLength > 0; prefixLength -= 1) {
|
|
71
|
+
const start = buffer.length - prefixLength;
|
|
72
|
+
const indexes: number[] = [];
|
|
61
73
|
|
|
62
|
-
for (let hotkeyIndex = 0; hotkeyIndex < hotkeys.length; hotkeyIndex
|
|
63
|
-
const hotkey = hotkeys[hotkeyIndex]
|
|
64
|
-
if (hotkey.steps.length <= prefixLength)
|
|
74
|
+
for (let hotkeyIndex = 0; hotkeyIndex < hotkeys.length; hotkeyIndex += 1) {
|
|
75
|
+
const hotkey = hotkeys[hotkeyIndex];
|
|
76
|
+
if (hotkey.steps.length <= prefixLength) {
|
|
77
|
+
continue;
|
|
78
|
+
}
|
|
65
79
|
|
|
66
|
-
let matches = true
|
|
67
|
-
for (let i = 0; i < prefixLength; i
|
|
68
|
-
if (!matchStep(buffer[start + i]
|
|
69
|
-
matches = false
|
|
70
|
-
break
|
|
80
|
+
let matches = true;
|
|
81
|
+
for (let i = 0; i < prefixLength; i += 1) {
|
|
82
|
+
if (!matchStep(buffer[start + i], hotkey.steps[i])) {
|
|
83
|
+
matches = false;
|
|
84
|
+
break;
|
|
71
85
|
}
|
|
72
86
|
}
|
|
73
87
|
|
|
74
|
-
if (matches)
|
|
88
|
+
if (matches) {
|
|
89
|
+
indexes.push(hotkeyIndex);
|
|
90
|
+
}
|
|
75
91
|
}
|
|
76
92
|
|
|
77
|
-
if (indexes.length > 0)
|
|
93
|
+
if (indexes.length > 0) {
|
|
94
|
+
return { indexes, prefixLength };
|
|
95
|
+
}
|
|
78
96
|
}
|
|
79
97
|
|
|
80
|
-
return null
|
|
81
|
-
}
|
|
98
|
+
return null;
|
|
99
|
+
};
|
|
82
100
|
|
|
83
101
|
/**
|
|
84
102
|
* Drop buffer entries older than the longest hotkey could ever consume.
|
|
85
103
|
* Returns the same array when nothing needs pruning.
|
|
86
104
|
*/
|
|
87
|
-
export function pruneBuffer(
|
|
105
|
+
export const pruneBuffer = function pruneBuffer(
|
|
88
106
|
buffer: readonly KeyEvent[],
|
|
89
107
|
hotkeys: readonly ParsedHotkey[],
|
|
90
108
|
): readonly KeyEvent[] {
|
|
91
|
-
const maxLen = Math.max(0, ...hotkeys.map((h) => h.steps.length))
|
|
109
|
+
const maxLen = Math.max(0, ...hotkeys.map((h) => h.steps.length));
|
|
92
110
|
if (maxLen > 0 && buffer.length > maxLen) {
|
|
93
|
-
return buffer.slice(buffer.length - maxLen)
|
|
111
|
+
return buffer.slice(buffer.length - maxLen);
|
|
94
112
|
}
|
|
95
|
-
return buffer
|
|
96
|
-
}
|
|
113
|
+
return buffer;
|
|
114
|
+
};
|
|
97
115
|
|
|
98
116
|
export class SequenceTracker {
|
|
99
|
-
private buffer: KeyEvent[] = []
|
|
100
|
-
private timer: ReturnType<typeof setTimeout> | null = null
|
|
101
|
-
private timeout: number
|
|
102
|
-
private onReset?: () => void
|
|
117
|
+
private buffer: KeyEvent[] = [];
|
|
118
|
+
private timer: ReturnType<typeof setTimeout> | null = null;
|
|
119
|
+
private readonly timeout: number;
|
|
120
|
+
private readonly onReset?: () => void;
|
|
103
121
|
|
|
104
122
|
constructor(options?: SequenceTrackerOptions) {
|
|
105
|
-
this.timeout = options?.timeout ?? DEFAULT_SEQUENCE_TIMEOUT_MS
|
|
106
|
-
this.onReset = options?.onReset
|
|
123
|
+
this.timeout = options?.timeout ?? DEFAULT_SEQUENCE_TIMEOUT_MS;
|
|
124
|
+
this.onReset = options?.onReset;
|
|
107
125
|
}
|
|
108
126
|
|
|
109
127
|
/**
|
|
@@ -111,49 +129,53 @@ export class SequenceTracker {
|
|
|
111
129
|
* Returns the index of the matched hotkey, or -1 if no full match.
|
|
112
130
|
*/
|
|
113
131
|
feed(event: KeyEvent, hotkeys: ParsedHotkey[]): number {
|
|
114
|
-
return this.feedWithState(event, hotkeys).matchedIndex
|
|
132
|
+
return this.feedWithState(event, hotkeys).matchedIndex;
|
|
115
133
|
}
|
|
116
134
|
|
|
117
135
|
/**
|
|
118
136
|
* Feed a key event and return both full-match and pending-prefix state.
|
|
119
137
|
*/
|
|
120
138
|
feedWithState(event: KeyEvent, hotkeys: ParsedHotkey[]): SequenceFeedResult {
|
|
121
|
-
this.buffer.push(event)
|
|
122
|
-
this.resetTimer()
|
|
139
|
+
this.buffer.push(event);
|
|
140
|
+
this.resetTimer();
|
|
123
141
|
|
|
124
|
-
for (let i = 0; i < hotkeys.length; i
|
|
125
|
-
if (matchesBuffer(this.buffer, hotkeys[i]
|
|
126
|
-
this.reset()
|
|
127
|
-
return { matchedIndex: i, pending: null }
|
|
142
|
+
for (let i = 0; i < hotkeys.length; i += 1) {
|
|
143
|
+
if (matchesBuffer(this.buffer, hotkeys[i])) {
|
|
144
|
+
this.reset();
|
|
145
|
+
return { matchedIndex: i, pending: null };
|
|
128
146
|
}
|
|
129
147
|
}
|
|
130
148
|
|
|
131
149
|
// Prune buffer if no hotkey could possibly match
|
|
132
|
-
this.buffer = [...pruneBuffer(this.buffer, hotkeys)]
|
|
150
|
+
this.buffer = [...pruneBuffer(this.buffer, hotkeys)];
|
|
133
151
|
|
|
134
|
-
return { matchedIndex: -1, pending: findPendingMatch(this.buffer, hotkeys) }
|
|
152
|
+
return { matchedIndex: -1, pending: findPendingMatch(this.buffer, hotkeys) };
|
|
135
153
|
}
|
|
136
154
|
|
|
137
155
|
getPendingMatch(hotkeys: ParsedHotkey[]): SequencePendingMatch | null {
|
|
138
|
-
return findPendingMatch(this.buffer, hotkeys)
|
|
156
|
+
return findPendingMatch(this.buffer, hotkeys);
|
|
139
157
|
}
|
|
140
158
|
|
|
141
159
|
reset(): void {
|
|
142
|
-
const hadBuffer = this.buffer.length > 0
|
|
143
|
-
this.buffer = []
|
|
144
|
-
this.clearTimer()
|
|
145
|
-
if (hadBuffer)
|
|
160
|
+
const hadBuffer = this.buffer.length > 0;
|
|
161
|
+
this.buffer = [];
|
|
162
|
+
this.clearTimer();
|
|
163
|
+
if (hadBuffer) {
|
|
164
|
+
this.onReset?.();
|
|
165
|
+
}
|
|
146
166
|
}
|
|
147
167
|
|
|
148
168
|
private resetTimer(): void {
|
|
149
|
-
this.clearTimer()
|
|
150
|
-
this.timer = setTimeout(() =>
|
|
169
|
+
this.clearTimer();
|
|
170
|
+
this.timer = setTimeout(() => {
|
|
171
|
+
this.reset();
|
|
172
|
+
}, this.timeout);
|
|
151
173
|
}
|
|
152
174
|
|
|
153
175
|
private clearTimer(): void {
|
|
154
176
|
if (this.timer !== null) {
|
|
155
|
-
clearTimeout(this.timer)
|
|
156
|
-
this.timer = null
|
|
177
|
+
clearTimeout(this.timer);
|
|
178
|
+
this.timer = null;
|
|
157
179
|
}
|
|
158
180
|
}
|
|
159
181
|
}
|