🐛 Give a grid copy's children free cells in the order of their mains

A grid copy that authors its cells gives its children without a cell
free cells at sync. It took them in the copy's order of its children,
which grid reordering changes, so a child given back by a shrunk track
and a child new from the main could take each other's cells. They now
take free cells in the order of the main shapes they refer to, as sync
adds children.

Test: a-grid-copy-gives-free-cells-to-children-in-their-main-order.
Grid, sync and golden master tests pass (399).

Benchmark (alone):

|                             :world | :baseline |    :now |   :mean | :ratio |
|------------------------------------+-----------+---------+---------+--------|
|  deepest-edit-through-three-levels |    35.215 |   21.19 |   22.83 |    0.6 |
| edit-through-a-chain-of-dependents |    68.931 |  42.408 |  44.279 |   0.62 |
|         library-update-into-copies |    83.083 |  53.897 |  55.394 |   0.65 |
|        main-edit-among-many-shapes |     4.837 |   2.367 |   2.548 |   0.49 |
|           main-edit-into-10-copies |      1.79 |   1.031 |   1.103 |   0.58 |
|          main-edit-into-100-copies |    15.415 |  10.483 |  10.835 |   0.68 |
|         main-edit-into-1000-copies |   153.086 | 100.626 | 107.239 |   0.66 |
|         variant-switch-into-copies |    32.052 |  23.355 |  23.926 |   0.73 |

AI-assisted-by: claude-opus-5-5
This commit is contained in:
alonso.torres 2026-11-13 12:04:48 +01:00
parent 73b4e0795b
commit 782623c935
2 changed files with 21 additions and 4 deletions

View File

@ -2322,11 +2322,22 @@
(or (> (:row cell) rows) (> (:column cell) cols)))))
cells)))))
(defn- assign-in-main-order
"`shape` with its children without a cell assigned free cells in the order
of the main shapes they refer to in `shape-main`, as sync adds them."
[shape objects shape-main]
(let [rank (into {} (map-indexed (fn [index id] [id index])) (:shapes shape-main))
main-order (vec (sort-by #(get rank (:shape-ref (get objects %)) ##Inf) (:shapes shape)))]
(-> shape
(assoc :shapes main-order)
(ctsl/assign-cells objects)
(assoc :shapes (:shapes shape)))))
(defn- assign-inherited-grid-cells
[shape before objects]
[shape before objects shape-main]
(if-not (or (< (count (:layout-grid-rows shape)) (count (:layout-grid-rows before)))
(< (count (:layout-grid-columns shape)) (count (:layout-grid-columns before))))
(ctsl/assign-cells shape objects)
(assign-in-main-order shape objects shape-main)
(let [active? (some #(not (ctsl/position-absolute? objects %)) (:shapes shape))
;; Like assign-cells, a grid with participating children needs at
;; least one track per axis, including an initially empty grid.
@ -2341,7 +2352,7 @@
(ctsl/check-deassigned-cells objects)
(ctsl/fit-cells-to-tracks)
;; a child without a free slot grows a default track to hold it
(ctsl/assign-cells objects)))))
(assign-in-main-order objects shape-main)))))
(defn- keep-cell-ids
"`shape` with the cells its tracks add, those its main `shape-main` lacks,
@ -2439,7 +2450,7 @@
(-> shape-copy
(assoc :layout-grid-cells copy-cells)
(add-inherited-grid-cells shape-before)
(assign-inherited-grid-cells shape-before objects))
(assign-inherited-grid-cells shape-before objects shape-main))
;; Take cells from main and remap the shapes to assign it to the copy
(copy-grid-cells shape-copy shape-main objects))))
{:ignore-touched true :with-objects? true})))

View File

@ -2592,6 +2592,12 @@
:layout-copies false :text false}]
:ops [[:grid-cell 0 15 0 1] [:grid-track 0 2]]}))))
(t/deftest a-grid-copy-gives-free-cells-to-children-in-their-main-order
(t/is (map? (cg/run {:world [3 1 {:variants false :layout :grid :layout-item-margins false :grid-square false
:rich-variants false :layout-children true :grid-empty false
:layout-copies false :text true}]
:ops [[:grid-cell 13] [:grid-track 0 0 11] [:grid-track 0 3] [:add-child 8] [:grid-track 0 2]]}))))
(t/deftest settling-a-bool-in-a-grid-copy-twice-keeps-its-shapes-in-place
(t/is (map? (cg/run {:world [3 0 {:variants true :rich-variants true :text true :layout :grid
:layout-copies true :layout-children true}]