Updating a library color updates texts that uses it

This commit is contained in:
alonso.torres 2020-10-02 21:33:34 +02:00 committed by Hirunatan
parent e01e689f69
commit 96fbc83a0a

View file

@ -101,13 +101,20 @@
(= (:component-file shape) library-id))) (= (:component-file shape) library-id)))
:colors :colors
(fn [shape] (some (fn [shape] (if (= (:type shape) :text)
(->> shape
:content
;; Check if any node in the content has a reference for the library
(ut/some-node
#(and (some? (:fill-color-ref-id %))
(= library-id (:fill-color-ref-file %)))))
(some
#(let [attr (name %) #(let [attr (name %)
attr-ref-id (keyword (str attr "-ref-id")) attr-ref-id (keyword (str attr "-ref-id"))
attr-ref-file (keyword (str attr "-ref-file"))] attr-ref-file (keyword (str attr "-ref-file"))]
(and (get shape attr-ref-id) (and (get shape attr-ref-id)
(= library-id (get shape attr-ref-file)))) (= library-id (get shape attr-ref-file))))
cp/color-sync-attrs)) cp/color-sync-attrs)))
:typographies :typographies
(fn [shape] (fn [shape]
@ -162,11 +169,38 @@
component-id component-id
reset-touched?))) reset-touched?)))
(defn generate-sync-text-shape [shape page-id component-id update-node]
(let [old-content (:content shape)
new-content (ut/map-node update-node old-content)
rchanges [(d/without-nils {:type :mod-obj
:page-id page-id
:component-id component-id
:id (:id shape)
:operations [{:type :set
:attr :content
:val new-content}]})]
lchanges [(d/without-nils {:type :mod-obj
:page-id page-id
:component-id component-id
:id (:id shape)
:operations [{:type :set
:attr :content
:val old-content}]})]]
(if (= new-content old-content)
empty-changes
[rchanges lchanges])))
(defmethod generate-sync-shape :colors (defmethod generate-sync-shape :colors
[_ library-id library-items _ page-id component-id shape] [_ library-id library-items _ page-id component-id shape]
;; Synchronize a shape that uses some colors of the library. The value of the ;; Synchronize a shape that uses some colors of the library. The value of the
;; color in the library is copied to the shape. ;; color in the library is copied to the shape.
(if (= :text (:type shape))
(let [update-node (fn [node]
(if-let [color (get library-items (:fill-color-ref-id node))]
(assoc node :fill-color (:value color))
node))]
(generate-sync-text-shape shape page-id component-id update-node))
(loop [attrs (seq cp/color-sync-attrs) (loop [attrs (seq cp/color-sync-attrs)
roperations [] roperations []
uoperations []] uoperations []]
@ -186,8 +220,7 @@
:operations uoperations})]] :operations uoperations})]]
[rchanges uchanges])) [rchanges uchanges]))
(let [attr-ref-id (keyword (str (name attr) "-ref-id"))] (let [attr-ref-id (keyword (str (name attr) "-ref-id"))]
(if (or (not (contains? shape attr-ref-id)) (if-not (contains? shape attr-ref-id)
(nil? (get library-items (get shape attr-ref-id))))
(recur (next attrs) (recur (next attrs)
roperations roperations
uoperations) uoperations)
@ -202,7 +235,7 @@
:ignore-touched true}] :ignore-touched true}]
(recur (next attrs) (recur (next attrs)
(conj roperations roperation) (conj roperations roperation)
(conj uoperations uoperation))))))))) (conj uoperations uoperation))))))))))
(defmethod generate-sync-shape :typographies (defmethod generate-sync-shape :typographies
[_ library-id library-items _ page-id component-id shape] [_ library-id library-items _ page-id component-id shape]
@ -212,26 +245,8 @@
(let [update-node (fn [node] (let [update-node (fn [node]
(if-let [typography (get library-items (:typography-ref-id node))] (if-let [typography (get library-items (:typography-ref-id node))]
(merge node (d/without-keys typography [:name :id])) (merge node (d/without-keys typography [:name :id]))
node)) node))]
old-content (:content shape) (generate-sync-text-shape shape page-id component-id update-node)))
new-content (ut/map-node update-node old-content)
rchanges [(d/without-nils {:type :mod-obj
:page-id page-id
:component-id component-id
:id (:id shape)
:operations [{:type :set
:attr :content
:val new-content}]})]
lchanges [(d/without-nils {:type :mod-obj
:page-id page-id
:component-id component-id
:id (:id shape)
:operations [{:type :set
:attr :content
:val old-content}]})]]
(if (= new-content old-content)
empty-changes
[rchanges lchanges])))
;; ---- Create a new component ---- ;; ---- Create a new component ----