0
Fork 0
mirror of https://github.com/penpot/penpot.git synced 2025-02-05 13:58:58 -05:00

Merge pull request #3791 from penpot/hiru-fix-duplicated-nested-main

🐛 Convert in copies nested main instances when duplicating
This commit is contained in:
Pablo Alba 2023-11-13 11:32:06 +01:00 committed by GitHub
commit de3605356c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 37 additions and 6 deletions

View file

@ -163,12 +163,17 @@
result))))
(defn get-parent-ids-seq
"Returns a vector of parents of the specified shape."
"Returns a sequence of parents of the specified shape."
[objects shape-id]
(let [parent-id (get-parent-id objects shape-id)]
(when (and (some? parent-id) (not= parent-id shape-id))
(lazy-seq (cons parent-id (get-parent-ids-seq objects parent-id))))))
(defn get-parent-ids-seq-with-self
"Returns a sequence of parents of the specified shape, including itself."
[objects shape-id]
(cons shape-id (get-parent-ids-seq objects shape-id)))
(defn get-parents
"Returns a vector of parents of the specified shape."
[objects shape-id]

View file

@ -73,17 +73,25 @@
main-instance-shape (ctf/get-component-root library-data component)
delta (gpt/point (+ (:width main-instance-shape) 50) 0)
ids-map (volatile! {})
ids-map (volatile! {})
inverted-ids-map (volatile! {})
nested-main-heads (volatile! #{})
update-original-shape
(fn [original-shape new-shape]
; Save some ids for later
(vswap! ids-map assoc (:id original-shape) (:id new-shape))
(vswap! inverted-ids-map assoc (:id new-shape) (:id original-shape))
(when (and (ctk/main-instance? original-shape)
(not= (:component-id original-shape) (:id component)))
(vswap! nested-main-heads conj (:id original-shape)))
original-shape)
update-new-shape
(fn [shape]
(cond-> shape
(= (:component-id shape) (:id component))
(fn [new-shape _]
(cond-> new-shape
; Link the new main to the new component
(= (:component-id new-shape) (:id component))
(assoc :component-id new-component-id)
:always
@ -98,10 +106,28 @@
remap-frame
(fn [shape]
; Remap all frame-ids internal to the component to the new shapes
(update shape :frame-id
#(get @ids-map % (:frame-id shape))))
new-instance-shapes (map remap-frame new-instance-shapes)]
convert-nested-main
(fn [shape]
; If there is some nested main instance, convert it into a copy of
; main nested in the original component.
(let [origin-shape-id (get @inverted-ids-map (:id shape))
objects (:objects main-instance-page)
parent-ids (cph/get-parent-ids-seq-with-self objects origin-shape-id)]
(cond-> shape
(@nested-main-heads origin-shape-id)
(dissoc :main-instance)
(some @nested-main-heads parent-ids)
(assoc :shape-ref origin-shape-id))))
xf-shape (comp (map remap-frame)
(map convert-nested-main))
new-instance-shapes (into [] xf-shape new-instance-shapes)]
[nil nil new-instance-shape new-instance-shapes])