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:
parent
c63126ba5f
commit
6ce1f8e66f
1 changed files with 14 additions and 0 deletions
|
@ -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])
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue