0
Fork 0
mirror of https://github.com/penpot/penpot.git synced 2025-01-21 06:02:32 -05:00

Collecting update functions by attributes

This commit is contained in:
Florian Schroedl 2024-07-31 14:36:09 +02:00
parent 6225f59ea0
commit 69d9359237
2 changed files with 73 additions and 0 deletions

View file

@ -111,6 +111,9 @@
(defonce !tokens-cache (atom nil))
(defn get-cached-tokens [tokens]
(get @!tokens-cache tokens tokens))
(defn use-resolved-tokens
"The StyleDictionary process function is async, so we can't use resolved values directly.

View file

@ -0,0 +1,70 @@
(ns app.main.ui.workspace.tokens.update
(:require
[app.common.types.token :as ctt]
[app.main.data.workspace.shape-layout :as dwsl]
[app.main.refs :as refs]
[app.main.ui.workspace.tokens.changes :as wtch]
[app.main.ui.workspace.tokens.style-dictionary :as wtsd]
[clojure.set :as set]))
(defn invert-collect-key-vals
[xs resolved-tokens]
(-> (reduce
(fn [acc [k v]]
(let [resolved-value (get-in resolved-tokens [v :resolved-value])]
(update acc resolved-value (fnil conj #{}) k)))
{} xs)))
(defn shape-ids-by-values
[attrs-values-map object-id]
(->> (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 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]}]]
(if (seq applied-tokens)
(let [applied-tokens (-> (invert-collect-key-vals applied-tokens resolved-tokens)
(shape-ids-by-values object-id))]
(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})
(merge {} (into {} [[:a 1]]))
(def attributes-collect-by-pairs
(reduce
(fn [acc [ks _]]
(into acc (map (fn [k] [k ks]) ks)))
{} attributes->shape-update))
(comment
(update-workspace-tokens)
nil)