From 53e46632a9765f34bd95097216566557ccc8ce67 Mon Sep 17 00:00:00 2001 From: Alf Eaton Date: Wed, 25 Sep 2024 12:12:56 +0100 Subject: [PATCH] Improve editor focus events (#20263) GitOrigin-RevId: 710f42eb633289b43858b8f283735ed0aced5c0b --- .../source-editor/extensions/spelling/index.ts | 2 +- .../hooks/use-codemirror-scope.ts | 18 +++++++++--------- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/services/web/frontend/js/features/source-editor/extensions/spelling/index.ts b/services/web/frontend/js/features/source-editor/extensions/spelling/index.ts index 7256921c65..aa8970b611 100644 --- a/services/web/frontend/js/features/source-editor/extensions/spelling/index.ts +++ b/services/web/frontend/js/features/source-editor/extensions/spelling/index.ts @@ -82,7 +82,7 @@ const spellCheckField = StateField.define({ EditorView.domEventHandlers({ focus: (event, view) => { if (view.state.facet(EditorView.editable)) { - view.state.field(field)?.spellCheckAsap(view) + view.state.field(field)?.scheduleSpellCheck(view) } }, }), diff --git a/services/web/frontend/js/features/source-editor/hooks/use-codemirror-scope.ts b/services/web/frontend/js/features/source-editor/hooks/use-codemirror-scope.ts index a6d54bdc09..977534d922 100644 --- a/services/web/frontend/js/features/source-editor/hooks/use-codemirror-scope.ts +++ b/services/web/frontend/js/features/source-editor/hooks/use-codemirror-scope.ts @@ -223,10 +223,16 @@ function useCodeMirrorScope(view: EditorView) { view.dispatch(setSpelling(spellingRef.current)) }, [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(() => { - const listener = () => { - scheduleFocus(view) + const listener: EventListener = event => { + const { isNewDoc } = (event as CustomEvent<{ isNewDoc: boolean }>).detail + + if (!isNewDoc) { + window.setTimeout(() => { + view.focus() + }, 0) + } } window.addEventListener('doc:after-opened', listener) return () => window.removeEventListener('doc:after-opened', listener) @@ -554,9 +560,3 @@ function useCodeMirrorScope(view: EditorView) { } export default useCodeMirrorScope - -const scheduleFocus = (view: EditorView) => { - window.setTimeout(() => { - view.focus() - }, 0) -}