0
Fork 0
mirror of https://github.com/penpot/penpot.git synced 2025-01-09 08:20:45 -05:00

🐛 Launch component sync when adding or removing shapes

This commit is contained in:
Andrés Moya 2023-05-18 11:28:51 +02:00
parent 36b016a37b
commit 19ea85d9cc
2 changed files with 38 additions and 6 deletions

View file

@ -27,7 +27,8 @@
[app.common.types.shape :as cts]
[app.common.types.shape-tree :as ctst]
[app.common.types.typographies-list :as ctyl]
[app.common.types.typography :as ctt]))
[app.common.types.typography :as ctt]
[clojure.set :as set]))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; SCHEMAS
@ -665,7 +666,7 @@
(when page-id
(let [page (ctpl/get-page file-data page-id)
shape-and-parents (map #(ctn/get-shape page %)
(into [id] (cph/get-parent-ids (:objects page) id)))
(cons id (cph/get-parent-ids (:objects page) id)))
need-sync? (fn [operation]
; We need to trigger a sync if the shape has changed any
; attribute that participates in components synchronization.
@ -677,6 +678,34 @@
(map :id))]
(into #{} xform shape-and-parents))))))
(defmethod components-changed :mov-objects
[file-data {:keys [page-id _component-id parent-id shapes] :as change}]
(when page-id
(let [page (ctpl/get-page file-data page-id)
xform (comp (filter :main-instance?)
(map :id))
check-shape
(fn [shape-id others]
(let [all-parents (map (partial ctn/get-shape page)
(concat others (cph/get-parent-ids (:objects page) shape-id)))]
(into #{} xform all-parents)))]
(reduce #(set/union %1 (check-shape %2 []))
(check-shape parent-id [parent-id])
shapes))))
(defmethod components-changed :del-obj
[file-data {:keys [id page-id _component-id] :as change}]
(when page-id
(let [page (ctpl/get-page file-data page-id)
shape-and-parents (map (partial ctn/get-shape page)
(cons id (cph/get-parent-ids (:objects page) id)))
xform (comp (filter :main-instance?)
(map :id))]
(into #{} xform shape-and-parents))))
(defmethod components-changed :default
[_ _]
nil)

View file

@ -871,7 +871,10 @@
workspace-data-s
(->> (rx/concat
(rx/of nil)
(rx/from-atom refs/workspace-data {:emit-current-value? true})))
(rx/from-atom refs/workspace-data {:emit-current-value? true}))
;; Need to get the file data before the change, so deleted shapes
;; still exist, for example
(rx/buffer 3 1))
change-s
(->> stream
@ -880,16 +883,16 @@
(rx/observe-on :async))
check-changes
(fn [[event data]]
(fn [[event [old-data _mid_data _new-data]]]
(let [{:keys [changes save-undo? undo-group]} (deref event)
components-changed (reduce #(into %1 (ch/components-changed data %2))
components-changed (reduce #(into %1 (ch/components-changed old-data %2))
#{}
changes)]
(when (and (d/not-empty? components-changed) save-undo?)
(log/info :msg "DETECTED COMPONENTS CHANGED"
:ids (map str components-changed))
(run! st/emit!
(map #(update-component-sync % (:id data) undo-group)
(map #(update-component-sync % (:id old-data) undo-group)
components-changed)))))]
(when components-v2