0
Fork 0
mirror of https://github.com/penpot/penpot.git synced 2025-01-21 14:12:36 -05:00

Working updates!

This commit is contained in:
Florian Schroedl 2024-07-31 17:26:50 +02:00
parent d22234fe2a
commit a1fefe66ae

View file

@ -1,23 +1,28 @@
(ns app.main.ui.workspace.tokens.update (ns app.main.ui.workspace.tokens.update
(:require (:require
[beicon.v2.core :as brx]
[app.common.types.token :as ctt] [app.common.types.token :as ctt]
[app.main.data.workspace.shape-layout :as dwsl] [app.main.data.workspace.shape-layout :as dwsl]
[app.main.refs :as refs] [app.main.refs :as refs]
[app.main.store :as st]
[app.main.ui.workspace.tokens.changes :as wtch] [app.main.ui.workspace.tokens.changes :as wtch]
[app.main.ui.workspace.tokens.style-dictionary :as wtsd] [app.main.ui.workspace.tokens.style-dictionary :as wtsd]
[clojure.data :as data]
[clojure.set :as set] [clojure.set :as set]
[clojure.data :as data])) [potok.v2.core :as ptk]
[beicon.v2.core :as rx]))
;; Constants ------------------------------------------------------------------- ;; Constants -------------------------------------------------------------------
(def filter-existing-values? false) (def filter-existing-values? false)
(def attributes->shape-update (def attributes->shape-update
{#{:rx :ry} wtch/update-shape-radius-single-corner {#{:rx :ry} (fn [v ids _] (wtch/update-shape-radius-all v ids))
#{:r1 :r2 :r3 :r4} 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/stroke-width-keys wtch/update-stroke-width
ctt/sizing-keys wtch/update-shape-dimensions ctt/sizing-keys wtch/update-shape-dimensions
ctt/opacity-keys wtch/update-opacity ctt/opacity-keys wtch/update-opacity
#{:x :y} wtch/update-shape-position
#{:p1 :p2 :p3 :p4} (fn [resolved-value shape-ids attrs] #{:p1 :p2 :p3 :p4} (fn [resolved-value shape-ids attrs]
(dwsl/update-layout shape-ids {:layout-padding (zipmap attrs (repeat resolved-value))})) (dwsl/update-layout shape-ids {:layout-padding (zipmap attrs (repeat resolved-value))}))
#{:column-gap :row-gap} wtch/update-layout-spacing #{:column-gap :row-gap} wtch/update-layout-spacing
@ -25,6 +30,12 @@
#{:layout-item-min-w :layout-item-min-h :layout-item-max-w :layout-item-max-h} wtch/update-layout-sizing-limits #{: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}) ctt/rotation-keys wtch/update-rotation})
(def attribute-actions-map
(reduce
(fn [acc [ks action]]
(into acc (map (fn [k] [k action]) ks)))
{} attributes->shape-update))
;; Helpers --------------------------------------------------------------------- ;; Helpers ---------------------------------------------------------------------
(defn deep-merge (defn deep-merge
@ -76,24 +87,42 @@
(->> (map (fn [[value attrs]] [attrs {value #{object-id}}]) attrs-values-map) (->> (map (fn [[value attrs]] [attrs {value #{object-id}}]) attrs-values-map)
(into {}))) (into {})))
(defn collect-shape-update-info [resolved-tokens shapes] (defn collect-shapes-update-info [resolved-tokens shapes]
(reduce (reduce
(fn [acc [object-id {:keys [applied-tokens] :as shape}]] (fn [acc [object-id {:keys [applied-tokens] :as shape}]]
(if (seq applied-tokens) (if (seq applied-tokens)
(let [applied-tokens (-> (let [applied-tokens (-> (invert-collect-key-vals applied-tokens resolved-tokens shape)
(invert-collect-key-vals applied-tokens resolved-tokens shape) (shape-ids-by-values object-id)
(shape-ids-by-values object-id) (split-attribute-groups))]
(split-attribute-groups))]
(deep-merge acc applied-tokens)) (deep-merge acc applied-tokens))
acc)) acc))
{} shapes)) {} shapes))
(defn actionize-shapes-update-info [shapes-update-info]
(mapcat (fn [[attrs update-infos]]
(let [action (some attribute-actions-map attrs)]
(map
(fn [[v shape-ids]]
(list action v shape-ids attrs))
update-infos)))
shapes-update-info))
(defn update-workspace-tokens [] (defn update-workspace-tokens []
(let [resolved-tokens (wtsd/get-cached-tokens @refs/workspace-tokens)] (let [resolved-tokens (wtsd/get-cached-tokens @refs/workspace-tokens)]
(->> @refs/workspace-page-objects (->> @refs/workspace-page-objects
(collect-shape-update-info resolved-tokens)))) (collect-shapes-update-info resolved-tokens)
(actionize-shapes-update-info))))
(defn update-workspace-tokens-event []
(ptk/reify ::update-shape-position
ptk/WatchEvent
(watch [_ _ _]
(->> (update-workspace-tokens)
(mapv (fn [[f & attrs]]
(apply f attrs)))
(brx/concat)))))
(comment (comment
(update-workspace-tokens) (update-workspace-tokens)
(st/emit! (update-workspace-tokens-event))
nil) nil)