Elffuss BraidHub

kikocis/continue public

8d9577e85559 · 2.0 KiBRawPermalink
1import { Editor } from "@tiptap/react";
2import { KeyboardEvent, useRef } from "react";
3import { isJetBrains, isMetaEquivalentKeyPressed } from "../../../../util";
4import {
5 handleJetBrainsOSRMetaKeyIssues,
6 handleVSCMetaKeyIssues,
7} from "../../util/handleMetaKeyIssues";
8
9export function useEditorEventHandlers(options: {
10 editor: Editor | null;
11 isOSREnabled: boolean;
12 editorFocusedRef: React.MutableRefObject<boolean | undefined>;
13 setActiveKey: (key: string | null) => void;
14}) {
15 const { editor, isOSREnabled, editorFocusedRef, setActiveKey } = options;
16 const metaActiveRef = useRef(false);
17
18 /**
19 * This handles various issues with meta key actions
20 * - In JetBrains, when using OSR in JCEF, there is a bug where using the meta key to
21 * highlight code using arrow keys is not working
22 * - In VS Code, while working with .ipynb files there is a problem where copy/paste/cut will affect
23 * the actual notebook cells, even when performing them in our GUI
24 *
25 * Currently keydown events for a number of keys are not registering if the
26 * meta/shift key is pressed, for example "x", "c", "v", "z", etc.
27 * Until this is resolved we can't turn on OSR for non-Mac users due to issues
28 * with those key actions.
29 */
30 const handleKeyDown = async (e: KeyboardEvent<HTMLDivElement>) => {
31 if (!editor) {
32 return;
33 }
34
35 if (!editorFocusedRef?.current || !isMetaEquivalentKeyPressed(e)) return;
36
37 // Only set activeKey for Meta/Control/Alt to drive toolbar highlighting
38 if (e.key === "Meta" || e.key === "Control" || e.key === "Alt") {
39 setActiveKey(e.key);
40 metaActiveRef.current = true;
41 }
42
43 if (isOSREnabled) {
44 handleJetBrainsOSRMetaKeyIssues(e, editor);
45 } else if (!isJetBrains()) {
46 await handleVSCMetaKeyIssues(e, editor);
47 }
48 };
49
50 const handleKeyUp = () => {
51 if (metaActiveRef.current) {
52 setActiveKey(null);
53 metaActiveRef.current = false;
54 }
55 };
56
57 return { handleKeyDown, handleKeyUp };
58}