mirror of
https://github.com/penpot/penpot.git
synced 2025-01-07 15:39:42 -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/gradient]))
|
||||
|
||||
|
||||
|
||||
|
||||
|
|
|
@ -35,7 +35,8 @@
|
|||
|
||||
(declare generate-sync-container)
|
||||
(declare generate-sync-shape)
|
||||
(declare has-asset-reference-fn)
|
||||
(declare generate-sync-text-shape)
|
||||
(declare uses-assets?)
|
||||
|
||||
(declare get-assets)
|
||||
(declare generate-sync-shape-direct)
|
||||
|
@ -60,7 +61,7 @@
|
|||
"<local>"
|
||||
(str "<" (get-in state [:workspace-libraries file-id :name]) ">")))
|
||||
|
||||
;; ---- Create a new component ----
|
||||
;; ---- Components and instances creation ----
|
||||
|
||||
(defn make-component-shape
|
||||
"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 component in local library" :component-id (:id container)))
|
||||
|
||||
(let [has-asset-reference? (has-asset-reference-fn asset-type library-id (cph/page? container))
|
||||
linked-shapes (->> (vals (:objects container))
|
||||
(filter has-asset-reference?))]
|
||||
(let [linked-shapes (->> (vals (:objects container))
|
||||
(filter #(uses-assets? asset-type % library-id (cph/page? container))))]
|
||||
(loop [shapes (seq linked-shapes)
|
||||
changes (-> (pcb/empty-changes it)
|
||||
(pcb/with-container container)
|
||||
|
@ -295,49 +295,49 @@
|
|||
shape))
|
||||
changes))))
|
||||
|
||||
(defn- has-asset-reference-fn
|
||||
"Gets a function that checks if a shape uses some asset of the given type
|
||||
in the given library."
|
||||
[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)))
|
||||
(defmulti uses-assets?
|
||||
"Checks if a shape uses some asset of the given type in the given library."
|
||||
(fn [asset-type shape library-id page?] asset-type))
|
||||
|
||||
:colors
|
||||
(fn [shape]
|
||||
(if (= (:type shape) :text)
|
||||
(->> shape
|
||||
:content
|
||||
;; 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))))
|
||||
(defmethod uses-assets? :components
|
||||
[_ shape library-id page?]
|
||||
(and (some? (:component-id shape))
|
||||
(= (:component-file shape) library-id)
|
||||
(or (:component-root? shape) (not page?)))) ; avoid nested components inside pages
|
||||
|
||||
:typographies
|
||||
(fn [shape]
|
||||
(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 %))
|
||||
(= library-id (:typography-ref-file %)))))))))
|
||||
(defmethod uses-assets? :colors
|
||||
[_ shape library-id _]
|
||||
(if (= (:type shape) :text)
|
||||
(->> shape
|
||||
:content
|
||||
;; Check if any node in the content has a reference for the library
|
||||
(txt/node-seq
|
||||
#(or (and (some? (:stroke-color-ref-id %))
|
||||
(= (: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
|
||||
"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."
|
||||
(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
|
||||
[_ changes _library-id state container shape]
|
||||
|
@ -345,29 +345,6 @@
|
|||
libraries (wsh/get-libraries state)]
|
||||
(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
|
||||
[_ changes library-id state container 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-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 ----
|
||||
|
||||
|
|
Loading…
Reference in a new issue