diff --git a/common/src/app/common/logic/libraries.cljc b/common/src/app/common/logic/libraries.cljc index c589d9bb70..d8dc915ed6 100644 --- a/common/src/app/common/logic/libraries.cljc +++ b/common/src/app/common/logic/libraries.cljc @@ -736,22 +736,12 @@ (defn update-main-nesting-loop? "Whether updating the main of the copy head `shape-id` of `container` from the copy would nest its component inside itself: the copy holds a copy of - its component, or of a component whose main holds one." + its component, or of a component whose main holds one, through every + level." [libraries container shape-id] - (let [component-id (:component-id (ctn/get-shape container shape-id)) - holds? (fn [{:keys [component-file] :as head}] - (let [library (dm/get-in libraries [component-file :data]) - component (ctkl/get-component library (:component-id head) true) - root (when component (ctf/get-component-root library component))] - (and root - (some #(= component-id (:component-id %)) - (cfh/get-children (:objects (ctf/get-component-container library component)) - (:id root))))))] - (boolean - (some (fn [shape] - (and (:component-id shape) - (or (= component-id (:component-id shape)) (holds? shape)))) - (cfh/get-children (:objects container) shape-id))))) + (let [component-id (:component-id (ctn/get-shape container shape-id))] + (contains? (ctn/held-component-ids (cfh/get-children (:objects container) shape-id) libraries) + component-id))) (defn generate-sync-shape-inverse "Generate changes to update the component a shape is linked to, from 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 9f8af79db6..b82013cd6c 100644 --- a/common/test/common_tests/logic/variants_switch_target_test.cljc +++ b/common/test/common_tests/logic/variants_switch_target_test.cljc @@ -8,6 +8,7 @@ (:require [app.common.data :as d] [app.common.files.changes-builder :as pcb] + [app.common.logic.libraries :as cll] [app.common.logic.variants :as clv] [app.common.test-helpers.components :as thc] [app.common.test-helpers.compositions :as tho] @@ -115,6 +116,33 @@ (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 update-main-through-a-held-main-is-a-nesting-loop + ;; a copy of :ck holds :ca; :ca's copy of :cb and :cb's copy of :cc each + ;; swapped their head away, but the main of :cc still holds :ck, so + ;; updating :ck from the copy would loop + (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 :mk) + (thc/instantiate-component :cw :k-head :parent-label :mk) + (thc/make-component :ck :mk) + (tho/add-frame :mc) + (thc/instantiate-component :ck :c-k :parent-label :mc) + (thc/make-component :cc :mc) + (tho/add-frame :mb) + (thc/instantiate-component :cc :b-c :parent-label :mb :children-labels [:b-c-k]) + (thc/make-component :cb :mb) + (thc/component-swap :b-c-k :cv :b-c-v) + (tho/add-frame :ma) + (thc/instantiate-component :cb :a-b :parent-label :ma :children-labels [:a-b-c]) + (thc/make-component :ca :ma) + (thc/component-swap :a-b-c :cv :a-b-v) + (thc/instantiate-component :ck :copyk :children-labels [:copyk-head]) + (thc/component-swap :copyk-head :ca :copyk-a)) + objects (:objects (thf/current-page file))] + (t/is (true? (cll/update-main-nesting-loop? (libraries file) {:objects objects} (thi/id :copyk)))) + (t/is (false? (cll/update-main-nesting-loop? (libraries file) {:objects objects} (thi/id :a-b)))))) + (t/deftest swap-in-place-keeps-index-and-layout-attrs (let [file (-> (thf/sample-file :file1) (thv/add-variant :v01 :c01 :m01 :c02 :m02)