From 5284bd1f9f2ea4cd54a3c7a4f7d68758e0d63508 Mon Sep 17 00:00:00 2001 From: "alonso.torres" Date: Mon, 9 Nov 2026 10:16:03 +0100 Subject: [PATCH] :bug: Keep a switched swap in the grid cell its copy gives it A switch between identical grids carries a copy's cell overrides, with each child mapped to the new copy's child at its path. A kept swap takes that place with its own id, so the cells named the child the switch removes and the swap had no cell. The kept swap now keeps its own id in the cells. Benchmark (scripts/bench-components, ms, lower 2.5% quantile): | :world | :baseline | :now | :mean | :ratio | |------------------------------------+-----------+--------+---------+--------| | deepest-edit-through-three-levels | 35.215 | 21.301 | 21.507 | 0.6 | | edit-through-a-chain-of-dependents | 68.931 | 38.218 | 42.273 | 0.55 | | library-update-into-copies | 83.083 | 46.586 | 48.732 | 0.56 | | main-edit-among-many-shapes | 4.837 | 1.842 | 2.032 | 0.38 | | main-edit-into-10-copies | 1.79 | 0.924 | 0.968 | 0.52 | | main-edit-into-100-copies | 15.415 | 9.135 | 9.733 | 0.59 | | main-edit-into-1000-copies | 153.086 | 99.654 | 101.131 | 0.65 | | variant-switch-into-copies | 32.052 | 20.939 | 22.049 | 0.65 | Common suite: 2379 tests, 1 failure, the known insufficient-random-coverage of random-stale-library-compositions (detach attempted once in 20 programs). AI-assisted-by: claude-opus-5-5 --- common/src/app/common/logic/variants.cljc | 12 ++-- .../logic/composable_gen_test.cljc | 8 +++ .../logic/variants_switch_test.cljc | 57 +++++++++++++++++-- 3 files changed, 69 insertions(+), 8 deletions(-) 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