From d0b9af53a6f22634d514519e0e3333797b3e517b Mon Sep 17 00:00:00 2001 From: "alonso.torres" Date: Sun, 15 Nov 2026 00:23:27 +0100 Subject: [PATCH] :bug: Undo a token edit and its propagation in one step Saving a token from its form committed the edit, then propagated the new value to the shapes in an undo transaction of its own, opened only once the tokens resolved. One undo reverted the shapes and left the token with its new value. The propagation opens its transaction before the tokens resolve, and the form commits the edit and the propagation in one transaction, which the propagation's nests in. The frontend replay commits a token edit as the form does. The benchmark covers JVM code; this change is ClojureScript only, so it is not run. AI-assisted-by: claude-opus-5-5 --- .../data/workspace/tokens/propagation.cljs | 45 ++++++++++--------- .../tokens/management/forms/generic_form.cljs | 29 +++++++----- .../composable_tests/generated_test.cljs | 6 +++ .../composable_tests/interpreter.cljs | 16 ++++--- 4 files changed, 60 insertions(+), 36 deletions(-) diff --git a/frontend/src/app/main/data/workspace/tokens/propagation.cljs b/frontend/src/app/main/data/workspace/tokens/propagation.cljs index e41201905d..3472b6ee2c 100644 --- a/frontend/src/app/main/data/workspace/tokens/propagation.cljs +++ b/frontend/src/app/main/data/workspace/tokens/propagation.cljs @@ -245,23 +245,28 @@ (let [tokens-lib (dsh/lookup-tokens-lib state) tokens-status (dsh/lookup-tokens-status state)] (when (and tokens-lib tokens-status) - (->> (if (contains? cf/flags :tokenscript) - (rx/of (ts/resolve-sets (cfo/get-active-token-sets tokens-status tokens-lib))) - (sd/resolve-tokens (cfo/get-tokens-in-active-sets tokens-status tokens-lib))) - (rx/mapcat - (fn [sd-tokens] - (let [undo-id (js/Symbol)] - (rx/concat - (rx/of (dwu/start-undo-transaction undo-id :timeout false)) - - ;; FIXME: now the tokens propagations is done by accumulating the update-shapes - ;; into a single commit-changes. This is not really the best way, the token application - ;; should be done with a changes_builder and sending only one `commit-changes` instead - ;; of creating lots of `update-shapes`. - (rx/of (dwsh/update-shapes-buffer-start)) - (->> (propagate-tokens state sd-tokens scope) - (rx/catch #(rx/concat - (rx/of (dwsh/update-shapes-buffer-stop)) - (rx/throw %)))) - (rx/of (dwsh/update-shapes-buffer-stop)) - (rx/of (dwu/commit-undo-transaction undo-id))))))))))))) + ;; the transaction opens before the tokens resolve, so a transaction + ;; the caller opened around this event holds the propagation too + (let [undo-id (js/Symbol)] + (rx/concat + (rx/of (dwu/start-undo-transaction undo-id :timeout false)) + (->> (if (contains? cf/flags :tokenscript) + (rx/of (ts/resolve-sets (cfo/get-active-token-sets tokens-status tokens-lib))) + (sd/resolve-tokens (cfo/get-tokens-in-active-sets tokens-status tokens-lib))) + (rx/mapcat + (fn [sd-tokens] + (rx/concat + ;; FIXME: now the tokens propagations is done by accumulating the update-shapes + ;; into a single commit-changes. This is not really the best way, the token application + ;; should be done with a changes_builder and sending only one `commit-changes` instead + ;; of creating lots of `update-shapes`. + (rx/of (dwsh/update-shapes-buffer-start)) + (->> (propagate-tokens state sd-tokens scope) + (rx/catch #(rx/concat + (rx/of (dwsh/update-shapes-buffer-stop)) + (rx/throw %)))) + (rx/of (dwsh/update-shapes-buffer-stop))))) + (rx/catch #(rx/concat + (rx/of (dwu/commit-undo-transaction undo-id)) + (rx/throw %)))) + (rx/of (dwu/commit-undo-transaction undo-id)))))))))) diff --git a/frontend/src/app/main/ui/workspace/tokens/management/forms/generic_form.cljs b/frontend/src/app/main/ui/workspace/tokens/management/forms/generic_form.cljs index 90ca16d9ad..eee48d2930 100644 --- a/frontend/src/app/main/ui/workspace/tokens/management/forms/generic_form.cljs +++ b/frontend/src/app/main/ui/workspace/tokens/management/forms/generic_form.cljs @@ -21,6 +21,7 @@ [app.main.data.workspace.tokens.library-edit :as dwtl] [app.main.data.workspace.tokens.propagation :as dwtp] [app.main.data.workspace.tokens.remapping :as remap] + [app.main.data.workspace.undo :as dwu] [app.main.refs :as refs] [app.main.store :as st] [app.main.ui.context :as muc] @@ -209,15 +210,19 @@ (mf/use-fn (mf/deps token token-type) (fn [valid-token new-name old-name description] - (st/emit! - (dwtl/toggle-nested-token-path token-type new-name) - (dwtl/update-token (:id token) - {:name new-name - :value (:value valid-token) - :description description}) - (remap/remap-tokens old-name new-name) - (dwtp/propagate-workspace-tokens) - (modal/hide!)))) + ;; the edit and its propagation are one undo step + (let [undo-id (js/Symbol)] + (st/emit! + (dwu/start-undo-transaction undo-id) + (dwtl/toggle-nested-token-path token-type new-name) + (dwtl/update-token (:id token) + {:name new-name + :value (:value valid-token) + :description description}) + (remap/remap-tokens old-name new-name) + (dwtp/propagate-workspace-tokens) + (dwu/commit-undo-transaction undo-id) + (modal/hide!))))) on-rename-token (mf/use-fn @@ -268,8 +273,11 @@ (ctob/make-token {:name name :type token-type :value (:value valid-token) - :description description}))] + :description description})) + ;; the edit and its propagation are one undo step + undo-id (js/Symbol)] (st/emit! + (dwu/start-undo-transaction undo-id) (if is-create (dwtl/create-token new-token) (dwtl/update-token (:id token) @@ -278,6 +286,7 @@ :description description})) (dwtl/open-token-type (:type token)) (dwtp/propagate-workspace-tokens) + (dwu/commit-undo-transaction undo-id) (when is-create (scroll-token-type-section-on-create (:id new-token))) (modal/hide!))))))) diff --git a/frontend/test/frontend_tests/composable_tests/generated_test.cljs b/frontend/test/frontend_tests/composable_tests/generated_test.cljs index e5d25cc7c5..408347ba00 100644 --- a/frontend/test/frontend_tests/composable_tests/generated_test.cljs +++ b/frontend/test/frontend_tests/composable_tests/generated_test.cljs @@ -1116,3 +1116,9 @@ :layout-copies true :layout-children true}] :ops [[:rotate 16 0 7 0] [:grid-direction 0 0 0 1] [:grid-cell 0 10 0 1] [:add-child 0] [:structure 3 6 15] [:grid-direction 0 0 0 1] [:grid-cell 0 4 2 1]]}))) + +(t/deftest ^:async generated-a-token-value-edit-and-its-propagation-undo-together + ;; the token form commits the edit and its propagation as one undo step + (await (replay {:world [1 0 {:tokens true :variants true :rich-variants false :text false + :layout :flex :layout-copies false}] + :ops [[:transform 0 10 0 8] [:apply-token 12 11] [:style 12 3 0 1] [:token-value 0 2]]}))) diff --git a/frontend/test/frontend_tests/composable_tests/interpreter.cljs b/frontend/test/frontend_tests/composable_tests/interpreter.cljs index ef114ce0f6..173e57bf4e 100644 --- a/frontend/test/frontend_tests/composable_tests/interpreter.cljs +++ b/frontend/test/frontend_tests/composable_tests/interpreter.cljs @@ -294,13 +294,17 @@ :on-update-shape dwta/update-shape-radius})]) (instance? n/EditTokenValue op) - ;; as the token form commits an edit: the token, then its propagation - (let [data (:data (current-file)) - lib (cfo/get-tokens-lib data) + ;; as the token form commits an edit: the token, then its propagation, + ;; in one undo step + (let [data (:data (current-file)) + lib (cfo/get-tokens-lib data) token-set (ctob/get-set-by-name lib "composable") - token (ctob/get-token-by-name lib "composable" (:token-name op))] - [(dwtl/update-token (ctob/get-id token-set) (:id token) {:value (:value op)}) - (dwtp/propagate-workspace-tokens)]) + token (ctob/get-token-by-name lib "composable" (:token-name op)) + undo-id (js/Symbol)] + [(dwu/start-undo-transaction undo-id) + (dwtl/update-token (ctob/get-id token-set) (:id token) {:value (:value op)}) + (dwtp/propagate-workspace-tokens) + (dwu/commit-undo-transaction undo-id)]) (instance? n/UnapplyToken op) [(dwta/unapply-token {:shape-ids [(tm/target-shape-id situation (:target op))]