🐛 Fix create a main component inside another and move one main inside another

This commit is contained in:
Pablo Alba 2023-11-22 13:14:57 +01:00 committed by Andrés Moya
parent cee827a97b
commit 23527b1d19
4 changed files with 22 additions and 17 deletions

View file

@ -187,7 +187,7 @@
exists and its main-instance does not point to this exists and its main-instance does not point to this
shape." shape."
[shape file page libraries] [shape file page libraries]
(when (some? (:main-instance shape)) (when (true? (:main-instance shape))
(report-error! :component-not-main (report-error! :component-not-main
"Shape not expected to be main instance" "Shape not expected to be main instance"
shape file page)) shape file page))
@ -210,7 +210,7 @@
(defn validate-component-not-main-not-head! (defn validate-component-not-main-not-head!
"Validate that this shape is not main instance and not head." "Validate that this shape is not main instance and not head."
[shape file page] [shape file page]
(when (some? (:main-instance shape)) (when (true? (:main-instance shape))
(report-error! :component-main (report-error! :component-main
"Shape not expected to be main instance" "Shape not expected to be main instance"
shape file page)) shape file page))
@ -231,7 +231,7 @@
(defn validate-component-not-root! (defn validate-component-not-root!
"Validate that this shape is not an instance root." "Validate that this shape is not an instance root."
[shape file page] [shape file page]
(when (some? (:component-root shape)) (when (true? (:component-root shape))
(report-error! :should-not-be-component-root (report-error! :should-not-be-component-root
"Shape should not be component root" "Shape should not be component root"
shape file page))) shape file page)))

View file

@ -97,7 +97,7 @@
(defn instance-root? (defn instance-root?
"Check if this shape is the head of a top instance." "Check if this shape is the head of a top instance."
[shape] [shape]
(some? (:component-root shape))) (true? (:component-root shape)))
(defn instance-head? (defn instance-head?
"Check if this shape is the head of a top instance or a subinstance." "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 "Check if this shape is the root of the main instance of some
component." component."
[shape] [shape]
(some? (:main-instance shape))) (true? (:main-instance shape)))
(defn in-component-copy? (defn in-component-copy?
"Check if the shape is inside a component non-main instance." "Check if the shape is inside a component non-main instance."
@ -156,7 +156,7 @@
(defn get-component-root (defn get-component-root
[component] [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 (:main-instance-id component)])
(get-in component [:objects (:id component)]))) (get-in component [:objects (:id component)])))

View file

@ -196,19 +196,21 @@
Also remove component-root of all children. Return the same structure Also remove component-root of all children. Return the same structure
as make-component-shape." as make-component-shape."
[root objects file-id] [root objects file-id]
(let [new-id (uuid/next) (let [new-id (uuid/next)
new-root (assoc root inside-component? (some? (get-instance-root objects root))
:component-id new-id new-root (cond-> (assoc root
:component-file file-id :component-id new-id
:component-root true :component-file file-id
:main-instance true) :main-instance true)
new-children (->> (cfh/get-children objects (:id root)) (not inside-component?)
(map #(dissoc % :component-root)))] (assoc :component-root true))
new-children (->> (cfh/get-children objects (:id root))
(map #(dissoc % :component-root)))]
[(assoc new-root :id new-id) [(assoc new-root :id new-id)
nil nil
(into [new-root] new-children)])) (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 "Clone the shape and all children. Generate new ids and detach
from parent and frame. Update the original shapes to have links from parent and frame. Update the original shapes to have links
to the new ones." to the new ones."
@ -280,7 +282,7 @@
component-shape (if components-v2 component-shape (if components-v2
(-> (get-shape component-page (:main-instance-id component)) (-> (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)) (assoc :frame-id uuid/zero))
(get-shape component (:id component))) (get-shape component (:id component)))
@ -333,7 +335,7 @@
:component-root true :component-root true
:name new-name) :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)))) (dissoc :component-root))))
[new-shape new-shapes _] [new-shape new-shapes _]

View file

@ -840,6 +840,9 @@
;; Remove layout-item properties when moving a shape outside a layout ;; Remove layout-item properties when moving a shape outside a layout
(cond-> (not (ctl/any-layout? objects frame-id)) (cond-> (not (ctl/any-layout? objects frame-id))
(pcb/update-shapes moving-shapes-ids ctl/remove-layout-item-data)) (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 moving-shapes-ids #(cond-> % (cfh/frame-shape? %) (assoc :hide-in-viewer true)))
(pcb/update-shapes shape-ids-to-detach ctk/detach-shape) (pcb/update-shapes shape-ids-to-detach ctk/detach-shape)
(pcb/change-parent frame-id moving-shapes drop-index) (pcb/change-parent frame-id moving-shapes drop-index)