Improve editor focus events (#20263)

GitOrigin-RevId: 710f42eb633289b43858b8f283735ed0aced5c0b
This commit is contained in:
Alf Eaton 2024-09-25 12:12:56 +01:00 committed by Copybot
parent ca72351c8e
commit 53e46632a9
2 changed files with 10 additions and 10 deletions

View file

@ -82,7 +82,7 @@ const spellCheckField = StateField.define<SpellChecker | null>({
EditorView.domEventHandlers({ EditorView.domEventHandlers({
focus: (event, view) => { focus: (event, view) => {
if (view.state.facet(EditorView.editable)) { if (view.state.facet(EditorView.editable)) {
view.state.field(field)?.spellCheckAsap(view) view.state.field(field)?.scheduleSpellCheck(view)
} }
}, },
}), }),

View file

@ -223,10 +223,16 @@ function useCodeMirrorScope(view: EditorView) {
view.dispatch(setSpelling(spellingRef.current)) view.dispatch(setSpelling(spellingRef.current))
}, [view, spellCheckLanguage]) }, [view, spellCheckLanguage])
// listen to doc:after-opened, and focus the editor // listen to doc:after-opened, and focus the editor if it's not a new doc
useEffect(() => { useEffect(() => {
const listener = () => { const listener: EventListener = event => {
scheduleFocus(view) const { isNewDoc } = (event as CustomEvent<{ isNewDoc: boolean }>).detail
if (!isNewDoc) {
window.setTimeout(() => {
view.focus()
}, 0)
}
} }
window.addEventListener('doc:after-opened', listener) window.addEventListener('doc:after-opened', listener)
return () => window.removeEventListener('doc:after-opened', listener) return () => window.removeEventListener('doc:after-opened', listener)
@ -554,9 +560,3 @@ function useCodeMirrorScope(view: EditorView) {
} }
export default useCodeMirrorScope export default useCodeMirrorScope
const scheduleFocus = (view: EditorView) => {
window.setTimeout(() => {
view.focus()
}, 0)
}