diff --git a/frontend/src/app/main/ui/workspace/tokens/update.cljs b/frontend/src/app/main/ui/workspace/tokens/update.cljs index 99c515699..d23ce86cc 100644 --- a/frontend/src/app/main/ui/workspace/tokens/update.cljs +++ b/frontend/src/app/main/ui/workspace/tokens/update.cljs @@ -8,8 +8,37 @@ [clojure.set :as set] [clojure.data :as data])) +;; Constants ------------------------------------------------------------------- + (def filter-existing-values? false) +(def attributes->shape-update + {#{:rx :ry} wtch/update-shape-radius-single-corner + #{:r1 :r2 :r3 :r4} wtch/update-shape-radius-single-corner + ctt/stroke-width-keys wtch/update-stroke-width + ctt/sizing-keys wtch/update-shape-dimensions + ctt/opacity-keys wtch/update-opacity + #{:p1 :p2 :p3 :p4} (fn [resolved-value shape-ids attrs] + (dwsl/update-layout shape-ids {:layout-padding (zipmap attrs (repeat resolved-value))})) + #{:column-gap :row-gap} wtch/update-layout-spacing + #{:width :height} wtch/update-shape-dimensions + #{:layout-item-min-w :layout-item-min-h :layout-item-max-w :layout-item-max-h} wtch/update-layout-sizing-limits + ctt/rotation-keys wtch/update-rotation}) + +;; Helpers --------------------------------------------------------------------- + +(defn deep-merge + "Like d/deep-merge but unions set values." + ([a b] + (cond + (map? a) (merge-with deep-merge a b) + (set? a) (set/union a b) + :else b)) + ([a b & rest] + (reduce deep-merge a (cons b rest)))) + +;; Data flows ------------------------------------------------------------------ + (defn invert-collect-key-vals [xs resolved-tokens shape] (-> (reduce @@ -47,51 +76,24 @@ (->> (map (fn [[value attrs]] [attrs {value #{object-id}}]) attrs-values-map) (into {}))) -(defn deep-merge - "Like d/deep-merge but unions set values." - ([a b] - (cond - (map? a) (merge-with deep-merge a b) - (set? a) (set/union a b) - :else b)) - ([a b & rest] - (reduce deep-merge a (cons b rest)))) +(defn collect-shape-update-info [resolved-tokens shapes] + (reduce + (fn [acc [object-id {:keys [applied-tokens] :as shape}]] + (if (seq applied-tokens) + (let [applied-tokens (-> + (invert-collect-key-vals applied-tokens resolved-tokens shape) + (shape-ids-by-values object-id) + (split-attribute-groups))] + (deep-merge acc applied-tokens)) + acc)) + {} shapes)) + (defn update-workspace-tokens [] (let [resolved-tokens (wtsd/get-cached-tokens @refs/workspace-tokens)] (->> @refs/workspace-page-objects - (reduce - (fn [acc [object-id {:keys [applied-tokens] :as shape}]] - (if (seq applied-tokens) - (let [applied-tokens (-> - (invert-collect-key-vals applied-tokens resolved-tokens shape) - (shape-ids-by-values object-id) - (split-attribute-groups))] - (deep-merge acc applied-tokens)) - acc)) - {})))) - -(def attributes->shape-update - {#{:rx :ry} wtch/update-shape-radius-single-corner - #{:r1 :r2 :r3 :r4} wtch/update-shape-radius-single-corner - ctt/stroke-width-keys wtch/update-stroke-width - ctt/sizing-keys wtch/update-shape-dimensions - ctt/opacity-keys wtch/update-opacity - #{:p1 :p2 :p3 :p4} (fn [resolved-value shape-ids attrs] - (dwsl/update-layout shape-ids {:layout-padding (zipmap attrs (repeat resolved-value))})) - #{:column-gap :row-gap} wtch/update-layout-spacing - #{:width :height} wtch/update-shape-dimensions - #{:layout-item-min-w :layout-item-min-h :layout-item-max-w :layout-item-max-h} wtch/update-layout-sizing-limits - ctt/rotation-keys wtch/update-rotation}) - - -(def attributes-collect-by-pairs - (reduce - (fn [acc [ks _]] - (into acc (map (fn [k] [k ks]) ks))) - {} attributes->shape-update)) + (collect-shape-update-info resolved-tokens)))) (comment (update-workspace-tokens) - nil)