diff --git a/common/src/app/common/logic/variants.cljc b/common/src/app/common/logic/variants.cljc index 333afbc92a..ca1d2228b6 100644 --- a/common/src/app/common/logic/variants.cljc +++ b/common/src/app/common/logic/variants.cljc @@ -544,13 +544,30 @@ (cll/orient-like new-base-ref-shape new-head (gsh/shape->center new-base-ref-shape))) identity) + ;; `ref-shape` turned too as the copy turns the parent of `old` below + ;; its root, so a turn a nested copy authors is not the child's own + nested-turn (fn [ref-shape old old-ref] + (let [ref-objects (:objects (:container (meta old-ref))) + ref-root (get ref-objects (:id orig-base-ref-shape)) + ref-parent (get ref-objects (:parent-id old-ref)) + old-parent (get page-objects (:parent-id old)) + root-turn (when (and ref-root ref-parent) + (cll/orient-like ref-root original-shape + (gsh/shape->center ref-root))) + turned (when ref-parent + (cond-> ref-parent (some? root-turn) root-turn)) + turn (when (and turned old-parent + (not= (:id old-parent) (:id original-shape))) + (cll/orient-like turned old-parent (gsh/shape->center turned)))] + (cond-> ref-shape (some? turn) turn))) changes (reduce (fn [changes {:keys [old old-ref new swapped?]}] (if swapped? (keep-swapped-item changes new old ldata page libraries) (clsw/update-attrs-on-switch changes new old new-shape original-shape - (orient-ref old-ref) container))) + (nested-turn (orient-ref old-ref) old old-ref) + container))) changes pairs) diff --git a/common/test/common_tests/logic/composable_gen_test.cljc b/common/test/common_tests/logic/composable_gen_test.cljc index 5da08dfa4e..3667982bf6 100644 --- a/common/test/common_tests/logic/composable_gen_test.cljc +++ b/common/test/common_tests/logic/composable_gen_test.cljc @@ -1982,6 +1982,14 @@ [:duplicate 1 0 0 0] [:transform 1 4 0 4] [:variant-property 0 0 0 0] [:switch 6] [:rotate 10 0 0 0] [:rotate 1 0 0 0]]})))) +(t/deftest switching-a-copy-places-the-child-of-its-turned-nested-copy + (t/is (map? (cg/run {:world [3 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 [[:nest 15 0 0 0] [:resize 9 0 0 0] [:nest 7 0 0 0] [:combine 4 0 0 0] + [:make-variant 2 0 0 0] [:make-variant 6 0 0 0] [:swap 3 0 0 0] + [:transform 14 3 0 13] [:make-variant 4] [:switch 9]]})))) + (t/deftest updating-a-main-keeps-a-pushed-swap-its-main-converged-to (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 diff --git a/common/test/common_tests/logic/variants_switch_test.cljc b/common/test/common_tests/logic/variants_switch_test.cljc index ade6912730..150bc32f41 100644 --- a/common/test/common_tests/logic/variants_switch_test.cljc +++ b/common/test/common_tests/logic/variants_switch_test.cljc @@ -71,6 +71,36 @@ (t/is (= (:data (tm/file before)) (:data (tm/file (r/step switched (n/undo))))))))) +(t/deftest switch-places-the-child-of-a-turned-nested-copy-by-its-parent + ;; the variants hold a copy whose child a main of its chain resized, and the + ;; switched copy turns that copy: the child follows its turned parent + (let [file (-> (thf/sample-file :file) + (tho/add-frame :leaf-main {:width 50 :height 50}) + (thc/make-component :leaf :leaf-main) + (tho/add-frame :holder-main {:width 100 :height 100}) + (thc/instantiate-component :leaf :holder-leaf :parent-label :holder-main) + (thc/make-component :holder :holder-main) + (thv/add-variant :variants :a :main-a :b :main-b + :variant1-params {:width 100 :height 100} + :variant2-params {:width 100 :height 100}) + (thc/instantiate-component :holder :a-holder :parent-label :main-a) + (thc/instantiate-component :holder :b-holder :parent-label :main-b) + (thc/instantiate-component :a :copy)) + nested (fn [situation] (first (:shapes (tm/resolve-shape situation :copy)))) + before (-> (tm/make-situation file) + (r/step (n/change-height :holder-leaf 40)) r/settle + (r/step (n/rotate nested 45)) r/settle) + switched (-> before (r/step (n/switch-variant :copy "Value2")) r/settle) + holder (tm/shape-by-id switched (nested switched)) + leaf (tm/shape-by-id switched (first (:shapes holder))) + file (tm/file switched) + full (:file (r/sync-cascade file {} (keys (get-in file [:data :components]))))] + (t/is (mth/close? 45 (:rotation holder) 0.001)) + (t/is (not (ctk/touched-group? leaf :geometry-group))) + (t/is (mth/close? 45 (:rotation leaf) 0.001)) + (t/is (gsh/close-attrs? :points (:points leaf) + (:points (ths/get-shape-by-id full (:id leaf))) 0.001)))) + (t/deftest switching-an-untouched-rotated-copy-authors-its-retained-orientation (let [file (-> (thf/sample-file :file) (thv/add-variant :variant :c01 :m01 :c02 :m02