| b69ab31 | | | 1 | /** |
| b69ab31 | | | 2 | * Copyright (c) Meta Platforms, Inc. and affiliates. |
| b69ab31 | | | 3 | * |
| b69ab31 | | | 4 | * This source code is licensed under the MIT license found in the |
| b69ab31 | | | 5 | * LICENSE file in the root directory of this source tree. |
| b69ab31 | | | 6 | */ |
| b69ab31 | | | 7 | |
| b69ab31 | | | 8 | import type {FunctionComponent, PropsWithChildren} from 'react'; |
| b69ab31 | | | 9 | |
| b69ab31 | | | 10 | import {createContext, useContext, useEffect} from 'react'; |
| b69ab31 | | | 11 | |
| b69ab31 | | | 12 | /* eslint-disable no-bitwise */ |
| b69ab31 | | | 13 | |
| b69ab31 | | | 14 | type Modifiers = Modifier | Array<Modifier>; |
| b69ab31 | | | 15 | /** |
| b69ab31 | | | 16 | * Modifiers for keyboard shortcuts, intended to be bitwise-OR'd together. |
| b69ab31 | | | 17 | * e.g. `Modifier.CMD | Modifier.CTRL`. |
| b69ab31 | | | 18 | */ |
| b69ab31 | | | 19 | export enum Modifier { |
| b69ab31 | | | 20 | NONE = 0, |
| b69ab31 | | | 21 | SHIFT = 1 << 0, |
| b69ab31 | | | 22 | CTRL = 1 << 1, |
| b69ab31 | | | 23 | ALT = 1 << 2, |
| b69ab31 | | | 24 | CMD = 1 << 3, |
| b69ab31 | | | 25 | } |
| b69ab31 | | | 26 | |
| b69ab31 | | | 27 | export enum KeyCode { |
| b69ab31 | | | 28 | Escape = 27, |
| b69ab31 | | | 29 | One = 49, |
| b69ab31 | | | 30 | Two = 50, |
| b69ab31 | | | 31 | Three = 51, |
| b69ab31 | | | 32 | Four = 52, |
| b69ab31 | | | 33 | Five = 53, |
| b69ab31 | | | 34 | A = 65, |
| b69ab31 | | | 35 | B = 66, |
| b69ab31 | | | 36 | C = 67, |
| b69ab31 | | | 37 | D = 68, |
| b69ab31 | | | 38 | F = 70, |
| b69ab31 | | | 39 | G = 71, |
| b69ab31 | | | 40 | M = 77, |
| b69ab31 | | | 41 | N = 78, |
| b69ab31 | | | 42 | P = 80, |
| b69ab31 | | | 43 | R = 82, |
| b69ab31 | | | 44 | S = 83, |
| b69ab31 | | | 45 | T = 84, |
| b69ab31 | | | 46 | Period = 190, |
| b69ab31 | | | 47 | QuestionMark = 191, |
| b69ab31 | | | 48 | SingleQuote = 222, |
| b69ab31 | | | 49 | LeftArrow = 37, |
| b69ab31 | | | 50 | UpArrow = 38, |
| b69ab31 | | | 51 | RightArrow = 39, |
| b69ab31 | | | 52 | DownArrow = 40, |
| b69ab31 | | | 53 | Backspace = 8, |
| b69ab31 | | | 54 | Plus = 187, |
| b69ab31 | | | 55 | Minus = 189, |
| b69ab31 | | | 56 | } |
| b69ab31 | | | 57 | |
| b69ab31 | | | 58 | type CommandDefinition = [Modifiers, KeyCode]; |
| b69ab31 | | | 59 | |
| b69ab31 | | | 60 | type CommandMap<CommandName extends string> = Record<CommandName, CommandDefinition>; |
| b69ab31 | | | 61 | |
| b69ab31 | | | 62 | function isTargetTextInputElement(event: KeyboardEvent): boolean { |
| b69ab31 | | | 63 | return ( |
| b69ab31 | | | 64 | event.target != null && |
| b69ab31 | | | 65 | /(vscode-text-area|vscode-text-field|textarea|input)/i.test( |
| b69ab31 | | | 66 | (event.target as HTMLElement).tagName, |
| b69ab31 | | | 67 | ) |
| b69ab31 | | | 68 | ); |
| b69ab31 | | | 69 | } |
| b69ab31 | | | 70 | |
| b69ab31 | | | 71 | class CommandDispatcher<CommandName extends string> extends ( |
| b69ab31 | | | 72 | window as { |
| b69ab31 | | | 73 | EventTarget: { |
| b69ab31 | | | 74 | new (): EventTarget; |
| b69ab31 | | | 75 | prototype: EventTarget; |
| b69ab31 | | | 76 | }; |
| b69ab31 | | | 77 | } |
| b69ab31 | | | 78 | ).EventTarget { |
| b69ab31 | | | 79 | private keydownListener: (event: KeyboardEvent) => void; |
| b69ab31 | | | 80 | constructor(commands: CommandMap<CommandName>) { |
| b69ab31 | | | 81 | super(); |
| b69ab31 | | | 82 | const knownKeysWithCommands = new Set<KeyCode>(); |
| b69ab31 | | | 83 | for (const cmdDef of Object.values(commands) as Array<CommandDefinition>) { |
| b69ab31 | | | 84 | const [, key] = cmdDef; |
| b69ab31 | | | 85 | knownKeysWithCommands.add(key); |
| b69ab31 | | | 86 | } |
| b69ab31 | | | 87 | this.keydownListener = (event: KeyboardEvent) => { |
| b69ab31 | | | 88 | if (!knownKeysWithCommands.has(event.keyCode)) { |
| b69ab31 | | | 89 | return; |
| b69ab31 | | | 90 | } |
| b69ab31 | | | 91 | if (isTargetTextInputElement(event)) { |
| b69ab31 | | | 92 | // we don't want shortcuts to interfere with text entry |
| b69ab31 | | | 93 | return; |
| b69ab31 | | | 94 | } |
| b69ab31 | | | 95 | const modValue = |
| b69ab31 | | | 96 | (event.shiftKey ? Modifier.SHIFT : 0) | |
| b69ab31 | | | 97 | (event.ctrlKey ? Modifier.CTRL : 0) | |
| b69ab31 | | | 98 | (event.altKey ? Modifier.ALT : 0) | |
| b69ab31 | | | 99 | (event.metaKey ? Modifier.CMD : 0); |
| b69ab31 | | | 100 | |
| b69ab31 | | | 101 | for (const [command, cmdAttrs] of Object.entries(commands) as Array< |
| b69ab31 | | | 102 | [CommandName, CommandDefinition] |
| b69ab31 | | | 103 | >) { |
| b69ab31 | | | 104 | const [mods, key] = cmdAttrs; |
| b69ab31 | | | 105 | if (key === event.keyCode && collapseModifiersToNumber(mods) === modValue) { |
| b69ab31 | | | 106 | this.dispatchEvent(new Event(command)); |
| b69ab31 | | | 107 | break; |
| b69ab31 | | | 108 | } |
| b69ab31 | | | 109 | } |
| b69ab31 | | | 110 | }; |
| b69ab31 | | | 111 | document.body.addEventListener('keydown', this.keydownListener); |
| b69ab31 | | | 112 | } |
| b69ab31 | | | 113 | } |
| b69ab31 | | | 114 | |
| b69ab31 | | | 115 | function collapseModifiersToNumber(mods: Modifiers): number { |
| b69ab31 | | | 116 | return Array.isArray(mods) ? mods.reduce((acc, mod) => acc | mod, Modifier.NONE) : mods; |
| b69ab31 | | | 117 | } |
| b69ab31 | | | 118 | |
| b69ab31 | | | 119 | /** |
| b69ab31 | | | 120 | * Add support for commands which are triggered by keyboard shortcuts. |
| b69ab31 | | | 121 | * return a top-level context provider which listens for global keyboard input, |
| b69ab31 | | | 122 | * plus a `useCommand` hook that lets you handle commands as they are dispatched, |
| b69ab31 | | | 123 | * plus a callback to dispatch events at any point in code (to simulate keyboard shortcuts). |
| b69ab31 | | | 124 | * |
| b69ab31 | | | 125 | * Commands are defined by mapping string command names to a key plus a set of modifiers. |
| b69ab31 | | | 126 | * CommandNames are statically known so that `useCommand` is type-safe. |
| b69ab31 | | | 127 | * Modifiers are a bitwise-OR union of {@link Modifier}, like `Modifier.CTRL | Modifier.CMD` |
| b69ab31 | | | 128 | * |
| b69ab31 | | | 129 | * Commands are not dispatched when the target is an input element, to ensure we don't affect typing. |
| b69ab31 | | | 130 | */ |
| b69ab31 | | | 131 | export function makeCommandDispatcher<CommandName extends string>( |
| b69ab31 | | | 132 | commands: CommandMap<CommandName>, |
| b69ab31 | | | 133 | ): [ |
| b69ab31 | | | 134 | FunctionComponent<PropsWithChildren>, |
| b69ab31 | | | 135 | (command: CommandName, handler: () => void) => void, |
| b69ab31 | | | 136 | (command: CommandName) => void, |
| b69ab31 | | | 137 | CommandMap<CommandName>, |
| b69ab31 | | | 138 | ] { |
| b69ab31 | | | 139 | const commandDispatcher = new CommandDispatcher(commands); |
| b69ab31 | | | 140 | const Context = createContext(commandDispatcher); |
| b69ab31 | | | 141 | |
| b69ab31 | | | 142 | function useCommand(command: CommandName, handler: () => void) { |
| b69ab31 | | | 143 | const dispatcher = useContext(Context); |
| b69ab31 | | | 144 | |
| b69ab31 | | | 145 | // register & unregister the event listener while the component is mounted |
| b69ab31 | | | 146 | useEffect(() => { |
| b69ab31 | | | 147 | dispatcher.addEventListener(command, handler); |
| b69ab31 | | | 148 | return () => dispatcher.removeEventListener(command, handler); |
| b69ab31 | | | 149 | }, [command, handler, dispatcher]); |
| b69ab31 | | | 150 | } |
| b69ab31 | | | 151 | |
| b69ab31 | | | 152 | return [ |
| b69ab31 | | | 153 | ({children}) => <Context.Provider value={commandDispatcher}>{children}</Context.Provider>, |
| b69ab31 | | | 154 | useCommand, |
| b69ab31 | | | 155 | (command: CommandName) => commandDispatcher.dispatchEvent(new Event(command)), |
| b69ab31 | | | 156 | commands, |
| b69ab31 | | | 157 | ]; |
| b69ab31 | | | 158 | } |