🐛 Manage correctly when components are dragged and dropped

This commit is contained in:
Andrés Moya 2021-01-20 17:12:39 +01:00 committed by Andrey Antukh
parent e240525a35
commit 11418501a4
5 changed files with 126 additions and 24 deletions

View file

@ -29,6 +29,7 @@
(d/export helpers/walk-pages)
(d/export helpers/select-objects)
(d/export helpers/update-object-list)
(d/export helpers/get-component-shape)
(d/export helpers/get-root-shape)
(d/export helpers/make-container)
(d/export helpers/page?)

View file

@ -222,24 +222,7 @@
(d/dissoc-in [pid :remote-synced?])))))))))
(update-parent-id [objects id]
(update objects id
(fn [object]
(let [prev-component-root (cph/get-root-shape object objects)
detach-component (fn [object]
(let [new-component-root
(cph/get-root-shape object objects)]
(cond-> object
(not= prev-component-root new-component-root)
(dissoc object
:component-id
:component-file
:component-root?
:remote-synced?
:shape-ref
:touched))))]
(-> object
(assoc :parent-id parent-id)
detach-component)))))
(assoc-in objects [id :parent-id] parent-id))
;; Updates the frame-id references that might be outdated
(assign-frame-id [frame-id objects id]
@ -276,7 +259,7 @@
;; Add the new shapes to the parent object.
(update $ parent-id #(add-to-parent % index shapes))
;; Update each individual shapre link to the new parent
;; Update each individual shape link to the new parent
(reduce update-parent-id $ shapes)
;; Analyze the old parents and clear the old links

View file

@ -33,12 +33,23 @@
(update page :objects
#(into % (d/index-by :id objects-list))))
(defn get-component-shape
"Get the parent shape linked to a component for this shape, if any"
[shape objects]
(if-not (:shape-ref shape)
nil
(if (:component-id shape)
shape
(if-let [parent-id (:parent-id shape)]
(get-component-shape (get objects parent-id) objects)
nil))))
(defn get-root-shape
"Get the root shape linked to a component for this shape, if any"
[shape objects]
(if-not (:shape-ref shape)
nil
(if (:component-id shape)
(if (:component-root? shape)
shape
(if-let [parent-id (:parent-id shape)]
(get-root-shape (get objects parent-id) objects)