diff --git a/common/src/app/common/types/container.cljc b/common/src/app/common/types/container.cljc index b487a2fba..b4db7f855 100644 --- a/common/src/app/common/types/container.cljc +++ b/common/src/app/common/types/container.cljc @@ -192,6 +192,23 @@ (ctk/instance-head? shape) (inside-component-main? objects shape))) +(defn convert-shape-in-component + "Set the shape as a main root instance, pointing to a new component. + Also remove component-root of all children. Return the same structure + as make-component-shape." + [root objects file-id] + (let [new-id (uuid/next) + new-root (assoc root + :component-id new-id + :component-file file-id + :component-root true + :main-instance true) + new-children (->> (cph/get-children objects (:id root)) + (map #(dissoc % :component-root)))] + [(assoc new-root :id new-id) + nil + (into [new-root] new-children)])) + (defn make-component-shape "Clone the shape and all children. Generate new ids and detach from parent and frame. Update the original shapes to have links diff --git a/frontend/src/app/main/data/workspace/libraries_helpers.cljs b/frontend/src/app/main/data/workspace/libraries_helpers.cljs index d84ba31c9..6691f1182 100644 --- a/frontend/src/app/main/data/workspace/libraries_helpers.cljs +++ b/frontend/src/app/main/data/workspace/libraries_helpers.cljs @@ -70,14 +70,7 @@ [root-shape new-shapes updated-shapes] (if-not components-v2 (ctn/make-component-shape root objects file-id components-v2) - (let [new-id (uuid/next)] - [(assoc root :id new-id) - nil - [(assoc root - :component-id new-id - :component-file file-id - :component-root true - :main-instance true)]])) + (ctn/convert-shape-in-component root objects file-id)) changes (-> changes (pcb/add-component (:id root-shape)