@tanstack/preact-hotkeys 0.11.0 → 0.12.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/HotkeysProvider.d.ts +6 -13
- package/dist/HotkeysProvider.js +1 -2
- package/dist/useHeldKeyCodes.d.ts +2 -4
- package/dist/useHeldKeyCodes.js +3 -3
- package/dist/useHeldKeys.d.ts +2 -4
- package/dist/useHeldKeys.js +3 -3
- package/dist/useHotkey.d.ts +3 -6
- package/dist/useHotkey.js +3 -3
- package/dist/useHotkeyHint.d.ts +2 -5
- package/dist/useHotkeyHint.js +1 -2
- package/dist/useHotkeyRecorder.d.ts +3 -6
- package/dist/useHotkeyRecorder.js +1 -2
- package/dist/useHotkeyRegistrations.d.ts +3 -6
- package/dist/useHotkeyRegistrations.js +1 -2
- package/dist/useHotkeySequence.d.ts +3 -6
- package/dist/useHotkeySequence.js +1 -2
- package/dist/useHotkeySequenceRecorder.d.ts +3 -6
- package/dist/useHotkeySequenceRecorder.js +1 -2
- package/dist/useHotkeySequences.d.ts +3 -6
- package/dist/useHotkeySequences.js +1 -2
- package/dist/useHotkeys.d.ts +3 -6
- package/dist/useHotkeys.js +1 -2
- package/dist/useKeyHold.d.ts +2 -5
- package/dist/useKeyHold.js +1 -2
- package/dist/utils.js +1 -2
- package/package.json +11 -16
- package/dist/HotkeysProvider.cjs +0 -26
- package/dist/HotkeysProvider.cjs.map +0 -1
- package/dist/HotkeysProvider.d.cts +0 -29
- package/dist/HotkeysProvider.js.map +0 -1
- package/dist/index.cjs +0 -35
- package/dist/index.d.cts +0 -14
- package/dist/useHeldKeyCodes.cjs +0 -37
- package/dist/useHeldKeyCodes.cjs.map +0 -1
- package/dist/useHeldKeyCodes.d.cts +0 -31
- package/dist/useHeldKeyCodes.js.map +0 -1
- package/dist/useHeldKeys.cjs +0 -33
- package/dist/useHeldKeys.cjs.map +0 -1
- package/dist/useHeldKeys.d.cts +0 -27
- package/dist/useHeldKeys.js.map +0 -1
- package/dist/useHotkey.cjs +0 -125
- package/dist/useHotkey.cjs.map +0 -1
- package/dist/useHotkey.d.cts +0 -75
- package/dist/useHotkey.js.map +0 -1
- package/dist/useHotkeyHint.cjs +0 -18
- package/dist/useHotkeyHint.cjs.map +0 -1
- package/dist/useHotkeyHint.d.cts +0 -14
- package/dist/useHotkeyHint.js.map +0 -1
- package/dist/useHotkeyRecorder.cjs +0 -71
- package/dist/useHotkeyRecorder.cjs.map +0 -1
- package/dist/useHotkeyRecorder.d.cts +0 -57
- package/dist/useHotkeyRecorder.js.map +0 -1
- package/dist/useHotkeyRegistrations.cjs +0 -29
- package/dist/useHotkeyRegistrations.cjs.map +0 -1
- package/dist/useHotkeyRegistrations.d.cts +0 -26
- package/dist/useHotkeyRegistrations.js.map +0 -1
- package/dist/useHotkeySequence.cjs +0 -120
- package/dist/useHotkeySequence.cjs.map +0 -1
- package/dist/useHotkeySequence.d.cts +0 -59
- package/dist/useHotkeySequence.js.map +0 -1
- package/dist/useHotkeySequenceRecorder.cjs +0 -39
- package/dist/useHotkeySequenceRecorder.cjs.map +0 -1
- package/dist/useHotkeySequenceRecorder.d.cts +0 -19
- package/dist/useHotkeySequenceRecorder.js.map +0 -1
- package/dist/useHotkeySequences.cjs +0 -126
- package/dist/useHotkeySequences.cjs.map +0 -1
- package/dist/useHotkeySequences.d.cts +0 -63
- package/dist/useHotkeySequences.js.map +0 -1
- package/dist/useHotkeys.cjs +0 -124
- package/dist/useHotkeys.cjs.map +0 -1
- package/dist/useHotkeys.d.cts +0 -63
- package/dist/useHotkeys.js.map +0 -1
- package/dist/useKeyHold.cjs +0 -53
- package/dist/useKeyHold.cjs.map +0 -1
- package/dist/useKeyHold.d.cts +0 -47
- package/dist/useKeyHold.js.map +0 -1
- package/dist/utils.cjs +0 -12
- package/dist/utils.cjs.map +0 -1
- package/dist/utils.js.map +0 -1
- package/src/HotkeysProvider.tsx +0 -56
- package/src/index.ts +0 -18
- package/src/useHeldKeyCodes.ts +0 -33
- package/src/useHeldKeys.ts +0 -29
- package/src/useHotkey.ts +0 -191
- package/src/useHotkeyHint.ts +0 -19
- package/src/useHotkeyRecorder.ts +0 -101
- package/src/useHotkeyRegistrations.ts +0 -51
- package/src/useHotkeySequence.ts +0 -192
- package/src/useHotkeySequenceRecorder.ts +0 -64
- package/src/useHotkeySequences.ts +0 -192
- package/src/useHotkeys.ts +0 -193
- package/src/useKeyHold.ts +0 -52
- package/src/utils.ts +0 -8
package/src/useHotkeyRecorder.ts
DELETED
|
@@ -1,101 +0,0 @@
|
|
|
1
|
-
import { useEffect, useRef } from 'preact/hooks'
|
|
2
|
-
import { useSelector } from '@tanstack/preact-store'
|
|
3
|
-
import { HotkeyRecorder } from '@tanstack/hotkeys'
|
|
4
|
-
import { useDefaultHotkeysOptions } from './HotkeysProvider'
|
|
5
|
-
import type { Hotkey, HotkeyRecorderOptions } from '@tanstack/hotkeys'
|
|
6
|
-
|
|
7
|
-
export interface PreactHotkeyRecorder {
|
|
8
|
-
/** Whether recording is currently active */
|
|
9
|
-
isRecording: boolean
|
|
10
|
-
/** The currently recorded hotkey (for live preview) */
|
|
11
|
-
recordedHotkey: Hotkey | null
|
|
12
|
-
/** Start recording a new hotkey */
|
|
13
|
-
startRecording: () => void
|
|
14
|
-
/** Stop recording (same as cancel) */
|
|
15
|
-
stopRecording: () => void
|
|
16
|
-
/** Cancel recording without saving */
|
|
17
|
-
cancelRecording: () => void
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
/**
|
|
21
|
-
* Preact hook for recording keyboard shortcuts.
|
|
22
|
-
*
|
|
23
|
-
* This hook provides a thin wrapper around the framework-agnostic `HotkeyRecorder`
|
|
24
|
-
* class, managing all the complexity of capturing keyboard events, converting them
|
|
25
|
-
* to hotkey strings, and handling edge cases like Escape to cancel or Backspace/Delete
|
|
26
|
-
* to clear.
|
|
27
|
-
*
|
|
28
|
-
* @param options - Configuration options for the recorder
|
|
29
|
-
* @returns An object with recording state and control functions
|
|
30
|
-
*
|
|
31
|
-
* @example
|
|
32
|
-
* ```tsx
|
|
33
|
-
* function ShortcutSettings() {
|
|
34
|
-
* const [shortcut, setShortcut] = useState<Hotkey>('Mod+S')
|
|
35
|
-
*
|
|
36
|
-
* const recorder = useHotkeyRecorder({
|
|
37
|
-
* onRecord: (hotkey) => {
|
|
38
|
-
* setShortcut(hotkey)
|
|
39
|
-
* },
|
|
40
|
-
* onCancel: () => {
|
|
41
|
-
* console.log('Recording cancelled')
|
|
42
|
-
* },
|
|
43
|
-
* })
|
|
44
|
-
*
|
|
45
|
-
* return (
|
|
46
|
-
* <div>
|
|
47
|
-
* <button onClick={recorder.startRecording}>
|
|
48
|
-
* {recorder.isRecording ? 'Recording...' : 'Edit Shortcut'}
|
|
49
|
-
* </button>
|
|
50
|
-
* {recorder.recordedHotkey && (
|
|
51
|
-
* <div>Recording: {recorder.recordedHotkey}</div>
|
|
52
|
-
* )}
|
|
53
|
-
* </div>
|
|
54
|
-
* )
|
|
55
|
-
* }
|
|
56
|
-
* ```
|
|
57
|
-
*/
|
|
58
|
-
export function useHotkeyRecorder(
|
|
59
|
-
options: HotkeyRecorderOptions,
|
|
60
|
-
): PreactHotkeyRecorder {
|
|
61
|
-
const mergedOptions = {
|
|
62
|
-
...useDefaultHotkeysOptions().hotkeyRecorder,
|
|
63
|
-
...options,
|
|
64
|
-
} as HotkeyRecorderOptions
|
|
65
|
-
|
|
66
|
-
const recorderRef = useRef<HotkeyRecorder | null>(null)
|
|
67
|
-
|
|
68
|
-
// Create recorder instance once
|
|
69
|
-
if (!recorderRef.current) {
|
|
70
|
-
recorderRef.current = new HotkeyRecorder(mergedOptions)
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
// Sync options on every render (same pattern as useHotkey)
|
|
74
|
-
// This ensures callbacks always have access to latest values
|
|
75
|
-
recorderRef.current.setOptions(mergedOptions)
|
|
76
|
-
|
|
77
|
-
// Subscribe to recorder state using useSelector (same pattern as useHeldKeys)
|
|
78
|
-
const isRecording = useSelector(
|
|
79
|
-
recorderRef.current.store,
|
|
80
|
-
(state) => state.isRecording,
|
|
81
|
-
)
|
|
82
|
-
const recordedHotkey = useSelector(
|
|
83
|
-
recorderRef.current.store,
|
|
84
|
-
(state) => state.recordedHotkey,
|
|
85
|
-
)
|
|
86
|
-
|
|
87
|
-
// Cleanup on unmount
|
|
88
|
-
useEffect(() => {
|
|
89
|
-
return () => {
|
|
90
|
-
recorderRef.current?.destroy()
|
|
91
|
-
}
|
|
92
|
-
}, [])
|
|
93
|
-
|
|
94
|
-
return {
|
|
95
|
-
isRecording,
|
|
96
|
-
recordedHotkey,
|
|
97
|
-
startRecording: () => recorderRef.current?.start(),
|
|
98
|
-
stopRecording: () => recorderRef.current?.stop(),
|
|
99
|
-
cancelRecording: () => recorderRef.current?.cancel(),
|
|
100
|
-
}
|
|
101
|
-
}
|
|
@@ -1,51 +0,0 @@
|
|
|
1
|
-
import { useMemo } from 'preact/hooks'
|
|
2
|
-
import { useSelector } from '@tanstack/preact-store'
|
|
3
|
-
import {
|
|
4
|
-
getHotkeyManager,
|
|
5
|
-
getSequenceManager,
|
|
6
|
-
toHotkeyRegistrationView,
|
|
7
|
-
} from '@tanstack/hotkeys'
|
|
8
|
-
import type {
|
|
9
|
-
HotkeyRegistrationView,
|
|
10
|
-
SequenceRegistrationView,
|
|
11
|
-
} from '@tanstack/hotkeys'
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
* Return type for useHotkeyRegistrations.
|
|
15
|
-
*/
|
|
16
|
-
export interface HotkeyRegistrationsResult {
|
|
17
|
-
/** All registered hotkeys (public view, no callbacks) */
|
|
18
|
-
hotkeys: Array<HotkeyRegistrationView>
|
|
19
|
-
/** All registered sequences */
|
|
20
|
-
sequences: Array<SequenceRegistrationView>
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
/**
|
|
24
|
-
* Preact hook that reactively reads all hotkey and sequence registrations
|
|
25
|
-
* from the singleton managers.
|
|
26
|
-
*
|
|
27
|
-
* This is a standalone hook that does NOT require the HotkeysProvider.
|
|
28
|
-
* It subscribes to both HotkeyManager and SequenceManager stores and
|
|
29
|
-
* re-renders when registrations change.
|
|
30
|
-
*
|
|
31
|
-
* @returns Object with `hotkeys` and `sequences` arrays
|
|
32
|
-
*/
|
|
33
|
-
export function useHotkeyRegistrations(): HotkeyRegistrationsResult {
|
|
34
|
-
const hotkeyManager = getHotkeyManager()
|
|
35
|
-
const sequenceManager = getSequenceManager()
|
|
36
|
-
|
|
37
|
-
// Select stable store snapshots before deriving arrays; fresh selector arrays
|
|
38
|
-
// make Preact's external-store subscription render continuously.
|
|
39
|
-
const hotkeyState = useSelector(hotkeyManager.registrations)
|
|
40
|
-
const sequenceState = useSelector(sequenceManager.registrations)
|
|
41
|
-
const hotkeys = useMemo(
|
|
42
|
-
() => Array.from(hotkeyState.values()).map(toHotkeyRegistrationView),
|
|
43
|
-
[hotkeyState],
|
|
44
|
-
)
|
|
45
|
-
const sequences = useMemo(
|
|
46
|
-
() => Array.from(sequenceState.values()),
|
|
47
|
-
[sequenceState],
|
|
48
|
-
)
|
|
49
|
-
|
|
50
|
-
return { hotkeys, sequences }
|
|
51
|
-
}
|
package/src/useHotkeySequence.ts
DELETED
|
@@ -1,192 +0,0 @@
|
|
|
1
|
-
import { useEffect, useRef } from 'preact/hooks'
|
|
2
|
-
import { formatHotkeySequence, getSequenceManager } from '@tanstack/hotkeys'
|
|
3
|
-
import { useDefaultHotkeysOptions } from './HotkeysProvider'
|
|
4
|
-
import { isRef } from './utils'
|
|
5
|
-
import type { RefObject } from 'preact'
|
|
6
|
-
import type {
|
|
7
|
-
HotkeyCallback,
|
|
8
|
-
HotkeyCallbackContext,
|
|
9
|
-
HotkeySequence,
|
|
10
|
-
SequenceOptions,
|
|
11
|
-
SequenceRegistrationHandle,
|
|
12
|
-
} from '@tanstack/hotkeys'
|
|
13
|
-
|
|
14
|
-
export interface UseHotkeySequenceOptions extends Omit<
|
|
15
|
-
SequenceOptions,
|
|
16
|
-
'target'
|
|
17
|
-
> {
|
|
18
|
-
/**
|
|
19
|
-
* The DOM element to attach the event listener to.
|
|
20
|
-
* Can be a Preact ref, direct DOM element, or null.
|
|
21
|
-
* Defaults to document.
|
|
22
|
-
*/
|
|
23
|
-
target?:
|
|
24
|
-
| RefObject<HTMLElement | null>
|
|
25
|
-
| HTMLElement
|
|
26
|
-
| Document
|
|
27
|
-
| Window
|
|
28
|
-
| null
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
/**
|
|
32
|
-
* Preact hook for registering a keyboard shortcut sequence (Vim-style).
|
|
33
|
-
*
|
|
34
|
-
* This hook allows you to register multi-key sequences like 'g g' or 'd d'
|
|
35
|
-
* that trigger when the full sequence is pressed within a timeout.
|
|
36
|
-
*
|
|
37
|
-
* Each step may include modifiers. You can chain the same modifier across
|
|
38
|
-
* steps (e.g. `Shift+R` then `Shift+T`). Modifier-only keydown events (Shift,
|
|
39
|
-
* Control, Alt, or Meta pressed alone) are ignored while matching—they do not
|
|
40
|
-
* advance the sequence or reset progress.
|
|
41
|
-
*
|
|
42
|
-
* @param sequence - Array of hotkey strings that form the sequence
|
|
43
|
-
* @param callback - Function to call when the sequence is completed
|
|
44
|
-
* @param options - Options for the sequence behavior. `enabled: false` keeps the registration (visible in devtools)
|
|
45
|
-
* and only suppresses firing; the hook updates the existing handle instead of unregistering.
|
|
46
|
-
*
|
|
47
|
-
* @example
|
|
48
|
-
* ```tsx
|
|
49
|
-
* function VimEditor() {
|
|
50
|
-
* // 'g g' to go to top
|
|
51
|
-
* useHotkeySequence(['G', 'G'], () => {
|
|
52
|
-
* scrollToTop()
|
|
53
|
-
* })
|
|
54
|
-
*
|
|
55
|
-
* // 'd d' to delete line
|
|
56
|
-
* useHotkeySequence(['D', 'D'], () => {
|
|
57
|
-
* deleteLine()
|
|
58
|
-
* })
|
|
59
|
-
*
|
|
60
|
-
* // 'd i w' to delete inner word
|
|
61
|
-
* useHotkeySequence(['D', 'I', 'W'], () => {
|
|
62
|
-
* deleteInnerWord()
|
|
63
|
-
* }, { timeout: 500 })
|
|
64
|
-
*
|
|
65
|
-
* // Same modifier on consecutive steps (bare Shift between chords is ignored)
|
|
66
|
-
* useHotkeySequence(['Shift+R', 'Shift+T'], () => {
|
|
67
|
-
* nextAction()
|
|
68
|
-
* })
|
|
69
|
-
*
|
|
70
|
-
* return <div>...</div>
|
|
71
|
-
* }
|
|
72
|
-
* ```
|
|
73
|
-
*/
|
|
74
|
-
export function useHotkeySequence(
|
|
75
|
-
sequence: HotkeySequence,
|
|
76
|
-
callback: HotkeyCallback,
|
|
77
|
-
options: UseHotkeySequenceOptions = {},
|
|
78
|
-
): void {
|
|
79
|
-
const mergedOptions = {
|
|
80
|
-
...useDefaultHotkeysOptions().hotkeySequence,
|
|
81
|
-
...options,
|
|
82
|
-
} as UseHotkeySequenceOptions
|
|
83
|
-
|
|
84
|
-
const manager = getSequenceManager()
|
|
85
|
-
|
|
86
|
-
// Stable ref for registration handle
|
|
87
|
-
const registrationRef = useRef<SequenceRegistrationHandle | null>(null)
|
|
88
|
-
|
|
89
|
-
// Refs to capture current values for use in effect without adding dependencies
|
|
90
|
-
const callbackRef = useRef(callback)
|
|
91
|
-
const optionsRef = useRef(mergedOptions)
|
|
92
|
-
const managerRef = useRef(manager)
|
|
93
|
-
const sequenceRef = useRef(sequence)
|
|
94
|
-
|
|
95
|
-
// Update refs on every render
|
|
96
|
-
callbackRef.current = callback
|
|
97
|
-
optionsRef.current = mergedOptions
|
|
98
|
-
managerRef.current = manager
|
|
99
|
-
sequenceRef.current = sequence
|
|
100
|
-
|
|
101
|
-
// Track previous target and sequence to detect changes requiring re-registration
|
|
102
|
-
const prevTargetRef = useRef<HTMLElement | Document | Window | null>(null)
|
|
103
|
-
const prevSequenceRef = useRef<string | null>(null)
|
|
104
|
-
|
|
105
|
-
// Normalize to hotkey sequence string (join with spaces)
|
|
106
|
-
const hotkeySequenceString = formatHotkeySequence(sequence)
|
|
107
|
-
|
|
108
|
-
// Extract options without target (target is handled separately)
|
|
109
|
-
const { target: _target, ...optionsWithoutTarget } = mergedOptions
|
|
110
|
-
|
|
111
|
-
useEffect(() => {
|
|
112
|
-
if (sequenceRef.current.length === 0) {
|
|
113
|
-
if (registrationRef.current?.isActive) {
|
|
114
|
-
registrationRef.current.unregister()
|
|
115
|
-
registrationRef.current = null
|
|
116
|
-
}
|
|
117
|
-
prevTargetRef.current = null
|
|
118
|
-
prevSequenceRef.current = null
|
|
119
|
-
return
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
// Resolve target inside the effect so refs are already attached after mount
|
|
123
|
-
const resolvedTarget = isRef(optionsRef.current.target)
|
|
124
|
-
? optionsRef.current.target.current
|
|
125
|
-
: (optionsRef.current.target ??
|
|
126
|
-
(typeof document !== 'undefined' ? document : null))
|
|
127
|
-
|
|
128
|
-
// Skip if no valid target (SSR or ref still null)
|
|
129
|
-
if (!resolvedTarget) {
|
|
130
|
-
if (registrationRef.current?.isActive) {
|
|
131
|
-
registrationRef.current.unregister()
|
|
132
|
-
registrationRef.current = null
|
|
133
|
-
}
|
|
134
|
-
prevTargetRef.current = null
|
|
135
|
-
prevSequenceRef.current = null
|
|
136
|
-
return
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
// Check if we need to re-register (target or sequence changed)
|
|
140
|
-
const targetChanged =
|
|
141
|
-
prevTargetRef.current !== null && prevTargetRef.current !== resolvedTarget
|
|
142
|
-
const sequenceChanged =
|
|
143
|
-
prevSequenceRef.current !== null &&
|
|
144
|
-
prevSequenceRef.current !== hotkeySequenceString
|
|
145
|
-
|
|
146
|
-
// If we have an active registration and target/sequence changed, unregister first
|
|
147
|
-
if (
|
|
148
|
-
registrationRef.current?.isActive &&
|
|
149
|
-
(targetChanged || sequenceChanged)
|
|
150
|
-
) {
|
|
151
|
-
registrationRef.current.unregister()
|
|
152
|
-
registrationRef.current = null
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
// Register if needed (no active registration)
|
|
156
|
-
if (!registrationRef.current || !registrationRef.current.isActive) {
|
|
157
|
-
registrationRef.current = managerRef.current.register(
|
|
158
|
-
sequenceRef.current,
|
|
159
|
-
(event, context) => callbackRef.current(event, context),
|
|
160
|
-
{
|
|
161
|
-
...optionsRef.current,
|
|
162
|
-
target: resolvedTarget,
|
|
163
|
-
},
|
|
164
|
-
)
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
// Update tracking refs
|
|
168
|
-
prevTargetRef.current = resolvedTarget
|
|
169
|
-
prevSequenceRef.current = hotkeySequenceString
|
|
170
|
-
})
|
|
171
|
-
|
|
172
|
-
useEffect(() => {
|
|
173
|
-
// Cleanup on unmount
|
|
174
|
-
return () => {
|
|
175
|
-
if (registrationRef.current?.isActive) {
|
|
176
|
-
registrationRef.current.unregister()
|
|
177
|
-
registrationRef.current = null
|
|
178
|
-
}
|
|
179
|
-
}
|
|
180
|
-
}, [])
|
|
181
|
-
|
|
182
|
-
// Publish option changes after commit, never during render.
|
|
183
|
-
useEffect(() => {
|
|
184
|
-
if (registrationRef.current?.isActive) {
|
|
185
|
-
registrationRef.current.callback = (
|
|
186
|
-
event: KeyboardEvent,
|
|
187
|
-
context: HotkeyCallbackContext,
|
|
188
|
-
) => callbackRef.current(event, context)
|
|
189
|
-
registrationRef.current.setOptions(optionsWithoutTarget)
|
|
190
|
-
}
|
|
191
|
-
})
|
|
192
|
-
}
|
|
@@ -1,64 +0,0 @@
|
|
|
1
|
-
import { useEffect, useRef } from 'preact/hooks'
|
|
2
|
-
import { useSelector } from '@tanstack/preact-store'
|
|
3
|
-
import { HotkeySequenceRecorder } from '@tanstack/hotkeys'
|
|
4
|
-
import { useDefaultHotkeysOptions } from './HotkeysProvider'
|
|
5
|
-
import type {
|
|
6
|
-
HotkeySequence,
|
|
7
|
-
HotkeySequenceRecorderOptions,
|
|
8
|
-
} from '@tanstack/hotkeys'
|
|
9
|
-
|
|
10
|
-
export interface PreactHotkeySequenceRecorder {
|
|
11
|
-
isRecording: boolean
|
|
12
|
-
steps: HotkeySequence
|
|
13
|
-
recordedSequence: HotkeySequence | null
|
|
14
|
-
startRecording: () => void
|
|
15
|
-
stopRecording: () => void
|
|
16
|
-
cancelRecording: () => void
|
|
17
|
-
commitRecording: () => void
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
/**
|
|
21
|
-
* Preact hook for recording multi-chord sequences (Vim-style shortcuts).
|
|
22
|
-
*/
|
|
23
|
-
export function useHotkeySequenceRecorder(
|
|
24
|
-
options: HotkeySequenceRecorderOptions,
|
|
25
|
-
): PreactHotkeySequenceRecorder {
|
|
26
|
-
const mergedOptions = {
|
|
27
|
-
...useDefaultHotkeysOptions().hotkeySequenceRecorder,
|
|
28
|
-
...options,
|
|
29
|
-
} as HotkeySequenceRecorderOptions
|
|
30
|
-
|
|
31
|
-
const recorderRef = useRef<HotkeySequenceRecorder | null>(null)
|
|
32
|
-
|
|
33
|
-
if (!recorderRef.current) {
|
|
34
|
-
recorderRef.current = new HotkeySequenceRecorder(mergedOptions)
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
recorderRef.current.setOptions(mergedOptions)
|
|
38
|
-
|
|
39
|
-
const isRecording = useSelector(
|
|
40
|
-
recorderRef.current.store,
|
|
41
|
-
(state) => state.isRecording,
|
|
42
|
-
)
|
|
43
|
-
const steps = useSelector(recorderRef.current.store, (state) => state.steps)
|
|
44
|
-
const recordedSequence = useSelector(
|
|
45
|
-
recorderRef.current.store,
|
|
46
|
-
(state) => state.recordedSequence,
|
|
47
|
-
)
|
|
48
|
-
|
|
49
|
-
useEffect(() => {
|
|
50
|
-
return () => {
|
|
51
|
-
recorderRef.current?.destroy()
|
|
52
|
-
}
|
|
53
|
-
}, [])
|
|
54
|
-
|
|
55
|
-
return {
|
|
56
|
-
isRecording,
|
|
57
|
-
steps,
|
|
58
|
-
recordedSequence,
|
|
59
|
-
startRecording: () => recorderRef.current?.start(),
|
|
60
|
-
stopRecording: () => recorderRef.current?.stop(),
|
|
61
|
-
cancelRecording: () => recorderRef.current?.cancel(),
|
|
62
|
-
commitRecording: () => recorderRef.current?.commit(),
|
|
63
|
-
}
|
|
64
|
-
}
|
|
@@ -1,192 +0,0 @@
|
|
|
1
|
-
import { useEffect, useRef } from 'preact/hooks'
|
|
2
|
-
import { formatHotkeySequence, getSequenceManager } from '@tanstack/hotkeys'
|
|
3
|
-
import { useDefaultHotkeysOptions } from './HotkeysProvider'
|
|
4
|
-
import { isRef } from './utils'
|
|
5
|
-
import type { UseHotkeySequenceOptions } from './useHotkeySequence'
|
|
6
|
-
import type {
|
|
7
|
-
HotkeyCallback,
|
|
8
|
-
HotkeySequence,
|
|
9
|
-
SequenceRegistrationHandle,
|
|
10
|
-
} from '@tanstack/hotkeys'
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* A single sequence definition for use with `useHotkeySequences`.
|
|
14
|
-
*/
|
|
15
|
-
export interface UseHotkeySequenceDefinition {
|
|
16
|
-
/** Array of hotkey strings that form the sequence */
|
|
17
|
-
sequence: HotkeySequence
|
|
18
|
-
/** The function to call when the sequence is completed */
|
|
19
|
-
callback: HotkeyCallback
|
|
20
|
-
/** Per-sequence options (merged on top of commonOptions) */
|
|
21
|
-
options?: UseHotkeySequenceOptions
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
/**
|
|
25
|
-
* Preact hook for registering multiple keyboard shortcut sequences at once (Vim-style).
|
|
26
|
-
*
|
|
27
|
-
* Uses the singleton SequenceManager. Accepts a dynamic array of definitions so you can
|
|
28
|
-
* register variable-length lists without violating the rules of hooks.
|
|
29
|
-
*
|
|
30
|
-
* Options are merged in this order:
|
|
31
|
-
* HotkeysProvider defaults < commonOptions < per-definition options
|
|
32
|
-
*
|
|
33
|
-
* Callbacks and options are synced on every render to avoid stale closures.
|
|
34
|
-
*
|
|
35
|
-
* Definitions with an empty `sequence` are skipped (no registration).
|
|
36
|
-
*
|
|
37
|
-
* @param definitions - Array of sequence definitions to register
|
|
38
|
-
* @param commonOptions - Shared options applied to all sequences (overridden by per-definition options).
|
|
39
|
-
* Per-row `enabled: false` still registers that sequence: `SequenceManager` suppresses execution only (the row
|
|
40
|
-
* stays in the store and appears in TanStack Hotkeys devtools). Toggling `enabled` updates the existing handle
|
|
41
|
-
* via `setOptions` (no unregister/re-register churn).
|
|
42
|
-
*
|
|
43
|
-
* @example
|
|
44
|
-
* ```tsx
|
|
45
|
-
* function VimPalette() {
|
|
46
|
-
* useHotkeySequences([
|
|
47
|
-
* { sequence: ['G', 'G'], callback: () => scrollToTop() },
|
|
48
|
-
* { sequence: ['D', 'D'], callback: () => deleteLine() },
|
|
49
|
-
* { sequence: ['C', 'I', 'W'], callback: () => changeInnerWord(), options: { timeout: 500 } },
|
|
50
|
-
* ])
|
|
51
|
-
* }
|
|
52
|
-
* ```
|
|
53
|
-
*
|
|
54
|
-
* @example
|
|
55
|
-
* ```tsx
|
|
56
|
-
* function DynamicSequences({ items }) {
|
|
57
|
-
* useHotkeySequences(
|
|
58
|
-
* items.map((item) => ({
|
|
59
|
-
* sequence: item.chords,
|
|
60
|
-
* callback: item.action,
|
|
61
|
-
* options: { enabled: item.enabled },
|
|
62
|
-
* })),
|
|
63
|
-
* { preventDefault: true },
|
|
64
|
-
* )
|
|
65
|
-
* }
|
|
66
|
-
* ```
|
|
67
|
-
*/
|
|
68
|
-
export function useHotkeySequences(
|
|
69
|
-
definitions: Array<UseHotkeySequenceDefinition>,
|
|
70
|
-
commonOptions: UseHotkeySequenceOptions = {},
|
|
71
|
-
): void {
|
|
72
|
-
type RegistrationRecord = {
|
|
73
|
-
handle: SequenceRegistrationHandle
|
|
74
|
-
target: Document | HTMLElement | Window
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
const defaultOptions = useDefaultHotkeysOptions().hotkeySequence
|
|
78
|
-
const manager = getSequenceManager()
|
|
79
|
-
|
|
80
|
-
const registrationsRef = useRef<Map<string, RegistrationRecord>>(new Map())
|
|
81
|
-
const definitionsRef = useRef(definitions)
|
|
82
|
-
const sequenceStringsRef = useRef<Array<string>>([])
|
|
83
|
-
const commonOptionsRef = useRef(commonOptions)
|
|
84
|
-
const defaultOptionsRef = useRef(defaultOptions)
|
|
85
|
-
const managerRef = useRef(manager)
|
|
86
|
-
|
|
87
|
-
const sequenceStrings = definitions.map((def) =>
|
|
88
|
-
formatHotkeySequence(def.sequence),
|
|
89
|
-
)
|
|
90
|
-
|
|
91
|
-
definitionsRef.current = definitions
|
|
92
|
-
sequenceStringsRef.current = sequenceStrings
|
|
93
|
-
commonOptionsRef.current = commonOptions
|
|
94
|
-
defaultOptionsRef.current = defaultOptions
|
|
95
|
-
managerRef.current = manager
|
|
96
|
-
|
|
97
|
-
useEffect(() => {
|
|
98
|
-
const prevRegistrations = registrationsRef.current
|
|
99
|
-
const nextRegistrations = new Map<string, RegistrationRecord>()
|
|
100
|
-
|
|
101
|
-
const rows: Array<{
|
|
102
|
-
registrationKey: string
|
|
103
|
-
def: (typeof definitionsRef.current)[number]
|
|
104
|
-
seq: HotkeySequence
|
|
105
|
-
seqStr: string
|
|
106
|
-
mergedOptions: UseHotkeySequenceOptions
|
|
107
|
-
resolvedTarget: Document | HTMLElement | Window
|
|
108
|
-
}> = []
|
|
109
|
-
|
|
110
|
-
for (let i = 0; i < definitionsRef.current.length; i++) {
|
|
111
|
-
const def = definitionsRef.current[i]!
|
|
112
|
-
const seqStr = sequenceStringsRef.current[i]!
|
|
113
|
-
const seq = def.sequence
|
|
114
|
-
if (seq.length === 0) {
|
|
115
|
-
continue
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
const mergedOptions = {
|
|
119
|
-
...defaultOptionsRef.current,
|
|
120
|
-
...commonOptionsRef.current,
|
|
121
|
-
...def.options,
|
|
122
|
-
} as UseHotkeySequenceOptions
|
|
123
|
-
|
|
124
|
-
const resolvedTarget = isRef(mergedOptions.target)
|
|
125
|
-
? mergedOptions.target.current
|
|
126
|
-
: (mergedOptions.target ??
|
|
127
|
-
(typeof document !== 'undefined' ? document : null))
|
|
128
|
-
|
|
129
|
-
if (!resolvedTarget) {
|
|
130
|
-
continue
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
const registrationKey = `${i}:${seqStr}`
|
|
134
|
-
rows.push({
|
|
135
|
-
registrationKey,
|
|
136
|
-
def,
|
|
137
|
-
seq,
|
|
138
|
-
seqStr,
|
|
139
|
-
mergedOptions,
|
|
140
|
-
resolvedTarget,
|
|
141
|
-
})
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
const nextKeys = new Set(rows.map((r) => r.registrationKey))
|
|
145
|
-
|
|
146
|
-
for (const [key, record] of prevRegistrations) {
|
|
147
|
-
if (!nextKeys.has(key) && record.handle.isActive) {
|
|
148
|
-
record.handle.unregister()
|
|
149
|
-
}
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
for (const row of rows) {
|
|
153
|
-
const { registrationKey, def, seq, mergedOptions, resolvedTarget } = row
|
|
154
|
-
|
|
155
|
-
const existing = prevRegistrations.get(registrationKey)
|
|
156
|
-
if (existing?.handle.isActive && existing.target === resolvedTarget) {
|
|
157
|
-
// Publish callback/options only after commit, never during render.
|
|
158
|
-
existing.handle.callback = def.callback
|
|
159
|
-
const { target: _target, ...optionsWithoutTarget } = mergedOptions
|
|
160
|
-
existing.handle.setOptions(optionsWithoutTarget)
|
|
161
|
-
nextRegistrations.set(registrationKey, existing)
|
|
162
|
-
continue
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
if (existing?.handle.isActive) {
|
|
166
|
-
existing.handle.unregister()
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
const handle = managerRef.current.register(seq, def.callback, {
|
|
170
|
-
...mergedOptions,
|
|
171
|
-
target: resolvedTarget,
|
|
172
|
-
})
|
|
173
|
-
nextRegistrations.set(registrationKey, {
|
|
174
|
-
handle,
|
|
175
|
-
target: resolvedTarget,
|
|
176
|
-
})
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
registrationsRef.current = nextRegistrations
|
|
180
|
-
})
|
|
181
|
-
|
|
182
|
-
useEffect(() => {
|
|
183
|
-
return () => {
|
|
184
|
-
for (const { handle } of registrationsRef.current.values()) {
|
|
185
|
-
if (handle.isActive) {
|
|
186
|
-
handle.unregister()
|
|
187
|
-
}
|
|
188
|
-
}
|
|
189
|
-
registrationsRef.current = new Map()
|
|
190
|
-
}
|
|
191
|
-
}, [])
|
|
192
|
-
}
|