0
Fork 0
mirror of https://github.com/penpot/penpot.git synced 2025-01-21 06:02:32 -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
(:require
[beicon.v2.core :as brx]
[app.common.types.token :as ctt]
[app.main.data.workspace.shape-layout :as dwsl]
[app.main.refs :as refs]
[app.main.store :as st]
[app.main.ui.workspace.tokens.changes :as wtch]
[app.main.ui.workspace.tokens.style-dictionary :as wtsd]
[clojure.data :as data]
[clojure.set :as set]
[clojure.data :as data]))
[potok.v2.core :as ptk]
[beicon.v2.core :as rx]))
;; Constants -------------------------------------------------------------------
(def filter-existing-values? false)
(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
ctt/stroke-width-keys wtch/update-stroke-width
ctt/sizing-keys wtch/update-shape-dimensions
ctt/opacity-keys wtch/update-opacity
#{:x :y} wtch/update-shape-position
#{: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
@ -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
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 ---------------------------------------------------------------------
(defn deep-merge
@ -76,24 +87,42 @@
(->> (map (fn [[value attrs]] [attrs {value #{object-id}}]) attrs-values-map)
(into {})))
(defn collect-shape-update-info [resolved-tokens shapes]
(defn collect-shapes-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))]
(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 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 []
(let [resolved-tokens (wtsd/get-cached-tokens @refs/workspace-tokens)]
(->> @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
(update-workspace-tokens)
(st/emit! (update-workspace-tokens-event))
nil)