| | |
| | |
| | |
| | |
| | |
| |
|
| | import type { Key } from './hooks/useKeypress.js'; |
| | import type { KeyBinding, KeyBindingConfig } from '../config/keyBindings.js'; |
| | import { Command, defaultKeyBindings } from '../config/keyBindings.js'; |
| |
|
| | |
| | |
| | |
| | |
| | function matchKeyBinding(keyBinding: KeyBinding, key: Key): boolean { |
| | |
| | let keyMatches = false; |
| |
|
| | if (keyBinding.key !== undefined) { |
| | keyMatches = keyBinding.key === key.name; |
| | } else if (keyBinding.sequence !== undefined) { |
| | keyMatches = keyBinding.sequence === key.sequence; |
| | } else { |
| | |
| | return false; |
| | } |
| |
|
| | if (!keyMatches) { |
| | return false; |
| | } |
| |
|
| | |
| | |
| | |
| | |
| |
|
| | if (keyBinding.ctrl !== undefined && key.ctrl !== keyBinding.ctrl) { |
| | return false; |
| | } |
| |
|
| | if (keyBinding.shift !== undefined && key.shift !== keyBinding.shift) { |
| | return false; |
| | } |
| |
|
| | if (keyBinding.command !== undefined && key.meta !== keyBinding.command) { |
| | return false; |
| | } |
| |
|
| | if (keyBinding.paste !== undefined && key.paste !== keyBinding.paste) { |
| | return false; |
| | } |
| |
|
| | return true; |
| | } |
| |
|
| | |
| | |
| | |
| | function matchCommand( |
| | command: Command, |
| | key: Key, |
| | config: KeyBindingConfig = defaultKeyBindings, |
| | ): boolean { |
| | const bindings = config[command]; |
| | return bindings.some((binding) => matchKeyBinding(binding, key)); |
| | } |
| |
|
| | |
| | |
| | |
| | type KeyMatcher = (key: Key) => boolean; |
| |
|
| | |
| | |
| | |
| | export type KeyMatchers = { |
| | readonly [C in Command]: KeyMatcher; |
| | }; |
| |
|
| | |
| | |
| | |
| | export function createKeyMatchers( |
| | config: KeyBindingConfig = defaultKeyBindings, |
| | ): KeyMatchers { |
| | const matchers = {} as { [C in Command]: KeyMatcher }; |
| |
|
| | for (const command of Object.values(Command)) { |
| | matchers[command] = (key: Key) => matchCommand(command, key, config); |
| | } |
| |
|
| | return matchers as KeyMatchers; |
| | } |
| |
|
| | |
| | |
| | |
| | export const keyMatchers: KeyMatchers = createKeyMatchers(defaultKeyBindings); |
| |
|
| | |
| | export { Command }; |
| |
|