diff --git a/frontend/src/app/main/data/workspace/tokens/propagation.cljs b/frontend/src/app/main/data/workspace/tokens/propagation.cljs index 2027e5be42..c979b108e6 100644 --- a/frontend/src/app/main/data/workspace/tokens/propagation.cljs +++ b/frontend/src/app/main/data/workspace/tokens/propagation.cljs @@ -215,23 +215,28 @@ tokens-status (dsh/lookup-tokens-status state)] (when (and tokens-lib tokens-status) (when-let [tokens-tree (cfo/get-tokens-in-active-sets tokens-status tokens-lib)] - (->> (if (contains? cf/flags :tokenscript) - (rx/of (ts/resolve-tokens tokens-tree)) - (sd/resolve-tokens tokens-tree)) - (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) - (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-tokens tokens-tree)) + (sd/resolve-tokens tokens-tree)) + (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) + (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 66b0bf93b7..fddcc0b6a7 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] @@ -203,15 +204,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 @@ -262,8 +267,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) @@ -272,6 +280,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))]