🐛 Redo duplicate main to avoid several bugs

This commit is contained in:
Andrés Moya 2023-11-07 13:45:12 +01:00
parent f1349facc1
commit 82c02634e9
2 changed files with 42 additions and 24 deletions

View file

@ -405,28 +405,29 @@
(ptk/reify ::duplicate-component (ptk/reify ::duplicate-component
ptk/WatchEvent ptk/WatchEvent
(watch [it state _] (watch [it state _]
(let [libraries (wsh/get-libraries state) (let [libraries (wsh/get-libraries state)
library (get libraries library-id) library (get libraries library-id)
component (ctkl/get-component (:data library) component-id) component (ctkl/get-component (:data library) component-id)
new-name (:name component) new-name (:name component)
components-v2 (features/active-feature? state "components/v2") components-v2 (features/active-feature? state "components/v2")
main-instance-page (when components-v2 main-instance-page (when components-v2
(ctf/get-component-page (:data library) component)) (ctf/get-component-page (:data library) component))
new-component (assoc component :id (uuid/next)) new-component-id (when components-v2
(uuid/next))
[new-component-shape new-component-shapes ; <- null in components-v2 [new-component-shape new-component-shapes ; <- null in components-v2
new-main-instance-shape new-main-instance-shapes] new-main-instance-shape new-main-instance-shapes]
(dwlh/duplicate-component new-component (:data library)) (dwlh/duplicate-component component new-component-id (:data library))
changes (-> (pcb/empty-changes it nil) changes (-> (pcb/empty-changes it nil)
(pcb/with-page main-instance-page) (pcb/with-page main-instance-page)
(pcb/with-objects (:objects main-instance-page)) (pcb/with-objects (:objects main-instance-page))
(pcb/add-objects new-main-instance-shapes {:ignore-touched true}) (pcb/add-objects new-main-instance-shapes {:ignore-touched true})
(pcb/add-component (if components-v2 (pcb/add-component (if components-v2
(:id new-component) new-component-id
(:id new-component-shape)) (:id new-component-shape))
(:path component) (:path component)
new-name new-name

View file

@ -65,26 +65,43 @@
(defn duplicate-component (defn duplicate-component
"Clone the root shape of the component and all children. Generate new "Clone the root shape of the component and all children. Generate new
ids from all of them." ids from all of them."
[component library-data] [component new-component-id library-data]
(let [components-v2 (dm/get-in library-data [:options :components-v2])] (let [components-v2 (dm/get-in library-data [:options :components-v2])]
(if components-v2 (if components-v2
(let [main-instance-page (ctf/get-component-page library-data component) (let [main-instance-page (ctf/get-component-page library-data component)
main-instance-shape (ctf/get-component-root library-data component) main-instance-shape (ctf/get-component-root library-data component)
position (gpt/point (+ (:x main-instance-shape) delta (gpt/point (+ (:width main-instance-shape) 50) 0)
(:width main-instance-shape)
50)
(:y main-instance-shape))
options (if components-v2 {:main-instance? true} {})
[new-instance-shape new-instance-shapes] ids-map (volatile! {})
(when (and (some? main-instance-page) (some? main-instance-shape))
(ctn/make-component-instance main-instance-page update-original-shape
component (fn [original-shape new-shape]
library-data (vswap! ids-map assoc (:id original-shape) (:id new-shape))
position original-shape)
true
options))] update-new-shape
(fn [shape]
(cond-> shape
(= (:component-id shape) (:id component))
(assoc :component-id new-component-id)
:always
(gsh/move delta)))
[new-instance-shape new-instance-shapes _]
(ctst/clone-object main-instance-shape
(:parent-id main-instance-shape)
(:objects main-instance-page)
update-new-shape
update-original-shape)
remap-frame
(fn [shape]
(update shape :frame-id
#(get @ids-map % (:frame-id shape))))
new-instance-shapes (map remap-frame new-instance-shapes)]
[nil nil new-instance-shape new-instance-shapes]) [nil nil new-instance-shape new-instance-shapes])