From c34cd056e4c43eea480c21ba4eb42b75817427c3 Mon Sep 17 00:00:00 2001 From: Pablo Alba Date: Tue, 17 Nov 2026 10:56:32 +0100 Subject: [PATCH] :bug: Skip undo step for auto-grow heal resizes On the WebGL renderer, the workspace resizes auto-grow texts whose stored size no longer matches their WASM layout. This runs when a page opens and when fonts finish loading. These resizes are not user actions, but each one recorded an undo step. So the first Ctrl+Z after opening a page undid the resize instead of the user's last edit, and the copy went back to the wrong size. The listener for the stale-text event now asks for the resize with :save-undo? false. The flag passes through the resize debounce to the commit, which hands it to apply-wasm-modifiers, so the shape update records no undo step. Resizes are batched in a debounce window. A batch skips the undo step only when every request in it asked for that. A user resize that lands in the same window keeps its undo entry. Batch options are now merged for every request, including those without options, so this check works. Tests: frontend-tests.data.workspace-heal-undo-test covers a heal resize that commits without an undo step, a resize without the flag that still records one, a mixed batch that keeps the undo step in either order, and a batch of heals that skips it. Relates to #12721 Relates to #13065 AI-assisted-by: claude-opus-5-5 --- frontend/src/app/main/data/workspace.cljs | 7 +- .../app/main/data/workspace/wasm_text.cljs | 31 +++--- .../data/workspace_heal_undo_test.cljs | 98 +++++++++++++++++++ frontend/test/frontend_tests/runner.cljs | 2 + 4 files changed, 124 insertions(+), 14 deletions(-) create mode 100644 frontend/test/frontend_tests/data/workspace_heal_undo_test.cljs diff --git a/frontend/src/app/main/data/workspace.cljs b/frontend/src/app/main/data/workspace.cljs index f5f2ce201d..31dc346d79 100644 --- a/frontend/src/app/main/data/workspace.cljs +++ b/frontend/src/app/main/data/workspace.cljs @@ -529,12 +529,15 @@ (rx/of (dwcm/watch-comment-thread-position-changes stoper-s)) ;; Resize auto-grow text shapes whose selrect does not match - ;; the WASM text layout once their fonts finish loading. + ;; the WASM text layout (on page load or once their fonts + ;; finish loading). These automatic heals record no undo + ;; step, so Ctrl+Z reverts the user's own last edit. (->> stream (rx/filter (ptk/type? :app.render-wasm.api/stale-text-selrects)) (rx/map deref) (rx/map (fn [{:keys [ids]}] - (dwwt/resize-wasm-text-all ids {:skip-component-sync? true})))) + (dwwt/resize-wasm-text-all ids {:skip-component-sync? true + :save-undo? false})))) (let [local-commits-s (->> stream diff --git a/frontend/src/app/main/data/workspace/wasm_text.cljs b/frontend/src/app/main/data/workspace/wasm_text.cljs index 821d2c3e8a..76f9551f8a 100644 --- a/frontend/src/app/main/data/workspace/wasm_text.cljs +++ b/frontend/src/app/main/data/workspace/wasm_text.cljs @@ -103,11 +103,16 @@ (wrf/with-pending :text-resize [id] resize-stream)))))) (defn- merge-resize-debounce-opts - [prev {:keys [undo-group undo-id skip-component-sync?]}] - (cond-> (or prev {}) - (some? undo-group) (assoc :undo-group undo-group) - (some? undo-id) (assoc :undo-id undo-id) - skip-component-sync? (assoc :skip-component-sync? true))) + "Merge the options of a request into its batch. The batch records no undo + step only when every request in it asked for that, so a user resize that + lands in the same window keeps its undo entry." + [prev {:keys [undo-group undo-id skip-component-sync? save-undo?]}] + (-> (cond-> (or prev {}) + (some? undo-group) (assoc :undo-group undo-group) + (some? undo-id) (assoc :undo-id undo-id) + skip-component-sync? (assoc :skip-component-sync? true)) + (assoc :save-undo? (or (:save-undo? prev false) + (not (false? save-undo?)))))) (defn resize-wasm-text-debounce-commit [] @@ -115,7 +120,7 @@ ptk/WatchEvent (watch [_ state _] (let [ids (get state ::resize-wasm-text-debounce-ids) - {:keys [undo-group undo-id skip-component-sync?]} (get state ::resize-wasm-text-debounce-opts) + {:keys [undo-group undo-id skip-component-sync? save-undo?]} (get state ::resize-wasm-text-debounce-opts) objects (dsh/lookup-page-objects state) modifiers @@ -138,7 +143,8 @@ apply-opts (cond-> {:ignore-touched true} (some? undo-group) (assoc :undo-group undo-group) extend-tx? (assoc :undo-transation? false) - skip-component-sync? (assoc :skip-component-sync? true))] + skip-component-sync? (assoc :skip-component-sync? true) + (false? save-undo?) (assoc :save-undo? false))] (cond (not (empty? modifiers)) (if extend-tx? @@ -170,8 +176,7 @@ (-> state (update ::resize-wasm-text-debounce-ids (fnil conj []) id) (update ::resize-wasm-text-reflow-tasks (fnil conj []) reflow-task) - (cond-> (seq opts) - (update ::resize-wasm-text-debounce-opts merge-resize-debounce-opts opts)) + (update ::resize-wasm-text-debounce-opts merge-resize-debounce-opts opts) (cond-> (nil? (::resize-wasm-text-debounce-event state)) (assoc ::resize-wasm-text-debounce-event cur-event)))) @@ -205,7 +210,7 @@ (defn resize-wasm-text-debounce ([id] (resize-wasm-text-debounce id nil)) - ([id {:keys [undo-group undo-id skip-component-sync?] :as opts}] + ([id {:keys [undo-group undo-id skip-component-sync? save-undo?] :as opts}] (ptk/reify ::resize-wasm-text-debounce ptk/WatchEvent (watch [_ state _] @@ -223,11 +228,13 @@ resize-wasm-stream (if fonts-ready? - (let [pass-opts (when (or (some? undo-group) (some? undo-id) skip-component-sync?) + (let [pass-opts (when (or (some? undo-group) (some? undo-id) skip-component-sync? + (false? save-undo?)) (cond-> {} (some? undo-group) (assoc :undo-group undo-group) (some? undo-id) (assoc :undo-id undo-id) - skip-component-sync? (assoc :skip-component-sync? true)))] + skip-component-sync? (assoc :skip-component-sync? true) + (false? save-undo?) (assoc :save-undo? false)))] (rx/of (resize-wasm-text-debounce-inner id pass-opts))) ;; Fonts not loaded; retry after 20 msecs diff --git a/frontend/test/frontend_tests/data/workspace_heal_undo_test.cljs b/frontend/test/frontend_tests/data/workspace_heal_undo_test.cljs new file mode 100644 index 0000000000..cf214edd12 --- /dev/null +++ b/frontend/test/frontend_tests/data/workspace_heal_undo_test.cljs @@ -0,0 +1,98 @@ +;; This Source Code Form is subject to the terms of the Mozilla Public +;; License, v. 2.0. If a copy of the MPL was not distributed with this +;; file, You can obtain one at http://mozilla.org/MPL/2.0/. +;; +;; Copyright (c) KALEIDOS SUBSIDIARY SL + +(ns frontend-tests.data.workspace-heal-undo-test + "Automatic text size heals (a text resized to its WASM layout without a + user action) must not record an undo step, so Ctrl+Z reverts the user's + own last edit." + (:require + [app.common.test-helpers.compositions :as ctho] + [app.common.test-helpers.files :as cthf] + [app.common.test-helpers.ids-map :as cthi] + [app.common.test-helpers.shapes :as cths] + [app.main.data.changes :as dch] + [app.main.data.workspace.wasm-text :as dwwt] + [beicon.v2.core :as rx] + [cljs.test :as t :include-macros true] + [frontend-tests.helpers.async :as async] + [frontend-tests.helpers.state :as ths] + [frontend-tests.helpers.wasm :as thw] + [potok.v2.core :as ptk])) + +(t/use-fixtures :each + {:before (fn [] + (cthi/reset-idmap!) + (thw/setup-wasm-mocks!)) + :after thw/teardown-wasm-mocks!}) + +(defn- collect-commits + "Subscribe before emitting; accumulates the commits that touch `id`." + [store id] + (let [acc (atom [])] + (->> (ptk/input-stream store) + (rx/filter dch/commit?) + (rx/map deref) + (rx/filter #(some (comp #{id} :id) (:redo-changes %))) + (rx/subs! #(swap! acc conj %))) + acc)) + +(defn- touches? + [commit id] + (some (comp #{id} :id) (:redo-changes commit))) + +(defn- ^:async batch-commit + "Emit one WASM text resize per entry of `opts-list`, each for its own text, + in the same debounce window. Resolves to the first commit that touches + the first text, with the ids of all the texts." + [& opts-list] + (let [labels (map-indexed (fn [i _] [(keyword (str "frame-" i)) (keyword (str "text-" i))]) + opts-list) + file (reduce (fn [file [frame text]] + (ctho/add-frame-with-text file frame text "hello")) + (cthf/sample-file :file1) + labels) + store (ths/setup-store file) + ids (mapv (fn [[_ text]] (:id (cths/get-shape file text))) labels) + commits (collect-commits store (first ids))] + (doseq [[id opts] (map vector ids opts-list)] + (ptk/emit! store (dwwt/resize-wasm-text-all [id] opts))) + (await (async/wait-for #(seq @commits) "a resize commit for the text" {:max-ticks 200})) + {:commit (first @commits) :ids ids})) + +(def ^:private heal {:skip-component-sync? true :save-undo? false}) + +(def ^:private user {:skip-component-sync? true}) + +(t/deftest ^:async heal-resize-records-no-undo-step + (let [{:keys [commit]} (await (batch-commit heal))] + (t/is (false? (:save-undo? commit)) + "the heal commit is not added to the undo history"))) + +(t/deftest ^:async user-resize-keeps-its-undo-step + (let [{:keys [commit]} (await (batch-commit user))] + (t/is (true? (:save-undo? commit)) + "a resize without the flag records undo as today"))) + +(t/deftest ^:async user-resize-batched-after-a-heal-keeps-its-undo-step + (let [{:keys [commit ids]} (await (batch-commit heal nil))] + (t/is (every? #(touches? commit %) ids) + "both resizes land in one commit") + (t/is (true? (:save-undo? commit)) + "a user resize without options keeps its undo step"))) + +(t/deftest ^:async heal-batched-after-a-user-resize-keeps-the-undo-step + (let [{:keys [commit ids]} (await (batch-commit nil heal))] + (t/is (every? #(touches? commit %) ids) + "both resizes land in one commit") + (t/is (true? (:save-undo? commit)) + "the user's resize keeps its undo step"))) + +(t/deftest ^:async batch-of-heals-records-no-undo-step + (let [{:keys [commit ids]} (await (batch-commit heal heal))] + (t/is (every? #(touches? commit %) ids) + "both heals land in one commit") + (t/is (false? (:save-undo? commit)) + "a batch of heals is not added to the undo history"))) diff --git a/frontend/test/frontend_tests/runner.cljs b/frontend/test/frontend_tests/runner.cljs index 38f8716aa2..9fd10acc04 100644 --- a/frontend/test/frontend_tests/runner.cljs +++ b/frontend/test/frontend_tests/runner.cljs @@ -40,6 +40,7 @@ [frontend-tests.data.workspace-comments-test] [frontend-tests.data.workspace-context-menu-test] [frontend-tests.data.workspace-focus-test] + [frontend-tests.data.workspace-heal-undo-test] [frontend-tests.data.workspace-interactions-test] [frontend-tests.data.workspace-layout-other-page-test] [frontend-tests.data.workspace-libraries-load-test] @@ -253,6 +254,7 @@ 'frontend-tests.data.workspace-comments-test 'frontend-tests.data.workspace-context-menu-test 'frontend-tests.data.workspace-focus-test + 'frontend-tests.data.workspace-heal-undo-test 'frontend-tests.data.workspace-interactions-test 'frontend-tests.data.workspace-layout-other-page-test 'frontend-tests.data.workspace-libraries-load-test