From c60c5ac34fb971bfb8deffb29b17349cfec0b9b1 Mon Sep 17 00:00:00 2001 From: Florian Schroedl Date: Thu, 16 May 2024 09:33:20 +0200 Subject: [PATCH] Apply tokens directly to shape --- frontend/src/app/main/data/tokens.cljs | 12 ++++++++---- .../workspace/sidebar/options/menus/measures.cljs | 14 ++++++++------ .../src/app/main/ui/workspace/tokens/core.cljs | 3 +++ 3 files changed, 19 insertions(+), 10 deletions(-) diff --git a/frontend/src/app/main/data/tokens.cljs b/frontend/src/app/main/data/tokens.cljs index f04dd7ad7..b960a4369 100644 --- a/frontend/src/app/main/data/tokens.cljs +++ b/frontend/src/app/main/data/tokens.cljs @@ -55,19 +55,23 @@ (->> (map (fn [attr] [attr token-id]) attributes) (into {}))) -(defn apply-token-id [{:keys [shape token-id attributes]}] +(defn apply-token-id-to-attributes [{:keys [shape token-id attributes]}] (let [token (token-from-attributes token-id attributes)] (toggle-or-apply-token shape token))) +(defn apply-token-to-shape [{:keys [shape _token-id _attributes] :as props}] + (let [applied-tokens (apply-token-id-to-attributes props)] + (update shape :applied-tokens #(merge % applied-tokens)))) + (defn update-token-from-attributes [{:keys [token-id shape-id attributes]}] (ptk/reify ::update-token-from-attributes ptk/WatchEvent (watch [_ state _] (let [shape (get-shape-from-state shape-id state) - applied-tokens (apply-token-id {:shape shape - :token-id token-id - :attributes attributes})] + applied-tokens (apply-token-id-to-attributes {:shape shape + :token-id token-id + :attributes attributes})] (rx/of (update-shape shape-id {:applied-tokens applied-tokens})))))) (defn add-token diff --git a/frontend/src/app/main/ui/workspace/sidebar/options/menus/measures.cljs b/frontend/src/app/main/ui/workspace/sidebar/options/menus/measures.cljs index f300dab8b..fd942ffe6 100644 --- a/frontend/src/app/main/ui/workspace/sidebar/options/menus/measures.cljs +++ b/frontend/src/app/main/ui/workspace/sidebar/options/menus/measures.cljs @@ -262,7 +262,7 @@ (update-fn shape) shape)) {:reg-objects? true - :attrs [:rx :ry :r1 :r2 :r3 :r4]}))) + :attrs [:rx :ry :r1 :r2 :r3 :r4 :applied-tokens]}))) on-switch-to-radius-1 (mf/use-fn @@ -293,12 +293,14 @@ (fn [value] (let [token (when (symbol? value) (get border-radius-tokens (str value))) - token-value (wtc/resolve-token-value token)] + token-value (some-> token wtc/resolve-token-value)] (st/emit! - (dt/update-token-from-attributes {:token-id (:id token) - :shape-id (first ids) - :attributes (get-in wtc/token-types [:border-radius :attributes])}) - (change-radius #(ctsr/set-radius-1 % (or token-value value))))))) + (change-radius (fn [shape] + (cond-> shape + token-value (#(dt/apply-token-to-shape {:token-id (:id token) + :shape % + :attributes (wtc/token-attributes :border-radius)})) + :always (ctsr/set-radius-1 (or token-value value))))))))) on-radius-multi-change (mf/use-fn diff --git a/frontend/src/app/main/ui/workspace/tokens/core.cljs b/frontend/src/app/main/ui/workspace/tokens/core.cljs index c237cae76..d502ae4aa 100644 --- a/frontend/src/app/main/ui/workspace/tokens/core.cljs +++ b/frontend/src/app/main/ui/workspace/tokens/core.cljs @@ -188,3 +188,6 @@ {:label "Paragraph Indent" :key :paragraph-indent} {:label "Text Decoration" :key :text-decoration} {:label "Text Case" :key :text-case}]}}])) + +(defn token-attributes [token-type] + (get-in token-types [token-type :attributes]))