Open History to see the last commit
1import { useEffect, useRef, useState } from "react";
2import { isEditableFocused } from "./lib/editableFocus";
3
4export interface ShortcutModifierState {
5 metaKey: boolean;
6 ctrlKey: boolean;
7 altKey: boolean;
8 shiftKey: boolean;
9}
10
11const EMPTY_SHORTCUT_MODIFIER_STATE: ShortcutModifierState = {
12 metaKey: false,
13 ctrlKey: false,
14 altKey: false,
15 shiftKey: false,
16};
17
18export function areShortcutModifierStatesEqual(
19 left: ShortcutModifierState,
20 right: ShortcutModifierState,
21): boolean {
22 return (
23 left.metaKey === right.metaKey &&
24 left.ctrlKey === right.ctrlKey &&
25 left.altKey === right.altKey &&
26 left.shiftKey === right.shiftKey
27 );
28}
29
30export function useShortcutModifierState(ignoreEditable = false): ShortcutModifierState {
31 const [state, setState] = useState(EMPTY_SHORTCUT_MODIFIER_STATE);
32 const stateRef = useRef(EMPTY_SHORTCUT_MODIFIER_STATE);
33
34 useEffect(() => {
35 const updateState = (next: ShortcutModifierState) => {
36 // Even a no-op state dispatch can cost work in the sidebar's large tree.
37 // Ordinary typing must return before dispatching a React update.
38 if (areShortcutModifierStatesEqual(stateRef.current, next)) return;
39 stateRef.current = next;
40 setState(next);
41 };
42 const onKeyboardEvent = (event: KeyboardEvent) => {
43 updateState(
44 ignoreEditable && isEditableFocused(event.target)
45 ? EMPTY_SHORTCUT_MODIFIER_STATE
46 : shortcutModifierStateAfterKeyboardEvent(stateRef.current, event),
47 );
48 };
49 // Dictation tools (Wispr Flow) paste with a synthetic ⌘V whose Meta keyup
50 // never reaches the page, so the tracked state stays "⌘ held" forever and
51 // the thread jump hints stick on screen.
52 const onResetEvent = () => {
53 updateState(EMPTY_SHORTCUT_MODIFIER_STATE);
54 };
55
56 const onFocus = (event: FocusEvent) => {
57 if (ignoreEditable && isEditableFocused(event.target)) onResetEvent();
58 };
59 window.addEventListener("focusin", onFocus);
60 window.addEventListener("keydown", onKeyboardEvent, true);
61 window.addEventListener("keyup", onKeyboardEvent, true);
62 window.addEventListener("paste", onResetEvent, true);
63 window.addEventListener("blur", onResetEvent);
64 return () => {
65 window.removeEventListener("focusin", onFocus);
66 window.removeEventListener("keydown", onKeyboardEvent, true);
67 window.removeEventListener("keyup", onKeyboardEvent, true);
68 window.removeEventListener("paste", onResetEvent, true);
69 window.removeEventListener("blur", onResetEvent);
70 };
71 }, [ignoreEditable]);
72
73 return state;
74}
75
76function normalizeModifierKey(key: string): keyof ShortcutModifierState | null {
77 switch (key) {
78 case "Meta":
79 case "OS":
80 case "Command":
81 return "metaKey";
82 case "Control":
83 return "ctrlKey";
84 case "Alt":
85 case "Option":
86 return "altKey";
87 case "Shift":
88 return "shiftKey";
89 default:
90 return null;
91 }
92}
93
94export function shortcutModifierStateAfterKeyboardEvent(
95 currentState: ShortcutModifierState,
96 event: KeyboardEvent,
97): ShortcutModifierState {
98 const normalizedModifierKey = normalizeModifierKey(event.key);
99 let nextState: ShortcutModifierState;
100 if (normalizedModifierKey) {
101 nextState = {
102 ...currentState,
103 [normalizedModifierKey]: event.type === "keydown",
104 };
105 } else {
106 // After a dictation tool's synthetic ⌘V (Wispr Flow), the browser can keep
107 // reporting metaKey=true on real key events (Enter to submit) until the
108 // user physically taps ⌘. Trusting that flag would mark ⌘ as held and stick
109 // the thread jump hints.
110 nextState = {
111 metaKey: currentState.metaKey && event.metaKey,
112 ctrlKey: currentState.ctrlKey && event.ctrlKey,
113 altKey: currentState.altKey && event.altKey,
114 shiftKey: currentState.shiftKey && event.shiftKey,
115 };
116 }
117
118 return areShortcutModifierStatesEqual(currentState, nextState) ? currentState : nextState;
119}