diff --git a/pkg/interface/src/views/apps/chat/components/ChatEditor.tsx b/pkg/interface/src/views/apps/chat/components/ChatEditor.tsx index 1149735ae1..3a5f29fb9d 100644 --- a/pkg/interface/src/views/apps/chat/components/ChatEditor.tsx +++ b/pkg/interface/src/views/apps/chat/components/ChatEditor.tsx @@ -136,7 +136,11 @@ const ChatEditor = React.forwardRef(({ inCodeMo setMessage } = useChatStore(); - function onKeyPress(e) { + const onKeyPress = (e: KeyboardEvent, editor: CodeMirrorShim) => { + if (!editor) { + return; + } + const focusedTag = document.activeElement?.nodeName?.toLowerCase(); const shouldCapture = !(focusedTag === 'textarea' || focusedTag === 'input' || e.metaKey || e.ctrlKey); if(/^[a-z]|[A-Z]$/.test(e.key) && shouldCapture) { @@ -145,13 +149,14 @@ const ChatEditor = React.forwardRef(({ inCodeMo if(e.key === 'Escape') { editor.getInputField().blur(); } - } + }; useEffect(() => { - document.addEventListener('keydown', onKeyPress); + const focusListener = (e: KeyboardEvent) => onKeyPress(e, editorRef.current); + document.addEventListener('keydown', focusListener); return () => { - document.removeEventListener('keydown', onKeyPress); + document.removeEventListener('keydown', focusListener); }; }, []);