0
Fork 0
mirror of https://github.com/penpot/penpot.git synced 2025-02-11 01:28:30 -05:00

🐛 Convert in copies nested main instances when duplicating

This commit is contained in:
Andrés Moya 2023-11-10 17:24:01 +01:00
parent 69435e32d9
commit 5fb4703d95
2 changed files with 37 additions and 6 deletions

View file

@ -163,12 +163,17 @@
result)))) result))))
(defn get-parent-ids-seq (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] [objects shape-id]
(let [parent-id (get-parent-id objects shape-id)] (let [parent-id (get-parent-id objects shape-id)]
(when (and (some? parent-id) (not= parent-id shape-id)) (when (and (some? parent-id) (not= parent-id shape-id))
(lazy-seq (cons parent-id (get-parent-ids-seq objects parent-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 (defn get-parents
"Returns a vector of parents of the specified shape." "Returns a vector of parents of the specified shape."
[objects shape-id] [objects shape-id]

View file

@ -73,17 +73,25 @@
main-instance-shape (ctf/get-component-root library-data component) main-instance-shape (ctf/get-component-root library-data component)
delta (gpt/point (+ (:width main-instance-shape) 50) 0) 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 update-original-shape
(fn [original-shape new-shape] (fn [original-shape new-shape]
; Save some ids for later
(vswap! ids-map assoc (:id original-shape) (:id new-shape)) (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) original-shape)
update-new-shape update-new-shape
(fn [shape] (fn [new-shape _]
(cond-> shape (cond-> new-shape
(= (:component-id shape) (:id component)) ; Link the new main to the new component
(= (:component-id new-shape) (:id component))
(assoc :component-id new-component-id) (assoc :component-id new-component-id)
:always :always
@ -98,10 +106,28 @@
remap-frame remap-frame
(fn [shape] (fn [shape]
; Remap all frame-ids internal to the component to the new shapes
(update shape :frame-id (update shape :frame-id
#(get @ids-map % (:frame-id shape)))) #(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]) [nil nil new-instance-shape new-instance-shapes])