Merge pull request #5125 from urbit/hm/chat-autofocus

ChatEditor: fix ref reference
This commit is contained in:
matildepark 2021-07-22 00:35:17 -04:00 committed by GitHub
commit 84e453daef
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -136,7 +136,11 @@ const ChatEditor = React.forwardRef<CodeMirrorShim, ChatEditorProps>(({ 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<CodeMirrorShim, ChatEditorProps>(({ 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);
};
}, []);