diff --git a/common/src/app/common/types/file.cljc b/common/src/app/common/types/file.cljc index 01d0976341..c748935610 100644 --- a/common/src/app/common/types/file.cljc +++ b/common/src/app/common/types/file.cljc @@ -476,7 +476,9 @@ `position` is that shape, found with context, or nil when it cannot be reached. The head refers to it, and is swapped while its component differs from the one of `position`; otherwise it is a plain copy of `position`, and - its descendants refer to the descendants of `position`." + its descendants refer to the descendants of `position`. A swapped + descendant that comes to refer to a shape of its own component is a plain + copy of it." [file libraries objects head ref-id position] (let [swapped? (or (nil? position) (not= (:component-id position) (:component-id head)) @@ -486,7 +488,12 @@ (keep #(:shape-ref (get objects %))) (cfh/get-children-ids objects (:id head)))) ref-map (when (seq main-ids) - (descendant-refs file libraries position main-ids))] + (descendant-refs file libraries position main-ids)) + ;; the shapes of the subtree of `position` + position-objects (:objects (:container (meta position))) + same-component? (fn [shape ref] + (and (= (:component-id ref) (:component-id shape)) + (= (:component-file ref) (:component-file shape))))] (fn [shape] (if (= (:id shape) (:id head)) (if swapped? @@ -494,7 +501,14 @@ (update :touched (fnil conj #{}) :swap-group)) (-> (assoc shape :shape-ref ref-id) (ctk/remove-swap-group))) - (d/update-when shape :shape-ref #(get ref-map % %)))))) + (let [shape-ref (get ref-map (:shape-ref shape))] + (cond-> shape + (some? shape-ref) + (assoc :shape-ref shape-ref) + + (and (some? shape-ref) (ctk/swapped? shape) + (same-component? shape (get position-objects shape-ref))) + (ctk/remove-swap-group))))))) (defn ref-outside-near-main? "Whether `shape` refers to a main shape that is not a child of the main diff --git a/common/test/common_tests/logic/composable_gen_test.cljc b/common/test/common_tests/logic/composable_gen_test.cljc index 1d5ad95306..a64fc434fc 100644 --- a/common/test/common_tests/logic/composable_gen_test.cljc +++ b/common/test/common_tests/logic/composable_gen_test.cljc @@ -2394,6 +2394,14 @@ :ops [[:delete] [:restore] [:combine 21] [:swap 24] [:switch 27] [:rotate 14] [:switch 11] [:combine 11] [:switch 6] [:switch 5] [:edit 16] [:edit 0 0 0 1] [:update-main 24]]})))) +(t/deftest switching-a-copy-takes-the-swap-of-a-nested-board-from-its-main + (t/is (map? (cg/run {:world [2 2 {:variants true :text-styles true :layout nil :variant-diff false + :layout-item-margins false :grid-square false :rich-variants true + :layout-children false :grid-empty false :layout-copies true :text true}] + :ops [[:delete 0 0 0 0] [:combine 6 0 0 0] [:add-variant 0] [:switch 1] [:add-variant 9 0 0 0] + [:combine 5] [:switch 0] [:variant-property 0 0 0 0] [:switch 1] [:combine 0] [:switch 4] + [:combine 0] [:switch 0]]})))) + (t/deftest random-shape-structure-compositions (let [coverage (atom {}) failure (atom nil)]