@tanstack/solid-hotkeys 0.4.2 → 0.5.1
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/createHotkeySequence.cjs +10 -0
- package/dist/createHotkeySequence.cjs.map +1 -1
- package/dist/createHotkeySequence.d.cts +10 -0
- package/dist/createHotkeySequence.d.ts +10 -0
- package/dist/createHotkeySequence.js +10 -0
- package/dist/createHotkeySequence.js.map +1 -1
- package/dist/createHotkeys.cjs +110 -0
- package/dist/createHotkeys.cjs.map +1 -0
- package/dist/createHotkeys.d.cts +57 -0
- package/dist/createHotkeys.d.ts +57 -0
- package/dist/createHotkeys.js +110 -0
- package/dist/createHotkeys.js.map +1 -0
- package/dist/index.cjs +2 -0
- package/dist/index.d.cts +2 -1
- package/dist/index.d.ts +2 -1
- package/dist/index.js +2 -1
- package/package.json +3 -3
- package/src/createHotkeySequence.ts +10 -0
- package/src/createHotkeys.ts +188 -0
- package/src/index.ts +1 -0
|
@@ -9,6 +9,11 @@ let solid_js = require("solid-js");
|
|
|
9
9
|
* This primitive allows you to register multi-key sequences like 'g g' or 'd d'
|
|
10
10
|
* that trigger when the full sequence is pressed within a timeout.
|
|
11
11
|
*
|
|
12
|
+
* Each step may include modifiers. You can chain the same modifier across
|
|
13
|
+
* steps (e.g. `Shift+R` then `Shift+T`). Modifier-only keydown events (Shift,
|
|
14
|
+
* Control, Alt, or Meta pressed alone) are ignored while matching—they do not
|
|
15
|
+
* advance the sequence or reset progress.
|
|
16
|
+
*
|
|
12
17
|
* @param sequence - Array of hotkey strings that form the sequence (or accessor function)
|
|
13
18
|
* @param callback - Function to call when the sequence is completed
|
|
14
19
|
* @param options - Options for the sequence behavior (or accessor function)
|
|
@@ -31,6 +36,11 @@ let solid_js = require("solid-js");
|
|
|
31
36
|
* deleteInnerWord()
|
|
32
37
|
* }, { timeout: 500 })
|
|
33
38
|
*
|
|
39
|
+
* // Same modifier on consecutive steps (bare Shift between chords is ignored)
|
|
40
|
+
* createHotkeySequence(['Shift+R', 'Shift+T'], () => {
|
|
41
|
+
* nextAction()
|
|
42
|
+
* })
|
|
43
|
+
*
|
|
34
44
|
* return <div>...</div>
|
|
35
45
|
* }
|
|
36
46
|
* ```
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createHotkeySequence.cjs","names":["useDefaultHotkeysOptions"],"sources":["../src/createHotkeySequence.ts"],"sourcesContent":["import { createEffect, onCleanup } from 'solid-js'\nimport { getSequenceManager } from '@tanstack/hotkeys'\nimport { useDefaultHotkeysOptions } from './HotkeysProvider'\nimport type {\n HotkeyCallback,\n HotkeySequence,\n SequenceOptions,\n SequenceRegistrationHandle,\n} from '@tanstack/hotkeys'\n\nexport interface CreateHotkeySequenceOptions extends Omit<\n SequenceOptions,\n 'target'\n> {\n /**\n * The DOM element to attach the event listener to.\n * Can be a direct DOM element, an accessor, or null. Defaults to document.\n */\n target?: HTMLElement | Document | Window | null\n}\n\n/**\n * SolidJS primitive for registering a keyboard shortcut sequence (Vim-style).\n *\n * This primitive allows you to register multi-key sequences like 'g g' or 'd d'\n * that trigger when the full sequence is pressed within a timeout.\n *\n * @param sequence - Array of hotkey strings that form the sequence (or accessor function)\n * @param callback - Function to call when the sequence is completed\n * @param options - Options for the sequence behavior (or accessor function)\n *\n * @example\n * ```tsx\n * function VimEditor() {\n * // 'g g' to go to top\n * createHotkeySequence(['G', 'G'], () => {\n * scrollToTop()\n * })\n *\n * // 'd d' to delete line\n * createHotkeySequence(['D', 'D'], () => {\n * deleteLine()\n * })\n *\n * // 'd i w' to delete inner word\n * createHotkeySequence(['D', 'I', 'W'], () => {\n * deleteInnerWord()\n * }, { timeout: 500 })\n *\n * return <div>...</div>\n * }\n * ```\n */\nexport function createHotkeySequence(\n sequence: HotkeySequence | (() => HotkeySequence),\n callback: HotkeyCallback,\n options:\n | CreateHotkeySequenceOptions\n | (() => CreateHotkeySequenceOptions) = {},\n): void {\n const defaultOptions = useDefaultHotkeysOptions()\n const manager = getSequenceManager()\n\n let registration: SequenceRegistrationHandle | null = null\n\n createEffect(() => {\n // Resolve reactive values\n const resolvedSequence =\n typeof sequence === 'function' ? sequence() : sequence\n const resolvedOptions = typeof options === 'function' ? options() : options\n\n const mergedOptions = {\n ...defaultOptions.hotkeySequence,\n ...resolvedOptions,\n } as CreateHotkeySequenceOptions\n\n // Extract options without target (target is handled separately)\n const { target: _target, ...optionsWithoutTarget } = mergedOptions\n\n if (resolvedSequence.length === 0) {\n return\n }\n\n // Resolve target: when explicitly provided (even as null), use it and skip if null.\n // When not provided, default to document. Matches createHotkey.\n const resolvedTarget =\n 'target' in mergedOptions\n ? (mergedOptions.target ?? null)\n : typeof document !== 'undefined'\n ? document\n : null\n\n if (!resolvedTarget) {\n return\n }\n\n // Unregister previous registration if it exists\n if (registration?.isActive) {\n registration.unregister()\n registration = null\n }\n\n // Register the sequence\n registration = manager.register(resolvedSequence, callback, {\n ...mergedOptions,\n target: resolvedTarget,\n })\n\n // Sync callback and options on every effect run\n if (registration.isActive) {\n registration.callback = callback\n registration.setOptions(optionsWithoutTarget)\n }\n\n // Cleanup on disposal\n onCleanup(() => {\n if (registration?.isActive) {\n registration.unregister()\n registration = null\n }\n })\n })\n}\n"],"mappings":"
|
|
1
|
+
{"version":3,"file":"createHotkeySequence.cjs","names":["useDefaultHotkeysOptions"],"sources":["../src/createHotkeySequence.ts"],"sourcesContent":["import { createEffect, onCleanup } from 'solid-js'\nimport { getSequenceManager } from '@tanstack/hotkeys'\nimport { useDefaultHotkeysOptions } from './HotkeysProvider'\nimport type {\n HotkeyCallback,\n HotkeySequence,\n SequenceOptions,\n SequenceRegistrationHandle,\n} from '@tanstack/hotkeys'\n\nexport interface CreateHotkeySequenceOptions extends Omit<\n SequenceOptions,\n 'target'\n> {\n /**\n * The DOM element to attach the event listener to.\n * Can be a direct DOM element, an accessor, or null. Defaults to document.\n */\n target?: HTMLElement | Document | Window | null\n}\n\n/**\n * SolidJS primitive for registering a keyboard shortcut sequence (Vim-style).\n *\n * This primitive allows you to register multi-key sequences like 'g g' or 'd d'\n * that trigger when the full sequence is pressed within a timeout.\n *\n * Each step may include modifiers. You can chain the same modifier across\n * steps (e.g. `Shift+R` then `Shift+T`). Modifier-only keydown events (Shift,\n * Control, Alt, or Meta pressed alone) are ignored while matching—they do not\n * advance the sequence or reset progress.\n *\n * @param sequence - Array of hotkey strings that form the sequence (or accessor function)\n * @param callback - Function to call when the sequence is completed\n * @param options - Options for the sequence behavior (or accessor function)\n *\n * @example\n * ```tsx\n * function VimEditor() {\n * // 'g g' to go to top\n * createHotkeySequence(['G', 'G'], () => {\n * scrollToTop()\n * })\n *\n * // 'd d' to delete line\n * createHotkeySequence(['D', 'D'], () => {\n * deleteLine()\n * })\n *\n * // 'd i w' to delete inner word\n * createHotkeySequence(['D', 'I', 'W'], () => {\n * deleteInnerWord()\n * }, { timeout: 500 })\n *\n * // Same modifier on consecutive steps (bare Shift between chords is ignored)\n * createHotkeySequence(['Shift+R', 'Shift+T'], () => {\n * nextAction()\n * })\n *\n * return <div>...</div>\n * }\n * ```\n */\nexport function createHotkeySequence(\n sequence: HotkeySequence | (() => HotkeySequence),\n callback: HotkeyCallback,\n options:\n | CreateHotkeySequenceOptions\n | (() => CreateHotkeySequenceOptions) = {},\n): void {\n const defaultOptions = useDefaultHotkeysOptions()\n const manager = getSequenceManager()\n\n let registration: SequenceRegistrationHandle | null = null\n\n createEffect(() => {\n // Resolve reactive values\n const resolvedSequence =\n typeof sequence === 'function' ? sequence() : sequence\n const resolvedOptions = typeof options === 'function' ? options() : options\n\n const mergedOptions = {\n ...defaultOptions.hotkeySequence,\n ...resolvedOptions,\n } as CreateHotkeySequenceOptions\n\n // Extract options without target (target is handled separately)\n const { target: _target, ...optionsWithoutTarget } = mergedOptions\n\n if (resolvedSequence.length === 0) {\n return\n }\n\n // Resolve target: when explicitly provided (even as null), use it and skip if null.\n // When not provided, default to document. Matches createHotkey.\n const resolvedTarget =\n 'target' in mergedOptions\n ? (mergedOptions.target ?? null)\n : typeof document !== 'undefined'\n ? document\n : null\n\n if (!resolvedTarget) {\n return\n }\n\n // Unregister previous registration if it exists\n if (registration?.isActive) {\n registration.unregister()\n registration = null\n }\n\n // Register the sequence\n registration = manager.register(resolvedSequence, callback, {\n ...mergedOptions,\n target: resolvedTarget,\n })\n\n // Sync callback and options on every effect run\n if (registration.isActive) {\n registration.callback = callback\n registration.setOptions(optionsWithoutTarget)\n }\n\n // Cleanup on disposal\n onCleanup(() => {\n if (registration?.isActive) {\n registration.unregister()\n registration = null\n }\n })\n })\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA+DA,SAAgB,qBACd,UACA,UACA,UAE0C,EAAE,EACtC;CACN,MAAM,iBAAiBA,kDAA0B;CACjD,MAAM,qDAA8B;CAEpC,IAAI,eAAkD;AAEtD,kCAAmB;EAEjB,MAAM,mBACJ,OAAO,aAAa,aAAa,UAAU,GAAG;EAChD,MAAM,kBAAkB,OAAO,YAAY,aAAa,SAAS,GAAG;EAEpE,MAAM,gBAAgB;GACpB,GAAG,eAAe;GAClB,GAAG;GACJ;EAGD,MAAM,EAAE,QAAQ,SAAS,GAAG,yBAAyB;AAErD,MAAI,iBAAiB,WAAW,EAC9B;EAKF,MAAM,iBACJ,YAAY,gBACP,cAAc,UAAU,OACzB,OAAO,aAAa,cAClB,WACA;AAER,MAAI,CAAC,eACH;AAIF,MAAI,cAAc,UAAU;AAC1B,gBAAa,YAAY;AACzB,kBAAe;;AAIjB,iBAAe,QAAQ,SAAS,kBAAkB,UAAU;GAC1D,GAAG;GACH,QAAQ;GACT,CAAC;AAGF,MAAI,aAAa,UAAU;AACzB,gBAAa,WAAW;AACxB,gBAAa,WAAW,qBAAqB;;AAI/C,gCAAgB;AACd,OAAI,cAAc,UAAU;AAC1B,iBAAa,YAAY;AACzB,mBAAe;;IAEjB;GACF"}
|
|
@@ -14,6 +14,11 @@ interface CreateHotkeySequenceOptions extends Omit<SequenceOptions, 'target'> {
|
|
|
14
14
|
* This primitive allows you to register multi-key sequences like 'g g' or 'd d'
|
|
15
15
|
* that trigger when the full sequence is pressed within a timeout.
|
|
16
16
|
*
|
|
17
|
+
* Each step may include modifiers. You can chain the same modifier across
|
|
18
|
+
* steps (e.g. `Shift+R` then `Shift+T`). Modifier-only keydown events (Shift,
|
|
19
|
+
* Control, Alt, or Meta pressed alone) are ignored while matching—they do not
|
|
20
|
+
* advance the sequence or reset progress.
|
|
21
|
+
*
|
|
17
22
|
* @param sequence - Array of hotkey strings that form the sequence (or accessor function)
|
|
18
23
|
* @param callback - Function to call when the sequence is completed
|
|
19
24
|
* @param options - Options for the sequence behavior (or accessor function)
|
|
@@ -36,6 +41,11 @@ interface CreateHotkeySequenceOptions extends Omit<SequenceOptions, 'target'> {
|
|
|
36
41
|
* deleteInnerWord()
|
|
37
42
|
* }, { timeout: 500 })
|
|
38
43
|
*
|
|
44
|
+
* // Same modifier on consecutive steps (bare Shift between chords is ignored)
|
|
45
|
+
* createHotkeySequence(['Shift+R', 'Shift+T'], () => {
|
|
46
|
+
* nextAction()
|
|
47
|
+
* })
|
|
48
|
+
*
|
|
39
49
|
* return <div>...</div>
|
|
40
50
|
* }
|
|
41
51
|
* ```
|
|
@@ -14,6 +14,11 @@ interface CreateHotkeySequenceOptions extends Omit<SequenceOptions, 'target'> {
|
|
|
14
14
|
* This primitive allows you to register multi-key sequences like 'g g' or 'd d'
|
|
15
15
|
* that trigger when the full sequence is pressed within a timeout.
|
|
16
16
|
*
|
|
17
|
+
* Each step may include modifiers. You can chain the same modifier across
|
|
18
|
+
* steps (e.g. `Shift+R` then `Shift+T`). Modifier-only keydown events (Shift,
|
|
19
|
+
* Control, Alt, or Meta pressed alone) are ignored while matching—they do not
|
|
20
|
+
* advance the sequence or reset progress.
|
|
21
|
+
*
|
|
17
22
|
* @param sequence - Array of hotkey strings that form the sequence (or accessor function)
|
|
18
23
|
* @param callback - Function to call when the sequence is completed
|
|
19
24
|
* @param options - Options for the sequence behavior (or accessor function)
|
|
@@ -36,6 +41,11 @@ interface CreateHotkeySequenceOptions extends Omit<SequenceOptions, 'target'> {
|
|
|
36
41
|
* deleteInnerWord()
|
|
37
42
|
* }, { timeout: 500 })
|
|
38
43
|
*
|
|
44
|
+
* // Same modifier on consecutive steps (bare Shift between chords is ignored)
|
|
45
|
+
* createHotkeySequence(['Shift+R', 'Shift+T'], () => {
|
|
46
|
+
* nextAction()
|
|
47
|
+
* })
|
|
48
|
+
*
|
|
39
49
|
* return <div>...</div>
|
|
40
50
|
* }
|
|
41
51
|
* ```
|
|
@@ -9,6 +9,11 @@ import { createEffect, onCleanup } from "solid-js";
|
|
|
9
9
|
* This primitive allows you to register multi-key sequences like 'g g' or 'd d'
|
|
10
10
|
* that trigger when the full sequence is pressed within a timeout.
|
|
11
11
|
*
|
|
12
|
+
* Each step may include modifiers. You can chain the same modifier across
|
|
13
|
+
* steps (e.g. `Shift+R` then `Shift+T`). Modifier-only keydown events (Shift,
|
|
14
|
+
* Control, Alt, or Meta pressed alone) are ignored while matching—they do not
|
|
15
|
+
* advance the sequence or reset progress.
|
|
16
|
+
*
|
|
12
17
|
* @param sequence - Array of hotkey strings that form the sequence (or accessor function)
|
|
13
18
|
* @param callback - Function to call when the sequence is completed
|
|
14
19
|
* @param options - Options for the sequence behavior (or accessor function)
|
|
@@ -31,6 +36,11 @@ import { createEffect, onCleanup } from "solid-js";
|
|
|
31
36
|
* deleteInnerWord()
|
|
32
37
|
* }, { timeout: 500 })
|
|
33
38
|
*
|
|
39
|
+
* // Same modifier on consecutive steps (bare Shift between chords is ignored)
|
|
40
|
+
* createHotkeySequence(['Shift+R', 'Shift+T'], () => {
|
|
41
|
+
* nextAction()
|
|
42
|
+
* })
|
|
43
|
+
*
|
|
34
44
|
* return <div>...</div>
|
|
35
45
|
* }
|
|
36
46
|
* ```
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createHotkeySequence.js","names":[],"sources":["../src/createHotkeySequence.ts"],"sourcesContent":["import { createEffect, onCleanup } from 'solid-js'\nimport { getSequenceManager } from '@tanstack/hotkeys'\nimport { useDefaultHotkeysOptions } from './HotkeysProvider'\nimport type {\n HotkeyCallback,\n HotkeySequence,\n SequenceOptions,\n SequenceRegistrationHandle,\n} from '@tanstack/hotkeys'\n\nexport interface CreateHotkeySequenceOptions extends Omit<\n SequenceOptions,\n 'target'\n> {\n /**\n * The DOM element to attach the event listener to.\n * Can be a direct DOM element, an accessor, or null. Defaults to document.\n */\n target?: HTMLElement | Document | Window | null\n}\n\n/**\n * SolidJS primitive for registering a keyboard shortcut sequence (Vim-style).\n *\n * This primitive allows you to register multi-key sequences like 'g g' or 'd d'\n * that trigger when the full sequence is pressed within a timeout.\n *\n * @param sequence - Array of hotkey strings that form the sequence (or accessor function)\n * @param callback - Function to call when the sequence is completed\n * @param options - Options for the sequence behavior (or accessor function)\n *\n * @example\n * ```tsx\n * function VimEditor() {\n * // 'g g' to go to top\n * createHotkeySequence(['G', 'G'], () => {\n * scrollToTop()\n * })\n *\n * // 'd d' to delete line\n * createHotkeySequence(['D', 'D'], () => {\n * deleteLine()\n * })\n *\n * // 'd i w' to delete inner word\n * createHotkeySequence(['D', 'I', 'W'], () => {\n * deleteInnerWord()\n * }, { timeout: 500 })\n *\n * return <div>...</div>\n * }\n * ```\n */\nexport function createHotkeySequence(\n sequence: HotkeySequence | (() => HotkeySequence),\n callback: HotkeyCallback,\n options:\n | CreateHotkeySequenceOptions\n | (() => CreateHotkeySequenceOptions) = {},\n): void {\n const defaultOptions = useDefaultHotkeysOptions()\n const manager = getSequenceManager()\n\n let registration: SequenceRegistrationHandle | null = null\n\n createEffect(() => {\n // Resolve reactive values\n const resolvedSequence =\n typeof sequence === 'function' ? sequence() : sequence\n const resolvedOptions = typeof options === 'function' ? options() : options\n\n const mergedOptions = {\n ...defaultOptions.hotkeySequence,\n ...resolvedOptions,\n } as CreateHotkeySequenceOptions\n\n // Extract options without target (target is handled separately)\n const { target: _target, ...optionsWithoutTarget } = mergedOptions\n\n if (resolvedSequence.length === 0) {\n return\n }\n\n // Resolve target: when explicitly provided (even as null), use it and skip if null.\n // When not provided, default to document. Matches createHotkey.\n const resolvedTarget =\n 'target' in mergedOptions\n ? (mergedOptions.target ?? null)\n : typeof document !== 'undefined'\n ? document\n : null\n\n if (!resolvedTarget) {\n return\n }\n\n // Unregister previous registration if it exists\n if (registration?.isActive) {\n registration.unregister()\n registration = null\n }\n\n // Register the sequence\n registration = manager.register(resolvedSequence, callback, {\n ...mergedOptions,\n target: resolvedTarget,\n })\n\n // Sync callback and options on every effect run\n if (registration.isActive) {\n registration.callback = callback\n registration.setOptions(optionsWithoutTarget)\n }\n\n // Cleanup on disposal\n onCleanup(() => {\n if (registration?.isActive) {\n registration.unregister()\n registration = null\n }\n })\n })\n}\n"],"mappings":"
|
|
1
|
+
{"version":3,"file":"createHotkeySequence.js","names":[],"sources":["../src/createHotkeySequence.ts"],"sourcesContent":["import { createEffect, onCleanup } from 'solid-js'\nimport { getSequenceManager } from '@tanstack/hotkeys'\nimport { useDefaultHotkeysOptions } from './HotkeysProvider'\nimport type {\n HotkeyCallback,\n HotkeySequence,\n SequenceOptions,\n SequenceRegistrationHandle,\n} from '@tanstack/hotkeys'\n\nexport interface CreateHotkeySequenceOptions extends Omit<\n SequenceOptions,\n 'target'\n> {\n /**\n * The DOM element to attach the event listener to.\n * Can be a direct DOM element, an accessor, or null. Defaults to document.\n */\n target?: HTMLElement | Document | Window | null\n}\n\n/**\n * SolidJS primitive for registering a keyboard shortcut sequence (Vim-style).\n *\n * This primitive allows you to register multi-key sequences like 'g g' or 'd d'\n * that trigger when the full sequence is pressed within a timeout.\n *\n * Each step may include modifiers. You can chain the same modifier across\n * steps (e.g. `Shift+R` then `Shift+T`). Modifier-only keydown events (Shift,\n * Control, Alt, or Meta pressed alone) are ignored while matching—they do not\n * advance the sequence or reset progress.\n *\n * @param sequence - Array of hotkey strings that form the sequence (or accessor function)\n * @param callback - Function to call when the sequence is completed\n * @param options - Options for the sequence behavior (or accessor function)\n *\n * @example\n * ```tsx\n * function VimEditor() {\n * // 'g g' to go to top\n * createHotkeySequence(['G', 'G'], () => {\n * scrollToTop()\n * })\n *\n * // 'd d' to delete line\n * createHotkeySequence(['D', 'D'], () => {\n * deleteLine()\n * })\n *\n * // 'd i w' to delete inner word\n * createHotkeySequence(['D', 'I', 'W'], () => {\n * deleteInnerWord()\n * }, { timeout: 500 })\n *\n * // Same modifier on consecutive steps (bare Shift between chords is ignored)\n * createHotkeySequence(['Shift+R', 'Shift+T'], () => {\n * nextAction()\n * })\n *\n * return <div>...</div>\n * }\n * ```\n */\nexport function createHotkeySequence(\n sequence: HotkeySequence | (() => HotkeySequence),\n callback: HotkeyCallback,\n options:\n | CreateHotkeySequenceOptions\n | (() => CreateHotkeySequenceOptions) = {},\n): void {\n const defaultOptions = useDefaultHotkeysOptions()\n const manager = getSequenceManager()\n\n let registration: SequenceRegistrationHandle | null = null\n\n createEffect(() => {\n // Resolve reactive values\n const resolvedSequence =\n typeof sequence === 'function' ? sequence() : sequence\n const resolvedOptions = typeof options === 'function' ? options() : options\n\n const mergedOptions = {\n ...defaultOptions.hotkeySequence,\n ...resolvedOptions,\n } as CreateHotkeySequenceOptions\n\n // Extract options without target (target is handled separately)\n const { target: _target, ...optionsWithoutTarget } = mergedOptions\n\n if (resolvedSequence.length === 0) {\n return\n }\n\n // Resolve target: when explicitly provided (even as null), use it and skip if null.\n // When not provided, default to document. Matches createHotkey.\n const resolvedTarget =\n 'target' in mergedOptions\n ? (mergedOptions.target ?? null)\n : typeof document !== 'undefined'\n ? document\n : null\n\n if (!resolvedTarget) {\n return\n }\n\n // Unregister previous registration if it exists\n if (registration?.isActive) {\n registration.unregister()\n registration = null\n }\n\n // Register the sequence\n registration = manager.register(resolvedSequence, callback, {\n ...mergedOptions,\n target: resolvedTarget,\n })\n\n // Sync callback and options on every effect run\n if (registration.isActive) {\n registration.callback = callback\n registration.setOptions(optionsWithoutTarget)\n }\n\n // Cleanup on disposal\n onCleanup(() => {\n if (registration?.isActive) {\n registration.unregister()\n registration = null\n }\n })\n })\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA+DA,SAAgB,qBACd,UACA,UACA,UAE0C,EAAE,EACtC;CACN,MAAM,iBAAiB,0BAA0B;CACjD,MAAM,UAAU,oBAAoB;CAEpC,IAAI,eAAkD;AAEtD,oBAAmB;EAEjB,MAAM,mBACJ,OAAO,aAAa,aAAa,UAAU,GAAG;EAChD,MAAM,kBAAkB,OAAO,YAAY,aAAa,SAAS,GAAG;EAEpE,MAAM,gBAAgB;GACpB,GAAG,eAAe;GAClB,GAAG;GACJ;EAGD,MAAM,EAAE,QAAQ,SAAS,GAAG,yBAAyB;AAErD,MAAI,iBAAiB,WAAW,EAC9B;EAKF,MAAM,iBACJ,YAAY,gBACP,cAAc,UAAU,OACzB,OAAO,aAAa,cAClB,WACA;AAER,MAAI,CAAC,eACH;AAIF,MAAI,cAAc,UAAU;AAC1B,gBAAa,YAAY;AACzB,kBAAe;;AAIjB,iBAAe,QAAQ,SAAS,kBAAkB,UAAU;GAC1D,GAAG;GACH,QAAQ;GACT,CAAC;AAGF,MAAI,aAAa,UAAU;AACzB,gBAAa,WAAW;AACxB,gBAAa,WAAW,qBAAqB;;AAI/C,kBAAgB;AACd,OAAI,cAAc,UAAU;AAC1B,iBAAa,YAAY;AACzB,mBAAe;;IAEjB;GACF"}
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
const require_HotkeysProvider = require('./HotkeysProvider.cjs');
|
|
2
|
+
let _tanstack_hotkeys = require("@tanstack/hotkeys");
|
|
3
|
+
let solid_js = require("solid-js");
|
|
4
|
+
|
|
5
|
+
//#region src/createHotkeys.ts
|
|
6
|
+
/**
|
|
7
|
+
* SolidJS primitive for registering multiple keyboard hotkeys at once.
|
|
8
|
+
*
|
|
9
|
+
* Uses the singleton HotkeyManager for efficient event handling.
|
|
10
|
+
* Accepts a dynamic array of hotkey definitions, making it safe to use
|
|
11
|
+
* with variable-length lists.
|
|
12
|
+
*
|
|
13
|
+
* Options are merged in this order:
|
|
14
|
+
* HotkeysProvider defaults < commonOptions < per-definition options
|
|
15
|
+
*
|
|
16
|
+
* @param hotkeys - Array of hotkey definitions, or accessor returning them
|
|
17
|
+
* @param commonOptions - Shared options applied to all hotkeys, or accessor
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* ```tsx
|
|
21
|
+
* function Editor() {
|
|
22
|
+
* createHotkeys([
|
|
23
|
+
* { hotkey: 'Mod+S', callback: () => save() },
|
|
24
|
+
* { hotkey: 'Mod+Z', callback: () => undo() },
|
|
25
|
+
* { hotkey: 'Escape', callback: () => close() },
|
|
26
|
+
* ])
|
|
27
|
+
* }
|
|
28
|
+
* ```
|
|
29
|
+
*
|
|
30
|
+
* @example
|
|
31
|
+
* ```tsx
|
|
32
|
+
* function MenuShortcuts(props) {
|
|
33
|
+
* createHotkeys(
|
|
34
|
+
* () => props.items.map((item) => ({
|
|
35
|
+
* hotkey: item.shortcut,
|
|
36
|
+
* callback: item.action,
|
|
37
|
+
* options: { enabled: item.enabled },
|
|
38
|
+
* })),
|
|
39
|
+
* { preventDefault: true },
|
|
40
|
+
* )
|
|
41
|
+
* }
|
|
42
|
+
* ```
|
|
43
|
+
*/
|
|
44
|
+
function createHotkeys(hotkeys, commonOptions = {}) {
|
|
45
|
+
const defaultOptions = require_HotkeysProvider.useDefaultHotkeysOptions();
|
|
46
|
+
const manager = (0, _tanstack_hotkeys.getHotkeyManager)();
|
|
47
|
+
const registrations = /* @__PURE__ */ new Map();
|
|
48
|
+
(0, solid_js.onCleanup)(() => {
|
|
49
|
+
for (const { handle } of registrations.values()) if (handle.isActive) handle.unregister();
|
|
50
|
+
registrations.clear();
|
|
51
|
+
});
|
|
52
|
+
(0, solid_js.createEffect)(() => {
|
|
53
|
+
const resolvedHotkeys = typeof hotkeys === "function" ? hotkeys() : hotkeys;
|
|
54
|
+
const resolvedCommonOptions = typeof commonOptions === "function" ? commonOptions() : commonOptions;
|
|
55
|
+
const nextKeys = /* @__PURE__ */ new Set();
|
|
56
|
+
const prepared = [];
|
|
57
|
+
for (let i = 0; i < resolvedHotkeys.length; i++) {
|
|
58
|
+
const def = resolvedHotkeys[i];
|
|
59
|
+
const resolvedDefOptions = def.options ?? {};
|
|
60
|
+
const mergedOptions = {
|
|
61
|
+
...defaultOptions.hotkey,
|
|
62
|
+
...resolvedCommonOptions,
|
|
63
|
+
...resolvedDefOptions
|
|
64
|
+
};
|
|
65
|
+
const platform = mergedOptions.platform ?? (0, _tanstack_hotkeys.detectPlatform)();
|
|
66
|
+
const hotkeyString = typeof def.hotkey === "string" ? def.hotkey : (0, _tanstack_hotkeys.formatHotkey)((0, _tanstack_hotkeys.rawHotkeyToParsedHotkey)(def.hotkey, platform));
|
|
67
|
+
const resolvedTarget = "target" in mergedOptions ? mergedOptions.target ?? null : typeof document !== "undefined" ? document : null;
|
|
68
|
+
if (!resolvedTarget) continue;
|
|
69
|
+
const registrationKey = `${i}:${hotkeyString}`;
|
|
70
|
+
nextKeys.add(registrationKey);
|
|
71
|
+
prepared.push({
|
|
72
|
+
registrationKey,
|
|
73
|
+
def,
|
|
74
|
+
mergedOptions,
|
|
75
|
+
hotkeyString,
|
|
76
|
+
resolvedTarget
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
for (const [key, record] of [...registrations.entries()]) if (!nextKeys.has(key)) {
|
|
80
|
+
if (record.handle.isActive) record.handle.unregister();
|
|
81
|
+
registrations.delete(key);
|
|
82
|
+
}
|
|
83
|
+
for (const p of prepared) {
|
|
84
|
+
const existing = registrations.get(p.registrationKey);
|
|
85
|
+
if (existing?.handle.isActive && existing.target === p.resolvedTarget) {
|
|
86
|
+
existing.handle.callback = p.def.callback;
|
|
87
|
+
const { target: _target, ...optionsWithoutTarget } = p.mergedOptions;
|
|
88
|
+
existing.handle.setOptions(optionsWithoutTarget);
|
|
89
|
+
continue;
|
|
90
|
+
}
|
|
91
|
+
if (existing) {
|
|
92
|
+
if (existing.handle.isActive) existing.handle.unregister();
|
|
93
|
+
registrations.delete(p.registrationKey);
|
|
94
|
+
}
|
|
95
|
+
const { target: _target, ...optionsWithoutTarget } = p.mergedOptions;
|
|
96
|
+
const handle = manager.register(p.hotkeyString, p.def.callback, {
|
|
97
|
+
...optionsWithoutTarget,
|
|
98
|
+
target: p.resolvedTarget
|
|
99
|
+
});
|
|
100
|
+
registrations.set(p.registrationKey, {
|
|
101
|
+
handle,
|
|
102
|
+
target: p.resolvedTarget
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
//#endregion
|
|
109
|
+
exports.createHotkeys = createHotkeys;
|
|
110
|
+
//# sourceMappingURL=createHotkeys.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"createHotkeys.cjs","names":["useDefaultHotkeysOptions"],"sources":["../src/createHotkeys.ts"],"sourcesContent":["import { createEffect, onCleanup } from 'solid-js'\nimport {\n detectPlatform,\n formatHotkey,\n getHotkeyManager,\n rawHotkeyToParsedHotkey,\n} from '@tanstack/hotkeys'\nimport { useDefaultHotkeysOptions } from './HotkeysProvider'\nimport type { CreateHotkeyOptions } from './createHotkey'\nimport type {\n Hotkey,\n HotkeyCallback,\n HotkeyRegistrationHandle,\n RegisterableHotkey,\n} from '@tanstack/hotkeys'\n\n/**\n * A single hotkey definition for use with `createHotkeys`.\n */\nexport interface CreateHotkeyDefinition {\n /** The hotkey string (e.g., 'Mod+S', 'Escape') or RawHotkey object */\n hotkey: RegisterableHotkey\n /** The function to call when the hotkey is pressed */\n callback: HotkeyCallback\n /** Per-hotkey options (merged on top of commonOptions) */\n options?: CreateHotkeyOptions\n}\n\n/**\n * SolidJS primitive for registering multiple keyboard hotkeys at once.\n *\n * Uses the singleton HotkeyManager for efficient event handling.\n * Accepts a dynamic array of hotkey definitions, making it safe to use\n * with variable-length lists.\n *\n * Options are merged in this order:\n * HotkeysProvider defaults < commonOptions < per-definition options\n *\n * @param hotkeys - Array of hotkey definitions, or accessor returning them\n * @param commonOptions - Shared options applied to all hotkeys, or accessor\n *\n * @example\n * ```tsx\n * function Editor() {\n * createHotkeys([\n * { hotkey: 'Mod+S', callback: () => save() },\n * { hotkey: 'Mod+Z', callback: () => undo() },\n * { hotkey: 'Escape', callback: () => close() },\n * ])\n * }\n * ```\n *\n * @example\n * ```tsx\n * function MenuShortcuts(props) {\n * createHotkeys(\n * () => props.items.map((item) => ({\n * hotkey: item.shortcut,\n * callback: item.action,\n * options: { enabled: item.enabled },\n * })),\n * { preventDefault: true },\n * )\n * }\n * ```\n */\nexport function createHotkeys(\n hotkeys:\n | Array<CreateHotkeyDefinition>\n | (() => Array<CreateHotkeyDefinition>),\n commonOptions: CreateHotkeyOptions | (() => CreateHotkeyOptions) = {},\n): void {\n type RegistrationRecord = {\n handle: HotkeyRegistrationHandle\n target: Document | HTMLElement | Window\n }\n\n const defaultOptions = useDefaultHotkeysOptions()\n const manager = getHotkeyManager()\n\n const registrations = new Map<string, RegistrationRecord>()\n\n // Clean up only when this component/scope disposes — not before every effect\n // re-run. An inner `onCleanup` inside `createEffect` runs whenever reactive\n // deps change (e.g. dynamic hotkey list), which unregistered every hotkey and\n // re-registered them, flooding the manager store and could freeze the tab.\n onCleanup(() => {\n for (const { handle } of registrations.values()) {\n if (handle.isActive) {\n handle.unregister()\n }\n }\n registrations.clear()\n })\n\n createEffect(() => {\n const resolvedHotkeys = typeof hotkeys === 'function' ? hotkeys() : hotkeys\n const resolvedCommonOptions =\n typeof commonOptions === 'function' ? commonOptions() : commonOptions\n\n const nextKeys = new Set<string>()\n const prepared: Array<{\n registrationKey: string\n def: CreateHotkeyDefinition\n mergedOptions: CreateHotkeyOptions\n hotkeyString: Hotkey\n resolvedTarget: Document | HTMLElement | Window\n }> = []\n\n for (let i = 0; i < resolvedHotkeys.length; i++) {\n const def = resolvedHotkeys[i]!\n const resolvedDefOptions = def.options ?? {}\n\n const mergedOptions = {\n ...defaultOptions.hotkey,\n ...resolvedCommonOptions,\n ...resolvedDefOptions,\n } as CreateHotkeyOptions\n\n const platform = mergedOptions.platform ?? detectPlatform()\n const hotkeyString: Hotkey =\n typeof def.hotkey === 'string'\n ? def.hotkey\n : (formatHotkey(\n rawHotkeyToParsedHotkey(def.hotkey, platform),\n ) as Hotkey)\n\n const resolvedTarget =\n 'target' in mergedOptions\n ? (mergedOptions.target ?? null)\n : typeof document !== 'undefined'\n ? document\n : null\n\n if (!resolvedTarget) {\n continue\n }\n\n const registrationKey = `${i}:${hotkeyString}`\n nextKeys.add(registrationKey)\n prepared.push({\n registrationKey,\n def,\n mergedOptions,\n hotkeyString,\n resolvedTarget,\n })\n }\n\n // Drop stale keys first so list index shifts don't briefly double-register\n // the same hotkey+target (conflictBehavior: 'warn' + devtools can loop).\n for (const [key, record] of [...registrations.entries()]) {\n if (!nextKeys.has(key)) {\n if (record.handle.isActive) {\n record.handle.unregister()\n }\n registrations.delete(key)\n }\n }\n\n for (const p of prepared) {\n const existing = registrations.get(p.registrationKey)\n if (existing?.handle.isActive && existing.target === p.resolvedTarget) {\n existing.handle.callback = p.def.callback\n const { target: _target, ...optionsWithoutTarget } = p.mergedOptions\n existing.handle.setOptions(optionsWithoutTarget)\n continue\n }\n\n if (existing) {\n if (existing.handle.isActive) {\n existing.handle.unregister()\n }\n registrations.delete(p.registrationKey)\n }\n\n const { target: _target, ...optionsWithoutTarget } = p.mergedOptions\n const handle = manager.register(p.hotkeyString, p.def.callback, {\n ...optionsWithoutTarget,\n target: p.resolvedTarget,\n })\n registrations.set(p.registrationKey, {\n handle,\n target: p.resolvedTarget,\n })\n }\n })\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAkEA,SAAgB,cACd,SAGA,gBAAmE,EAAE,EAC/D;CAMN,MAAM,iBAAiBA,kDAA0B;CACjD,MAAM,mDAA4B;CAElC,MAAM,gCAAgB,IAAI,KAAiC;AAM3D,+BAAgB;AACd,OAAK,MAAM,EAAE,YAAY,cAAc,QAAQ,CAC7C,KAAI,OAAO,SACT,QAAO,YAAY;AAGvB,gBAAc,OAAO;GACrB;AAEF,kCAAmB;EACjB,MAAM,kBAAkB,OAAO,YAAY,aAAa,SAAS,GAAG;EACpE,MAAM,wBACJ,OAAO,kBAAkB,aAAa,eAAe,GAAG;EAE1D,MAAM,2BAAW,IAAI,KAAa;EAClC,MAAM,WAMD,EAAE;AAEP,OAAK,IAAI,IAAI,GAAG,IAAI,gBAAgB,QAAQ,KAAK;GAC/C,MAAM,MAAM,gBAAgB;GAC5B,MAAM,qBAAqB,IAAI,WAAW,EAAE;GAE5C,MAAM,gBAAgB;IACpB,GAAG,eAAe;IAClB,GAAG;IACH,GAAG;IACJ;GAED,MAAM,WAAW,cAAc,mDAA4B;GAC3D,MAAM,eACJ,OAAO,IAAI,WAAW,WAClB,IAAI,4FAEsB,IAAI,QAAQ,SAAS,CAC9C;GAEP,MAAM,iBACJ,YAAY,gBACP,cAAc,UAAU,OACzB,OAAO,aAAa,cAClB,WACA;AAER,OAAI,CAAC,eACH;GAGF,MAAM,kBAAkB,GAAG,EAAE,GAAG;AAChC,YAAS,IAAI,gBAAgB;AAC7B,YAAS,KAAK;IACZ;IACA;IACA;IACA;IACA;IACD,CAAC;;AAKJ,OAAK,MAAM,CAAC,KAAK,WAAW,CAAC,GAAG,cAAc,SAAS,CAAC,CACtD,KAAI,CAAC,SAAS,IAAI,IAAI,EAAE;AACtB,OAAI,OAAO,OAAO,SAChB,QAAO,OAAO,YAAY;AAE5B,iBAAc,OAAO,IAAI;;AAI7B,OAAK,MAAM,KAAK,UAAU;GACxB,MAAM,WAAW,cAAc,IAAI,EAAE,gBAAgB;AACrD,OAAI,UAAU,OAAO,YAAY,SAAS,WAAW,EAAE,gBAAgB;AACrE,aAAS,OAAO,WAAW,EAAE,IAAI;IACjC,MAAM,EAAE,QAAQ,SAAS,GAAG,yBAAyB,EAAE;AACvD,aAAS,OAAO,WAAW,qBAAqB;AAChD;;AAGF,OAAI,UAAU;AACZ,QAAI,SAAS,OAAO,SAClB,UAAS,OAAO,YAAY;AAE9B,kBAAc,OAAO,EAAE,gBAAgB;;GAGzC,MAAM,EAAE,QAAQ,SAAS,GAAG,yBAAyB,EAAE;GACvD,MAAM,SAAS,QAAQ,SAAS,EAAE,cAAc,EAAE,IAAI,UAAU;IAC9D,GAAG;IACH,QAAQ,EAAE;IACX,CAAC;AACF,iBAAc,IAAI,EAAE,iBAAiB;IACnC;IACA,QAAQ,EAAE;IACX,CAAC;;GAEJ"}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { CreateHotkeyOptions } from "./createHotkey.cjs";
|
|
2
|
+
import { HotkeyCallback, RegisterableHotkey } from "@tanstack/hotkeys";
|
|
3
|
+
|
|
4
|
+
//#region src/createHotkeys.d.ts
|
|
5
|
+
/**
|
|
6
|
+
* A single hotkey definition for use with `createHotkeys`.
|
|
7
|
+
*/
|
|
8
|
+
interface CreateHotkeyDefinition {
|
|
9
|
+
/** The hotkey string (e.g., 'Mod+S', 'Escape') or RawHotkey object */
|
|
10
|
+
hotkey: RegisterableHotkey;
|
|
11
|
+
/** The function to call when the hotkey is pressed */
|
|
12
|
+
callback: HotkeyCallback;
|
|
13
|
+
/** Per-hotkey options (merged on top of commonOptions) */
|
|
14
|
+
options?: CreateHotkeyOptions;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* SolidJS primitive for registering multiple keyboard hotkeys at once.
|
|
18
|
+
*
|
|
19
|
+
* Uses the singleton HotkeyManager for efficient event handling.
|
|
20
|
+
* Accepts a dynamic array of hotkey definitions, making it safe to use
|
|
21
|
+
* with variable-length lists.
|
|
22
|
+
*
|
|
23
|
+
* Options are merged in this order:
|
|
24
|
+
* HotkeysProvider defaults < commonOptions < per-definition options
|
|
25
|
+
*
|
|
26
|
+
* @param hotkeys - Array of hotkey definitions, or accessor returning them
|
|
27
|
+
* @param commonOptions - Shared options applied to all hotkeys, or accessor
|
|
28
|
+
*
|
|
29
|
+
* @example
|
|
30
|
+
* ```tsx
|
|
31
|
+
* function Editor() {
|
|
32
|
+
* createHotkeys([
|
|
33
|
+
* { hotkey: 'Mod+S', callback: () => save() },
|
|
34
|
+
* { hotkey: 'Mod+Z', callback: () => undo() },
|
|
35
|
+
* { hotkey: 'Escape', callback: () => close() },
|
|
36
|
+
* ])
|
|
37
|
+
* }
|
|
38
|
+
* ```
|
|
39
|
+
*
|
|
40
|
+
* @example
|
|
41
|
+
* ```tsx
|
|
42
|
+
* function MenuShortcuts(props) {
|
|
43
|
+
* createHotkeys(
|
|
44
|
+
* () => props.items.map((item) => ({
|
|
45
|
+
* hotkey: item.shortcut,
|
|
46
|
+
* callback: item.action,
|
|
47
|
+
* options: { enabled: item.enabled },
|
|
48
|
+
* })),
|
|
49
|
+
* { preventDefault: true },
|
|
50
|
+
* )
|
|
51
|
+
* }
|
|
52
|
+
* ```
|
|
53
|
+
*/
|
|
54
|
+
declare function createHotkeys(hotkeys: Array<CreateHotkeyDefinition> | (() => Array<CreateHotkeyDefinition>), commonOptions?: CreateHotkeyOptions | (() => CreateHotkeyOptions)): void;
|
|
55
|
+
//#endregion
|
|
56
|
+
export { CreateHotkeyDefinition, createHotkeys };
|
|
57
|
+
//# sourceMappingURL=createHotkeys.d.cts.map
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { CreateHotkeyOptions } from "./createHotkey.js";
|
|
2
|
+
import { HotkeyCallback, RegisterableHotkey } from "@tanstack/hotkeys";
|
|
3
|
+
|
|
4
|
+
//#region src/createHotkeys.d.ts
|
|
5
|
+
/**
|
|
6
|
+
* A single hotkey definition for use with `createHotkeys`.
|
|
7
|
+
*/
|
|
8
|
+
interface CreateHotkeyDefinition {
|
|
9
|
+
/** The hotkey string (e.g., 'Mod+S', 'Escape') or RawHotkey object */
|
|
10
|
+
hotkey: RegisterableHotkey;
|
|
11
|
+
/** The function to call when the hotkey is pressed */
|
|
12
|
+
callback: HotkeyCallback;
|
|
13
|
+
/** Per-hotkey options (merged on top of commonOptions) */
|
|
14
|
+
options?: CreateHotkeyOptions;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* SolidJS primitive for registering multiple keyboard hotkeys at once.
|
|
18
|
+
*
|
|
19
|
+
* Uses the singleton HotkeyManager for efficient event handling.
|
|
20
|
+
* Accepts a dynamic array of hotkey definitions, making it safe to use
|
|
21
|
+
* with variable-length lists.
|
|
22
|
+
*
|
|
23
|
+
* Options are merged in this order:
|
|
24
|
+
* HotkeysProvider defaults < commonOptions < per-definition options
|
|
25
|
+
*
|
|
26
|
+
* @param hotkeys - Array of hotkey definitions, or accessor returning them
|
|
27
|
+
* @param commonOptions - Shared options applied to all hotkeys, or accessor
|
|
28
|
+
*
|
|
29
|
+
* @example
|
|
30
|
+
* ```tsx
|
|
31
|
+
* function Editor() {
|
|
32
|
+
* createHotkeys([
|
|
33
|
+
* { hotkey: 'Mod+S', callback: () => save() },
|
|
34
|
+
* { hotkey: 'Mod+Z', callback: () => undo() },
|
|
35
|
+
* { hotkey: 'Escape', callback: () => close() },
|
|
36
|
+
* ])
|
|
37
|
+
* }
|
|
38
|
+
* ```
|
|
39
|
+
*
|
|
40
|
+
* @example
|
|
41
|
+
* ```tsx
|
|
42
|
+
* function MenuShortcuts(props) {
|
|
43
|
+
* createHotkeys(
|
|
44
|
+
* () => props.items.map((item) => ({
|
|
45
|
+
* hotkey: item.shortcut,
|
|
46
|
+
* callback: item.action,
|
|
47
|
+
* options: { enabled: item.enabled },
|
|
48
|
+
* })),
|
|
49
|
+
* { preventDefault: true },
|
|
50
|
+
* )
|
|
51
|
+
* }
|
|
52
|
+
* ```
|
|
53
|
+
*/
|
|
54
|
+
declare function createHotkeys(hotkeys: Array<CreateHotkeyDefinition> | (() => Array<CreateHotkeyDefinition>), commonOptions?: CreateHotkeyOptions | (() => CreateHotkeyOptions)): void;
|
|
55
|
+
//#endregion
|
|
56
|
+
export { CreateHotkeyDefinition, createHotkeys };
|
|
57
|
+
//# sourceMappingURL=createHotkeys.d.ts.map
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import { useDefaultHotkeysOptions } from "./HotkeysProvider.js";
|
|
2
|
+
import { detectPlatform, formatHotkey, getHotkeyManager, rawHotkeyToParsedHotkey } from "@tanstack/hotkeys";
|
|
3
|
+
import { createEffect, onCleanup } from "solid-js";
|
|
4
|
+
|
|
5
|
+
//#region src/createHotkeys.ts
|
|
6
|
+
/**
|
|
7
|
+
* SolidJS primitive for registering multiple keyboard hotkeys at once.
|
|
8
|
+
*
|
|
9
|
+
* Uses the singleton HotkeyManager for efficient event handling.
|
|
10
|
+
* Accepts a dynamic array of hotkey definitions, making it safe to use
|
|
11
|
+
* with variable-length lists.
|
|
12
|
+
*
|
|
13
|
+
* Options are merged in this order:
|
|
14
|
+
* HotkeysProvider defaults < commonOptions < per-definition options
|
|
15
|
+
*
|
|
16
|
+
* @param hotkeys - Array of hotkey definitions, or accessor returning them
|
|
17
|
+
* @param commonOptions - Shared options applied to all hotkeys, or accessor
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* ```tsx
|
|
21
|
+
* function Editor() {
|
|
22
|
+
* createHotkeys([
|
|
23
|
+
* { hotkey: 'Mod+S', callback: () => save() },
|
|
24
|
+
* { hotkey: 'Mod+Z', callback: () => undo() },
|
|
25
|
+
* { hotkey: 'Escape', callback: () => close() },
|
|
26
|
+
* ])
|
|
27
|
+
* }
|
|
28
|
+
* ```
|
|
29
|
+
*
|
|
30
|
+
* @example
|
|
31
|
+
* ```tsx
|
|
32
|
+
* function MenuShortcuts(props) {
|
|
33
|
+
* createHotkeys(
|
|
34
|
+
* () => props.items.map((item) => ({
|
|
35
|
+
* hotkey: item.shortcut,
|
|
36
|
+
* callback: item.action,
|
|
37
|
+
* options: { enabled: item.enabled },
|
|
38
|
+
* })),
|
|
39
|
+
* { preventDefault: true },
|
|
40
|
+
* )
|
|
41
|
+
* }
|
|
42
|
+
* ```
|
|
43
|
+
*/
|
|
44
|
+
function createHotkeys(hotkeys, commonOptions = {}) {
|
|
45
|
+
const defaultOptions = useDefaultHotkeysOptions();
|
|
46
|
+
const manager = getHotkeyManager();
|
|
47
|
+
const registrations = /* @__PURE__ */ new Map();
|
|
48
|
+
onCleanup(() => {
|
|
49
|
+
for (const { handle } of registrations.values()) if (handle.isActive) handle.unregister();
|
|
50
|
+
registrations.clear();
|
|
51
|
+
});
|
|
52
|
+
createEffect(() => {
|
|
53
|
+
const resolvedHotkeys = typeof hotkeys === "function" ? hotkeys() : hotkeys;
|
|
54
|
+
const resolvedCommonOptions = typeof commonOptions === "function" ? commonOptions() : commonOptions;
|
|
55
|
+
const nextKeys = /* @__PURE__ */ new Set();
|
|
56
|
+
const prepared = [];
|
|
57
|
+
for (let i = 0; i < resolvedHotkeys.length; i++) {
|
|
58
|
+
const def = resolvedHotkeys[i];
|
|
59
|
+
const resolvedDefOptions = def.options ?? {};
|
|
60
|
+
const mergedOptions = {
|
|
61
|
+
...defaultOptions.hotkey,
|
|
62
|
+
...resolvedCommonOptions,
|
|
63
|
+
...resolvedDefOptions
|
|
64
|
+
};
|
|
65
|
+
const platform = mergedOptions.platform ?? detectPlatform();
|
|
66
|
+
const hotkeyString = typeof def.hotkey === "string" ? def.hotkey : formatHotkey(rawHotkeyToParsedHotkey(def.hotkey, platform));
|
|
67
|
+
const resolvedTarget = "target" in mergedOptions ? mergedOptions.target ?? null : typeof document !== "undefined" ? document : null;
|
|
68
|
+
if (!resolvedTarget) continue;
|
|
69
|
+
const registrationKey = `${i}:${hotkeyString}`;
|
|
70
|
+
nextKeys.add(registrationKey);
|
|
71
|
+
prepared.push({
|
|
72
|
+
registrationKey,
|
|
73
|
+
def,
|
|
74
|
+
mergedOptions,
|
|
75
|
+
hotkeyString,
|
|
76
|
+
resolvedTarget
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
for (const [key, record] of [...registrations.entries()]) if (!nextKeys.has(key)) {
|
|
80
|
+
if (record.handle.isActive) record.handle.unregister();
|
|
81
|
+
registrations.delete(key);
|
|
82
|
+
}
|
|
83
|
+
for (const p of prepared) {
|
|
84
|
+
const existing = registrations.get(p.registrationKey);
|
|
85
|
+
if (existing?.handle.isActive && existing.target === p.resolvedTarget) {
|
|
86
|
+
existing.handle.callback = p.def.callback;
|
|
87
|
+
const { target: _target, ...optionsWithoutTarget } = p.mergedOptions;
|
|
88
|
+
existing.handle.setOptions(optionsWithoutTarget);
|
|
89
|
+
continue;
|
|
90
|
+
}
|
|
91
|
+
if (existing) {
|
|
92
|
+
if (existing.handle.isActive) existing.handle.unregister();
|
|
93
|
+
registrations.delete(p.registrationKey);
|
|
94
|
+
}
|
|
95
|
+
const { target: _target, ...optionsWithoutTarget } = p.mergedOptions;
|
|
96
|
+
const handle = manager.register(p.hotkeyString, p.def.callback, {
|
|
97
|
+
...optionsWithoutTarget,
|
|
98
|
+
target: p.resolvedTarget
|
|
99
|
+
});
|
|
100
|
+
registrations.set(p.registrationKey, {
|
|
101
|
+
handle,
|
|
102
|
+
target: p.resolvedTarget
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
//#endregion
|
|
109
|
+
export { createHotkeys };
|
|
110
|
+
//# sourceMappingURL=createHotkeys.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"createHotkeys.js","names":[],"sources":["../src/createHotkeys.ts"],"sourcesContent":["import { createEffect, onCleanup } from 'solid-js'\nimport {\n detectPlatform,\n formatHotkey,\n getHotkeyManager,\n rawHotkeyToParsedHotkey,\n} from '@tanstack/hotkeys'\nimport { useDefaultHotkeysOptions } from './HotkeysProvider'\nimport type { CreateHotkeyOptions } from './createHotkey'\nimport type {\n Hotkey,\n HotkeyCallback,\n HotkeyRegistrationHandle,\n RegisterableHotkey,\n} from '@tanstack/hotkeys'\n\n/**\n * A single hotkey definition for use with `createHotkeys`.\n */\nexport interface CreateHotkeyDefinition {\n /** The hotkey string (e.g., 'Mod+S', 'Escape') or RawHotkey object */\n hotkey: RegisterableHotkey\n /** The function to call when the hotkey is pressed */\n callback: HotkeyCallback\n /** Per-hotkey options (merged on top of commonOptions) */\n options?: CreateHotkeyOptions\n}\n\n/**\n * SolidJS primitive for registering multiple keyboard hotkeys at once.\n *\n * Uses the singleton HotkeyManager for efficient event handling.\n * Accepts a dynamic array of hotkey definitions, making it safe to use\n * with variable-length lists.\n *\n * Options are merged in this order:\n * HotkeysProvider defaults < commonOptions < per-definition options\n *\n * @param hotkeys - Array of hotkey definitions, or accessor returning them\n * @param commonOptions - Shared options applied to all hotkeys, or accessor\n *\n * @example\n * ```tsx\n * function Editor() {\n * createHotkeys([\n * { hotkey: 'Mod+S', callback: () => save() },\n * { hotkey: 'Mod+Z', callback: () => undo() },\n * { hotkey: 'Escape', callback: () => close() },\n * ])\n * }\n * ```\n *\n * @example\n * ```tsx\n * function MenuShortcuts(props) {\n * createHotkeys(\n * () => props.items.map((item) => ({\n * hotkey: item.shortcut,\n * callback: item.action,\n * options: { enabled: item.enabled },\n * })),\n * { preventDefault: true },\n * )\n * }\n * ```\n */\nexport function createHotkeys(\n hotkeys:\n | Array<CreateHotkeyDefinition>\n | (() => Array<CreateHotkeyDefinition>),\n commonOptions: CreateHotkeyOptions | (() => CreateHotkeyOptions) = {},\n): void {\n type RegistrationRecord = {\n handle: HotkeyRegistrationHandle\n target: Document | HTMLElement | Window\n }\n\n const defaultOptions = useDefaultHotkeysOptions()\n const manager = getHotkeyManager()\n\n const registrations = new Map<string, RegistrationRecord>()\n\n // Clean up only when this component/scope disposes — not before every effect\n // re-run. An inner `onCleanup` inside `createEffect` runs whenever reactive\n // deps change (e.g. dynamic hotkey list), which unregistered every hotkey and\n // re-registered them, flooding the manager store and could freeze the tab.\n onCleanup(() => {\n for (const { handle } of registrations.values()) {\n if (handle.isActive) {\n handle.unregister()\n }\n }\n registrations.clear()\n })\n\n createEffect(() => {\n const resolvedHotkeys = typeof hotkeys === 'function' ? hotkeys() : hotkeys\n const resolvedCommonOptions =\n typeof commonOptions === 'function' ? commonOptions() : commonOptions\n\n const nextKeys = new Set<string>()\n const prepared: Array<{\n registrationKey: string\n def: CreateHotkeyDefinition\n mergedOptions: CreateHotkeyOptions\n hotkeyString: Hotkey\n resolvedTarget: Document | HTMLElement | Window\n }> = []\n\n for (let i = 0; i < resolvedHotkeys.length; i++) {\n const def = resolvedHotkeys[i]!\n const resolvedDefOptions = def.options ?? {}\n\n const mergedOptions = {\n ...defaultOptions.hotkey,\n ...resolvedCommonOptions,\n ...resolvedDefOptions,\n } as CreateHotkeyOptions\n\n const platform = mergedOptions.platform ?? detectPlatform()\n const hotkeyString: Hotkey =\n typeof def.hotkey === 'string'\n ? def.hotkey\n : (formatHotkey(\n rawHotkeyToParsedHotkey(def.hotkey, platform),\n ) as Hotkey)\n\n const resolvedTarget =\n 'target' in mergedOptions\n ? (mergedOptions.target ?? null)\n : typeof document !== 'undefined'\n ? document\n : null\n\n if (!resolvedTarget) {\n continue\n }\n\n const registrationKey = `${i}:${hotkeyString}`\n nextKeys.add(registrationKey)\n prepared.push({\n registrationKey,\n def,\n mergedOptions,\n hotkeyString,\n resolvedTarget,\n })\n }\n\n // Drop stale keys first so list index shifts don't briefly double-register\n // the same hotkey+target (conflictBehavior: 'warn' + devtools can loop).\n for (const [key, record] of [...registrations.entries()]) {\n if (!nextKeys.has(key)) {\n if (record.handle.isActive) {\n record.handle.unregister()\n }\n registrations.delete(key)\n }\n }\n\n for (const p of prepared) {\n const existing = registrations.get(p.registrationKey)\n if (existing?.handle.isActive && existing.target === p.resolvedTarget) {\n existing.handle.callback = p.def.callback\n const { target: _target, ...optionsWithoutTarget } = p.mergedOptions\n existing.handle.setOptions(optionsWithoutTarget)\n continue\n }\n\n if (existing) {\n if (existing.handle.isActive) {\n existing.handle.unregister()\n }\n registrations.delete(p.registrationKey)\n }\n\n const { target: _target, ...optionsWithoutTarget } = p.mergedOptions\n const handle = manager.register(p.hotkeyString, p.def.callback, {\n ...optionsWithoutTarget,\n target: p.resolvedTarget,\n })\n registrations.set(p.registrationKey, {\n handle,\n target: p.resolvedTarget,\n })\n }\n })\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAkEA,SAAgB,cACd,SAGA,gBAAmE,EAAE,EAC/D;CAMN,MAAM,iBAAiB,0BAA0B;CACjD,MAAM,UAAU,kBAAkB;CAElC,MAAM,gCAAgB,IAAI,KAAiC;AAM3D,iBAAgB;AACd,OAAK,MAAM,EAAE,YAAY,cAAc,QAAQ,CAC7C,KAAI,OAAO,SACT,QAAO,YAAY;AAGvB,gBAAc,OAAO;GACrB;AAEF,oBAAmB;EACjB,MAAM,kBAAkB,OAAO,YAAY,aAAa,SAAS,GAAG;EACpE,MAAM,wBACJ,OAAO,kBAAkB,aAAa,eAAe,GAAG;EAE1D,MAAM,2BAAW,IAAI,KAAa;EAClC,MAAM,WAMD,EAAE;AAEP,OAAK,IAAI,IAAI,GAAG,IAAI,gBAAgB,QAAQ,KAAK;GAC/C,MAAM,MAAM,gBAAgB;GAC5B,MAAM,qBAAqB,IAAI,WAAW,EAAE;GAE5C,MAAM,gBAAgB;IACpB,GAAG,eAAe;IAClB,GAAG;IACH,GAAG;IACJ;GAED,MAAM,WAAW,cAAc,YAAY,gBAAgB;GAC3D,MAAM,eACJ,OAAO,IAAI,WAAW,WAClB,IAAI,SACH,aACC,wBAAwB,IAAI,QAAQ,SAAS,CAC9C;GAEP,MAAM,iBACJ,YAAY,gBACP,cAAc,UAAU,OACzB,OAAO,aAAa,cAClB,WACA;AAER,OAAI,CAAC,eACH;GAGF,MAAM,kBAAkB,GAAG,EAAE,GAAG;AAChC,YAAS,IAAI,gBAAgB;AAC7B,YAAS,KAAK;IACZ;IACA;IACA;IACA;IACA;IACD,CAAC;;AAKJ,OAAK,MAAM,CAAC,KAAK,WAAW,CAAC,GAAG,cAAc,SAAS,CAAC,CACtD,KAAI,CAAC,SAAS,IAAI,IAAI,EAAE;AACtB,OAAI,OAAO,OAAO,SAChB,QAAO,OAAO,YAAY;AAE5B,iBAAc,OAAO,IAAI;;AAI7B,OAAK,MAAM,KAAK,UAAU;GACxB,MAAM,WAAW,cAAc,IAAI,EAAE,gBAAgB;AACrD,OAAI,UAAU,OAAO,YAAY,SAAS,WAAW,EAAE,gBAAgB;AACrE,aAAS,OAAO,WAAW,EAAE,IAAI;IACjC,MAAM,EAAE,QAAQ,SAAS,GAAG,yBAAyB,EAAE;AACvD,aAAS,OAAO,WAAW,qBAAqB;AAChD;;AAGF,OAAI,UAAU;AACZ,QAAI,SAAS,OAAO,SAClB,UAAS,OAAO,YAAY;AAE9B,kBAAc,OAAO,EAAE,gBAAgB;;GAGzC,MAAM,EAAE,QAAQ,SAAS,GAAG,yBAAyB,EAAE;GACvD,MAAM,SAAS,QAAQ,SAAS,EAAE,cAAc,EAAE,IAAI,UAAU;IAC9D,GAAG;IACH,QAAQ,EAAE;IACX,CAAC;AACF,iBAAc,IAAI,EAAE,iBAAiB;IACnC;IACA,QAAQ,EAAE;IACX,CAAC;;GAEJ"}
|
package/dist/index.cjs
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
2
2
|
const require_HotkeysProvider = require('./HotkeysProvider.cjs');
|
|
3
3
|
const require_createHotkey = require('./createHotkey.cjs');
|
|
4
|
+
const require_createHotkeys = require('./createHotkeys.cjs');
|
|
4
5
|
const require_createHeldKeys = require('./createHeldKeys.cjs');
|
|
5
6
|
const require_createHeldKeyCodes = require('./createHeldKeyCodes.cjs');
|
|
6
7
|
const require_createKeyHold = require('./createKeyHold.cjs');
|
|
@@ -13,6 +14,7 @@ exports.createHeldKeys = require_createHeldKeys.createHeldKeys;
|
|
|
13
14
|
exports.createHotkey = require_createHotkey.createHotkey;
|
|
14
15
|
exports.createHotkeyRecorder = require_createHotkeyRecorder.createHotkeyRecorder;
|
|
15
16
|
exports.createHotkeySequence = require_createHotkeySequence.createHotkeySequence;
|
|
17
|
+
exports.createHotkeys = require_createHotkeys.createHotkeys;
|
|
16
18
|
exports.createKeyHold = require_createKeyHold.createKeyHold;
|
|
17
19
|
exports.useDefaultHotkeysOptions = require_HotkeysProvider.useDefaultHotkeysOptions;
|
|
18
20
|
exports.useHotkeysContext = require_HotkeysProvider.useHotkeysContext;
|
package/dist/index.d.cts
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import { CreateHotkeyOptions, createHotkey } from "./createHotkey.cjs";
|
|
2
2
|
import { CreateHotkeySequenceOptions, createHotkeySequence } from "./createHotkeySequence.cjs";
|
|
3
3
|
import { HotkeysProvider, HotkeysProviderOptions, HotkeysProviderProps, useDefaultHotkeysOptions, useHotkeysContext } from "./HotkeysProvider.cjs";
|
|
4
|
+
import { CreateHotkeyDefinition, createHotkeys } from "./createHotkeys.cjs";
|
|
4
5
|
import { createHeldKeys } from "./createHeldKeys.cjs";
|
|
5
6
|
import { createHeldKeyCodes } from "./createHeldKeyCodes.cjs";
|
|
6
7
|
import { createKeyHold } from "./createKeyHold.cjs";
|
|
7
8
|
import { SolidHotkeyRecorder, createHotkeyRecorder } from "./createHotkeyRecorder.cjs";
|
|
8
9
|
export * from "@tanstack/hotkeys";
|
|
9
|
-
export { CreateHotkeyOptions, CreateHotkeySequenceOptions, HotkeysProvider, HotkeysProviderOptions, HotkeysProviderProps, SolidHotkeyRecorder, createHeldKeyCodes, createHeldKeys, createHotkey, createHotkeyRecorder, createHotkeySequence, createKeyHold, useDefaultHotkeysOptions, useHotkeysContext };
|
|
10
|
+
export { CreateHotkeyDefinition, CreateHotkeyOptions, CreateHotkeySequenceOptions, HotkeysProvider, HotkeysProviderOptions, HotkeysProviderProps, SolidHotkeyRecorder, createHeldKeyCodes, createHeldKeys, createHotkey, createHotkeyRecorder, createHotkeySequence, createHotkeys, createKeyHold, useDefaultHotkeysOptions, useHotkeysContext };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import { CreateHotkeyOptions, createHotkey } from "./createHotkey.js";
|
|
2
2
|
import { CreateHotkeySequenceOptions, createHotkeySequence } from "./createHotkeySequence.js";
|
|
3
3
|
import { HotkeysProvider, HotkeysProviderOptions, HotkeysProviderProps, useDefaultHotkeysOptions, useHotkeysContext } from "./HotkeysProvider.js";
|
|
4
|
+
import { CreateHotkeyDefinition, createHotkeys } from "./createHotkeys.js";
|
|
4
5
|
import { createHeldKeys } from "./createHeldKeys.js";
|
|
5
6
|
import { createHeldKeyCodes } from "./createHeldKeyCodes.js";
|
|
6
7
|
import { createKeyHold } from "./createKeyHold.js";
|
|
7
8
|
import { SolidHotkeyRecorder, createHotkeyRecorder } from "./createHotkeyRecorder.js";
|
|
8
9
|
export * from "@tanstack/hotkeys";
|
|
9
|
-
export { CreateHotkeyOptions, CreateHotkeySequenceOptions, HotkeysProvider, HotkeysProviderOptions, HotkeysProviderProps, SolidHotkeyRecorder, createHeldKeyCodes, createHeldKeys, createHotkey, createHotkeyRecorder, createHotkeySequence, createKeyHold, useDefaultHotkeysOptions, useHotkeysContext };
|
|
10
|
+
export { CreateHotkeyDefinition, CreateHotkeyOptions, CreateHotkeySequenceOptions, HotkeysProvider, HotkeysProviderOptions, HotkeysProviderProps, SolidHotkeyRecorder, createHeldKeyCodes, createHeldKeys, createHotkey, createHotkeyRecorder, createHotkeySequence, createHotkeys, createKeyHold, useDefaultHotkeysOptions, useHotkeysContext };
|
package/dist/index.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { HotkeysProvider, useDefaultHotkeysOptions, useHotkeysContext } from "./HotkeysProvider.js";
|
|
2
2
|
import { createHotkey } from "./createHotkey.js";
|
|
3
|
+
import { createHotkeys } from "./createHotkeys.js";
|
|
3
4
|
import { createHeldKeys } from "./createHeldKeys.js";
|
|
4
5
|
import { createHeldKeyCodes } from "./createHeldKeyCodes.js";
|
|
5
6
|
import { createKeyHold } from "./createKeyHold.js";
|
|
@@ -8,4 +9,4 @@ import { createHotkeyRecorder } from "./createHotkeyRecorder.js";
|
|
|
8
9
|
|
|
9
10
|
export * from "@tanstack/hotkeys"
|
|
10
11
|
|
|
11
|
-
export { HotkeysProvider, createHeldKeyCodes, createHeldKeys, createHotkey, createHotkeyRecorder, createHotkeySequence, createKeyHold, useDefaultHotkeysOptions, useHotkeysContext };
|
|
12
|
+
export { HotkeysProvider, createHeldKeyCodes, createHeldKeys, createHotkey, createHotkeyRecorder, createHotkeySequence, createHotkeys, createKeyHold, useDefaultHotkeysOptions, useHotkeysContext };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tanstack/solid-hotkeys",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.5.1",
|
|
4
4
|
"description": "SolidJS adapter for TanStack Hotkeys",
|
|
5
5
|
"author": "Tanner Linsley",
|
|
6
6
|
"license": "MIT",
|
|
@@ -43,13 +43,13 @@
|
|
|
43
43
|
],
|
|
44
44
|
"dependencies": {
|
|
45
45
|
"@tanstack/solid-store": "^0.9.2",
|
|
46
|
-
"@tanstack/hotkeys": "0.4.
|
|
46
|
+
"@tanstack/hotkeys": "0.4.3"
|
|
47
47
|
},
|
|
48
48
|
"devDependencies": {
|
|
49
49
|
"@solidjs/testing-library": "^0.8.10",
|
|
50
50
|
"@testing-library/jest-dom": "^6.9.1",
|
|
51
51
|
"solid-js": "^1.9.11",
|
|
52
|
-
"vite-plugin-solid": "^2.11.
|
|
52
|
+
"vite-plugin-solid": "^2.11.11"
|
|
53
53
|
},
|
|
54
54
|
"peerDependencies": {
|
|
55
55
|
"solid-js": ">=1.7.0"
|
|
@@ -25,6 +25,11 @@ export interface CreateHotkeySequenceOptions extends Omit<
|
|
|
25
25
|
* This primitive allows you to register multi-key sequences like 'g g' or 'd d'
|
|
26
26
|
* that trigger when the full sequence is pressed within a timeout.
|
|
27
27
|
*
|
|
28
|
+
* Each step may include modifiers. You can chain the same modifier across
|
|
29
|
+
* steps (e.g. `Shift+R` then `Shift+T`). Modifier-only keydown events (Shift,
|
|
30
|
+
* Control, Alt, or Meta pressed alone) are ignored while matching—they do not
|
|
31
|
+
* advance the sequence or reset progress.
|
|
32
|
+
*
|
|
28
33
|
* @param sequence - Array of hotkey strings that form the sequence (or accessor function)
|
|
29
34
|
* @param callback - Function to call when the sequence is completed
|
|
30
35
|
* @param options - Options for the sequence behavior (or accessor function)
|
|
@@ -47,6 +52,11 @@ export interface CreateHotkeySequenceOptions extends Omit<
|
|
|
47
52
|
* deleteInnerWord()
|
|
48
53
|
* }, { timeout: 500 })
|
|
49
54
|
*
|
|
55
|
+
* // Same modifier on consecutive steps (bare Shift between chords is ignored)
|
|
56
|
+
* createHotkeySequence(['Shift+R', 'Shift+T'], () => {
|
|
57
|
+
* nextAction()
|
|
58
|
+
* })
|
|
59
|
+
*
|
|
50
60
|
* return <div>...</div>
|
|
51
61
|
* }
|
|
52
62
|
* ```
|
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
import { createEffect, onCleanup } from 'solid-js'
|
|
2
|
+
import {
|
|
3
|
+
detectPlatform,
|
|
4
|
+
formatHotkey,
|
|
5
|
+
getHotkeyManager,
|
|
6
|
+
rawHotkeyToParsedHotkey,
|
|
7
|
+
} from '@tanstack/hotkeys'
|
|
8
|
+
import { useDefaultHotkeysOptions } from './HotkeysProvider'
|
|
9
|
+
import type { CreateHotkeyOptions } from './createHotkey'
|
|
10
|
+
import type {
|
|
11
|
+
Hotkey,
|
|
12
|
+
HotkeyCallback,
|
|
13
|
+
HotkeyRegistrationHandle,
|
|
14
|
+
RegisterableHotkey,
|
|
15
|
+
} from '@tanstack/hotkeys'
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* A single hotkey definition for use with `createHotkeys`.
|
|
19
|
+
*/
|
|
20
|
+
export interface CreateHotkeyDefinition {
|
|
21
|
+
/** The hotkey string (e.g., 'Mod+S', 'Escape') or RawHotkey object */
|
|
22
|
+
hotkey: RegisterableHotkey
|
|
23
|
+
/** The function to call when the hotkey is pressed */
|
|
24
|
+
callback: HotkeyCallback
|
|
25
|
+
/** Per-hotkey options (merged on top of commonOptions) */
|
|
26
|
+
options?: CreateHotkeyOptions
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* SolidJS primitive for registering multiple keyboard hotkeys at once.
|
|
31
|
+
*
|
|
32
|
+
* Uses the singleton HotkeyManager for efficient event handling.
|
|
33
|
+
* Accepts a dynamic array of hotkey definitions, making it safe to use
|
|
34
|
+
* with variable-length lists.
|
|
35
|
+
*
|
|
36
|
+
* Options are merged in this order:
|
|
37
|
+
* HotkeysProvider defaults < commonOptions < per-definition options
|
|
38
|
+
*
|
|
39
|
+
* @param hotkeys - Array of hotkey definitions, or accessor returning them
|
|
40
|
+
* @param commonOptions - Shared options applied to all hotkeys, or accessor
|
|
41
|
+
*
|
|
42
|
+
* @example
|
|
43
|
+
* ```tsx
|
|
44
|
+
* function Editor() {
|
|
45
|
+
* createHotkeys([
|
|
46
|
+
* { hotkey: 'Mod+S', callback: () => save() },
|
|
47
|
+
* { hotkey: 'Mod+Z', callback: () => undo() },
|
|
48
|
+
* { hotkey: 'Escape', callback: () => close() },
|
|
49
|
+
* ])
|
|
50
|
+
* }
|
|
51
|
+
* ```
|
|
52
|
+
*
|
|
53
|
+
* @example
|
|
54
|
+
* ```tsx
|
|
55
|
+
* function MenuShortcuts(props) {
|
|
56
|
+
* createHotkeys(
|
|
57
|
+
* () => props.items.map((item) => ({
|
|
58
|
+
* hotkey: item.shortcut,
|
|
59
|
+
* callback: item.action,
|
|
60
|
+
* options: { enabled: item.enabled },
|
|
61
|
+
* })),
|
|
62
|
+
* { preventDefault: true },
|
|
63
|
+
* )
|
|
64
|
+
* }
|
|
65
|
+
* ```
|
|
66
|
+
*/
|
|
67
|
+
export function createHotkeys(
|
|
68
|
+
hotkeys:
|
|
69
|
+
| Array<CreateHotkeyDefinition>
|
|
70
|
+
| (() => Array<CreateHotkeyDefinition>),
|
|
71
|
+
commonOptions: CreateHotkeyOptions | (() => CreateHotkeyOptions) = {},
|
|
72
|
+
): void {
|
|
73
|
+
type RegistrationRecord = {
|
|
74
|
+
handle: HotkeyRegistrationHandle
|
|
75
|
+
target: Document | HTMLElement | Window
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
const defaultOptions = useDefaultHotkeysOptions()
|
|
79
|
+
const manager = getHotkeyManager()
|
|
80
|
+
|
|
81
|
+
const registrations = new Map<string, RegistrationRecord>()
|
|
82
|
+
|
|
83
|
+
// Clean up only when this component/scope disposes — not before every effect
|
|
84
|
+
// re-run. An inner `onCleanup` inside `createEffect` runs whenever reactive
|
|
85
|
+
// deps change (e.g. dynamic hotkey list), which unregistered every hotkey and
|
|
86
|
+
// re-registered them, flooding the manager store and could freeze the tab.
|
|
87
|
+
onCleanup(() => {
|
|
88
|
+
for (const { handle } of registrations.values()) {
|
|
89
|
+
if (handle.isActive) {
|
|
90
|
+
handle.unregister()
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
registrations.clear()
|
|
94
|
+
})
|
|
95
|
+
|
|
96
|
+
createEffect(() => {
|
|
97
|
+
const resolvedHotkeys = typeof hotkeys === 'function' ? hotkeys() : hotkeys
|
|
98
|
+
const resolvedCommonOptions =
|
|
99
|
+
typeof commonOptions === 'function' ? commonOptions() : commonOptions
|
|
100
|
+
|
|
101
|
+
const nextKeys = new Set<string>()
|
|
102
|
+
const prepared: Array<{
|
|
103
|
+
registrationKey: string
|
|
104
|
+
def: CreateHotkeyDefinition
|
|
105
|
+
mergedOptions: CreateHotkeyOptions
|
|
106
|
+
hotkeyString: Hotkey
|
|
107
|
+
resolvedTarget: Document | HTMLElement | Window
|
|
108
|
+
}> = []
|
|
109
|
+
|
|
110
|
+
for (let i = 0; i < resolvedHotkeys.length; i++) {
|
|
111
|
+
const def = resolvedHotkeys[i]!
|
|
112
|
+
const resolvedDefOptions = def.options ?? {}
|
|
113
|
+
|
|
114
|
+
const mergedOptions = {
|
|
115
|
+
...defaultOptions.hotkey,
|
|
116
|
+
...resolvedCommonOptions,
|
|
117
|
+
...resolvedDefOptions,
|
|
118
|
+
} as CreateHotkeyOptions
|
|
119
|
+
|
|
120
|
+
const platform = mergedOptions.platform ?? detectPlatform()
|
|
121
|
+
const hotkeyString: Hotkey =
|
|
122
|
+
typeof def.hotkey === 'string'
|
|
123
|
+
? def.hotkey
|
|
124
|
+
: (formatHotkey(
|
|
125
|
+
rawHotkeyToParsedHotkey(def.hotkey, platform),
|
|
126
|
+
) as Hotkey)
|
|
127
|
+
|
|
128
|
+
const resolvedTarget =
|
|
129
|
+
'target' in mergedOptions
|
|
130
|
+
? (mergedOptions.target ?? null)
|
|
131
|
+
: typeof document !== 'undefined'
|
|
132
|
+
? document
|
|
133
|
+
: null
|
|
134
|
+
|
|
135
|
+
if (!resolvedTarget) {
|
|
136
|
+
continue
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
const registrationKey = `${i}:${hotkeyString}`
|
|
140
|
+
nextKeys.add(registrationKey)
|
|
141
|
+
prepared.push({
|
|
142
|
+
registrationKey,
|
|
143
|
+
def,
|
|
144
|
+
mergedOptions,
|
|
145
|
+
hotkeyString,
|
|
146
|
+
resolvedTarget,
|
|
147
|
+
})
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
// Drop stale keys first so list index shifts don't briefly double-register
|
|
151
|
+
// the same hotkey+target (conflictBehavior: 'warn' + devtools can loop).
|
|
152
|
+
for (const [key, record] of [...registrations.entries()]) {
|
|
153
|
+
if (!nextKeys.has(key)) {
|
|
154
|
+
if (record.handle.isActive) {
|
|
155
|
+
record.handle.unregister()
|
|
156
|
+
}
|
|
157
|
+
registrations.delete(key)
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
for (const p of prepared) {
|
|
162
|
+
const existing = registrations.get(p.registrationKey)
|
|
163
|
+
if (existing?.handle.isActive && existing.target === p.resolvedTarget) {
|
|
164
|
+
existing.handle.callback = p.def.callback
|
|
165
|
+
const { target: _target, ...optionsWithoutTarget } = p.mergedOptions
|
|
166
|
+
existing.handle.setOptions(optionsWithoutTarget)
|
|
167
|
+
continue
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
if (existing) {
|
|
171
|
+
if (existing.handle.isActive) {
|
|
172
|
+
existing.handle.unregister()
|
|
173
|
+
}
|
|
174
|
+
registrations.delete(p.registrationKey)
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
const { target: _target, ...optionsWithoutTarget } = p.mergedOptions
|
|
178
|
+
const handle = manager.register(p.hotkeyString, p.def.callback, {
|
|
179
|
+
...optionsWithoutTarget,
|
|
180
|
+
target: p.resolvedTarget,
|
|
181
|
+
})
|
|
182
|
+
registrations.set(p.registrationKey, {
|
|
183
|
+
handle,
|
|
184
|
+
target: p.resolvedTarget,
|
|
185
|
+
})
|
|
186
|
+
}
|
|
187
|
+
})
|
|
188
|
+
}
|
package/src/index.ts
CHANGED