0
Fork 0
mirror of https://github.com/penpot/penpot.git synced 2025-01-10 08:50:57 -05:00

♻️ Simplify add container id to changes

This commit is contained in:
Andrés Moya 2021-01-14 13:00:43 +01:00 committed by Alonso Torres
parent fe7faf0d0d
commit 091ea785e5
2 changed files with 158 additions and 172 deletions

View file

@ -127,7 +127,7 @@
(d/update-in-when data [:pages-index page-id :objects] delete-object id) (d/update-in-when data [:pages-index page-id :objects] delete-object id)
(d/update-in-when data [:components component-id :objects] delete-object id)))) (d/update-in-when data [:components component-id :objects] delete-object id))))
;; reg-objects operation "regenerates" the values for the parent groups ;; reg-objects operation "regenerates" the geometry and selrect of the parent groups
(defmethod process-change :reg-objects (defmethod process-change :reg-objects
[data {:keys [page-id component-id shapes]}] [data {:keys [page-id component-id shapes]}]
(letfn [(reg-objects [objects] (letfn [(reg-objects [objects]

View file

@ -52,6 +52,7 @@
(declare change-remote-synced) (declare change-remote-synced)
(declare update-attrs) (declare update-attrs)
(declare reposition-shape) (declare reposition-shape)
(declare make-change)
(defn concat-changes (defn concat-changes
[[rchanges1 uchanges1] [rchanges2 uchanges2]] [[rchanges1 uchanges1] [rchanges2 uchanges2]]
@ -268,22 +269,20 @@
[shape container update-node] [shape container update-node]
(let [old-content (:content shape) (let [old-content (:content shape)
new-content (ut/map-node update-node old-content) new-content (ut/map-node update-node old-content)
rchanges [(as-> {:type :mod-obj rchanges [(make-change
:id (:id shape) container
:operations [{:type :set {:type :mod-obj
:attr :content :id (:id shape)
:val new-content}]} $ :operations [{:type :set
(if (cp/page? container) :attr :content
(assoc $ :page-id (:id container)) :val new-content}]})]
(assoc $ :component-id (:id container))))] uchanges [(make-change
uchanges [(as-> {:type :mod-obj container
:id (:id shape) {:type :mod-obj
:operations [{:type :set :id (:id shape)
:attr :content :operations [{:type :set
:val old-content}]} $ :attr :content
(if (cp/page? container) :val old-content}]})]]
(assoc $ :page-id (:id container))
(assoc $ :component-id (:id container))))]]
(if (= new-content old-content) (if (= new-content old-content)
empty-changes empty-changes
@ -312,18 +311,16 @@
(if (nil? attr) (if (nil? attr)
(if (empty? roperations) (if (empty? roperations)
empty-changes empty-changes
(let [rchanges [(as-> {:type :mod-obj (let [rchanges [(make-change
:id (:id shape) container
:operations roperations} $ {:type :mod-obj
(if (cp/page? container) :id (:id shape)
(assoc $ :page-id (:id container)) :operations roperations})]
(assoc $ :component-id (:id container))))] uchanges [(make-change
uchanges [(as-> {:type :mod-obj container
:id (:id shape) {:type :mod-obj
:operations uoperations} $ :id (:id shape)
(if (cp/page? container) :operations uoperations})]]
(assoc $ :page-id (:id container))
(assoc $ :component-id (:id container))))]]
[rchanges uchanges])) [rchanges uchanges]))
(if-not (contains? shape attr-ref-id) (if-not (contains? shape attr-ref-id)
(recur (next attrs) (recur (next attrs)
@ -793,32 +790,29 @@
rchanges (d/concat rchanges (d/concat
(mapv (fn [shape'] (mapv (fn [shape']
(as-> {:type :add-obj (make-change
:id (:id shape') container
:parent-id (:parent-id shape') (as-> {:type :add-obj
:ignore-touched true :id (:id shape')
:obj shape'} $ :parent-id (:parent-id shape')
(cond-> $ :ignore-touched true
(:frame-id shape') :obj shape'} $
(assoc :frame-id (:frame-id shape'))) (cond-> $
(if (cp/page? container) (:frame-id shape')
(assoc $ :page-id (:id container)) (assoc :frame-id (:frame-id shape'))))))
(assoc $ :component-id (:id container)))))
new-shapes) new-shapes)
[(as-> {:type :reg-objects [(make-change
:shapes all-parents} $ container
(if (cp/page? container) {:type :reg-objects
(assoc $ :page-id (:id container)) :shapes all-parents})])
(assoc $ :component-id (:id container))))])
uchanges (d/concat uchanges (d/concat
(mapv (fn [shape'] (mapv (fn [shape']
(as-> {:type :del-obj (make-change
:id (:id shape') container
:ignore-touched true} $ {:type :del-obj
(if (cp/page? container) :id (:id shape')
(assoc $ :page-id (:id container)) :ignore-touched true}))
(assoc $ :component-id (:id container)))))
new-shapes))] new-shapes))]
(if (and (cp/touched-group? parent-shape :shapes-group) omit-touched?) (if (and (cp/touched-group? parent-shape :shapes-group) omit-touched?)
@ -907,36 +901,33 @@
parent (first parents) parent (first parents)
children (cp/get-children (:id shape) objects) children (cp/get-children (:id shape) objects)
rchanges [(as-> {:type :del-obj rchanges [(make-change
:id (:id shape) container
:ignore-touched true} $ {:type :del-obj
(if (cp/page? container) :id (:id shape)
(assoc $ :page-id (:id container)) :ignore-touched true})]
(assoc $ :component-id (:id container))))]
add-change (fn [id] add-change (fn [id]
(let [shape' (get objects id)] (let [shape' (get objects id)]
(as-> {:type :add-obj (make-change
:id id container
:index (cp/position-on-parent id objects) (as-> {:type :add-obj
:parent-id (:parent-id shape') :id id
:ignore-touched true :index (cp/position-on-parent id objects)
:obj shape'} $ :parent-id (:parent-id shape')
(cond-> $ :ignore-touched true
(:frame-id shape') :obj shape'} $
(assoc :frame-id (:frame-id shape'))) (cond-> $
(if (cp/page? container) (:frame-id shape')
(assoc $ :page-id (:id container)) (assoc :frame-id (:frame-id shape')))))))
(assoc $ :component-id (:id container))))))
uchanges (d/concat uchanges (d/concat
[(add-change (:id shape))] [(add-change (:id shape))]
(map add-change children) (map add-change children)
[(as-> {:type :reg-objects [(make-change
:shapes (vec parents)} $ container
(if (cp/page? container) {:type :reg-objects
(assoc $ :page-id (:id container)) :shapes (vec parents)})])]
(assoc $ :component-id (:id container))))])]
(if (and (cp/touched-group? parent :shapes-group) omit-touched?) (if (and (cp/touched-group? parent :shapes-group) omit-touched?)
empty-changes empty-changes
@ -953,22 +944,20 @@
index-after)) index-after))
(let [parent (cp/get-shape container (:parent-id shape)) (let [parent (cp/get-shape container (:parent-id shape))
rchanges [(as-> {:type :mov-objects rchanges [(make-change
:parent-id (:parent-id shape) container
:shapes [(:id shape)] {:type :mov-objects
:index index-after :parent-id (:parent-id shape)
:ignore-touched true} $ :shapes [(:id shape)]
(if (cp/page? container) :index index-after
(assoc $ :page-id (:id container)) :ignore-touched true})]
(assoc $ :component-id (:id container))))] uchanges [(make-change
uchanges [(as-> {:type :mov-objects container
:parent-id (:parent-id shape) {:type :mov-objects
:shapes [(:id shape)] :parent-id (:parent-id shape)
:index index-before :shapes [(:id shape)]
:ignore-touched true} $ :index index-before
(if (cp/page? container) :ignore-touched true})]]
(assoc $ :page-id (:id container))
(assoc $ :component-id (:id container))))]]
(if (and (cp/touched-group? parent :shapes-group) omit-touched?) (if (and (cp/touched-group? parent :shapes-group) omit-touched?)
empty-changes empty-changes
@ -985,32 +974,31 @@
(if (cp/page? container) "[P] " "[C] ") (if (cp/page? container) "[P] " "[C] ")
(:name dest-shape)) (:name dest-shape))
:options options) :options options)
(let [rchanges [(as-> {:type :mod-obj (let [new-touched (cond
:id (:id dest-shape) reset-touched?
:operations nil
[{:type :set-touched copy-touched?
:touched (if (:remote-synced? origin-shape)
(cond nil
reset-touched? (set/union
nil (:touched dest-shape)
copy-touched? (:touched origin-shape))))
(if (:remote-synced? origin-shape)
nil
(set/union
(:touched dest-shape)
(:touched origin-shape))))}]} $
(if (cp/page? container)
(assoc $ :page-id (:id container))
(assoc $ :component-id (:id container))))]
uchanges [(as-> {:type :mod-obj rchanges [(make-change
:id (:id dest-shape) container
:operations {:type :mod-obj
[{:type :set-touched :id (:id dest-shape)
:touched (:touched dest-shape)}]} $ :operations
(if (cp/page? container) [{:type :set-touched
(assoc $ :page-id (:id container)) :touched new-touched}]})]
(assoc $ :component-id (:id container))))]]
uchanges [(make-change
container
{:type :mod-obj
:id (:id dest-shape)
:operations
[{:type :set-touched
:touched (:touched dest-shape)}]})]]
[rchanges uchanges])))) [rchanges uchanges]))))
(defn- change-remote-synced (defn- change-remote-synced
@ -1022,23 +1010,21 @@
(if (cp/page? container) "[P] " "[C] ") (if (cp/page? container) "[P] " "[C] ")
(:name shape)) (:name shape))
:remote-synced? remote-synced?) :remote-synced? remote-synced?)
(let [rchanges [(as-> {:type :mod-obj (let [rchanges [(make-change
:id (:id shape) container
:operations {:type :mod-obj
[{:type :set-remote-synced :id (:id shape)
:remote-synced? remote-synced?}]} $ :operations
(if (cp/page? container) [{:type :set-remote-synced
(assoc $ :page-id (:id container)) :remote-synced? remote-synced?}]})]
(assoc $ :component-id (:id container))))]
uchanges [(as-> {:type :mod-obj uchanges [(make-change
:id (:id shape) container
:operations {:type :mod-obj
[{:type :set-remote-synced :id (:id shape)
:remote-synced? (:remote-synced? shape)}]} $ :operations
(if (cp/page? container) [{:type :set-remote-synced
(assoc $ :page-id (:id container)) :remote-synced? (:remote-synced? shape)}]})]]
(assoc $ :component-id (:id container))))]]
[rchanges uchanges])))) [rchanges uchanges]))))
(defn- set-touched-shapes-group (defn- set-touched-shapes-group
@ -1049,25 +1035,23 @@
(log/info :msg (str "SET-TOUCHED-SHAPES-GROUP " (log/info :msg (str "SET-TOUCHED-SHAPES-GROUP "
(if (cp/page? container) "[P] " "[C] ") (if (cp/page? container) "[P] " "[C] ")
(:name shape))) (:name shape)))
(let [rchanges [(as-> {:type :mod-obj (let [rchanges [(make-change
:id (:id shape) container
:operations {:type :mod-obj
[{:type :set-touched :id (:id shape)
:touched (cp/set-touched-group :operations
(:touched shape) [{:type :set-touched
:shapes-group)}]} $ :touched (cp/set-touched-group
(if (cp/page? container) (:touched shape)
(assoc $ :page-id (:id container)) :shapes-group)}]})]
(assoc $ :component-id (:id container))))]
uchanges [(as-> {:type :mod-obj uchanges [(make-change
:id (:id shape) container
:operations {:type :mod-obj
[{:type :set-touched :id (:id shape)
:touched (:touched shape)}]} $ :operations
(if (cp/page? container) [{:type :set-touched
(assoc $ :page-id (:id container)) :touched (:touched shape)}]})]]
(assoc $ :component-id (:id container))))]]
[rchanges uchanges])))) [rchanges uchanges]))))
(defn- update-attrs (defn- update-attrs
@ -1100,29 +1084,24 @@
(if (nil? attr) (if (nil? attr)
(let [all-parents (vec (or (cp/get-parents (:id dest-shape) (let [all-parents (vec (or (cp/get-parents (:id dest-shape)
(:objects container)) [])) (:objects container)) []))
rchanges [(make-change
rchanges [(as-> {:type :mod-obj container
:id (:id dest-shape) {:type :mod-obj
:operations roperations} $ :id (:id dest-shape)
(if (cp/page? container) :operations roperations})
(assoc $ :page-id (:id container)) (make-change
(assoc $ :component-id (:id container)))) container
(as-> {:type :reg-objects {:type :reg-objects
:shapes all-parents} $ :shapes all-parents})]
(if (cp/page? container) uchanges [(make-change
(assoc $ :page-id (:id container)) container
(assoc $ :component-id (:id container))))] {:type :mod-obj
uchanges [(as-> {:type :mod-obj :id (:id dest-shape)
:id (:id dest-shape) :operations uoperations})
:operations uoperations} $ (make-change
(if (cp/page? container) container
(assoc $ :page-id (:id container)) {:type :reg-objects
(assoc $ :component-id (:id container)))) :shapes all-parents})]]
(as-> {:type :reg-objects
:shapes all-parents} $
(if (cp/page? container)
(assoc $ :page-id (:id container))
(assoc $ :component-id (:id container))))]]
(if (seq roperations) (if (seq roperations)
[rchanges uchanges] [rchanges uchanges]
empty-changes)) empty-changes))
@ -1156,3 +1135,10 @@
dest-root-pos (shape-pos dest-root) dest-root-pos (shape-pos dest-root)
delta (gpt/subtract dest-root-pos origin-root-pos)] delta (gpt/subtract dest-root-pos origin-root-pos)]
(geom/move shape delta))) (geom/move shape delta)))
(defn- make-change
[container change]
(if (cp/page? container)
(assoc change :page-id (:id container))
(assoc change :component-id (:id container))))