diff --git a/shared/editor/plugins/CodeHighlightingPlugin.ts b/shared/editor/plugins/CodeHighlightingPlugin.ts index b7849a82fb..459f04127c 100644 --- a/shared/editor/plugins/CodeHighlightingPlugin.ts +++ b/shared/editor/plugins/CodeHighlightingPlugin.ts @@ -1,6 +1,7 @@ import type { Node } from "prosemirror-model"; import type { Transaction } from "prosemirror-state"; import { Plugin, PluginKey } from "prosemirror-state"; +import type { EditorView } from "prosemirror-view"; import { Decoration, DecorationSet } from "prosemirror-view"; import type { Refractor } from "refractor/core"; import { getLoaderForLanguage, getRefractorLangForLanguage } from "../lib/code"; @@ -221,6 +222,36 @@ function findMovedBlocks(tr: Transaction, previous: CodeBlock[]) { return moved; } +/** + * Set the DOM selection again at the cursor in a code block. When new + * decorations move the text node that holds the cursor, Chrome can report the + * cursor in the right place but insert typed text somewhere else. + * + * @param view - the editor view. + * @param name - the code block node name. + */ +function syncCursor(view: EditorView, name: string) { + const { selection } = view.state; + if ( + !selection.empty || + selection.$head.parent.type.name !== name || + view.composing || + !view.hasFocus() + ) { + return; + } + + const domSelection = window.getSelection(); + const { node, offset } = view.domAtPos(selection.head, -1); + if ( + !domSelection?.isCollapsed || + (domSelection.anchorNode === node && domSelection.anchorOffset === offset) + ) { + return; + } + domSelection.collapse(node, offset); +} + /** * Create a plugin that adds syntax highlighting and line numbers to code * blocks. Highlighted tokens are cached for each block node, so only blocks @@ -424,6 +455,13 @@ export function CodeHighlighting({ return { update: (_view, prevState) => { + if ( + key.getState(view.state)?.decorations !== + key.getState(prevState)?.decorations + ) { + syncCursor(view, name); + } + // Only edits postpone the refresh, so moving the cursor cannot delay it. if ( key.getState(view.state)?.stale &&