From 503330d178e4b2ed23775e09b6f28b5728147db4 Mon Sep 17 00:00:00 2001 From: "alonso.torres" Date: Tue, 27 Oct 2026 10:03:36 +0100 Subject: [PATCH] :bug: Keep delayed font resizes in their undo group Stack a delayed derived change into its applied undo group when that group exists, instead of an unrelated later entry or transaction. Preserve an older entry's selection and exclude the redo tail. The delayed-font regression fails before the fix and passes afterward. Async transaction controls pass 18 assertions; the completion suite passes 17 tests / 646 assertions. Full frontend passes 1,563 tests / 10,860 assertions, including unchanged goldens. Lint and formatting pass. A standalone-text WASM history passes 9 checks, with exact undo/redo, full-sync consistency, the original page restored and no pending work. Benchmark ratios against the unchanged Phase 1e baseline: main 10/100/1,000 copies 0.62/0.59/0.62; three levels 0.68; dependency chain 0.54; library update 0.54; many shapes 0.48; variant switch 0.76. All worlds pass without regression flags. The separate late copy-measurement path remains tracked as F222. AI-assisted-by: gpt-6 --- .../src/app/main/data/workspace/undo.cljs | 54 ++++++++++++------- .../completion_contract_test.cljs | 54 +++++++++++++++++++ .../data/workspace_undo_stack_test.cljs | 34 ++++++++++++ 3 files changed, 122 insertions(+), 20 deletions(-) diff --git a/frontend/src/app/main/data/workspace/undo.cljs b/frontend/src/app/main/data/workspace/undo.cljs index 32e97291a6..037fa6b4ad 100644 --- a/frontend/src/app/main/data/workspace/undo.cljs +++ b/frontend/src/app/main/data/workspace/undo.cljs @@ -137,22 +137,22 @@ state)) (defn- stack-undo-entry - "Extends the current undo entry in the workspace with new changes if it + "Extends the selected undo entry with new changes if it exists, or creates a new entry if it doesn't. When stacking onto an existing entry, the entry's original timestamp is preserved so the history panel keeps showing when the action originated. Issue #7660." - [state {:keys [undo-changes redo-changes undo-group selected-after translation] :as entry}] - (let [index (get-in state [:workspace-undo :index] -1)] - (if (>= index 0) - (update-in state [:workspace-undo :items index] - (fn [item] - (-> item - (update :undo-changes #(into undo-changes %)) - (update :redo-changes #(into % redo-changes)) - (update :undo-groups (fnil conj #{}) undo-group) - (update :translation #(and % translation)) - (assoc :selected-after selected-after)))) - (add-undo-entry state entry)))) + [state {:keys [undo-changes redo-changes undo-group selected-after translation] :as entry} index] + (if (>= index 0) + (update-in state [:workspace-undo :items index] + (fn [item] + (cond-> (-> item + (update :undo-changes #(into undo-changes %)) + (update :redo-changes #(into % redo-changes)) + (update :undo-groups (fnil conj #{}) undo-group) + (update :translation #(and % translation))) + (= index (get-in state [:workspace-undo :index])) + (assoc :selected-after selected-after)))) + (add-undo-entry state entry))) (defn- accumulate-undo-entry "Extends the current undo transaction with new changes." @@ -171,11 +171,9 @@ (assoc-in [:workspace-undo :transaction :tags] tags))) (defn append-undo - "UpdateEvent to add an entry to the undo stack, or extend the current undo transaction - or last undo entry. - - If `stack?` is true, it will stack the entry on top of the current undo entry. - - If `stack?` is false, it will add a new entry to the undo stack. - - If there is an open transaction, it will accumulate the changes in that transaction." + "Add changes to undo history. Stacked changes extend their applied undo + group when present, otherwise the current entry. An open transaction + accumulates changes unless they stack into another applied group." [entry stack?] (assert (check-undo-entry entry)) @@ -187,8 +185,24 @@ (let [selected-after (dm/get-in state [:workspace-local :selected]) entry (assoc entry :selected-after selected-after - :undo-groups #{(:undo-group entry)})] + :undo-groups #{(:undo-group entry)}) + index (get-in state [:workspace-undo :index] -1) + transaction (get-in state [:workspace-undo :transaction]) + in-group? (fn [item group] + (or (= group (:undo-group item)) + (contains? (:undo-groups item) group))) + group-index (when (and stack? (:undo-group entry) + (not (in-group? transaction (:undo-group entry)))) + (last (keep-indexed + (fn [index item] + (when (in-group? item (:undo-group entry)) index)) + (take (inc index) (get-in state [:workspace-undo :items])))))] (cond + ;; A delayed derived change still belongs to its primary action, + ;; even after another edit or transaction has started. + (some? group-index) + (stack-undo-entry state entry group-index) + (and (get-in state [:workspace-undo :transaction]) (or (not stack?) (d/not-empty? (get-in state [:workspace-undo :transaction :undo-changes])) @@ -196,7 +210,7 @@ (accumulate-undo-entry state entry) stack? - (stack-undo-entry state entry) + (stack-undo-entry state entry index) :else (add-undo-entry state entry)))))) diff --git a/frontend/test/frontend_tests/composable_tests/completion_contract_test.cljs b/frontend/test/frontend_tests/composable_tests/completion_contract_test.cljs index 55908fe2d4..da44e8c142 100644 --- a/frontend/test/frontend_tests/composable_tests/completion_contract_test.cljs +++ b/frontend/test/frontend_tests/composable_tests/completion_contract_test.cljs @@ -37,6 +37,7 @@ [app.main.data.workspace.wasm-text :as dwwt] [app.main.store :as st] [app.render-wasm.api :as wasm] + [app.render-wasm.api.fonts :as wasm.fonts] [app.util.text-editor :as ted] [beicon.v2.core :as rx] [cljs.test :as t :include-macros true] @@ -180,6 +181,59 @@ (thw/teardown-wasm-mocks!) (st/emit! #(assoc % :features features)))))) +(t/deftest ^:async a-delayed-font-resize-stays-with-its-font-edit + (let [features (:features @st/state) + ready (atom true) + face {:font-id "gfont-roboto" :weight 400 :style 0 :emoji? false}] + (thw/setup-wasm-mocks!) + (try + (await + (mock/with-mocks* + {wasm/text-editor-has-focus? (mock/stub (constantly false)) + wasm.fonts/get-content-fonts (mock/stub (constantly [face])) + wasm.fonts/make-font-data (mock/stub identity) + wasm.fonts/font-ready? (mock/stub (fn [_] @ready)) + wasm/get-text-dimensions (mock/stub (fn [] {:x 0 :y 0 :width 100 :max-width 100 + :height (if @ready 40 20)})) + dwthw/render-thumbnail (mock/stub (fn [& _] (ptk/data-event ::thumbnail-skipped)))} + (let [file (-> (thf/sample-file :file1) + (ths/add-sample-shape :text :type :text :width 100 :height 20 + :grow-type :auto-height + :content (thtx/content [[["Read the guide"]]])) + (ths/add-sample-shape :rect :type :rect :opacity 1)) + _ (ftm/install! (tm/make-situation file)) + before (ftm/snapshot)] + (st/emit! #(update % :features (fnil conj #{}) "render-wasm/v1")) + (reset! ready false) + (st/emit! (dwt/update-all-attrs [(thi/id :text)] + {:font-id "gfont-roboto" :font-family "Roboto" + :font-variant-id "regular"})) + (await (ha/wait-for #(some (comp seq :font) (vals (wrf/pending))) + "font edit is pending")) + (st/emit! (dwsh/update-shapes [(thi/id :rect)] #(assoc % :opacity 0.4))) + (await (ha/wait-for #(= 0.4 (:opacity (ths/get-shape (ftm/current-file) :rect))) + "unrelated edit commits before the font arrives")) + (let [text (ths/get-shape (ftm/current-file) :text) + faces (map (comp wasm.fonts/font-data-key wasm.fonts/make-font-data) + (wasm.fonts/get-content-fonts (:content text)))] + (t/is (seq faces) "the font wait has a face to release") + (reset! ready true) + (doseq [face faces] (rx/push! wasm.fonts/font-stored-stream face))) + (t/is (= :settled (await (ftm/await-step [])))) + (t/is (mth/close? 40 (:height (ths/get-shape (ftm/current-file) :text)))) + (let [after (ftm/snapshot)] + (t/is (= :settled (await (ftm/await-step [dwu/undo])))) + (t/is (= 1 (:opacity (ths/get-shape (ftm/current-file) :rect)))) + (t/is (mth/close? 40 (:height (ths/get-shape (ftm/current-file) :text))) + "undoing the unrelated edit keeps the font's measured height") + (t/is (= :settled (await (ftm/await-step [dwu/undo])))) + (t/is (uc/same-state? before (ftm/snapshot))) + (t/is (= :settled (await (ftm/await-step [dwu/redo dwu/redo])))) + (t/is (uc/same-state? after (ftm/snapshot))))))) + (finally + (thw/teardown-wasm-mocks!) + (st/emit! #(assoc % :features features)))))) + (t/deftest ^:async hiding-a-grid-main-child-then-moving-its-main-stays-synced (loop [live (ftm/start-generated (cg/world [2 1 {:text true :text-styles true :layout :grid}])) tuples [[:style 3 1 3 0] [:transform 0 0 0 0]]] diff --git a/frontend/test/frontend_tests/data/workspace_undo_stack_test.cljs b/frontend/test/frontend_tests/data/workspace_undo_stack_test.cljs index 9fff8d3b80..108095a776 100644 --- a/frontend/test/frontend_tests/data/workspace_undo_stack_test.cljs +++ b/frontend/test/frontend_tests/data/workspace_undo_stack_test.cljs @@ -13,9 +13,12 @@ [app.common.files.changes-builder :as pcb] [app.common.logic.shapes :as cls] [app.common.test-helpers.composable.comp.setups :as setup] + [app.common.test-helpers.composable.comp.undo-check :as uc] + [app.common.test-helpers.composable.core :as tm] [app.common.test-helpers.files :as thf] [app.common.test-helpers.ids-map :as thi] [app.common.test-helpers.shapes :as ths] + [app.common.uuid :as uuid] [app.main.data.changes :as dch] [app.main.data.workspace.undo :as dwu] [app.main.store :as st] @@ -56,6 +59,37 @@ [] (get-in @st/state [:workspace-undo :index])) +(t/deftest ^:async delayed-derived-changes-do-not-join-an-unrelated-transaction + (let [file (-> (thf/sample-file :file) + (ths/add-sample-shape :first :type :rect :opacity 1) + (ths/add-sample-shape :second :type :rect :opacity 1)) + _ (ftm/install! (tm/make-situation file)) + before (ftm/snapshot) + first-group (uuid/next) + second-group (uuid/next) + transaction (js/Symbol) + edit (fn [label attr value opts] + (let [page (thf/current-page (ftm/current-file)) + changes (cls/generate-update-shapes + (pcb/empty-changes nil (:id page)) [(thi/id label)] + #(assoc % attr value) (:objects page) {})] + (dch/commit-changes (merge changes opts))))] + (t/is (= :settled (await (ftm/await-step [(edit :first :opacity 0.4 {:undo-group first-group})])))) + (t/is (= :settled + (await (ftm/await-step [(dwu/start-undo-transaction transaction) + (edit :second :opacity 0.5 {:undo-group second-group}) + (edit :first :r1 8 {:undo-group first-group :stack-undo? true}) + (dwu/commit-undo-transaction transaction)])))) + (let [after (ftm/snapshot)] + (t/is (= [first-group second-group] (mapv :undo-group (get-in @st/state [:workspace-undo :items])))) + (t/is (= :settled (await (ftm/await-step [dwu/undo])))) + (t/is (= 1 (:opacity (ths/get-shape (ftm/current-file) :second)))) + (t/is (= 8 (:r1 (ths/get-shape (ftm/current-file) :first)))) + (t/is (= :settled (await (ftm/await-step [dwu/undo])))) + (t/is (uc/same-state? before (ftm/snapshot))) + (t/is (= :settled (await (ftm/await-step [dwu/redo dwu/redo])))) + (t/is (uc/same-state? after (ftm/snapshot)))))) + (t/deftest ^:async undo-and-redo-an-entry-without-undo-group (ftm/install! (setup/simple-component-with-labeled-copy)) (let [changes (fill-main-child-changes red)]