🐛 Limit token propagation to the new instance on swap and instance (#13021)

* 🐛 Limit token propagation to the new instance on swap and instantiate

Switching a variant and adding a component instance re-applied every
active token to every shape of every page. In files with thousands of
token-styled shapes this took over 10 seconds, while only the new
instance needed the file's token values.

propagate-workspace-tokens takes an optional scope (page id and root
ids) that limits the propagation to those shapes and their descendants.
component-swap and instantiate-component pass the new instance's root.
Calls without a scope still cover the whole file.

Closes #13019

AI-assisted-by: claude-opus-5-5

* ✨ Add token tests for instantiate, swap and multi-page propagation

Cover instantiate-component and component-swap with components whose
stored values differ from the resolved tokens, checking that the new
instance gets the file's token values and other shapes are untouched.
Add a second page to the propagation tests to check that unscoped
propagation reaches other pages and scoped propagation does not.

AI-assisted-by: claude-opus-5-5
This commit is contained in:
Alonso Torres 2026-11-13 09:02:44 +01:00 committed by GitHub
parent a39dee6d79
commit 3e48ab96e4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 213 additions and 41 deletions

View File

@ -653,7 +653,8 @@
(merge (meta it))))
(dwu/start-undo-transaction undo-id)
(dch/commit-changes changes)
(dwtp/propagate-workspace-tokens) ;; Make the new instance get the token values from the current file, not from the component's library
;; Make the new instance get the token values from the current file, not from the component's library
(dwtp/propagate-workspace-tokens {:page-id (:id page) :root-ids [(:id new-shape)]})
(ptk/data-event :layout/update {:ids [(:id new-shape)]})
(dws/select-shapes (d/ordered-set (:id new-shape)))
(when start-move?
@ -1159,7 +1160,7 @@
(dch/commit-changes changes)
;; Make the new instance get the token values from the current file,
;; not from the component's library
(dwtp/propagate-workspace-tokens)
(dwtp/propagate-workspace-tokens {:page-id (:id page) :root-ids [(:id new-shape)]})
(when (and (features/active-feature? state "render-wasm/v1")
(seq new-text-ids))
(dwwt/resize-wasm-text-all new-text-ids {:skip-component-sync? true})))

View File

@ -98,8 +98,11 @@
(->> (map (fn [[value attrs]] [attrs {value #{object-id}}]) attrs-values-map)
(into {})))
(defn- collect-shapes-update-info [resolved-tokens objects]
(loop [items (seq objects)
(defn- collect-shapes-update-info
"Collect the token updates of `shapes`, a seq of [id shape] entries of
`objects`."
[resolved-tokens objects shapes]
(loop [items (seq shapes)
frame-ids #{}
text-ids []
tokens {}]
@ -151,27 +154,49 @@
(pcb/with-page page)
(pcb/mod-page {:background color})))))))))
(defn- scoped-shapes
"Entries of `objects` for the shapes in `root-ids` and all their descendants."
[objects root-ids]
(into []
(comp (mapcat #(cfh/get-children-ids-with-self objects %))
(distinct)
(keep (fn [id]
(when-let [shape (get objects id)]
[id shape]))))
root-ids))
(defn propagate-tokens
"Propagate tokens values to all shapes and page backgrounds where they
are applied"
[state resolved-tokens]
are applied. A non-nil `scope` of `:page-id` and `:root-ids` limits it
to those shapes and their descendants in that page."
[state resolved-tokens {scope-page-id :page-id root-ids :root-ids :as scope}]
(let [file-id (get state :current-file-id)
current-page-id (get state :current-page-id)
fdata (dsh/lookup-file-data state file-id)
scoped? (some? scope)
tpoint (ct/tpoint-ms)]
(l/inf :status "START" :hint "propagate-tokens")
(->> (rx/concat
(rx/of current-page-id)
(->> (rx/from (:pages fdata))
(rx/filter (fn [id] (not= id current-page-id)))))
(l/inf :status "START" :hint "propagate-tokens" :scoped scoped?)
(->> (if scoped?
(rx/of scope-page-id)
(rx/concat
(rx/of current-page-id)
(->> (rx/from (:pages fdata))
(rx/filter (fn [id] (not= id current-page-id))))))
(rx/mapcat
(fn [page-id]
(let [page
(dsh/get-page fdata page-id)
objects
(:objects page)
[attrs frame-ids text-ids]
(collect-shapes-update-info resolved-tokens (:objects page))
(collect-shapes-update-info resolved-tokens
objects
(if scoped?
(scoped-shapes objects root-ids)
objects))
actions
(actionize-shapes-update-info page-id attrs)
@ -189,7 +214,7 @@
(when (seq observable) (apply rx/merge observable))
(when (seq normal) (rx/concat-all (rx/of normal)))
(when (:background-token page)
(when (and (not scoped?) (:background-token page))
(rx/of (update-canvas-background page-id resolved-tokens)))
(->> (rx/from frame-ids)
@ -208,32 +233,38 @@
(l/inf :status "END" :hint "propagate-tokens" :elapsed elapsed)))))))
(defn propagate-workspace-tokens
[]
(ptk/reify ::propagate-workspace-tokens
ptk/WatchEvent
(watch [_ state _]
(let [tokens-lib (dsh/lookup-tokens-lib state)
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)
(d/update-vals #(update % :resolved-value ts/tokenscript-symbols->penpot-unit))))
(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))
"Apply the resolved values of the active tokens to the shapes that use
them. Without `scope` it covers every page of the file. A `scope` of
`:page-id` and `:root-ids` limits it to those shapes and their
descendants."
([]
(propagate-workspace-tokens nil))
([scope]
(ptk/reify ::propagate-workspace-tokens
ptk/WatchEvent
(watch [_ state _]
(let [tokens-lib (dsh/lookup-tokens-lib state)
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)
(d/update-vals #(update % :resolved-value ts/tokenscript-symbols->penpot-unit))))
(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)))))))))))))
;; 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))))))))))))))

View File

@ -9,6 +9,7 @@
[app.common.math :as mth]
[app.common.test-helpers.components :as cthc]
[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.common.test-helpers.tokens :as ctht]
@ -36,6 +37,7 @@
(defn- setup-base-file
[]
(-> (ctht/sample-file-with-tokens
:page-label :page1
:lib-fn #(-> %
(ctob/add-set (ctob/make-token-set :id (cthi/new-id! :test-token-set)
:name "test-token-set"))
@ -439,3 +441,141 @@
(tohs/run-store-async
store step2 events identity))))
(defn- setup-file-with-stale-values
"Adds :frame2 (with a :rect1 child) and :frame3 in :page1, and :frame4
in :page2, whose stored radius differs from the resolved token value."
[]
(-> (setup-base-file)
(ctho/add-frame-with-child :frame2 :rect1)
(ctho/add-frame :frame3)
(ctht/apply-token-to-shape :frame2 "test-token-2" [:r1 :r2 :r3 :r4] [:r1 :r2 :r3 :r4] 10)
(ctht/apply-token-to-shape :rect1 "test-token-3" [:r1 :r2 :r3 :r4] [:r1 :r2 :r3 :r4] 10)
(ctht/apply-token-to-shape :frame3 "test-token-2" [:r1 :r2 :r3 :r4] [:r1 :r2 :r3 :r4] 10)
(cthf/add-sample-page :page2)
(ctho/add-frame :frame4)
(ctht/apply-token-to-shape :frame4 "test-token-2" [:r1 :r2 :r3 :r4] [:r1 :r2 :r3 :r4] 10)
(cthf/switch-to-page :page1)))
(t/deftest scoped-propagation-updates-only-the-scoped-tree
(t/async
done
(let [;; ==== Setup
file (setup-file-with-stale-values)
store (ths/setup-store file)
;; ==== Action
events [(dwtp/propagate-workspace-tokens
{:page-id (cthf/current-page-id file)
:root-ids [(cthi/id :frame2)]})]]
(tohs/run-store-async
store done events
(fn [new-state]
(let [;; ==== Get
file' (ths/get-file-from-state new-state)
frame2' (cths/get-shape file' :frame2)
rect1' (cths/get-shape file' :rect1)
frame3' (cths/get-shape file' :frame3)
frame4' (cths/get-shape file' :frame4 :page-label :page2)]
;; ==== Check
(t/is (= (:r1 frame2') 50))
(t/is (= (:r1 rect1') 75))
(t/is (= (:r1 frame3') 10))
(t/is (= (:r1 frame4') 10))))))))
(t/deftest unscoped-propagation-updates-every-shape
(t/async
done
(let [;; ==== Setup
file (setup-file-with-stale-values)
store (ths/setup-store file)
;; ==== Action
events [(dwtp/propagate-workspace-tokens)]]
(tohs/run-store-async
store done events
(fn [new-state]
(let [;; ==== Get
file' (ths/get-file-from-state new-state)
frame2' (cths/get-shape file' :frame2)
rect1' (cths/get-shape file' :rect1)
frame3' (cths/get-shape file' :frame3)
frame4' (cths/get-shape file' :frame4 :page-label :page2)]
;; ==== Check
(t/is (= (:r1 frame2') 50))
(t/is (= (:r1 rect1') 75))
(t/is (= (:r1 frame3') 50))
(t/is (= (:r1 frame4') 50))))))))
(defn- setup-file-with-stale-components
"Adds :component1 (from :frame1) and :component2 (from :frame2), and a
plain :frame3, all of them with a stored radius that differs from the
resolved token value."
[]
(-> (setup-base-file)
(ctht/apply-token-to-shape :frame1 "test-token-1" [:r1 :r2 :r3 :r4] [:r1 :r2 :r3 :r4] 10)
(cthc/make-component :component1 :frame1)
(ctho/add-frame :frame2)
(ctht/apply-token-to-shape :frame2 "test-token-2" [:r1 :r2 :r3 :r4] [:r1 :r2 :r3 :r4] 10)
(cthc/make-component :component2 :frame2)
(ctho/add-frame :frame3)
(ctht/apply-token-to-shape :frame3 "test-token-3" [:r1 :r2 :r3 :r4] [:r1 :r2 :r3 :r4] 10)))
(t/deftest instantiate-component-applies-token-values
(t/async
done
(let [;; ==== Setup
file (setup-file-with-stale-components)
store (ths/setup-store file)
;; ==== Action
events [(dwl/instantiate-component (:id file)
(cthi/id :component1)
(geom/point 0 0))]]
(tohs/run-store-async
store done events
(fn [new-state]
(let [;; ==== Get
copy' (dsh/lookup-shape new-state (first (dsh/lookup-selected new-state)))
file' (ths/get-file-from-state new-state)
frame3' (cths/get-shape file' :frame3)]
;; ==== Check
(t/is (= (get-in copy' [:applied-tokens :r1]) "test-token-1"))
(t/is (= (:r1 copy') 25))
(t/is (= (:r4 copy') 25))
(t/is (= (:r1 frame3') 10))))))))
(t/deftest component-swap-applies-token-values
(t/async
done
(let [;; ==== Setup
file (-> (setup-file-with-stale-components)
(cthc/instantiate-component :component1 :c-frame1))
store (ths/setup-store file)
;; ==== Action
events [(dwl/component-swap (cths/get-shape file :c-frame1)
(:id file)
(cthi/id :component2)
true)]]
(tohs/run-store-async
store done events
(fn [new-state]
(let [;; ==== Get
copy' (dsh/lookup-shape new-state (first (dsh/lookup-selected new-state)))
file' (ths/get-file-from-state new-state)
frame3' (cths/get-shape file' :frame3)]
;; ==== Check
(t/is (= (:component-id copy') (cthi/id :component2)))
(t/is (= (get-in copy' [:applied-tokens :r1]) "test-token-2"))
(t/is (= (:r1 copy') 50))
(t/is (= (:r4 copy') 50))
(t/is (= (:r1 frame3') 10))))))))