🐛 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
This commit is contained in:
alonso.torres 2026-11-15 00:23:27 +01:00
parent 8c57e7b3a9
commit 06f5abdce3
4 changed files with 60 additions and 36 deletions

View File

@ -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))))))))))

View File

@ -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!)))))))

View File

@ -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]]})))

View File

@ -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))]