@inklu/keys 2.2.0 → 3.0.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 +67 -213
- package/dist/attributes.d.mts +1 -1
- package/dist/{index-DRXMJmbc.d.mts → index-BnB38jCF.d.mts} +20 -54
- package/dist/index.d.mts +3 -3
- package/dist/index.mjs +2 -2
- package/dist/react.d.mts +7 -12
- package/dist/react.mjs +9 -90
- package/dist/{src-TaZw4V61.mjs → src-CGKvYKjd.mjs} +331 -795
- package/dist/{types-D-hUKGoN.d.mts → types-BxMI3b4a.d.mts} +16 -117
- package/package.json +2 -3
package/dist/index.mjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { a as
|
|
2
|
-
export { ariaKeyShortcuts, bindingTokens, createCommands, createRebinder, exportKeymap, formatBinding, importKeymap, isSingleKeyBinding,
|
|
1
|
+
import { a as webStorage, c as formatBinding, i as importKeymap, l as isSingleKeyBinding, n as createRebinder, o as ariaKeyShortcuts, r as exportKeymap, s as bindingTokens, t as createCommands, u as seq } from "./src-CGKvYKjd.mjs";
|
|
2
|
+
export { ariaKeyShortcuts, bindingTokens, createCommands, createRebinder, exportKeymap, formatBinding, importKeymap, isSingleKeyBinding, seq, webStorage };
|
package/dist/react.d.mts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { C as
|
|
2
|
-
import {
|
|
1
|
+
import { C as Chord, S as SerializedKeymap, T as Platform, _ as RunBlockReason, a as CommandDefinition, b as SequenceBinding, c as CommandSource, d as Conflict, f as ConflictOptions, g as PlatformBindings, h as KeymapStore, i as CommandContext, l as Commands, m as KeyCapture, n as BindingInput, o as CommandHandler, p as Json, r as Command, s as CommandKeymap, t as Binding, u as CommandsOptions, v as RunOptions, w as Key, x as SerializedBinding, y as ScopeOptions } from "./types-BxMI3b4a.mjs";
|
|
2
|
+
import { a as Rebinder, c as importKeymap, d as bindingTokens, f as formatBinding, i as RebindState, l as webStorage, m as seq, n as createCommands, o as createRebinder, p as isSingleKeyBinding, r as RebindOptions, s as exportKeymap, t as CommandId, u as ariaKeyShortcuts } from "./index-BnB38jCF.mjs";
|
|
3
3
|
//#region src/react/hooks.d.ts
|
|
4
4
|
/**
|
|
5
5
|
* Mounts a commands registry for the lifecycle of the component.
|
|
@@ -22,15 +22,13 @@ export declare function useCommands<Id extends string>(registry: Commands<Id>):
|
|
|
22
22
|
/** Returns one command's snapshot, re-rendering only when that command changes. */
|
|
23
23
|
export declare function useCommand<Id extends string>(registry: Commands<Id>, id: NoInfer<Id>): Command<Id> | undefined;
|
|
24
24
|
/**
|
|
25
|
-
* Returns
|
|
26
|
-
*
|
|
27
|
-
* (chords only), and `spoken` for screen-reader text such as `"G then I"`.
|
|
25
|
+
* Returns a command's primary binding for people: `display` to read,
|
|
26
|
+
* `tokens` for `<kbd>` elements, and `aria` for `aria-keyshortcuts` (chords only).
|
|
28
27
|
*/
|
|
29
28
|
export declare function useShortcut<Id extends string>(registry: Commands<Id>, id: NoInfer<Id>): {
|
|
30
29
|
display: string;
|
|
31
30
|
tokens: ReadonlyArray<string>;
|
|
32
31
|
aria: string;
|
|
33
|
-
spoken: string;
|
|
34
32
|
};
|
|
35
33
|
/**
|
|
36
34
|
* Activates a scope for the lifecycle of the component. Scopes rank in the
|
|
@@ -65,17 +63,14 @@ interface UseRebindResult<Id extends string = string> extends RebindState<Id> {
|
|
|
65
63
|
/** Whether the shortcut has been customized by the user. */
|
|
66
64
|
overridden: boolean;
|
|
67
65
|
start: () => void;
|
|
68
|
-
stop: () => void;
|
|
69
66
|
cancel: () => void;
|
|
70
67
|
commit: () => void;
|
|
68
|
+
/** Unbinds the command. */
|
|
71
69
|
clear: () => void;
|
|
70
|
+
/** Restores the default shortcut. */
|
|
72
71
|
reset: () => void;
|
|
73
72
|
}
|
|
74
73
|
/** Hook for recording and configuring a command's keybindings. */
|
|
75
74
|
export declare function useRebind<Id extends string>(registry: Commands<Id>, id: NoInfer<Id>, options?: RebindOptions<Id>): UseRebindResult<Id>;
|
|
76
|
-
/** Returns the keys held down right now, by the name `KeyboardEvent.key` gives each. */
|
|
77
|
-
export declare function useHeldKeys(): ReadonlyArray<string>;
|
|
78
|
-
/** Returns the half-typed sequences, for a hint such as "G …". */
|
|
79
|
-
export declare function usePendingSequences<Id extends string>(registry: Commands<Id>): ReadonlyArray<PendingSequence<Id>>;
|
|
80
75
|
//#endregion
|
|
81
|
-
export { type Binding, type BindingInput, type Chord, type Command, type CommandContext, type CommandDefinition, type CommandHandler, type CommandId, type CommandKeymap, type CommandSource, type Commands, type CommandsOptions, type Conflict, type ConflictOptions, type
|
|
76
|
+
export { type Binding, type BindingInput, type Chord, type Command, type CommandContext, type CommandDefinition, type CommandHandler, type CommandId, type CommandKeymap, type CommandSource, type Commands, type CommandsOptions, type Conflict, type ConflictOptions, type Json, type Key, type KeyCapture, type KeymapStore, type Platform, type PlatformBindings, type RebindOptions, type RebindState, type Rebinder, type RunBlockReason, type RunOptions, type ScopeOptions, type SequenceBinding, type SerializedBinding, type SerializedKeymap, type UseRebindResult, ariaKeyShortcuts, bindingTokens, createCommands, createRebinder, exportKeymap, formatBinding, importKeymap, isSingleKeyBinding, seq, webStorage };
|
package/dist/react.mjs
CHANGED
|
@@ -1,66 +1,8 @@
|
|
|
1
1
|
"use client";
|
|
2
|
-
import { a as
|
|
2
|
+
import { a as webStorage, c as formatBinding, i as importKeymap, l as isSingleKeyBinding, n as createRebinder, o as ariaKeyShortcuts, r as exportKeymap, s as bindingTokens, t as createCommands, u as seq } from "./src-CGKvYKjd.mjs";
|
|
3
3
|
import { useCallback, useEffect, useMemo, useRef, useSyncExternalStore } from "react";
|
|
4
|
-
//#region src/held.ts
|
|
5
|
-
/** Which keys are down right now. Listens only while something is subscribed. */
|
|
6
|
-
const NONE = [];
|
|
7
|
-
/** Held keys by physical key, so two keys producing one character stay apart. */
|
|
8
|
-
const down = /* @__PURE__ */ new Map();
|
|
9
|
-
const listeners = /* @__PURE__ */ new Set();
|
|
10
|
-
let held = NONE;
|
|
11
|
-
const identify = (event) => event.code || `key:${keyName(event.key)}`;
|
|
12
|
-
function publish() {
|
|
13
|
-
held = down.size === 0 ? NONE : [...new Set(down.values())];
|
|
14
|
-
for (const listener of listeners) listener();
|
|
15
|
-
}
|
|
16
|
-
function onKeyDown(event) {
|
|
17
|
-
if (!event.key || down.has(identify(event))) return;
|
|
18
|
-
down.set(identify(event), keyName(event.key));
|
|
19
|
-
publish();
|
|
20
|
-
}
|
|
21
|
-
function onKeyUp(event) {
|
|
22
|
-
down.delete(identify(event));
|
|
23
|
-
if (event.key === "Meta") {
|
|
24
|
-
for (const [identity, key] of down) if (!isModifierKey(key)) down.delete(identity);
|
|
25
|
-
}
|
|
26
|
-
publish();
|
|
27
|
-
}
|
|
28
|
-
/** Leaving the window or hiding the page swallows the keyups, so nothing is held any more. */
|
|
29
|
-
function onLeave() {
|
|
30
|
-
if (down.size === 0) return;
|
|
31
|
-
down.clear();
|
|
32
|
-
publish();
|
|
33
|
-
}
|
|
34
|
-
/** The keys held right now, by name. Empty unless something is subscribed. */
|
|
35
|
-
function heldKeys() {
|
|
36
|
-
return held;
|
|
37
|
-
}
|
|
38
|
-
/** Watches held keys until the returned function is called. */
|
|
39
|
-
function subscribeHeldKeys(listener) {
|
|
40
|
-
listeners.add(listener);
|
|
41
|
-
if (listeners.size === 1) {
|
|
42
|
-
document.addEventListener("keydown", onKeyDown, true);
|
|
43
|
-
document.addEventListener("keyup", onKeyUp, true);
|
|
44
|
-
document.addEventListener("visibilitychange", onLeave);
|
|
45
|
-
window.addEventListener("blur", onLeave);
|
|
46
|
-
}
|
|
47
|
-
return () => {
|
|
48
|
-
listeners.delete(listener);
|
|
49
|
-
if (listeners.size > 0) return;
|
|
50
|
-
document.removeEventListener("keydown", onKeyDown, true);
|
|
51
|
-
document.removeEventListener("keyup", onKeyUp, true);
|
|
52
|
-
document.removeEventListener("visibilitychange", onLeave);
|
|
53
|
-
window.removeEventListener("blur", onLeave);
|
|
54
|
-
down.clear();
|
|
55
|
-
held = NONE;
|
|
56
|
-
};
|
|
57
|
-
}
|
|
58
|
-
//#endregion
|
|
59
4
|
//#region src/react/hooks.ts
|
|
60
5
|
const EMPTY_TOKENS = [];
|
|
61
|
-
const EMPTY_PENDING = [];
|
|
62
|
-
const getEmptyTokens = () => EMPTY_TOKENS;
|
|
63
|
-
const getEmptyPending = () => EMPTY_PENDING;
|
|
64
6
|
/**
|
|
65
7
|
* Mounts a commands registry for the lifecycle of the component.
|
|
66
8
|
*
|
|
@@ -94,17 +36,15 @@ function useCommand(registry, id) {
|
|
|
94
36
|
return useSyncExternalStore(subscribe, getSnapshot, getServerSnapshot);
|
|
95
37
|
}
|
|
96
38
|
/**
|
|
97
|
-
* Returns
|
|
98
|
-
*
|
|
99
|
-
* (chords only), and `spoken` for screen-reader text such as `"G then I"`.
|
|
39
|
+
* Returns a command's primary binding for people: `display` to read,
|
|
40
|
+
* `tokens` for `<kbd>` elements, and `aria` for `aria-keyshortcuts` (chords only).
|
|
100
41
|
*/
|
|
101
42
|
function useShortcut(registry, id) {
|
|
102
43
|
const command = useCommand(registry, id);
|
|
103
44
|
return useMemo(() => ({
|
|
104
45
|
display: command?.display[0] ?? "",
|
|
105
46
|
tokens: command?.tokens[0] ?? EMPTY_TOKENS,
|
|
106
|
-
aria: command?.aria ?? ""
|
|
107
|
-
spoken: command?.spoken ?? ""
|
|
47
|
+
aria: command?.aria ?? ""
|
|
108
48
|
}), [command]);
|
|
109
49
|
}
|
|
110
50
|
/**
|
|
@@ -157,31 +97,21 @@ function useCommandHandler(registry, id, run, enabled = true) {
|
|
|
157
97
|
function useRebind(registry, id, options = {}) {
|
|
158
98
|
const latest = useRef(options);
|
|
159
99
|
latest.current = options;
|
|
160
|
-
const
|
|
100
|
+
const sequence = options.sequence === true;
|
|
161
101
|
const rebinder = useMemo(() => createRebinder(registry, id, {
|
|
162
|
-
|
|
102
|
+
sequence,
|
|
163
103
|
get apply() {
|
|
164
104
|
return latest.current.apply;
|
|
165
105
|
},
|
|
166
|
-
get add() {
|
|
167
|
-
return latest.current.add;
|
|
168
|
-
},
|
|
169
106
|
get rejectConflicts() {
|
|
170
107
|
return latest.current.rejectConflicts;
|
|
171
108
|
},
|
|
172
|
-
get ignoreInputs() {
|
|
173
|
-
return latest.current.ignoreInputs;
|
|
174
|
-
},
|
|
175
|
-
get idleTimeout() {
|
|
176
|
-
return latest.current.idleTimeout;
|
|
177
|
-
},
|
|
178
109
|
onRecord: (binding, conflicts) => latest.current.onRecord?.(binding, conflicts),
|
|
179
|
-
onCancel: () => latest.current.onCancel?.()
|
|
180
|
-
onClear: () => latest.current.onClear?.()
|
|
110
|
+
onCancel: () => latest.current.onCancel?.()
|
|
181
111
|
}), [
|
|
182
112
|
registry,
|
|
183
113
|
id,
|
|
184
|
-
|
|
114
|
+
sequence
|
|
185
115
|
]);
|
|
186
116
|
useEffect(() => rebinder.destroy, [rebinder]);
|
|
187
117
|
const getState = useCallback(() => rebinder.state, [rebinder]);
|
|
@@ -193,7 +123,6 @@ function useRebind(registry, id, options = {}) {
|
|
|
193
123
|
tokens: command?.tokens[0] ?? EMPTY_TOKENS,
|
|
194
124
|
overridden: command?.overridden ?? false,
|
|
195
125
|
start: rebinder.start,
|
|
196
|
-
stop: rebinder.stop,
|
|
197
126
|
cancel: rebinder.cancel,
|
|
198
127
|
commit: rebinder.commit,
|
|
199
128
|
clear: () => registry.keymap.set(id, null),
|
|
@@ -206,15 +135,5 @@ function useRebind(registry, id, options = {}) {
|
|
|
206
135
|
id
|
|
207
136
|
]);
|
|
208
137
|
}
|
|
209
|
-
/** Returns the keys held down right now, by the name `KeyboardEvent.key` gives each. */
|
|
210
|
-
function useHeldKeys() {
|
|
211
|
-
return useSyncExternalStore(subscribeHeldKeys, heldKeys, getEmptyTokens);
|
|
212
|
-
}
|
|
213
|
-
/** Returns the half-typed sequences, for a hint such as "G …". */
|
|
214
|
-
function usePendingSequences(registry) {
|
|
215
|
-
const subscribe = useCallback((onChange) => registry.subscribePending(onChange), [registry]);
|
|
216
|
-
const getSnapshot = useCallback(() => registry.pending(), [registry]);
|
|
217
|
-
return useSyncExternalStore(subscribe, getSnapshot, getEmptyPending);
|
|
218
|
-
}
|
|
219
138
|
//#endregion
|
|
220
|
-
export { ariaKeyShortcuts, bindingTokens, createCommands, createRebinder, exportKeymap, formatBinding, importKeymap, isSingleKeyBinding,
|
|
139
|
+
export { ariaKeyShortcuts, bindingTokens, createCommands, createRebinder, exportKeymap, formatBinding, importKeymap, isSingleKeyBinding, seq, useCommand, useCommandHandler, useCommands, useMountCommands, useRebind, useScope, useShortcut, webStorage };
|