(
hotkey: Hotkey | ParsedHotkey,
callback: HotkeyCallback,
options: CreateHotkeyHandlerOptions = {},
)
| 157 | * ``` |
| 158 | */ |
| 159 | export function createHotkeyHandler( |
| 160 | hotkey: Hotkey | ParsedHotkey, |
| 161 | callback: HotkeyCallback, |
| 162 | options: CreateHotkeyHandlerOptions = {}, |
| 163 | ): (event: KeyboardEvent) => void { |
| 164 | const { preventDefault = true, stopPropagation = true, platform } = options |
| 165 | const resolvedPlatform = platform ?? detectPlatform() |
| 166 | |
| 167 | const hotkeyString: Hotkey = |
| 168 | typeof hotkey === 'string' ? hotkey : formatParsedHotkey(hotkey) |
| 169 | const parsed = |
| 170 | typeof hotkey === 'string' ? parseHotkey(hotkey, resolvedPlatform) : hotkey |
| 171 | |
| 172 | const context: HotkeyCallbackContext = { |
| 173 | hotkey: hotkeyString, |
| 174 | parsedHotkey: parsed, |
| 175 | } |
| 176 | |
| 177 | return (event: KeyboardEvent) => { |
| 178 | if (matchesKeyboardEvent(event, parsed, resolvedPlatform)) { |
| 179 | if (preventDefault) { |
| 180 | event.preventDefault() |
| 181 | } |
| 182 | if (stopPropagation) { |
| 183 | event.stopPropagation() |
| 184 | } |
| 185 | callback(event, context) |
| 186 | } |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | type MultiHotkeyHandler = { [K in Hotkey]?: HotkeyCallback } |
| 191 |
no test coverage detected
searching dependent graphs…