diff --git a/common/src/app/common/logic/variants.cljc b/common/src/app/common/logic/variants.cljc index 22316a0dbc..333afbc92a 100644 --- a/common/src/app/common/logic/variants.cljc +++ b/common/src/app/common/logic/variants.cljc @@ -556,11 +556,15 @@ ;; Cell overrides carry only between identical grids. new-paths (into {} (map (juxt :id :shape-path)) new-shapes-w-path) + ;; a kept swap takes the place of its new child with its own id + kept-swaps (into #{} (comp (filter :swapped?) (map (comp :id :old))) pairs) counterpart (fn [id] - (when-let [old-child (get page-objects id)] - (let [old-ref (find-shape-ref-child-of container libraries old-child - (:id orig-base-ref-shape))] - (:id (get new-shapes-map (get o-ref-shapes-p-map (:id old-ref))))))) + (if (contains? kept-swaps id) + id + (when-let [old-child (get page-objects id)] + (let [old-ref (find-shape-ref-child-of container libraries old-child + (:id orig-base-ref-shape))] + (:id (get new-shapes-map (get o-ref-shapes-p-map (:id old-ref)))))))) grid-pairs (filter (fn [{:keys [old new swapped?]}] (and (not swapped?) (ctsl/grid-layout? old) diff --git a/common/test/common_tests/logic/composable_gen_test.cljc b/common/test/common_tests/logic/composable_gen_test.cljc index 888ac5b8e4..cf1c2f98e7 100644 --- a/common/test/common_tests/logic/composable_gen_test.cljc +++ b/common/test/common_tests/logic/composable_gen_test.cljc @@ -1956,6 +1956,14 @@ [:library-add-variant 0 0 0 0] [:library-transform 0 3 4 0] [:library-add-child 5] [:library-sync] [:library-variant-property 0 0 0 0] [:library-sync] [:switch 10]]})))) +(t/deftest switching-a-grid-copy-places-its-kept-swap-in-its-cell + (t/is (map? (cg/run {:world [2 2 {:variants true :text-styles true :layout :grid :variant-diff false + :layout-item-margins false :grid-square true :rich-variants true + :layout-children false :grid-empty false :layout-copies true :text true}] + :ops [[:combine 14] [:switch 5] [:make-variant 6 0 0 0] [:make-variant 16] [:switch 1] + [:nest 19 0 0 0] [:grid-direction 9 0 3 0] [:nest 8 0 0 0] [:add-variant 0] + [:switch 12] [:make-variant 2] [:switch 14]]})))) + (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 598763316b..ade6912730 100644 --- a/common/test/common_tests/logic/variants_switch_test.cljc +++ b/common/test/common_tests/logic/variants_switch_test.cljc @@ -3958,13 +3958,16 @@ (defn- add-grid-member "Add to the variant `variant-label` the member `component-label`: a grid board `root-label` with two rows and `columns` columns, holding Item 1, - Item 2 and Item 3 at the `[row column]` of `cells`." - [file variant-label component-label root-label value columns cells] + Item 2 and Item 3 at the `[row column]` of `cells`. With `item3`, Item 3 + is a copy of that component." + [file variant-label component-label root-label value columns cells & {:keys [item3]}] (let [variant-id (thi/id variant-label) labels (mapv #(keyword (str (name root-label) "-item" %)) [1 2 3]) file (reduce (fn [file [index label]] - (ths/add-sample-shape file label :type :rect :name (str "Item " index) - :parent-label root-label)) + (if (and item3 (= index 3)) + (thc/instantiate-component file item3 label :parent-label root-label) + (ths/add-sample-shape file label :type :rect :name (str "Item " index) + :parent-label root-label))) (ths/add-sample-shape file root-label :type :frame :parent-label variant-label :variant-id variant-id :variant-name value :layout :grid :layout-grid-dir :row @@ -4042,6 +4045,52 @@ (t/is (= [1 1] (item-cell file' copy' "Item 1"))) (t/is (not (ctk/touched-group? copy' :layout-grid-cells))))) +(t/deftest a-switch-between-identical-grids-places-a-kept-swap-in-its-cell + (let [;; ==== Setup: the copy moves its Item 3, a copy of :icon-a, to row 2, + ;; column 2, and swaps it for :icon-b + file (-> (thf/sample-file :file1) + (tho/add-simple-component :icon-a :icon-a-root :icon-a-child) + (tho/add-simple-component :icon-b :icon-b-root :icon-b-child) + (ths/add-sample-shape :v01 :type :frame :is-variant-container true) + (add-grid-member :v01 :blue :blue-root "Blue" 2 [[1 1] [1 2] [2 1]] :item3 :icon-a) + (add-grid-member :v01 :green :green-root "Green" 2 [[1 1] [1 2] [2 1]] :item3 :icon-a) + (thc/instantiate-component :blue :copy01)) + page (thf/current-page file) + copy (ths/get-shape file :copy01) + item3 (d/seek #(:component-id (get (:objects page) %)) (:shapes copy)) + _ (thi/set-id! :copy-item3 item3) + moved (fn [cells] + (-> cells + (update-vals #(update % :shapes (fn [shapes] (filterv (partial not= item3) shapes)))) + (as-> cells + (let [[id _] (d/seek #(and (= 2 (:row (val %))) (= 2 (:column (val %)))) cells)] + (update cells id assoc :shapes [item3] :position :manual))))) + file (thf/apply-changes file (cls/generate-update-shapes (pcb/empty-changes nil (:id page)) + #{(:id copy)} + #(update % :layout-grid-cells moved) + (:objects page) {})) + cells (:layout-grid-cells (ths/get-shape file :copy01)) + file (thc/component-swap file :copy-item3 :icon-b :copy-icon-b) + ;; the swap takes the cell of the shape it replaces + file (ths/update-shape file :copy01 :layout-grid-cells + (update-vals cells #(update % :shapes (partial replace {item3 (thi/id :copy-icon-b)})))) + + swap-id (thi/id :copy-icon-b) + ;; the switch moves the labels, so read the copy before it + before (ctl/get-cell-by-shape-id (ths/get-shape file :copy01) swap-id) + + ;; ==== Action + file' (tho/swap-component-in-shape file :copy01 :green {:new-shape-label :copy02 :keep-touched? true}) + copy' (ths/get-shape file' :copy02) + cell (ctl/get-cell-by-shape-id copy' swap-id)] + + ;; ==== Check: the kept swap is in the cell the copy moved Item 3 to, and + ;; every cell holds children of the copy + (t/is (= [2 2] [(:row before) (:column before)])) + (t/is (some #{swap-id} (:shapes copy'))) + (t/is (= [2 2] [(:row cell) (:column cell)])) + (t/is (every? (set (:shapes copy')) (mapcat :shapes (vals (:layout-grid-cells copy'))))))) + (t/deftest a-switch-between-identical-grids-carries-no-cells-the-new-copy-shows ;; the copy's cells are marked as an override but equal its main's, so a ;; switch to an identical grid has nothing to carry