diff --git a/common/src/app/common/types/container.cljc b/common/src/app/common/types/container.cljc index 46d3a2de23..558c6d0bd6 100644 --- a/common/src/app/common/types/container.cljc +++ b/common/src/app/common/types/container.cljc @@ -499,6 +499,45 @@ [shape libraries] (get-in libraries [(:component-file shape) :data :components (:component-id shape)])) +(defn- main-shapes + "The shapes of the main of `component` in the file data `data`: its main + instance on a page, or the shapes a deleted component keeps." + [data component] + (if (:deleted component) + (vals (:objects component)) + (cfh/get-children-with-self + (get-in data [:pages-index (:main-instance-page component) :objects]) + (:main-instance-id component)))) + +(defn held-component-ids + "Ids of the components of the copies and mains among `shapes`, and of the + components the mains of these hold, through every level. A sync or a + reset brings back what a main holds, even where a copy swapped it." + [shapes libraries] + (loop [pending (filterv :component-id shapes) + ids #{}] + (if-let [shape (peek pending)] + (let [id (:component-id shape)] + (if (contains? ids id) + (recur (pop pending) ids) + (let [data (get-in libraries [(:component-file shape) :data]) + component (get-in data [:components id])] + (recur (cond-> (pop pending) + (some? component) + (into (filter :component-id) (main-shapes data component))) + (conj ids id))))) + ids))) + +(defn- nesting-loop? + "Whether nesting `children` below `parent` would hold a component inside + its own main, directly or through the mains of the components it holds." + [objects parent children libraries] + (let [parent-ids (into #{} (keep :component-id) (cfh/get-parents-with-self objects (:id parent)))] + (and (seq parent-ids) + (boolean (some parent-ids + (held-component-ids (mapcat #(cfh/get-children-with-self objects (:id %)) children) + libraries)))))) + (defn invalid-structure-for-component? "Check if the structure generated nesting children in parent is invalid in terms of nested components: a change of a copy's structure, a main inside @@ -522,9 +561,7 @@ selected-main-instance? (seq selected-components) parent-in-component? (in-any-component? objects parent) - comps-nesting-loop? (not (->> children - (map #(cfh/components-nesting-loop? objects (:id %) (:id parent))) - (every? nil?)))] + comps-nesting-loop? (nesting-loop? objects parent children libraries)] (or ;;We don't want to change the structure of component copies (ctk/in-component-copy? parent) @@ -532,7 +569,8 @@ ;; If we are moving something containing a main instance the container can't be part of a component (neither main nor copy) (and selected-main-instance? parent-in-component?) ;; Avoid a component nesting loop: a main inside itself, or a copy - ;; inside its own main. A plain shape moved into its own subtree passes; + ;; inside its own main or the main of a component it holds. A plain + ;; shape moved into its own subtree passes; ;; the move itself (`relocate-shapes`) refuses it. comps-nesting-loop?))) diff --git a/common/test/common_tests/logic/composable_gen_test.cljc b/common/test/common_tests/logic/composable_gen_test.cljc index dc6a431f96..1b1d486a93 100644 --- a/common/test/common_tests/logic/composable_gen_test.cljc +++ b/common/test/common_tests/logic/composable_gen_test.cljc @@ -965,3 +965,11 @@ (t/is (map? (cg/run {:world [2 1 {:text true :text-styles true :layout :flex :layout-children true}] :ops [[:remove-child 0 0 0 0] [:nest 0 0 0 0] [:style 2 27 1] [:edit 0 1 1 :copy-head] [:reset 0] [:nest 0 0 0 0]]})))) + +(t/deftest generated-nest-refuses-a-loop-through-a-switched-copy + ;; a copy whose nested head was switched away from Board is nested into + ;; Board's main; the main of that head still holds a Board, which a reset + ;; brings back, so the nest would loop + (t/is (map? (cg/run {:world [2 0 {:variants true :text-styles true :library true :rich-variants true + :layout-copies true :text true}] + :ops [[:switch 3 0 0 0] [:nest 9 0 0 0] [:edit 7 1 1 :copy-head] [:reset 7]]})))) diff --git a/common/test/common_tests/types/container_test.cljc b/common/test/common_tests/types/container_test.cljc index f88bde84fd..64c554e234 100644 --- a/common/test/common_tests/types/container_test.cljc +++ b/common/test/common_tests/types/container_test.cljc @@ -410,6 +410,13 @@ (t/testing "a copy does not go inside its own main" (t/is (= [:root :root] (place file :main1 [:copy1] :pasting? true))) (t/is (= [:main2 :main2] (place file :main2 [:copy1] :pasting? true)))) + (t/testing "a copy does not go inside the main of a component its main holds, though it swapped it" + (let [file (-> file + (tho/add-nested-component :n1 :nmain1 :nmain1-child :n2 :nmain2 :nested-head) + (thc/instantiate-component :n2 :ncopy2 :children-labels [:ncopy2-head]) + (thc/component-swap :ncopy2-head :c2 :ncopy2-swapped))] + (t/is (= [:root :root] (place file :nmain1 [:ncopy2]))) + (t/is (= [:board :board] (place file :board [:ncopy2]))))) (t/testing "a main does not go inside itself, even pasted" (t/is (= [:root :root] (place file :main1-child [:main1]))) (t/is (= [:root :root] (place file :main1-child [:main1] :pasting? true))))