From 7070a0d410973a47dbf54f99ec3f9b45d2e7160f Mon Sep 17 00:00:00 2001 From: Alf Eaton Date: Tue, 25 Feb 2025 09:23:22 +0000 Subject: [PATCH] Focus the editor and place cursor after the learned/corrected word (#23782) GitOrigin-RevId: 605b13a4bb0d8e9375d8c4edc17a1866e29a3169 --- .../extensions/spelling/context-menu.tsx | 21 +++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/services/web/frontend/js/features/source-editor/extensions/spelling/context-menu.tsx b/services/web/frontend/js/features/source-editor/extensions/spelling/context-menu.tsx index 0007b85f84..4cb9c9f7eb 100644 --- a/services/web/frontend/js/features/source-editor/extensions/spelling/context-menu.tsx +++ b/services/web/frontend/js/features/source-editor/extensions/spelling/context-menu.tsx @@ -1,4 +1,4 @@ -import { StateField, StateEffect, Prec } from '@codemirror/state' +import { StateField, StateEffect, Prec, EditorSelection } from '@codemirror/state' import { EditorView, showTooltip, Tooltip, keymap } from '@codemirror/view' import { Word, Mark, getMarkAtPosition } from './spellchecker' import { debugConsole } from '@/utils/debugging' @@ -169,6 +169,16 @@ const createSpellingSuggestionList = (word: Word) => (view: EditorView) => { } }} handleLearnWord={() => { + const tooltip = view.state.field(spellingMenuField) + if (tooltip) { + window.setTimeout(() => { + view.dispatch({ + selection: EditorSelection.cursor(tooltip.end ?? tooltip.pos), + }) + }) + } + view.focus() + postJSON('/spelling/learn', { body: { word: word.text, @@ -201,9 +211,16 @@ const createSpellingSuggestionList = (word: Word) => (view: EditorView) => { } window.setTimeout(() => { + const changes = view.state.changes([ + { from: tooltip.pos, to: tooltip.end, insert: text }, + ]) + view.dispatch({ - changes: [{ from: tooltip.pos, to: tooltip.end, insert: text }], + changes, effects: [hideSpellingMenu.of(null)], + selection: EditorSelection.cursor(tooltip.end ?? tooltip.pos).map( + changes + ), }) }) view.focus()