diff --git a/common/src/app/common/files/libraries_helpers.cljc b/common/src/app/common/files/libraries_helpers.cljc index 37d893b12..a31aa948f 100644 --- a/common/src/app/common/files/libraries_helpers.cljc +++ b/common/src/app/common/files/libraries_helpers.cljc @@ -148,6 +148,39 @@ [new-component-shape new-component-shapes nil nil])))) + + +(defn generate-duplicate-component + "Create a new component copied from the one with the given id." + [changes library component-id components-v2] + (let [component (ctkl/get-component (:data library) component-id) + new-name (:name component) + + main-instance-page (when components-v2 + (ctf/get-component-page (:data library) component)) + + 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] + (duplicate-component (:data library) component new-component-id)] + + (-> changes + (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 + new-component-id + (:id new-component-shape)) + (:path component) + new-name + new-component-shapes + [] + (:id new-main-instance-shape) + (:id main-instance-page) + (:annotation component))))) + (defn generate-instantiate-component "Generate changes to create a new instance from a component." ([changes objects file-id component-id position page libraries] diff --git a/frontend/src/app/main/data/workspace/libraries.cljs b/frontend/src/app/main/data/workspace/libraries.cljs index 9e944f9f4..c88a6b8e9 100644 --- a/frontend/src/app/main/data/workspace/libraries.cljs +++ b/frontend/src/app/main/data/workspace/libraries.cljs @@ -465,35 +465,9 @@ (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) - components-v2 (features/active-feature? state "components/v2") - - main-instance-page (when components-v2 - (ctf/get-component-page (:data library) component)) - - 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] - (cflh/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 - new-component-id - (:id new-component-shape)) - (:path component) - new-name - new-component-shapes - [] - (:id new-main-instance-shape) - (:id main-instance-page) - (:annotation component)))] + (cflh/generate-duplicate-component library component-id components-v2))] (rx/of (dch/commit-changes changes))))))