From 8165fec50ed45942fff59f73f01056e925516a1b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Moya?= Date: Thu, 29 Oct 2020 16:11:20 +0100 Subject: [PATCH] :tada: Allow to duplicate a component --- frontend/resources/locales.json | 8 ++++++ .../app/main/data/workspace/libraries.cljs | 27 +++++++++++++++++++ .../data/workspace/libraries_helpers.cljs | 10 +++++++ .../app/main/ui/workspace/sidebar/assets.cljs | 8 +++++- 4 files changed, 52 insertions(+), 1 deletion(-) diff --git a/frontend/resources/locales.json b/frontend/resources/locales.json index 22187701b..8a849f409 100644 --- a/frontend/resources/locales.json +++ b/frontend/resources/locales.json @@ -2118,6 +2118,14 @@ "es" : "Borrar" } }, + "workspace.assets.duplicate" : { + "translations" : { + "en" : "Duplicate", + "fr" : "", + "ru" : "", + "es" : "Duplicar" + } + }, "workspace.assets.edit" : { "used-in" : [ "src/app/main/ui/workspace/sidebar/assets.cljs:305", "src/app/main/ui/workspace/sidebar/assets.cljs:433" ], "translations" : { diff --git a/frontend/src/app/main/data/workspace/libraries.cljs b/frontend/src/app/main/data/workspace/libraries.cljs index a20a6bb7f..51c4fc3a9 100644 --- a/frontend/src/app/main/data/workspace/libraries.cljs +++ b/frontend/src/app/main/data/workspace/libraries.cljs @@ -253,6 +253,33 @@ (rx/of (dwc/commit-changes rchanges uchanges {:commit-local? true}) (dws/select-shapes (d/ordered-set (:id group)))))))))) +(defn duplicate-component + "Create a new component copied from the one with the given id." + [{:keys [id] :as params}] + (ptk/reify ::duplicate-component + ptk/WatchEvent + (watch [_ state stream] + (let [component (cph/get-component id + nil + (get state :workspace-data) + nil) + all-components (vals (get-in state [:workspace-data :components])) + unames (set (map :name all-components)) + new-name (dwc/generate-unique-name unames (:name component)) + + [new-shape new-shapes updated-shapes] + (dwlh/duplicate-component component) + + rchanges [{:type :add-component + :id (:id new-shape) + :name new-name + :shapes new-shapes}] + + uchanges [{:type :del-component + :id (:id new-shape)}]] + + (rx/of (dwc/commit-changes rchanges uchanges {:commit-local? true})))))) + (defn delete-component "Delete the component with the given id, from the current file library." [{:keys [id] :as params}] diff --git a/frontend/src/app/main/data/workspace/libraries_helpers.cljs b/frontend/src/app/main/data/workspace/libraries_helpers.cljs index 92df54412..05665e212 100644 --- a/frontend/src/app/main/data/workspace/libraries_helpers.cljs +++ b/frontend/src/app/main/data/workspace/libraries_helpers.cljs @@ -93,6 +93,16 @@ (cph/clone-object shape nil objects update-new-shape update-original-shape))) +(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)] + (cph/clone-object component-root + nil + (get component :objects) + identity))) + ;; ---- General library synchronization functions ---- diff --git a/frontend/src/app/main/ui/workspace/sidebar/assets.cljs b/frontend/src/app/main/ui/workspace/sidebar/assets.cljs index 4cf2d1153..92f6310f8 100644 --- a/frontend/src/app/main/ui/workspace/sidebar/assets.cljs +++ b/frontend/src/app/main/ui/workspace/sidebar/assets.cljs @@ -51,6 +51,11 @@ :top nil :left nil :component-id nil}) + on-duplicate + (mf/use-callback + (mf/deps state) + (st/emitf (dwl/duplicate-component {:id (:component-id @state)}))) + on-delete (mf/use-callback (mf/deps state) @@ -101,7 +106,8 @@ :on-close #(swap! state assoc :menu-open false) :top (:top @state) :left (:left @state) - :options [[(tr "workspace.assets.delete") on-delete]]}])])) + :options [[(tr "workspace.assets.duplicate") on-duplicate] + [(tr "workspace.assets.delete") on-delete]]}])])) (mf/defc graphics-box [{:keys [file-id local? objects open? on-open on-close] :as props}]