Re-focus on focus. (#2010)

This PR improves the focus handling (I think!) in the editor.

### Change Type

- [x] `patch` — Bug fix
This commit is contained in:
Steve Ruiz 2023-10-03 16:33:43 +01:00 committed by GitHub
parent c63126ba5f
commit 6ce1f8e66f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -5,11 +5,25 @@ import { useEditor } from './useEditor'
export function useFocusEvents(autoFocus: boolean) {
const editor = useEditor()
useLayoutEffect(() => {
const container = editor.getContainer()
function handleFocus() {
if (!editor.instanceState.isFocused) {
editor.updateInstanceState({ isFocused: true })
}
}
container.addEventListener('focus', handleFocus)
if (autoFocus && !editor.instanceState.isFocused) {
editor.updateInstanceState({ isFocused: true })
editor.getContainer().focus()
} else if (editor.instanceState.isFocused) {
editor.updateInstanceState({ isFocused: false })
}
return () => {
container.removeEventListener('focus', handleFocus)
}
}, [editor, autoFocus])
}