(
keybindingsMap: KeybindingsMap,
options: KeybindingHandlerOptions = {},
)
| 261 | * ``` |
| 262 | */ |
| 263 | export function createKeybindingsHandler( |
| 264 | keybindingsMap: KeybindingsMap, |
| 265 | options: KeybindingHandlerOptions = {}, |
| 266 | ): EventListener { |
| 267 | let timeout = options.timeout ?? DEFAULT_TIMEOUT |
| 268 | let ignore = options.ignore ?? defaultKeybindingsHandlerIgnore |
| 269 | |
| 270 | let keybindings = Object.keys(keybindingsMap).map(input => { |
| 271 | return [input, parseKeybinding(input), keybindingsMap[input]] as const |
| 272 | }) |
| 273 | |
| 274 | let pending = new Map<string, KeybindingPress[]>() |
| 275 | let timer: number | null = null |
| 276 | |
| 277 | return event => { |
| 278 | if (!isKeyboardEvent(event) || ignore(event)) { |
| 279 | return |
| 280 | } |
| 281 | |
| 282 | let conflicts: Array<string> = [] |
| 283 | for (let [input, sequence, handler] of keybindings) { |
| 284 | let prev = pending.get(input) |
| 285 | let expected = prev ? prev : sequence |
| 286 | let [current, ...rest] = expected |
| 287 | |
| 288 | let matches = matchKeybindingPress(event, current) |
| 289 | |
| 290 | if (!matches) { |
| 291 | // Modifier keydown events shouldn't break sequences |
| 292 | // Note: This works because: |
| 293 | // - non-modifiers will always return false |
| 294 | // - if the current keypress is a modifier then it will return true when we check its state |
| 295 | // MDN: https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/getModifierState |
| 296 | if (!getModifierState(event, event.key)) { |
| 297 | pending.delete(input) |
| 298 | } |
| 299 | } else if (rest.length > 0) { |
| 300 | pending.set(input, rest) |
| 301 | conflicts.push(input) |
| 302 | } else { |
| 303 | pending.delete(input) |
| 304 | if (conflicts.length) { |
| 305 | console.warn( |
| 306 | `tinykeys: Conflict found, "${input}" did not fire, waiting for:`, |
| 307 | conflicts, |
| 308 | ) |
| 309 | } else { |
| 310 | handler(event) |
| 311 | break |
| 312 | } |
| 313 | } |
| 314 | } |
| 315 | |
| 316 | if (timer) { |
| 317 | clearTimeout(timer) |
| 318 | } |
| 319 | |
| 320 | timer = setTimeout(() => pending.clear(), timeout) |
no test coverage detected
searching dependent graphs…