diff --git a/common/src/app/common/logic/variants.cljc b/common/src/app/common/logic/variants.cljc index 9d492213c7..5240863919 100644 --- a/common/src/app/common/logic/variants.cljc +++ b/common/src/app/common/logic/variants.cljc @@ -666,14 +666,16 @@ (defn swap-nesting-loop? "Whether swapping, or switching, the copy head `shape` of `objects` for a copy of the component `component-id` of library data `ldata` would nest a - component inside itself." + component inside itself, directly or through the mains of the components + that component holds in `ldata`." [objects shape ldata component-id] (let [component (ctkl/get-component ldata component-id true) page (ctf/get-component-page ldata component) - root (ctf/get-component-root ldata component)] + root (ctf/get-component-root ldata component) + parents (into #{} (keep :component-id) (cfh/get-parents-with-self objects (:parent-id shape)))] (boolean (and page root - (cfh/components-nesting-loop? - (cfh/get-children-with-self (:objects page) (:id root)) - (cfh/get-parents-with-self objects (:parent-id shape))))))) + (some parents + (ctn/held-component-ids (cfh/get-children-with-self (:objects page) (:id root)) + {(:id ldata) {:data ldata}})))))) diff --git a/common/test/common_tests/logic/variants_switch_target_test.cljc b/common/test/common_tests/logic/variants_switch_target_test.cljc index 58ca9ed299..9f8af79db6 100644 --- a/common/test/common_tests/logic/variants_switch_target_test.cljc +++ b/common/test/common_tests/logic/variants_switch_target_test.cljc @@ -95,6 +95,26 @@ (t/is (true? (loop? :nested))) (t/is (false? (loop? :copy01))))) +(t/deftest swap-through-a-held-main-is-a-nesting-loop + ;; :cy holds a copy of :cz whose own copy of :cx was swapped away; the + ;; main of :cz still holds :cx, which a reset brings back + (let [file (-> (thf/sample-file :file1) + (tho/add-simple-component :cw :mw :mw-child) + (tho/add-simple-component :cv :mv :mv-child) + (tho/add-frame :mx) + (thc/instantiate-component :cw :x-head :parent-label :mx) + (thc/make-component :cx :mx) + (tho/add-frame :mz) + (thc/instantiate-component :cx :z-x :parent-label :mz) + (thc/make-component :cz :mz) + (tho/add-frame :my) + (thc/instantiate-component :cz :y-z :parent-label :my :children-labels [:y-z-x]) + (thc/make-component :cy :my) + (thc/component-swap :y-z-x :cv :y-z-v)) + objects (:objects (thf/current-page file))] + (t/is (true? (clv/swap-nesting-loop? objects (ths/get-shape file :x-head) (:data file) (thi/id :cy)))) + (t/is (false? (clv/swap-nesting-loop? objects (ths/get-shape file :x-head) (:data file) (thi/id :cv)))))) + (t/deftest swap-in-place-keeps-index-and-layout-attrs (let [file (-> (thf/sample-file :file1) (thv/add-variant :v01 :c01 :m01 :c02 :m02)