diff --git a/common/src/app/common/logic/libraries.cljc b/common/src/app/common/logic/libraries.cljc index 535ddbd47c..dc9907133e 100644 --- a/common/src/app/common/logic/libraries.cljc +++ b/common/src/app/common/logic/libraries.cljc @@ -1292,13 +1292,33 @@ take-position (ctf/take-position nil libraries objects new-head (:shape-ref replaced) position) position-objects (get-in (meta position) [:container :objects]) ;; the head takes the place the replaced child authored - placement (set/intersection (set (:touched replaced)) placement-groups)] + placement (set/intersection (set (:touched replaced)) placement-groups) + positioned (mapv take-position new-shapes) + by-id (d/index-by :id positioned) + head (get by-id (:id new-head)) + ;; the position's subtree placed like a head that is a plain copy of it + placed (when (and position (not (ctk/swapped? head))) + (placed-like position-objects position head + (authored-constraints by-id (cfh/get-children-ids by-id (:id head))))) + ;; the shapes that keep their pushed geometry: the head and the subtrees of those that author theirs + kept-ids (into #{(:id head)} + (comp (filter #(ctk/touched-group? % :geometry-group)) + (mapcat #(cfh/get-children-ids-with-self by-id (:id %)))) + positioned) + follow-position (fn [shape] + (let [expected (get placed (:shape-ref shape))] + (if (and expected + (not (contains? kept-ids (:id shape))) + (= (:type shape) (:type expected))) + (merge shape (select-keys expected (cond-> placement-geometry-attrs + (= :path (:type shape)) (conj :content)))) + shape)))] (mapv (fn [shape] - (let [shape (cond-> (take-position shape) + (let [shape (cond-> (follow-position shape) (and (= (:id shape) (:id new-head)) (seq placement)) (update :touched (fnil into #{}) placement))] (author-pushed-content shape (get position-objects (:shape-ref shape))))) - new-shapes)) + positioned)) new-shapes) ;; the link of a copy shape with its main diff --git a/common/test/common_tests/logic/composable_gen_test.cljc b/common/test/common_tests/logic/composable_gen_test.cljc index e1e72d19e6..60bdf17dc2 100644 --- a/common/test/common_tests/logic/composable_gen_test.cljc +++ b/common/test/common_tests/logic/composable_gen_test.cljc @@ -2095,6 +2095,14 @@ :ops [[:detach 2] [:switch 8] [:text 4] [:switch] [:nest 2] [:rotate] [:switch] [:combine] [:switch 2]]})))) +(t/deftest updating-a-main-with-a-swap-to-its-position-component-places-its-children-like-the-position + (t/is (map? (cg/run {:world [1 2 {:variants true :text-styles false :layout :flex :variant-diff false + :layout-item-margins false :grid-square false :rich-variants true + :layout-children false :grid-empty false :layout-copies false :text false}] + :ops [[:delete] [:add-variant] [:make-variant 1] [:switch 19] [:style 18 29] + [:add-variant] [:add-variant] [:add-variant] [:switch 4] [:transform 0 0 0 7] + [:update-main 22]]})))) + (t/deftest turning-a-flipped-copy-turns-a-child-stored-as-the-other-mirror-turn (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