From 3e7fb07931b437c2ffc2be9a785eec88aba94c48 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bel=C3=A9n=20Albeza?= Date: Wed, 29 Jul 2026 15:53:41 +0200 Subject: [PATCH] :bug: Fix undo crash after deleting a word (#10862) --- frontend/src/app/main/data/workspace/texts.cljs | 9 ++++++++- render-wasm/src/state/text_editor.rs | 2 +- render-wasm/src/wasm/text/helpers.rs | 8 ++++++++ 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/frontend/src/app/main/data/workspace/texts.cljs b/frontend/src/app/main/data/workspace/texts.cljs index c3a907dde1..5f7041b654 100644 --- a/frontend/src/app/main/data/workspace/texts.cljs +++ b/frontend/src/app/main/data/workspace/texts.cljs @@ -1311,7 +1311,14 @@ {:new-shape? new-shape? :content-has-text? content-has-text? :content content - :original-content original-content + ;; Undo baseline for the finalize commit: restore the + ;; content as it was right before this commit. For existing + ;; shapes that's `prev-content`; using the (unset, nil) + ;; `original-content` here wiped `:content` to nil on undo, + ;; which emptied the shape and crashed the WASM editor's + ;; select-all on 0 paragraphs. New shapes keep the previous + ;; behavior (their create is bundled in the undo group). + :original-content (if new-shape? original-content prev-content) :update-name? update-name? :name name}))) (rx/empty)) diff --git a/render-wasm/src/state/text_editor.rs b/render-wasm/src/state/text_editor.rs index e112d49732..7ae4421246 100644 --- a/render-wasm/src/state/text_editor.rs +++ b/render-wasm/src/state/text_editor.rs @@ -436,7 +436,7 @@ impl TextEditorState { pub fn select_all(&mut self, text_content: &TextContent) -> bool { self.is_pointer_selection_active = false; self.set_caret_from_position(&TextPositionWithAffinity::empty()); - let num_paragraphs = text_content.paragraphs().len() - 1; + let num_paragraphs = text_content.paragraphs().len().saturating_sub(1); let Some(last_paragraph) = text_content.paragraphs().last() else { return false; }; diff --git a/render-wasm/src/wasm/text/helpers.rs b/render-wasm/src/wasm/text/helpers.rs index 67044b4788..32ed8238d7 100644 --- a/render-wasm/src/wasm/text/helpers.rs +++ b/render-wasm/src/wasm/text/helpers.rs @@ -195,6 +195,10 @@ pub fn move_cursor_up( _text_content: &TextContent, ) -> TextPositionWithAffinity { // TODO: Implement proper line-based navigation using line metrics + if paragraphs.is_empty() || cursor.paragraph >= paragraphs.len() { + return *cursor; + } + if cursor.paragraph > 0 { let prev_para = cursor.paragraph - 1; let char_count = paragraph_char_count(¶graphs[prev_para]); @@ -212,6 +216,10 @@ pub fn move_cursor_down( _text_content: &TextContent, ) -> TextPositionWithAffinity { // TODO: Implement proper line-based navigation using line metrics + if paragraphs.is_empty() || cursor.paragraph >= paragraphs.len() { + return *cursor; + } + if cursor.paragraph < paragraphs.len() - 1 { let next_para = cursor.paragraph + 1; let char_count = paragraph_char_count(¶graphs[next_para]);