Elffuss BraidHub

kikocis/continue public

a6ac770a1291 · 1.6 KiBRawPermalink
1import { Editor } from "@tiptap/react";
2import { KeyboardEvent } 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
17 /**
18 * This handles various issues with meta key actions
19 * - In JetBrains, when using OSR in JCEF, there is a bug where using the meta key to
20 * highlight code using arrow keys is not working
21 * - In VS Code, while working with .ipynb files there is a problem where copy/paste/cut will affect
22 * the actual notebook cells, even when performing them in our GUI
23 *
24 * Currently keydown events for a number of keys are not registering if the
25 * meta/shift key is pressed, for example "x", "c", "v", "z", etc.
26 * Until this is resolved we can't turn on OSR for non-Mac users due to issues
27 * with those key actions.
28 */
29 const handleKeyDown = async (e: KeyboardEvent<HTMLDivElement>) => {
30 if (!editor) {
31 return;
32 }
33
34 setActiveKey(e.key);
35
36 if (!editorFocusedRef?.current || !isMetaEquivalentKeyPressed(e)) return;
37
38 if (isOSREnabled) {
39 handleJetBrainsOSRMetaKeyIssues(e, editor);
40 } else if (!isJetBrains()) {
41 await handleVSCMetaKeyIssues(e, editor);
42 }
43 };
44
45 const handleKeyUp = () => setActiveKey(null);
46
47 return { handleKeyDown, handleKeyUp };
48}