Skip to content

Commit

Permalink
Fix a bug where typing at the end of the document didn't produce the …
Browse files Browse the repository at this point in the history
…correct selection

FIX: Fix an issue on Chrome where typing at the end of the document would insert a
character after the cursor.

See https://discuss.codemirror.net/t/cursor-position-issue-on-the-end-of-the-line/8866
  • Loading branch information
marijnh committed Dec 6, 2024
1 parent 492a7f8 commit 2aa5d7f
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions src/domobserver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -567,9 +567,11 @@ class EditContextManager {
if (change.from == change.to && !change.insert.length) return

this.pendingContextChange = change
if (!view.state.readOnly)
applyDOMChangeInner(view, change, EditorSelection.single(this.toEditorPos(e.selectionStart),
this.toEditorPos(e.selectionEnd)))
if (!view.state.readOnly) {
let newLen = this.to - this.from + (change.to - change.from + change.insert.length)
applyDOMChangeInner(view, change, EditorSelection.single(this.toEditorPos(e.selectionStart, newLen),
this.toEditorPos(e.selectionEnd, newLen)))
}
// If the transaction didn't flush our change, revert it so
// that the context is in sync with the editor state again.
if (this.pendingContextChange) {
Expand Down Expand Up @@ -714,8 +716,8 @@ class EditContextManager {
this.to - this.from > CxVp.Margin * 3)
}

toEditorPos(contextPos: number) {
contextPos = Math.min(contextPos, this.to - this.from)
toEditorPos(contextPos: number, clipLen = this.to - this.from) {
contextPos = Math.min(contextPos, clipLen)
let c = this.composing
return c && c.drifted ? c.editorBase + (contextPos - c.contextBase) : contextPos + this.from
}
Expand Down

0 comments on commit 2aa5d7f

Please sign in to comment.