diff --git a/gui/src/components/mainInput/TipTapEditor.tsx b/gui/src/components/mainInput/TipTapEditor.tsx index f393595f3..857bd5a59 100644 --- a/gui/src/components/mainInput/TipTapEditor.tsx +++ b/gui/src/components/mainInput/TipTapEditor.tsx @@ -420,7 +420,7 @@ function TipTapEditor(props: TipTapEditorProps) { }, }); - const shortcutTimeoutRef = useRef(undefined); + const editorFocusedRef = useUpdatingRef(editor?.isFocused, [editor]); useEffect(() => { if (isJetBrains()) { @@ -429,35 +429,18 @@ function TipTapEditor(props: TipTapEditorProps) { } const handleKeyDown = async (event: KeyboardEvent) => { - if (!editor) return; - - const shouldSkip = () => { - const skip = shortcutTimeoutRef.current !== undefined; - - if (!skip) { - shortcutTimeoutRef.current = setTimeout(async () => { - shortcutTimeoutRef.current = undefined; - }, 100); - } - return skip - } + if (!editor || !editorFocusedRef.current) return; if (event.metaKey && event.key === "x") { - if (!shouldSkip()) { - document.execCommand("cut"); - } + document.execCommand("cut"); event.stopPropagation(); event.preventDefault(); } else if (event.metaKey && event.key === "v") { - if (!shouldSkip()) { - document.execCommand("paste"); - } + document.execCommand("paste"); event.stopPropagation(); event.preventDefault(); } else if (event.metaKey && event.key === "c") { - if (!shouldSkip()) { - document.execCommand("copy"); - } + document.execCommand("copy"); event.stopPropagation(); event.preventDefault(); } @@ -468,7 +451,7 @@ function TipTapEditor(props: TipTapEditorProps) { return () => { document.removeEventListener("keydown", handleKeyDown); }; - }, [editor, shortcutTimeoutRef]); + }, [editor, editorFocusedRef]); useEffect(() => { if (mainEditorContent && editor) { @@ -525,7 +508,7 @@ function TipTapEditor(props: TipTapEditorProps) { // Re-focus main input after done generating useEffect(() => { if (editor && !active && props.isMainInput && document.hasFocus()) { - editor.commands.focus(undefined, {scrollIntoView: false}); + editor.commands.focus(undefined, { scrollIntoView: false }); } }, [props.isMainInput, active, editor]);