MCPcopy
hub / github.com/jamiebuilds/tinykeys / createKeybindingsHandler

Function createKeybindingsHandler

src/tinykeys.ts:263–322  ·  view source on GitHub ↗
(
	keybindingsMap: KeybindingsMap,
	options: KeybindingHandlerOptions = {},
)

Source from the content-addressed store, hash-verified

261 * ```
262 */
263export 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)

Callers 1

tinykeysFunction · 0.85

Calls 4

parseKeybindingFunction · 0.85
isKeyboardEventFunction · 0.85
matchKeybindingPressFunction · 0.85
getModifierStateFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…