diff --git a/common/src/app/common/logic/variants.cljc b/common/src/app/common/logic/variants.cljc index fb1677977d..715d48dd32 100644 --- a/common/src/app/common/logic/variants.cljc +++ b/common/src/app/common/logic/variants.cljc @@ -435,7 +435,8 @@ identical grids: the same tracks and the same cells holding corresponding children, and they differ from the cells `new` shows. Otherwise the new variant's cells win. `counterpart` gives the child of - `new` that relates to a child of `old`." + `new` that relates to a child of `old`. The children of `new` then follow + the flow of the carried cells." [changes old old-ref new old-path new-path counterpart] (let [identical? (and (= (:layout-grid-rows old-ref) (:layout-grid-rows new)) (= (:layout-grid-columns old-ref) (:layout-grid-columns new)) @@ -458,11 +459,15 @@ (= (set (map #(dissoc % :id) (vals cells))) (set (map #(dissoc % :id) (vals (:layout-grid-cells new))))))] (if (and cells (not (shown? cells))) - (pcb/update-shapes changes [(:id new)] - #(-> % - (assoc :layout-grid-cells cells) - (update :touched (fnil conj #{}) :layout-grid-cells)) - {:ignore-touched true}) + (-> changes + (pcb/update-shapes [(:id new)] + #(-> % + (assoc :layout-grid-cells cells) + (update :touched (fnil conj #{}) :layout-grid-cells)) + {:ignore-touched true}) + (pcb/apply-changes-local) + ;; the children follow the flow of the carried cells + (pcb/reorder-grid-children [(:id new)])) changes))) (defn generate-keep-touched diff --git a/common/test/common_tests/logic/composable_gen_test.cljc b/common/test/common_tests/logic/composable_gen_test.cljc index 46becccf7c..e7609cb884 100644 --- a/common/test/common_tests/logic/composable_gen_test.cljc +++ b/common/test/common_tests/logic/composable_gen_test.cljc @@ -1991,6 +1991,13 @@ [:duplicate 1 0 0 0] [:rotate 7 0 0 0] [:variant-property 0 19 4 0] [:switch 20]]})))) +(t/deftest switching-a-grid-copy-orders-its-children-as-its-kept-cells-flow + (t/is (map? (cg/run {:world [2 2 {:variants true :text-styles true :layout :grid :variant-diff false + :layout-item-margins false :grid-square false :rich-variants true + :layout-children true :grid-empty false :layout-copies true :text true}] + :ops [[:grid-direction 11 0 0 1] [:grid-cell 11 0 0 1] [:swap 16 0 0 0] [:make-variant 4] + [:switch 10] [:add-variant 4] [:switch 9]]})))) + (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 diff --git a/common/test/common_tests/logic/variants_switch_test.cljc b/common/test/common_tests/logic/variants_switch_test.cljc index 9d17e59730..f3843f3e22 100644 --- a/common/test/common_tests/logic/variants_switch_test.cljc +++ b/common/test/common_tests/logic/variants_switch_test.cljc @@ -4067,6 +4067,36 @@ (t/is (= [1 2] (item-cell file' copy' "Item 2"))) (t/is (ctk/touched-group? copy' :layout-grid-cells)))) +(t/deftest a-switch-between-identical-grids-orders-the-children-as-the-kept-cells-flow + (let [file (-> (thf/sample-file :file1) + (ths/add-sample-shape :v01 :type :frame :is-variant-container true) + (add-grid-member :v01 :blue :blue-root "Blue" 3 [[1 1] [1 2] [1 3]]) + (add-grid-member :v01 :green :green-root "Green" 3 [[1 1] [1 2] [1 3]]) + (thc/instantiate-component :blue :copy01)) + page (thf/current-page file) + copy (ths/get-shape file :copy01) + named (fn [item-name] (d/seek #(= item-name (:name (get (:objects page) %))) (:shapes copy))) + ;; each item takes the cell of the one before it, Item 1 the cell of Item 3 + traded {(named "Item 1") (named "Item 3") (named "Item 2") (named "Item 1") (named "Item 3") (named "Item 2")} + changes (-> (pcb/empty-changes nil (:id page)) + (pcb/with-objects (:objects page)) + (cls/generate-update-shapes #{(:id copy)} + (fn [shape] + (update shape :layout-grid-cells update-vals + (fn [cell] (update cell :shapes (partial mapv #(get traded % %)))))) + (:objects page) {}) + (pcb/apply-changes-local) + (pcb/reorder-grid-children [(:id copy)])) + file (thf/apply-changes file changes) + copy (ths/get-shape file :copy01) + file' (tho/swap-component-in-shape file :copy01 :green {:new-shape-label :copy02 :keep-touched? true}) + copy' (ths/get-shape file' :copy02)] + (t/is (= [1 2] (item-cell file copy "Item 1"))) + (t/is (= (:shapes copy) (:shapes (ctl/reorder-grid-children copy)))) + (t/is (= [1 2] (item-cell file' copy' "Item 1"))) + (t/is (= [1 1] (item-cell file' copy' "Item 3"))) + (t/is (= (:shapes copy') (:shapes (ctl/reorder-grid-children copy')))))) + (t/deftest a-switch-to-a-different-grid-takes-the-new-variants-cells (let [file (grid-variants-with-moved-copy) file' (tho/swap-component-in-shape file :copy01 :wide {:new-shape-label :copy02 :keep-touched? true})