diff --git a/common/src/app/common/logic/variants.cljc b/common/src/app/common/logic/variants.cljc index d5bd2b1298..0bbcca249c 100644 --- a/common/src/app/common/logic/variants.cljc +++ b/common/src/app/common/logic/variants.cljc @@ -7,6 +7,7 @@ [app.common.files.variant :as cfv] [app.common.geom.point :as gpt] [app.common.geom.rect :as grc] + [app.common.geom.shapes :as gsh] [app.common.logic.duplicate :as cld] [app.common.logic.libraries :as cll] [app.common.logic.shapes :as cls] @@ -513,11 +514,21 @@ ;; A swapped child is kept as it is, and the others carry their ;; touched attributes into their new child + new-head (get objects (:id new-shape)) + new-base-ref-shape (ctf/find-remote-shape (assoc container :objects objects) libraries new-head) + ;; the old main's shapes turned as the copy turns its new main, so + ;; the switch compares them with the new copy's in one frame + orient-ref (or (when new-base-ref-shape + (cll/orient-like new-base-ref-shape new-head + (gsh/shape->center new-base-ref-shape))) + identity) + 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 old-ref container))) + (clsw/update-attrs-on-switch changes new old new-shape original-shape + (orient-ref 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 7616c042db..1efe6d3322 100644 --- a/common/test/common_tests/logic/composable_gen_test.cljc +++ b/common/test/common_tests/logic/composable_gen_test.cljc @@ -1834,6 +1834,13 @@ :layout-children false :grid-empty false :layout-copies true :text true}] :ops [[:text 4 1 1 0] [:text-style 0 8 1] [:switch 1 0 0 0] [:switch 0 0 0 0]]})))) +(t/deftest switching-a-turned-copy-keeps-the-flip-its-child-inherits + (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 17 0 0 0] [:duplicate 9 0 0 0] [:make-variant 21 0 0 0] + [:transform 6 16 0 18] [:rotate 15 0 0 0] [:switch 14 0 0 0]]})))) + (t/deftest random-variant-compositions (let [coverage (atom {}) failure (atom nil)] diff --git a/common/test/common_tests/logic/variants_switch_test.cljc b/common/test/common_tests/logic/variants_switch_test.cljc index 9a3dca2fa5..af97ef4764 100644 --- a/common/test/common_tests/logic/variants_switch_test.cljc +++ b/common/test/common_tests/logic/variants_switch_test.cljc @@ -2394,6 +2394,32 @@ (t/is (= (:transform flipped) (:transform rect02'))) (t/is (gsh/close-attrs? :points (:points flipped) (:points rect02'))))) +(t/deftest test-switch-of-a-turned-copy-carries-the-flip-of-its-child + ;; the variants agree on the child, whose flip carries though the copy's + ;; turn sets its child apart from the main's + (let [file (-> (thf/sample-file :file1) + (thv/add-variant-with-child + :v01 :c01 :m01 :c02 :m02 :r01 :r02 + {:child1-params {:width 100 :height 50} + :child2-params {:width 100 :height 50}}) + (thc/instantiate-component :c01 :copy01 :children-labels [:copy-r01])) + update (fn [file ids f] + (let [page (thf/current-page file)] + (thf/apply-changes file (cls/generate-update-shapes (pcb/empty-changes nil (:id page)) + ids f (:objects page) {})))) + file (update file #{(thi/id :copy-r01)} + #(gsh/transform-shape % (ctm/resize-modifiers (gpt/point -1 1) (gsh/shape->center %)))) + center (gsh/shape->center (ths/get-shape file :copy01)) + file (update file #{(thi/id :copy01) (thi/id :copy-r01)} + #(gsh/transform-shape % (ctm/rotation-modifiers % center 45))) + turned (ths/get-shape file :copy-r01) + file' (tho/swap-component-in-shape file :copy01 :c02 {:new-shape-label :copy02 :keep-touched? true}) + rect02' (ths/get-shape-by-id file' (-> (ths/get-shape file' :copy02) :shapes first))] + (t/is (true? (:flip-x turned))) + (t/is (true? (:flip-x rect02'))) + (t/is (gsh/close-attrs? :transform (:transform turned) (:transform rect02'))) + (t/is (gsh/close-attrs? :points (:points turned) (:points rect02'))))) + (t/deftest test-switch-with-rotation-different-variants (let [;; ==== Setup file (-> (thf/sample-file :file1)