From 165cdd871f346fc823ec3e8e367cd81e36709dc2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Moya?= Date: Fri, 17 Jun 2022 16:44:32 +0200 Subject: [PATCH] :tada: Allow to duplicate components with main instance --- .../src/app/common/pages/changes_builder.cljc | 9 ++++++ .../app/main/data/workspace/libraries.cljs | 20 ++++++++----- .../data/workspace/libraries_helpers.cljs | 29 +++++++++++++++---- frontend/src/debug.cljs | 6 +++- 4 files changed, 49 insertions(+), 15 deletions(-) diff --git a/common/src/app/common/pages/changes_builder.cljc b/common/src/app/common/pages/changes_builder.cljc index 13e9d710f..bdf4a1a96 100644 --- a/common/src/app/common/pages/changes_builder.cljc +++ b/common/src/app/common/pages/changes_builder.cljc @@ -223,6 +223,15 @@ (update :undo-changes d/preconj del-change) (apply-changes-local))))) +(defn add-objects + ([changes objects] + (add-objects changes objects nil)) + + ([changes objects params] + (reduce #(add-object %1 %2 params) + changes + objects))) + (defn change-parent ([changes parent-id shapes] (change-parent changes parent-id shapes nil)) diff --git a/frontend/src/app/main/data/workspace/libraries.cljs b/frontend/src/app/main/data/workspace/libraries.cljs index 9a8acafb0..2217af1dd 100644 --- a/frontend/src/app/main/data/workspace/libraries.cljs +++ b/frontend/src/app/main/data/workspace/libraries.cljs @@ -355,19 +355,23 @@ unames (into #{} (map :name) all-components) new-name (ctst/generate-unique-name unames (:name component)) - [new-shape new-shapes _updated-shapes main-instance main-instance-page] - (dwlh/duplicate-component component) + main-instance-page (wsh/lookup-page state (:main-instance-page component)) + main-instance-shape (ctn/get-shape main-instance-page (:main-instance-id component)) - _ (prn "OJOOOOOOOOOOOOOOO falta calcular main-instance") + [new-component-shape new-component-shapes + new-main-instance-shape new-main-instance-shapes] + (dwlh/duplicate-component component main-instance-page main-instance-shape) - changes (-> (pcb/empty-changes it nil) ;; no objects are changed - (pcb/with-objects nil) ;; in the current page - (pcb/add-component (:id new-shape) + 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 (:id new-component-shape) (:path component) new-name - new-shapes + new-component-shapes [] - (:id main-instance) + (:id new-main-instance-shape) (:id main-instance-page)))] (rx/of (dch/commit-changes changes)))))) diff --git a/frontend/src/app/main/data/workspace/libraries_helpers.cljs b/frontend/src/app/main/data/workspace/libraries_helpers.cljs index 2914f4095..39885b847 100644 --- a/frontend/src/app/main/data/workspace/libraries_helpers.cljs +++ b/frontend/src/app/main/data/workspace/libraries_helpers.cljs @@ -130,12 +130,29 @@ (defn duplicate-component "Clone the root shape of the component and all children. Generate new ids from all of them." - [component] - (let [component-root (cph/get-component-root component)] - (ctst/clone-object component-root - nil - (get component :objects) - identity))) + [component main-instance-page main-instance-shape] + (let [position (gpt/add (gpt/point (:x main-instance-shape) (:y main-instance-shape)) + (gpt/point (+ (:width main-instance-shape) 50) 0)) + + component-root (cph/get-component-root component) + + [new-component-shape new-component-shapes _] + (ctst/clone-object component-root + nil + (get component :objects) + identity) + + + [new-instance-shape new-instance-shapes] + (ctn/instantiate-component main-instance-page + {:id (:id new-component-shape) + :name (:name new-component-shape) + :objects (d/index-by :id new-component-shapes)} + (:component-file main-instance-shape) + position)] + + [new-component-shape new-component-shapes + new-instance-shape new-instance-shapes])) (defn generate-instantiate-component "Generate changes to create a new instance from a component." diff --git a/frontend/src/debug.cljs b/frontend/src/debug.cljs index bef2d105d..09d651d14 100644 --- a/frontend/src/debug.cljs +++ b/frontend/src/debug.cljs @@ -276,7 +276,11 @@ (dorun (for [component (vals components)] (do (println) - (println (str/format "[%s]" (:name component))) + (println (str/format "[%s]" (:name component)) + (when show-ids + (str/format " (main: %s/%s)" + (:main-instance-page component) + (:main-instance-id component)))) (show-shape (:id component) 0 (:objects component))))))))) (defn ^:export dump-tree