fix: Cursor moves before highlighted token in code blocks with line numbers (#14382)

This commit is contained in:
Tom Moor authored and GitHub committed 2026-10-31 10:40:39 -04:00
1 parent 8852acb5f9
commit 880f58b20d
1 file changed
+38
@@ -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 &&