mirror of
https://github.com/penpot/penpot.git
synced 2025-01-08 16:00:19 -05:00
🔧 Small refactor of sync helper
This commit is contained in:
parent
049f4ce784
commit
7581230b6e
2 changed files with 66 additions and 69 deletions
|
@ -70,6 +70,3 @@
|
||||||
:internal.color/opacity
|
:internal.color/opacity
|
||||||
:internal.color/gradient]))
|
:internal.color/gradient]))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -35,7 +35,8 @@
|
||||||
|
|
||||||
(declare generate-sync-container)
|
(declare generate-sync-container)
|
||||||
(declare generate-sync-shape)
|
(declare generate-sync-shape)
|
||||||
(declare has-asset-reference-fn)
|
(declare generate-sync-text-shape)
|
||||||
|
(declare uses-assets?)
|
||||||
|
|
||||||
(declare get-assets)
|
(declare get-assets)
|
||||||
(declare generate-sync-shape-direct)
|
(declare generate-sync-shape-direct)
|
||||||
|
@ -60,7 +61,7 @@
|
||||||
"<local>"
|
"<local>"
|
||||||
(str "<" (get-in state [:workspace-libraries file-id :name]) ">")))
|
(str "<" (get-in state [:workspace-libraries file-id :name]) ">")))
|
||||||
|
|
||||||
;; ---- Create a new component ----
|
;; ---- Components and instances creation ----
|
||||||
|
|
||||||
(defn make-component-shape
|
(defn make-component-shape
|
||||||
"Clone the shape and all children. Generate new ids and detach
|
"Clone the shape and all children. Generate new ids and detach
|
||||||
|
@ -278,9 +279,8 @@
|
||||||
(log/debug :msg "Sync page in local file" :page-id (:id container))
|
(log/debug :msg "Sync page in local file" :page-id (:id container))
|
||||||
(log/debug :msg "Sync component in local library" :component-id (:id container)))
|
(log/debug :msg "Sync component in local library" :component-id (:id container)))
|
||||||
|
|
||||||
(let [has-asset-reference? (has-asset-reference-fn asset-type library-id (cph/page? container))
|
(let [linked-shapes (->> (vals (:objects container))
|
||||||
linked-shapes (->> (vals (:objects container))
|
(filter #(uses-assets? asset-type % library-id (cph/page? container))))]
|
||||||
(filter has-asset-reference?))]
|
|
||||||
(loop [shapes (seq linked-shapes)
|
(loop [shapes (seq linked-shapes)
|
||||||
changes (-> (pcb/empty-changes it)
|
changes (-> (pcb/empty-changes it)
|
||||||
(pcb/with-container container)
|
(pcb/with-container container)
|
||||||
|
@ -295,49 +295,49 @@
|
||||||
shape))
|
shape))
|
||||||
changes))))
|
changes))))
|
||||||
|
|
||||||
(defn- has-asset-reference-fn
|
(defmulti uses-assets?
|
||||||
"Gets a function that checks if a shape uses some asset of the given type
|
"Checks if a shape uses some asset of the given type in the given library."
|
||||||
in the given library."
|
(fn [asset-type shape library-id page?] asset-type))
|
||||||
[asset-type library-id page?]
|
|
||||||
(case asset-type
|
|
||||||
:components
|
|
||||||
(fn [shape] (and (:component-id shape)
|
|
||||||
(or (:component-root? shape) (not page?))
|
|
||||||
(= (:component-file shape) library-id)))
|
|
||||||
|
|
||||||
:colors
|
(defmethod uses-assets? :components
|
||||||
(fn [shape]
|
[_ shape library-id page?]
|
||||||
(if (= (:type shape) :text)
|
(and (some? (:component-id shape))
|
||||||
(->> shape
|
(= (:component-file shape) library-id)
|
||||||
:content
|
(or (:component-root? shape) (not page?)))) ; avoid nested components inside pages
|
||||||
;; Check if any node in the content has a reference for the library
|
|
||||||
(txt/node-seq
|
|
||||||
#(or (and (some? (:stroke-color-ref-id %))
|
|
||||||
(= library-id (:stroke-color-ref-file %)))
|
|
||||||
(and (some? (:fill-color-ref-id %))
|
|
||||||
(= library-id (:fill-color-ref-file %))))))
|
|
||||||
(some
|
|
||||||
#(let [attr (name %)
|
|
||||||
attr-ref-id (keyword (str attr "-ref-id"))
|
|
||||||
attr-ref-file (keyword (str attr "-ref-file"))]
|
|
||||||
(and (get shape attr-ref-id)
|
|
||||||
(= library-id (get shape attr-ref-file))))
|
|
||||||
(map #(nth % 3) color-sync-attrs))))
|
|
||||||
|
|
||||||
:typographies
|
(defmethod uses-assets? :colors
|
||||||
(fn [shape]
|
[_ shape library-id _]
|
||||||
(and (= (:type shape) :text)
|
(if (= (:type shape) :text)
|
||||||
(->> shape
|
(->> shape
|
||||||
:content
|
:content
|
||||||
;; Check if any node in the content has a reference for the library
|
;; Check if any node in the content has a reference for the library
|
||||||
(txt/node-seq
|
(txt/node-seq
|
||||||
#(and (some? (:typography-ref-id %))
|
#(or (and (some? (:stroke-color-ref-id %))
|
||||||
(= library-id (:typography-ref-file %)))))))))
|
(= (:stroke-color-ref-file %) library-id))
|
||||||
|
(and (some? (:fill-color-ref-id %))
|
||||||
|
(= (:fill-color-ref-file %) library-id)))))
|
||||||
|
(some
|
||||||
|
#(let [attr (name %)
|
||||||
|
attr-ref-id (keyword (str attr "-ref-id"))
|
||||||
|
attr-ref-file (keyword (str attr "-ref-file"))]
|
||||||
|
(and (get shape attr-ref-id)
|
||||||
|
(= library-id (get shape attr-ref-file))))
|
||||||
|
(map #(nth % 3) color-sync-attrs))))
|
||||||
|
|
||||||
|
(defmethod uses-assets? :typographies
|
||||||
|
[_ shape library-id _]
|
||||||
|
(and (= (:type shape) :text)
|
||||||
|
(->> shape
|
||||||
|
:content
|
||||||
|
;; Check if any node in the content has a reference for the library
|
||||||
|
(txt/node-seq
|
||||||
|
#(and (some? (:typography-ref-id %))
|
||||||
|
(= (:typography-ref-file %) library-id))))))
|
||||||
|
|
||||||
(defmulti generate-sync-shape
|
(defmulti generate-sync-shape
|
||||||
"Generate changes to synchronize one shape with all assets of the given type
|
"Generate changes to synchronize one shape from all assets of the given type
|
||||||
that is using, in the given library."
|
that is using, in the given library."
|
||||||
(fn [type _changes _library-id _state _container _shape] type))
|
(fn [asset-type _changes _library-id _state _container _shape] asset-type))
|
||||||
|
|
||||||
(defmethod generate-sync-shape :components
|
(defmethod generate-sync-shape :components
|
||||||
[_ changes _library-id state container shape]
|
[_ changes _library-id state container shape]
|
||||||
|
@ -345,29 +345,6 @@
|
||||||
libraries (wsh/get-libraries state)]
|
libraries (wsh/get-libraries state)]
|
||||||
(generate-sync-shape-direct changes libraries container shape-id false)))
|
(generate-sync-shape-direct changes libraries container shape-id false)))
|
||||||
|
|
||||||
(defn- generate-sync-text-shape
|
|
||||||
[changes shape container update-node]
|
|
||||||
(let [old-content (:content shape)
|
|
||||||
new-content (txt/transform-nodes update-node old-content)
|
|
||||||
changes' (-> changes
|
|
||||||
(update :redo-changes conj (make-change
|
|
||||||
container
|
|
||||||
{:type :mod-obj
|
|
||||||
:id (:id shape)
|
|
||||||
:operations [{:type :set
|
|
||||||
:attr :content
|
|
||||||
:val new-content}]}))
|
|
||||||
(update :undo-changes d/preconj (make-change
|
|
||||||
container
|
|
||||||
{:type :mod-obj
|
|
||||||
:id (:id shape)
|
|
||||||
:operations [{:type :set
|
|
||||||
:attr :content
|
|
||||||
:val old-content}]})))]
|
|
||||||
(if (= new-content old-content)
|
|
||||||
changes
|
|
||||||
changes')))
|
|
||||||
|
|
||||||
(defmethod generate-sync-shape :colors
|
(defmethod generate-sync-shape :colors
|
||||||
[_ changes library-id state container shape]
|
[_ changes library-id state container shape]
|
||||||
(log/debug :msg "Sync colors of shape" :shape (:name shape))
|
(log/debug :msg "Sync colors of shape" :shape (:name shape))
|
||||||
|
@ -461,6 +438,29 @@
|
||||||
(get-in state [:workspace-data asset-type])
|
(get-in state [:workspace-data asset-type])
|
||||||
(get-in state [:workspace-libraries library-id :data asset-type])))
|
(get-in state [:workspace-libraries library-id :data asset-type])))
|
||||||
|
|
||||||
|
(defn- generate-sync-text-shape
|
||||||
|
[changes shape container update-node]
|
||||||
|
(let [old-content (:content shape)
|
||||||
|
new-content (txt/transform-nodes update-node old-content)
|
||||||
|
changes' (-> changes
|
||||||
|
(update :redo-changes conj (make-change
|
||||||
|
container
|
||||||
|
{:type :mod-obj
|
||||||
|
:id (:id shape)
|
||||||
|
:operations [{:type :set
|
||||||
|
:attr :content
|
||||||
|
:val new-content}]}))
|
||||||
|
(update :undo-changes d/preconj (make-change
|
||||||
|
container
|
||||||
|
{:type :mod-obj
|
||||||
|
:id (:id shape)
|
||||||
|
:operations [{:type :set
|
||||||
|
:attr :content
|
||||||
|
:val old-content}]})))]
|
||||||
|
(if (= new-content old-content)
|
||||||
|
changes
|
||||||
|
changes')))
|
||||||
|
|
||||||
|
|
||||||
;; ---- Component synchronization helpers ----
|
;; ---- Component synchronization helpers ----
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue