From 82c02634e92dc96f0d444692875a92b997a447e9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Moya?= Date: Tue, 7 Nov 2023 13:45:12 +0100 Subject: [PATCH] :bug: Redo duplicate main to avoid several bugs --- .../app/main/data/workspace/libraries.cljs | 21 ++++----- .../data/workspace/libraries_helpers.cljs | 45 +++++++++++++------ 2 files changed, 42 insertions(+), 24 deletions(-) diff --git a/frontend/src/app/main/data/workspace/libraries.cljs b/frontend/src/app/main/data/workspace/libraries.cljs index a7c56a38b..05f4d3698 100644 --- a/frontend/src/app/main/data/workspace/libraries.cljs +++ b/frontend/src/app/main/data/workspace/libraries.cljs @@ -405,28 +405,29 @@ (ptk/reify ::duplicate-component ptk/WatchEvent (watch [it state _] - (let [libraries (wsh/get-libraries state) - library (get libraries library-id) - component (ctkl/get-component (:data library) component-id) - new-name (:name component) + (let [libraries (wsh/get-libraries state) + library (get libraries library-id) + component (ctkl/get-component (:data library) component-id) + 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 - (ctf/get-component-page (:data library) component)) + main-instance-page (when components-v2 + (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-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) (pcb/with-page main-instance-page) (pcb/with-objects (:objects main-instance-page)) (pcb/add-objects new-main-instance-shapes {:ignore-touched true}) (pcb/add-component (if components-v2 - (:id new-component) + new-component-id (:id new-component-shape)) (:path component) new-name diff --git a/frontend/src/app/main/data/workspace/libraries_helpers.cljs b/frontend/src/app/main/data/workspace/libraries_helpers.cljs index ffa0a1240..e7fe477ce 100644 --- a/frontend/src/app/main/data/workspace/libraries_helpers.cljs +++ b/frontend/src/app/main/data/workspace/libraries_helpers.cljs @@ -65,26 +65,43 @@ (defn duplicate-component "Clone the root shape of the component and all children. Generate new 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])] (if components-v2 (let [main-instance-page (ctf/get-component-page library-data component) main-instance-shape (ctf/get-component-root library-data component) - position (gpt/point (+ (:x main-instance-shape) - (:width main-instance-shape) - 50) - (:y main-instance-shape)) - options (if components-v2 {:main-instance? true} {}) + delta (gpt/point (+ (:width main-instance-shape) 50) 0) - [new-instance-shape new-instance-shapes] - (when (and (some? main-instance-page) (some? main-instance-shape)) - (ctn/make-component-instance main-instance-page - component - library-data - position - true - options))] + ids-map (volatile! {}) + + update-original-shape + (fn [original-shape new-shape] + (vswap! ids-map assoc (:id original-shape) (:id new-shape)) + original-shape) + + 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])