🐛 Give a switched swap the grid cell of the child it replaces

A switch keeps a copy's swapped child: it moves the swap into the new
copy in place of the new child at its path and deletes that child. In a
grid copy whose cells were its main's, nothing carries the cells, so the
deleted child's cell stayed empty and the swap had no cell; the next
layout of the grid failed on the uncelled child.

`keep-swapped-item` gives the swap the cell of the child it replaces.

Benchmark (scripts/bench-components, ms, lower 2.5% quantile):

|                             :world | :baseline |   :now |   :mean | :ratio |
|------------------------------------+-----------+--------+---------+--------|
|  deepest-edit-through-three-levels |    35.215 | 22.929 |  23.421 |   0.65 |
| edit-through-a-chain-of-dependents |    68.931 | 41.595 |  45.427 |    0.6 |
|         library-update-into-copies |    83.083 | 47.657 |  49.963 |   0.57 |
|        main-edit-among-many-shapes |     4.837 |  2.118 |    2.33 |   0.44 |
|           main-edit-into-10-copies |      1.79 |  1.126 |   1.158 |   0.63 |
|          main-edit-into-100-copies |    15.415 | 10.291 |  11.027 |   0.67 |
|         main-edit-into-1000-copies |   153.086 | 101.89 | 104.528 |   0.67 |
|         variant-switch-into-copies |    32.052 | 21.203 |  22.033 |   0.66 |

AI-assisted-by: claude-opus-5-5
This commit is contained in:
alonso.torres 2026-11-09 19:09:36 +01:00
parent 4674812f62
commit 72304d023b
3 changed files with 51 additions and 0 deletions

View File

@ -324,6 +324,17 @@
(and (ctsl/any-layout-immediate-child? objects related-shape-in-new)
(not (ctsl/position-absolute? orig-swapped-child))))
;; and its cell in a grid parent
(cond-> (ctsl/grid-layout? current-parent)
(pcb/update-shapes
[(:id current-parent)]
(fn [parent]
(let [;; the child in place of the related shape
take-id #(if (= % (:id related-shape-in-new)) child-id %)]
(update parent :layout-grid-cells update-vals
(fn [cell] (update cell :shapes (partial mapv take-id))))))
{:ignore-touched true}))
;; Delete new non-swapped item
(cls/generate-delete-shapes ldata page objects (d/ordered-set (:id related-shape-in-new)) {:allow-altering-copies true})
second)))

View File

@ -1971,6 +1971,17 @@
:grid-empty false :layout-copies true :text true}]
:ops [[:nest 7 0 0 0] [:transform 3 3 0 0] [:switch 1 0 0 0] [:style 3 29 1]]}))))
(t/deftest switching-a-grid-copy-places-its-kept-swap-in-a-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 false :rich-variants true
:layout-children true :grid-empty false :layout-copies true :text true}]
:ops [[:combine 21 0 0 0] [:variant-property 15 4 0 0] [:switch 0] [:add-variant 4]
[:switch 0] [:make-variant 11 0 0 0] [:combine 4 0 0 0] [:make-variant 7 0 0 0]
[:nest 12 0 0 0] [:detach 0 0 0 0] [:delete 4] [:restore 4] [:make-variant 5]
[:switch 3] [:variant-property 0 0 0 0] [:switch 14] [:add-variant 0 0 0 0]
[:duplicate 1 0 0 0] [:rotate 7 0 0 0] [:variant-property 0 19 4 0]
[:switch 20]]}))))
(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

View File

@ -4121,6 +4121,35 @@
(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-places-a-kept-swap-in-the-cell-of-the-child-it-replaces
(let [;; ==== Setup: the copy swaps its Item 3, a copy of :icon-a, for :icon-b;
;; its cells are its main's
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 2]] :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)
file (thc/component-swap file :copy-item3 :icon-b :copy-icon-b)
swap-id (thi/id :copy-icon-b)
;; ==== 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 takes Green's cell for Item 3, and every
;; child of the copy has a cell
(t/is (not (ctk/touched-group? (ths/get-shape file :copy01) :layout-grid-cells)))
(t/is (some #{swap-id} (:shapes copy')))
(t/is (= [2 2] [(:row cell) (:column cell)]))
(t/is (= (set (:shapes copy')) (set (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