From 23527b1d19ea03c0779a79b61e65d400396601d9 Mon Sep 17 00:00:00 2001 From: Pablo Alba Date: Wed, 22 Nov 2023 13:14:57 +0100 Subject: [PATCH] :bug: Fix create a main component inside another and move one main inside another --- common/src/app/common/files/validate.cljc | 6 ++--- common/src/app/common/types/component.cljc | 6 ++--- common/src/app/common/types/container.cljc | 24 ++++++++++--------- .../app/main/data/workspace/transforms.cljs | 3 +++ 4 files changed, 22 insertions(+), 17 deletions(-) diff --git a/common/src/app/common/files/validate.cljc b/common/src/app/common/files/validate.cljc index fb3782473..83192b8e2 100644 --- a/common/src/app/common/files/validate.cljc +++ b/common/src/app/common/files/validate.cljc @@ -187,7 +187,7 @@ exists and its main-instance does not point to this shape." [shape file page libraries] - (when (some? (:main-instance shape)) + (when (true? (:main-instance shape)) (report-error! :component-not-main "Shape not expected to be main instance" shape file page)) @@ -210,7 +210,7 @@ (defn validate-component-not-main-not-head! "Validate that this shape is not main instance and not head." [shape file page] - (when (some? (:main-instance shape)) + (when (true? (:main-instance shape)) (report-error! :component-main "Shape not expected to be main instance" shape file page)) @@ -231,7 +231,7 @@ (defn validate-component-not-root! "Validate that this shape is not an instance root." [shape file page] - (when (some? (:component-root shape)) + (when (true? (:component-root shape)) (report-error! :should-not-be-component-root "Shape should not be component root" shape file page))) diff --git a/common/src/app/common/types/component.cljc b/common/src/app/common/types/component.cljc index ae580a0d9..32217feca 100644 --- a/common/src/app/common/types/component.cljc +++ b/common/src/app/common/types/component.cljc @@ -97,7 +97,7 @@ (defn instance-root? "Check if this shape is the head of a top instance." [shape] - (some? (:component-root shape))) + (true? (:component-root shape))) (defn instance-head? "Check if this shape is the head of a top instance or a subinstance." @@ -127,7 +127,7 @@ "Check if this shape is the root of the main instance of some component." [shape] - (some? (:main-instance shape))) + (true? (:main-instance shape))) (defn in-component-copy? "Check if the shape is inside a component non-main instance." @@ -156,7 +156,7 @@ (defn get-component-root [component] - (if (some? (:main-instance-id component)) + (if (true? (:main-instance-id component)) (get-in component [:objects (:main-instance-id component)]) (get-in component [:objects (:id component)]))) diff --git a/common/src/app/common/types/container.cljc b/common/src/app/common/types/container.cljc index 9669fc9e1..6e08073cc 100644 --- a/common/src/app/common/types/container.cljc +++ b/common/src/app/common/types/container.cljc @@ -196,19 +196,21 @@ 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 (->> (cfh/get-children objects (:id root)) - (map #(dissoc % :component-root)))] + (let [new-id (uuid/next) + inside-component? (some? (get-instance-root objects root)) + new-root (cond-> (assoc root + :component-id new-id + :component-file file-id + :main-instance true) + (not inside-component?) + (assoc :component-root true)) + new-children (->> (cfh/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 +(defn make-component-shape ;; Only used for components v1 "Clone the shape and all children. Generate new ids and detach from parent and frame. Update the original shapes to have links to the new ones." @@ -280,7 +282,7 @@ component-shape (if components-v2 (-> (get-shape component-page (:main-instance-id component)) - (assoc :parent-id nil) + (assoc :parent-id nil) ;; On v2 we force parent-id to nil in order to behave like v1 (assoc :frame-id uuid/zero)) (get-shape component (:id component))) @@ -333,7 +335,7 @@ :component-root true :name new-name) - (some? (:parent-id original-shape)) + (some? (:parent-id original-shape)) ;; On v2 we have removed the parent-id for component roots (see above) (dissoc :component-root)))) [new-shape new-shapes _] diff --git a/frontend/src/app/main/data/workspace/transforms.cljs b/frontend/src/app/main/data/workspace/transforms.cljs index 5a33fd7b6..0e286a7e6 100644 --- a/frontend/src/app/main/data/workspace/transforms.cljs +++ b/frontend/src/app/main/data/workspace/transforms.cljs @@ -840,6 +840,9 @@ ;; Remove layout-item properties when moving a shape outside a layout (cond-> (not (ctl/any-layout? objects frame-id)) (pcb/update-shapes moving-shapes-ids ctl/remove-layout-item-data)) + ;; Remove componnet-root property when moving a shape inside a component + (cond-> (ctn/get-instance-root objects frame) + (pcb/update-shapes moving-shapes-ids #(dissoc % :component-root))) (pcb/update-shapes moving-shapes-ids #(cond-> % (cfh/frame-shape? %) (assoc :hide-in-viewer true))) (pcb/update-shapes shape-ids-to-detach ctk/detach-shape) (pcb/change-parent frame-id moving-shapes drop-index)