From 59ff6252237b1e34e04f3575f062be766decd8a2 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Tue, 17 Dec 2024 20:21:40 -0500 Subject: [PATCH] fix cursor position on state changes (#1035) fix cursor position on state changes - fixes #935 --- packages/editor/src/lib/Workspace.svelte.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/packages/editor/src/lib/Workspace.svelte.ts b/packages/editor/src/lib/Workspace.svelte.ts index d73f4f65eac8..9bfa1e908a18 100644 --- a/packages/editor/src/lib/Workspace.svelte.ts +++ b/packages/editor/src/lib/Workspace.svelte.ts @@ -556,7 +556,10 @@ export class Workspace { const existing = state.doc.toString(); if (file.contents !== existing) { - const current_cursor_position = this.#view?.state.selection.ranges[0].from!; + const current_cursor_position = Math.min( + this.#view?.state.selection.ranges[0].from!, + file.contents.length + ); const transaction = state.update({ changes: {