@tanstack/lit-hotkeys 0.24.0 → 0.26.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/dist/controllers/_server.js +10 -3
- package/dist/controllers/app-hotkeys.d.ts +10 -5
- package/dist/controllers/app-hotkeys.js +11 -6
- package/dist/controllers/format-for-display.d.ts +77 -0
- package/dist/controllers/format-for-display.js +94 -0
- package/dist/controllers/hotkey-registrations.d.ts +2 -1
- package/dist/controllers/hotkey-registrations.js +2 -1
- package/dist/controllers/index.d.ts +3 -1
- package/dist/controllers/keyboard-layout.d.ts +60 -0
- package/dist/controllers/keyboard-layout.js +92 -0
- package/dist/format-for-display.d.ts +12 -0
- package/dist/format-for-display.js +15 -0
- package/dist/index.d.ts +4 -1
- package/dist/index.js +4 -1
- package/package.json +3 -2
- package/skills/lit-hotkeys/SKILL.md +17 -16
- package/skills/lit-hotkeys/references/app-hotkeys.md +8 -5
|
@@ -1,11 +1,18 @@
|
|
|
1
|
+
import { isServer } from "lit";
|
|
2
|
+
|
|
1
3
|
// src/controllers/_server.ts
|
|
4
|
+
const nodeBuild = isServer;
|
|
2
5
|
/**
|
|
3
6
|
* Whether the code runs on a server. With `@lit-labs/ssr`'s
|
|
4
7
|
* `connectedCallback: true` option, the server calls `hostConnected` and never
|
|
5
8
|
* calls `hostDisconnected`, so controllers must not subscribe there.
|
|
9
|
+
*
|
|
10
|
+
* `@lit-labs/ssr`'s DOM shim defines a `document` with no window. Lit's own
|
|
11
|
+
* `isServer` is also true where Lit's Node build runs with a real DOM, such as
|
|
12
|
+
* tests in happy-dom or jsdom, and controllers must work there.
|
|
6
13
|
*/
|
|
7
|
-
function isServer() {
|
|
8
|
-
return typeof document === "undefined";
|
|
14
|
+
function isServer$1() {
|
|
15
|
+
return typeof document === "undefined" || nodeBuild && !document.defaultView;
|
|
9
16
|
}
|
|
10
17
|
|
|
11
|
-
export { isServer };
|
|
18
|
+
export { isServer$1 as isServer };
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { AppHotkey, AppHotkeyListItem, CustomHotkeys, Hotkey, RegisteredAppHotkeys } from "@tanstack/hotkeys";
|
|
1
|
+
import { AppHotkey, AppHotkeyListItem, CustomHotkeys, Hotkey, RecordedKeyPresses, RegisteredAppHotkeys } from "@tanstack/hotkeys";
|
|
2
2
|
import { ReactiveController, ReactiveControllerHost } from "lit";
|
|
3
3
|
/**
|
|
4
4
|
* A Lit ReactiveController for app hotkeys provided with `provideAppHotkeys`:
|
|
@@ -32,15 +32,20 @@ export declare class AppHotkeysController implements ReactiveController {
|
|
|
32
32
|
get customHotkeys(): CustomHotkeys;
|
|
33
33
|
/**
|
|
34
34
|
* Sets the user's hotkey for an app hotkey. `null` unbinds it.
|
|
35
|
-
* Returns conflicting app hotkeys; when any exist, nothing changes.
|
|
35
|
+
* Returns conflicting app hotkeys; when any exist, nothing changes. Pass
|
|
36
|
+
* the recorder's `context` for a recorded hotkey, as for `findConflicts`.
|
|
36
37
|
*/
|
|
37
|
-
setCustomHotkey(id: keyof RegisteredAppHotkeys & string, hotkey: Hotkey | null): Array<AppHotkey>;
|
|
38
|
+
setCustomHotkey(id: keyof RegisteredAppHotkeys & string, hotkey: Hotkey | null, recordedWith?: RecordedKeyPresses): Array<AppHotkey>;
|
|
38
39
|
/** Restores the default hotkey for an app hotkey. */
|
|
39
40
|
resetCustomHotkey(id: keyof RegisteredAppHotkeys & string): void;
|
|
40
41
|
/** Restores every default hotkey. */
|
|
41
42
|
resetCustomHotkeys(): void;
|
|
42
|
-
/**
|
|
43
|
-
|
|
43
|
+
/**
|
|
44
|
+
* Other app hotkeys that would collide if `id` used `hotkey`. Pass the
|
|
45
|
+
* recorder's `context` for a recorded hotkey: it has the key press, which
|
|
46
|
+
* also finds a hotkey written differently that the same press fires.
|
|
47
|
+
*/
|
|
48
|
+
findConflicts(id: keyof RegisteredAppHotkeys & string, hotkey: Hotkey, recordedWith?: RecordedKeyPresses): Array<AppHotkey>;
|
|
44
49
|
hostConnected(): void;
|
|
45
50
|
hostDisconnected(): void;
|
|
46
51
|
/**
|
|
@@ -49,10 +49,11 @@ var AppHotkeysController = class {
|
|
|
49
49
|
}
|
|
50
50
|
/**
|
|
51
51
|
* Sets the user's hotkey for an app hotkey. `null` unbinds it.
|
|
52
|
-
* Returns conflicting app hotkeys; when any exist, nothing changes.
|
|
52
|
+
* Returns conflicting app hotkeys; when any exist, nothing changes. Pass
|
|
53
|
+
* the recorder's `context` for a recorded hotkey, as for `findConflicts`.
|
|
53
54
|
*/
|
|
54
|
-
setCustomHotkey(id, hotkey) {
|
|
55
|
-
return requireStore().setCustomHotkey(id, hotkey);
|
|
55
|
+
setCustomHotkey(id, hotkey, recordedWith) {
|
|
56
|
+
return requireStore().setCustomHotkey(id, hotkey, recordedWith);
|
|
56
57
|
}
|
|
57
58
|
/** Restores the default hotkey for an app hotkey. */
|
|
58
59
|
resetCustomHotkey(id) {
|
|
@@ -62,9 +63,13 @@ var AppHotkeysController = class {
|
|
|
62
63
|
resetCustomHotkeys() {
|
|
63
64
|
requireStore().resetCustomHotkeys();
|
|
64
65
|
}
|
|
65
|
-
/**
|
|
66
|
-
|
|
67
|
-
|
|
66
|
+
/**
|
|
67
|
+
* Other app hotkeys that would collide if `id` used `hotkey`. Pass the
|
|
68
|
+
* recorder's `context` for a recorded hotkey: it has the key press, which
|
|
69
|
+
* also finds a hotkey written differently that the same press fires.
|
|
70
|
+
*/
|
|
71
|
+
findConflicts(id, hotkey, recordedWith) {
|
|
72
|
+
return requireStore().findConflicts(id, hotkey, recordedWith);
|
|
68
73
|
}
|
|
69
74
|
hostConnected() {
|
|
70
75
|
if (this._unsubscribe) return;
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { HotkeyPlatform, formatForDisplay } from "@tanstack/hotkeys";
|
|
2
|
+
import { ReactiveController, ReactiveControllerHost } from "lit";
|
|
3
|
+
/** Options for `FormatForDisplayController`. */
|
|
4
|
+
export interface FormatForDisplayControllerOptions {
|
|
5
|
+
/**
|
|
6
|
+
* The user's platform, for every label from `format`. Detected when unset.
|
|
7
|
+
* On a server, set it from the request with `detectPlatform(userAgent)`.
|
|
8
|
+
*/
|
|
9
|
+
platform?: HotkeyPlatform;
|
|
10
|
+
/**
|
|
11
|
+
* Whether labels follow the user's keyboard layout, so `[KeyQ]` reads `A`
|
|
12
|
+
* on a French AZERTY keyboard. The layout loads from the browser when the
|
|
13
|
+
* first host connects. Only Chromium browsers report one. Set `false` to
|
|
14
|
+
* never ask the browser for it. Default: `true`.
|
|
15
|
+
*/
|
|
16
|
+
keyboardLayout?: boolean;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* A Lit ReactiveController whose `format` is `formatForDisplay` with the
|
|
20
|
+
* controller's `platform` option, or the detected platform without one. A
|
|
21
|
+
* `platform` passed to `format` wins.
|
|
22
|
+
*
|
|
23
|
+
* Physical bindings such as `[KeyQ]` are labeled with the character the key
|
|
24
|
+
* types for this user in Chromium browsers, once the keyboard layout has
|
|
25
|
+
* loaded. The host updates when the layout loads or changes. See
|
|
26
|
+
* `KeyboardLayoutController`. A `layoutMap` passed to `format` wins. Labels
|
|
27
|
+
* have no layout on the server and for the host's first render, so a
|
|
28
|
+
* server-rendered element hydrates unchanged. Firefox and Safari report no
|
|
29
|
+
* layout, so labels there keep the key's US character.
|
|
30
|
+
*
|
|
31
|
+
* Lit has no provider, so the options are the only defaults. Pass a getter to
|
|
32
|
+
* read them from host properties. Server-rendered elements should set
|
|
33
|
+
* `platform` from the request with `detectPlatform(userAgent)`, so the server
|
|
34
|
+
* HTML matches the user's platform.
|
|
35
|
+
*
|
|
36
|
+
* @example
|
|
37
|
+
* ```ts
|
|
38
|
+
* class SaveButton extends LitElement {
|
|
39
|
+
* private formatForDisplay = new FormatForDisplayController(this)
|
|
40
|
+
*
|
|
41
|
+
* render() {
|
|
42
|
+
* return html`
|
|
43
|
+
* <button>Save <kbd>${this.formatForDisplay.format('Mod+S')}</kbd></button>
|
|
44
|
+
* `
|
|
45
|
+
* }
|
|
46
|
+
* }
|
|
47
|
+
* ```
|
|
48
|
+
*/
|
|
49
|
+
export declare class FormatForDisplayController implements ReactiveController {
|
|
50
|
+
private _host;
|
|
51
|
+
private _options;
|
|
52
|
+
/** Reads the layout for the host, unless `keyboardLayout` is false. */
|
|
53
|
+
private _layout;
|
|
54
|
+
/**
|
|
55
|
+
* Formats a hotkey as a label, with the controller's platform and the
|
|
56
|
+
* user's keyboard layout filled in. It takes the same options as
|
|
57
|
+
* `formatForDisplay` and needs no `this`, so it can be passed around.
|
|
58
|
+
*/
|
|
59
|
+
format: typeof formatForDisplay;
|
|
60
|
+
/**
|
|
61
|
+
* @param _host - The Lit component that owns this controller. The controller adds itself to it.
|
|
62
|
+
* @param _options - Options or a getter. `platform` is read for each label. `keyboardLayout` is read on connection and before host updates.
|
|
63
|
+
*/
|
|
64
|
+
constructor(_host: ReactiveControllerHost, _options?: FormatForDisplayControllerOptions | (() => FormatForDisplayControllerOptions));
|
|
65
|
+
/** Starts reading the layout, unless `keyboardLayout` is false. */
|
|
66
|
+
hostConnected(): void;
|
|
67
|
+
/** Follows a `keyboardLayout` option that a host property changed. */
|
|
68
|
+
hostUpdate(): void;
|
|
69
|
+
/**
|
|
70
|
+
* Releases the controller now and removes it from its host, with the
|
|
71
|
+
* layout controller it added. Use it to remove a controller early: Lit's
|
|
72
|
+
* `removeController` alone leaves it active.
|
|
73
|
+
*/
|
|
74
|
+
dispose(): void;
|
|
75
|
+
private _read;
|
|
76
|
+
private _sync;
|
|
77
|
+
}
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { KeyboardLayoutController } from "./keyboard-layout.js";
|
|
2
|
+
import { formatForDisplay } from "@tanstack/hotkeys";
|
|
3
|
+
|
|
4
|
+
// src/controllers/format-for-display.ts
|
|
5
|
+
/**
|
|
6
|
+
* A Lit ReactiveController whose `format` is `formatForDisplay` with the
|
|
7
|
+
* controller's `platform` option, or the detected platform without one. A
|
|
8
|
+
* `platform` passed to `format` wins.
|
|
9
|
+
*
|
|
10
|
+
* Physical bindings such as `[KeyQ]` are labeled with the character the key
|
|
11
|
+
* types for this user in Chromium browsers, once the keyboard layout has
|
|
12
|
+
* loaded. The host updates when the layout loads or changes. See
|
|
13
|
+
* `KeyboardLayoutController`. A `layoutMap` passed to `format` wins. Labels
|
|
14
|
+
* have no layout on the server and for the host's first render, so a
|
|
15
|
+
* server-rendered element hydrates unchanged. Firefox and Safari report no
|
|
16
|
+
* layout, so labels there keep the key's US character.
|
|
17
|
+
*
|
|
18
|
+
* Lit has no provider, so the options are the only defaults. Pass a getter to
|
|
19
|
+
* read them from host properties. Server-rendered elements should set
|
|
20
|
+
* `platform` from the request with `detectPlatform(userAgent)`, so the server
|
|
21
|
+
* HTML matches the user's platform.
|
|
22
|
+
*
|
|
23
|
+
* @example
|
|
24
|
+
* ```ts
|
|
25
|
+
* class SaveButton extends LitElement {
|
|
26
|
+
* private formatForDisplay = new FormatForDisplayController(this)
|
|
27
|
+
*
|
|
28
|
+
* render() {
|
|
29
|
+
* return html`
|
|
30
|
+
* <button>Save <kbd>${this.formatForDisplay.format('Mod+S')}</kbd></button>
|
|
31
|
+
* `
|
|
32
|
+
* }
|
|
33
|
+
* }
|
|
34
|
+
* ```
|
|
35
|
+
*/
|
|
36
|
+
var FormatForDisplayController = class {
|
|
37
|
+
_host;
|
|
38
|
+
_options;
|
|
39
|
+
/** Reads the layout for the host, unless `keyboardLayout` is false. */
|
|
40
|
+
_layout;
|
|
41
|
+
/**
|
|
42
|
+
* Formats a hotkey as a label, with the controller's platform and the
|
|
43
|
+
* user's keyboard layout filled in. It takes the same options as
|
|
44
|
+
* `formatForDisplay` and needs no `this`, so it can be passed around.
|
|
45
|
+
*/
|
|
46
|
+
format = ((hotkey, options) => {
|
|
47
|
+
const { platform, keyboardLayout } = this._read();
|
|
48
|
+
return formatForDisplay(hotkey, {
|
|
49
|
+
...options,
|
|
50
|
+
platform: options?.platform ?? platform,
|
|
51
|
+
layoutMap: options?.layoutMap ?? (keyboardLayout !== false && this._layout?.value || void 0)
|
|
52
|
+
});
|
|
53
|
+
});
|
|
54
|
+
/**
|
|
55
|
+
* @param _host - The Lit component that owns this controller. The controller adds itself to it.
|
|
56
|
+
* @param _options - Options or a getter. `platform` is read for each label. `keyboardLayout` is read on connection and before host updates.
|
|
57
|
+
*/
|
|
58
|
+
constructor(_host, _options = {}) {
|
|
59
|
+
this._host = _host;
|
|
60
|
+
this._options = _options;
|
|
61
|
+
this._host.addController(this);
|
|
62
|
+
}
|
|
63
|
+
/** Starts reading the layout, unless `keyboardLayout` is false. */
|
|
64
|
+
hostConnected() {
|
|
65
|
+
this._sync();
|
|
66
|
+
}
|
|
67
|
+
/** Follows a `keyboardLayout` option that a host property changed. */
|
|
68
|
+
hostUpdate() {
|
|
69
|
+
this._sync();
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Releases the controller now and removes it from its host, with the
|
|
73
|
+
* layout controller it added. Use it to remove a controller early: Lit's
|
|
74
|
+
* `removeController` alone leaves it active.
|
|
75
|
+
*/
|
|
76
|
+
dispose() {
|
|
77
|
+
this._layout?.dispose();
|
|
78
|
+
this._layout = void 0;
|
|
79
|
+
this._host.removeController(this);
|
|
80
|
+
}
|
|
81
|
+
_read() {
|
|
82
|
+
return typeof this._options === "function" ? this._options() : this._options;
|
|
83
|
+
}
|
|
84
|
+
_sync() {
|
|
85
|
+
if (this._read().keyboardLayout === false) {
|
|
86
|
+
this._layout?.dispose();
|
|
87
|
+
this._layout = void 0;
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
this._layout ??= new KeyboardLayoutController(this._host);
|
|
91
|
+
}
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
export { FormatForDisplayController };
|
|
@@ -9,11 +9,12 @@ import { ReactiveController, ReactiveControllerHost } from "lit";
|
|
|
9
9
|
* ```ts
|
|
10
10
|
* class ShortcutPalette extends LitElement {
|
|
11
11
|
* private registrations = new HotkeyRegistrationsController(this)
|
|
12
|
+
* private formatForDisplay = new FormatForDisplayController(this)
|
|
12
13
|
*
|
|
13
14
|
* render() {
|
|
14
15
|
* return html`<ul>
|
|
15
16
|
* ${this.registrations.hotkeys.map(
|
|
16
|
-
* (reg) => html`<li>${reg.options.meta?.name}: ${formatForDisplay(reg.hotkey)}</li>`,
|
|
17
|
+
* (reg) => html`<li>${reg.options.meta?.name}: ${this.formatForDisplay.format(reg.hotkey)}</li>`,
|
|
17
18
|
* )}
|
|
18
19
|
* </ul>`
|
|
19
20
|
* }
|
|
@@ -11,11 +11,12 @@ import { getHotkeyManager, toHotkeyRegistrationView } from "@tanstack/hotkeys";
|
|
|
11
11
|
* ```ts
|
|
12
12
|
* class ShortcutPalette extends LitElement {
|
|
13
13
|
* private registrations = new HotkeyRegistrationsController(this)
|
|
14
|
+
* private formatForDisplay = new FormatForDisplayController(this)
|
|
14
15
|
*
|
|
15
16
|
* render() {
|
|
16
17
|
* return html`<ul>
|
|
17
18
|
* ${this.registrations.hotkeys.map(
|
|
18
|
-
* (reg) => html`<li>${reg.options.meta?.name}: ${formatForDisplay(reg.hotkey)}</li>`,
|
|
19
|
+
* (reg) => html`<li>${reg.options.meta?.name}: ${this.formatForDisplay.format(reg.hotkey)}</li>`,
|
|
19
20
|
* )}
|
|
20
21
|
* </ul>`
|
|
21
22
|
* }
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { AppHotkeysController } from "./app-hotkeys.js";
|
|
2
|
+
import { FormatForDisplayController, FormatForDisplayControllerOptions } from "./format-for-display.js";
|
|
2
3
|
import { HeldKeyCodesController } from "./held-key-codes.js";
|
|
3
4
|
import { HeldKeysController } from "./held-keys.js";
|
|
4
5
|
import { AppHotkeyControllerOptions, HotkeyController, HotkeyControllerChordOptions, HotkeyControllerCommonOptions, HotkeyControllerOptions, HotkeyControllerOptionsFor, HotkeyControllerSequenceOptions, HotkeyControllerTarget } from "./hotkey.js";
|
|
@@ -7,4 +8,5 @@ import { HotkeyRegistrationsController } from "./hotkey-registrations.js";
|
|
|
7
8
|
import { KeyHoldController } from "./key-hold.js";
|
|
8
9
|
import { HotkeyHintController } from "./hotkey-hint.js";
|
|
9
10
|
import { HotkeyScopesStateController } from "./hotkey-scopes-state.js";
|
|
10
|
-
|
|
11
|
+
import { KeyboardLayoutController } from "./keyboard-layout.js";
|
|
12
|
+
export { AppHotkeyControllerOptions, AppHotkeysController, FormatForDisplayController, FormatForDisplayControllerOptions, HeldKeyCodesController, HeldKeysController, HotkeyController, HotkeyControllerChordOptions, HotkeyControllerCommonOptions, HotkeyControllerOptions, HotkeyControllerOptionsFor, HotkeyControllerSequenceOptions, HotkeyControllerTarget, HotkeyHintController, HotkeyRecorderController, HotkeyRegistrationsController, HotkeyScopesStateController, KeyHoldController, KeyboardLayoutController };
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { KeyboardLayoutMap } from "@tanstack/hotkeys";
|
|
2
|
+
import { ReactiveController, ReactiveControllerHost } from "lit";
|
|
3
|
+
/**
|
|
4
|
+
* A Lit ReactiveController that reads the browser's keyboard layout, or `null`
|
|
5
|
+
* until it loads and in browsers that don't report one. The layout maps each
|
|
6
|
+
* physical code to the character it types, such as `KeyQ` to `a` on a French
|
|
7
|
+
* AZERTY keyboard. Only Chromium browsers report one. Triggers host updates
|
|
8
|
+
* when the layout loads or changes.
|
|
9
|
+
*
|
|
10
|
+
* Creating the controller is what loads the layout: it loads when the first
|
|
11
|
+
* host connects, and is read again when the window regains focus while a host
|
|
12
|
+
* is connected. `FormatForDisplayController` uses this controller, so you
|
|
13
|
+
* only need it for labels you draw yourself. To never ask the browser for the
|
|
14
|
+
* layout, don't create the controller.
|
|
15
|
+
*
|
|
16
|
+
* `value` is `null` on the server and for the host's first render, so a
|
|
17
|
+
* server-rendered element hydrates unchanged.
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* ```ts
|
|
21
|
+
* class MoveKeys extends LitElement {
|
|
22
|
+
* private layout = new KeyboardLayoutController(this)
|
|
23
|
+
*
|
|
24
|
+
* render() {
|
|
25
|
+
* const label = (code: string) =>
|
|
26
|
+
* (this.layout.value?.get(code) ?? code).toUpperCase()
|
|
27
|
+
* return html`<kbd>${label('KeyW')}</kbd>`
|
|
28
|
+
* }
|
|
29
|
+
* }
|
|
30
|
+
* ```
|
|
31
|
+
*/
|
|
32
|
+
export declare class KeyboardLayoutController implements ReactiveController {
|
|
33
|
+
private _host;
|
|
34
|
+
/** Unsubscribes from the layout store and ends this host's interest in it. */
|
|
35
|
+
private _release;
|
|
36
|
+
/** Whether the host has finished the first update after it connected. */
|
|
37
|
+
private _rendered;
|
|
38
|
+
/** The loaded keyboard layout, or `null`. */
|
|
39
|
+
get value(): KeyboardLayoutMap | null;
|
|
40
|
+
/**
|
|
41
|
+
* @param _host - The Lit component that owns this controller.
|
|
42
|
+
*/
|
|
43
|
+
constructor(_host: ReactiveControllerHost);
|
|
44
|
+
/**
|
|
45
|
+
* Loads the layout, subscribes to the layout store, and updates the host
|
|
46
|
+
* when the layout changes. Connecting requests an update too, since the
|
|
47
|
+
* layout may have changed while disconnected.
|
|
48
|
+
*/
|
|
49
|
+
hostConnected(): void;
|
|
50
|
+
/**
|
|
51
|
+
* Unsubscribes from the layout store and ends this host's interest in the
|
|
52
|
+
* layout. The loaded layout stays for other hosts.
|
|
53
|
+
*/
|
|
54
|
+
hostDisconnected(): void;
|
|
55
|
+
/**
|
|
56
|
+
* Releases the controller now and removes it from its host. Use it to
|
|
57
|
+
* remove a controller early: Lit's `removeController` alone leaves it active.
|
|
58
|
+
*/
|
|
59
|
+
dispose(): void;
|
|
60
|
+
}
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import { isServer } from "./_server.js";
|
|
2
|
+
import { getKeyboardLayoutStore, loadKeyboardLayout } from "@tanstack/hotkeys";
|
|
3
|
+
|
|
4
|
+
// src/controllers/keyboard-layout.ts
|
|
5
|
+
/**
|
|
6
|
+
* A Lit ReactiveController that reads the browser's keyboard layout, or `null`
|
|
7
|
+
* until it loads and in browsers that don't report one. The layout maps each
|
|
8
|
+
* physical code to the character it types, such as `KeyQ` to `a` on a French
|
|
9
|
+
* AZERTY keyboard. Only Chromium browsers report one. Triggers host updates
|
|
10
|
+
* when the layout loads or changes.
|
|
11
|
+
*
|
|
12
|
+
* Creating the controller is what loads the layout: it loads when the first
|
|
13
|
+
* host connects, and is read again when the window regains focus while a host
|
|
14
|
+
* is connected. `FormatForDisplayController` uses this controller, so you
|
|
15
|
+
* only need it for labels you draw yourself. To never ask the browser for the
|
|
16
|
+
* layout, don't create the controller.
|
|
17
|
+
*
|
|
18
|
+
* `value` is `null` on the server and for the host's first render, so a
|
|
19
|
+
* server-rendered element hydrates unchanged.
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* ```ts
|
|
23
|
+
* class MoveKeys extends LitElement {
|
|
24
|
+
* private layout = new KeyboardLayoutController(this)
|
|
25
|
+
*
|
|
26
|
+
* render() {
|
|
27
|
+
* const label = (code: string) =>
|
|
28
|
+
* (this.layout.value?.get(code) ?? code).toUpperCase()
|
|
29
|
+
* return html`<kbd>${label('KeyW')}</kbd>`
|
|
30
|
+
* }
|
|
31
|
+
* }
|
|
32
|
+
* ```
|
|
33
|
+
*/
|
|
34
|
+
var KeyboardLayoutController = class {
|
|
35
|
+
_host;
|
|
36
|
+
/** Unsubscribes from the layout store and ends this host's interest in it. */
|
|
37
|
+
_release;
|
|
38
|
+
/** Whether the host has finished the first update after it connected. */
|
|
39
|
+
_rendered = false;
|
|
40
|
+
/** The loaded keyboard layout, or `null`. */
|
|
41
|
+
get value() {
|
|
42
|
+
return this._rendered ? getKeyboardLayoutStore().state : null;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* @param _host - The Lit component that owns this controller.
|
|
46
|
+
*/
|
|
47
|
+
constructor(_host) {
|
|
48
|
+
this._host = _host;
|
|
49
|
+
this._host.addController(this);
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Loads the layout, subscribes to the layout store, and updates the host
|
|
53
|
+
* when the layout changes. Connecting requests an update too, since the
|
|
54
|
+
* layout may have changed while disconnected.
|
|
55
|
+
*/
|
|
56
|
+
hostConnected() {
|
|
57
|
+
if (this._release) return;
|
|
58
|
+
this._host.requestUpdate();
|
|
59
|
+
if (isServer()) return;
|
|
60
|
+
const store = getKeyboardLayoutStore();
|
|
61
|
+
const subscription = store.subscribe(() => this._host.requestUpdate());
|
|
62
|
+
const stop = loadKeyboardLayout();
|
|
63
|
+
this._release = () => {
|
|
64
|
+
subscription.unsubscribe();
|
|
65
|
+
stop();
|
|
66
|
+
};
|
|
67
|
+
if (this._rendered) return;
|
|
68
|
+
const show = () => {
|
|
69
|
+
this._rendered = true;
|
|
70
|
+
if (store.state) this._host.requestUpdate();
|
|
71
|
+
};
|
|
72
|
+
this._host.updateComplete.then(show, show);
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Unsubscribes from the layout store and ends this host's interest in the
|
|
76
|
+
* layout. The loaded layout stays for other hosts.
|
|
77
|
+
*/
|
|
78
|
+
hostDisconnected() {
|
|
79
|
+
this._release?.();
|
|
80
|
+
this._release = void 0;
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Releases the controller now and removes it from its host. Use it to
|
|
84
|
+
* remove a controller early: Lit's `removeController` alone leaves it active.
|
|
85
|
+
*/
|
|
86
|
+
dispose() {
|
|
87
|
+
this.hostDisconnected();
|
|
88
|
+
this._host.removeController(this);
|
|
89
|
+
}
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
export { KeyboardLayoutController };
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { formatForDisplay } from "@tanstack/hotkeys";
|
|
2
|
+
/**
|
|
3
|
+
* Formats a hotkey as a label, without a controller's platform or the user's
|
|
4
|
+
* keyboard layout.
|
|
5
|
+
*
|
|
6
|
+
* @deprecated Use `FormatForDisplayController` in elements. Its `format` is
|
|
7
|
+
* this function with the controller's platform and the user's keyboard layout
|
|
8
|
+
* filled in. Outside an element, keep calling this function and pass `platform`
|
|
9
|
+
* yourself.
|
|
10
|
+
*/
|
|
11
|
+
declare const formatForDisplay$1: typeof formatForDisplay;
|
|
12
|
+
export { formatForDisplay$1 as formatForDisplay };
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { formatForDisplay } from "@tanstack/hotkeys";
|
|
2
|
+
|
|
3
|
+
// src/format-for-display.ts
|
|
4
|
+
/**
|
|
5
|
+
* Formats a hotkey as a label, without a controller's platform or the user's
|
|
6
|
+
* keyboard layout.
|
|
7
|
+
*
|
|
8
|
+
* @deprecated Use `FormatForDisplayController` in elements. Its `format` is
|
|
9
|
+
* this function with the controller's platform and the user's keyboard layout
|
|
10
|
+
* filled in. Outside an element, keep calling this function and pass `platform`
|
|
11
|
+
* yourself.
|
|
12
|
+
*/
|
|
13
|
+
const formatForDisplay$1 = formatForDisplay;
|
|
14
|
+
|
|
15
|
+
export { formatForDisplay$1 as formatForDisplay };
|
package/dist/index.d.ts
CHANGED
|
@@ -2,6 +2,7 @@ import { AppHotkeyControllerOptions, HotkeyController, HotkeyControllerChordOpti
|
|
|
2
2
|
import { HotkeyDecorator, hotkey } from "./decorators/hotkey.js";
|
|
3
3
|
import "./decorators/index.js";
|
|
4
4
|
import { AppHotkeysController } from "./controllers/app-hotkeys.js";
|
|
5
|
+
import { FormatForDisplayController, FormatForDisplayControllerOptions } from "./controllers/format-for-display.js";
|
|
5
6
|
import { HeldKeyCodesController } from "./controllers/held-key-codes.js";
|
|
6
7
|
import { HeldKeysController } from "./controllers/held-keys.js";
|
|
7
8
|
import { HotkeyRecorderController } from "./controllers/hotkey-recorder.js";
|
|
@@ -9,6 +10,8 @@ import { HotkeyRegistrationsController } from "./controllers/hotkey-registration
|
|
|
9
10
|
import { KeyHoldController } from "./controllers/key-hold.js";
|
|
10
11
|
import { HotkeyHintController } from "./controllers/hotkey-hint.js";
|
|
11
12
|
import { HotkeyScopesStateController } from "./controllers/hotkey-scopes-state.js";
|
|
13
|
+
import { KeyboardLayoutController } from "./controllers/keyboard-layout.js";
|
|
12
14
|
import "./controllers/index.js";
|
|
15
|
+
import { formatForDisplay } from "./format-for-display.js";
|
|
13
16
|
export * from "@tanstack/hotkeys";
|
|
14
|
-
export { AppHotkeyControllerOptions, AppHotkeysController, HeldKeyCodesController, HeldKeysController, HotkeyController, HotkeyControllerChordOptions, HotkeyControllerCommonOptions, HotkeyControllerOptions, HotkeyControllerOptionsFor, HotkeyControllerSequenceOptions, HotkeyControllerTarget, HotkeyDecorator, HotkeyHintController, HotkeyRecorderController, HotkeyRegistrationsController, HotkeyScopesStateController, KeyHoldController, hotkey };
|
|
17
|
+
export { AppHotkeyControllerOptions, AppHotkeysController, FormatForDisplayController, FormatForDisplayControllerOptions, HeldKeyCodesController, HeldKeysController, HotkeyController, HotkeyControllerChordOptions, HotkeyControllerCommonOptions, HotkeyControllerOptions, HotkeyControllerOptionsFor, HotkeyControllerSequenceOptions, HotkeyControllerTarget, HotkeyDecorator, HotkeyHintController, HotkeyRecorderController, HotkeyRegistrationsController, HotkeyScopesStateController, KeyHoldController, KeyboardLayoutController, formatForDisplay, hotkey };
|
package/dist/index.js
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import { HotkeyController } from "./controllers/hotkey.js";
|
|
2
2
|
import { hotkey } from "./decorators/hotkey.js";
|
|
3
3
|
import { AppHotkeysController } from "./controllers/app-hotkeys.js";
|
|
4
|
+
import { KeyboardLayoutController } from "./controllers/keyboard-layout.js";
|
|
5
|
+
import { FormatForDisplayController } from "./controllers/format-for-display.js";
|
|
4
6
|
import { HeldKeyCodesController } from "./controllers/held-key-codes.js";
|
|
5
7
|
import { HeldKeysController } from "./controllers/held-keys.js";
|
|
6
8
|
import { HotkeyRecorderController } from "./controllers/hotkey-recorder.js";
|
|
@@ -8,7 +10,8 @@ import { HotkeyRegistrationsController } from "./controllers/hotkey-registration
|
|
|
8
10
|
import { KeyHoldController } from "./controllers/key-hold.js";
|
|
9
11
|
import { HotkeyHintController } from "./controllers/hotkey-hint.js";
|
|
10
12
|
import { HotkeyScopesStateController } from "./controllers/hotkey-scopes-state.js";
|
|
13
|
+
import { formatForDisplay } from "./format-for-display.js";
|
|
11
14
|
|
|
12
15
|
export * from "@tanstack/hotkeys"
|
|
13
16
|
|
|
14
|
-
export { AppHotkeysController, HeldKeyCodesController, HeldKeysController, HotkeyController, HotkeyHintController, HotkeyRecorderController, HotkeyRegistrationsController, HotkeyScopesStateController, KeyHoldController, hotkey };
|
|
17
|
+
export { AppHotkeysController, FormatForDisplayController, HeldKeyCodesController, HeldKeysController, HotkeyController, HotkeyHintController, HotkeyRecorderController, HotkeyRegistrationsController, HotkeyScopesStateController, KeyHoldController, KeyboardLayoutController, formatForDisplay, hotkey };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tanstack/lit-hotkeys",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.26.0",
|
|
4
4
|
"description": "Lit adapter for TanStack Hotkeys",
|
|
5
5
|
"author": "Tanner Linsley",
|
|
6
6
|
"license": "MIT",
|
|
@@ -35,9 +35,10 @@
|
|
|
35
35
|
"skills"
|
|
36
36
|
],
|
|
37
37
|
"dependencies": {
|
|
38
|
-
"@tanstack/hotkeys": "0.
|
|
38
|
+
"@tanstack/hotkeys": "0.29.0"
|
|
39
39
|
},
|
|
40
40
|
"devDependencies": {
|
|
41
|
+
"@lit-labs/ssr": "^4.1.0",
|
|
41
42
|
"@tanstack/hotkeys-test-utils": "0.0.0",
|
|
42
43
|
"lit": "^3.3.3"
|
|
43
44
|
},
|
|
@@ -58,22 +58,23 @@ export class MyEditor extends LitElement {
|
|
|
58
58
|
|
|
59
59
|
## API
|
|
60
60
|
|
|
61
|
-
| API | What it's for
|
|
62
|
-
| -------------------------------------------------------- |
|
|
63
|
-
| `@hotkey(hotkey, options?)` | Run the decorated method when the hotkey fires, with options fixed when the class is defined.
|
|
64
|
-
| `new HotkeyController(host, hotkey, callback, options?)` | Register a hotkey whose options can change. It adds itself to `host`, so a class field is enough.
|
|
65
|
-
| `provideAppHotkeys(appHotkeys, options?)` | Declare app hotkeys and load the user's custom hotkeys once at startup, since Lit has no provider.
|
|
66
|
-
| `getHotkeyManager().activateScope(scope, options?)` | Turn on a scope, such as an exclusive one for a dialog; it returns the function that turns the scope off.
|
|
67
|
-
| `new
|
|
68
|
-
| `new
|
|
69
|
-
| `new
|
|
70
|
-
| `new
|
|
71
|
-
| `new
|
|
72
|
-
| `new
|
|
73
|
-
| `new
|
|
74
|
-
| `new
|
|
75
|
-
|
|
76
|
-
|
|
61
|
+
| API | What it's for |
|
|
62
|
+
| -------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
63
|
+
| `@hotkey(hotkey, options?)` | Run the decorated method when the hotkey fires, with options fixed when the class is defined. |
|
|
64
|
+
| `new HotkeyController(host, hotkey, callback, options?)` | Register a hotkey whose options can change. It adds itself to `host`, so a class field is enough. |
|
|
65
|
+
| `provideAppHotkeys(appHotkeys, options?)` | Declare app hotkeys and load the user's custom hotkeys once at startup, since Lit has no provider. |
|
|
66
|
+
| `getHotkeyManager().activateScope(scope, options?)` | Turn on a scope, such as an exclusive one for a dialog; it returns the function that turns the scope off. |
|
|
67
|
+
| `new FormatForDisplayController(host, options?)` | Read `.format`, the function that formats a hotkey as a label, with the controller's `platform` option and the user's keyboard layout filled in. Only Chromium browsers report a layout, so physical keys keep their US labels in Firefox and Safari. Use it for every label in an element: `private formatForDisplay = new FormatForDisplayController(this)` as a field, then `this.formatForDisplay.format(hotkey)`. With Lit SSR, pass `() => ({ platform: this.platform })` and set that property from `detectPlatform(userAgent)`. |
|
|
68
|
+
| `new AppHotkeysController(host)` | Read `appHotkeys`, the cheat sheet `list`, and `customHotkeys`, and change custom hotkeys with `setCustomHotkey`, `resetCustomHotkey`, and `findConflicts`. |
|
|
69
|
+
| `new HotkeyRecorderController(host, options)` | Record a chord or sequence the user presses, for a settings screen. |
|
|
70
|
+
| `new HotkeyRegistrationsController(host)` | Read `.hotkeys`, every registered hotkey with its options, `meta`, and `status` (whether it's enabled and in an active scope). |
|
|
71
|
+
| `new HotkeyScopesStateController(host)` | Read `.value`, the active scopes and the exclusive scope that owns the keyboard. |
|
|
72
|
+
| `new HotkeyHintController(host, hotkey, options?)` | Read `.value`, whether held modifiers should reveal a shortcut's hint. False unless a registration of the hotkey can fire: enabled, in an active scope, and with focus inside its element target. |
|
|
73
|
+
| `new KeyHoldController(host, key)` | Read `.value`, whether one key is held. Pass a getter, such as `() => this.key`, for a key that changes. Letters match in any case; other names, such as `Shift`, are case-sensitive. |
|
|
74
|
+
| `new HeldKeysController(host)` | Read `.value`, every key that's held right now. |
|
|
75
|
+
| `new HeldKeyCodesController(host)` | Read `.value`, which maps each held key to its physical `event.code`. |
|
|
76
|
+
|
|
77
|
+
The package also re-exports `@tanstack/hotkeys`, such as `matchesKeyboardEvent` and `defineAppHotkeys`. Its plain `formatForDisplay` export is deprecated: use `FormatForDisplayController` in elements. Outside an element, call the plain function and pass `platform`.
|
|
77
78
|
|
|
78
79
|
## Pitfalls
|
|
79
80
|
|
|
@@ -37,8 +37,8 @@ A settings row records with `HotkeyRecorderController`:
|
|
|
37
37
|
```ts no-check
|
|
38
38
|
import {
|
|
39
39
|
AppHotkeysController,
|
|
40
|
+
FormatForDisplayController,
|
|
40
41
|
HotkeyRecorderController,
|
|
41
|
-
formatForDisplay,
|
|
42
42
|
} from '@tanstack/lit-hotkeys'
|
|
43
43
|
import { LitElement, html } from 'lit'
|
|
44
44
|
import { property } from 'lit/decorators.js'
|
|
@@ -47,20 +47,23 @@ import type { AppHotkeyId } from '@tanstack/lit-hotkeys'
|
|
|
47
47
|
export class ShortcutRow extends LitElement {
|
|
48
48
|
@property() hotkeyId!: AppHotkeyId
|
|
49
49
|
private app = new AppHotkeysController(this)
|
|
50
|
+
private formatForDisplay = new FormatForDisplayController(this)
|
|
50
51
|
private recorder = new HotkeyRecorderController(this, () => ({
|
|
51
52
|
maxSteps: Infinity,
|
|
52
|
-
validate: (hotkey) => {
|
|
53
|
-
const [conflict] = this.app.findConflicts(this.hotkeyId, hotkey)
|
|
53
|
+
validate: (hotkey, context) => {
|
|
54
|
+
const [conflict] = this.app.findConflicts(this.hotkeyId, hotkey, context)
|
|
54
55
|
return conflict ? `Used by ${conflict.meta?.name ?? conflict.id}` : true
|
|
55
56
|
},
|
|
56
|
-
onRecord: (hotkey) =>
|
|
57
|
+
onRecord: (hotkey, context) =>
|
|
58
|
+
this.app.setCustomHotkey(this.hotkeyId, hotkey, context),
|
|
57
59
|
onClear: () => this.app.setCustomHotkey(this.hotkeyId, null),
|
|
58
60
|
}))
|
|
59
61
|
|
|
60
62
|
render() {
|
|
61
63
|
const { meta, hotkey } = this.app.appHotkeys[this.hotkeyId]
|
|
62
64
|
return html`
|
|
63
|
-
${meta?.name}:
|
|
65
|
+
${meta?.name}:
|
|
66
|
+
${hotkey ? this.formatForDisplay.format(hotkey) : 'Unassigned'}
|
|
64
67
|
<button @click=${() => this.recorder.startRecording()}>Change</button>
|
|
65
68
|
<button @click=${() => this.app.resetCustomHotkey(this.hotkeyId)}>
|
|
66
69
|
Reset
|