🐛 Fill a grid copy's free cells in its references' layer order

An ungroup in a grid main ungroups its copies too. A copy with its own
cells gave the shapes the ungroup brings into it its free cells in the
reverse of its own layer order. That matches sync only when the copy
keeps the reverse of its references' order, as under a main, whose grid
the layout update reorders by its cells. A copy of a copy refers to a
copy, which keeps the order the ungroup gives, so its shapes took each
other's cells (F581).

The copy now ranks them by where their references sit in the grid it
refers to; a main's grid is ranked as `ctl/reorder-grid-children` leaves
it. Shapes whose reference is not there follow, in the reverse of the
copy's own order.

Test (composable-gen-test, fails before the fix; the earlier ungroup
tests pass):
`an-ungroup-fills-a-grid-copy-of-a-copy-in-its-references-layer-order`.

Benchmark (bench-components, alone):

|                             :world | :baseline |   :now |   :mean | :ratio |
|------------------------------------+-----------+--------+---------+--------|
|  deepest-edit-through-three-levels |    35.215 | 22.348 |  22.994 |   0.63 |
| edit-through-a-chain-of-dependents |    68.931 |  44.01 |  45.457 |   0.64 |
|         library-update-into-copies |    83.083 | 51.822 |  52.723 |   0.62 |
|        main-edit-among-many-shapes |     4.837 |  2.384 |   2.681 |   0.49 |
|           main-edit-into-10-copies |      1.79 |  1.107 |   1.139 |   0.62 |
|          main-edit-into-100-copies |    15.415 | 10.509 |  10.797 |   0.68 |
|         main-edit-into-1000-copies |   153.086 | 99.974 | 103.416 |   0.65 |
|         variant-switch-into-copies |    32.052 | 21.852 |  22.437 |   0.68 |

AI-assisted-by: claude-opus-5-5
This commit is contained in:
alonso.torres 2026-11-17 03:24:15 +01:00
parent 62257194c6
commit 8242ddbec7
2 changed files with 29 additions and 9 deletions

View File

@ -235,16 +235,21 @@
"`grid` with the cells of the children an ungroup brings into it assigned.
A copy with its own cells first takes the tracks of `ref-grid`, the grid
it refers to as the ungroup leaves it, on the axes it does not override;
then it gives its free cells to them in the layer order of its main, as
sync does, which is the reverse of the order the main's grid flows them
in."
then it gives its free cells to them in the layer order of their
references there, as sync adds them, and to the others in the reverse of
its own layer order."
[grid objects ref-grid]
(if (ctk/touched-group? grid :layout-grid-cells)
(-> grid
(cond-> (some? ref-grid) (with-ref-tracks ref-grid))
(update :shapes (comp vec reverse))
(ctl/assign-cells objects)
(assoc :shapes (:shapes grid)))
(let [;; where the reference of each child sits among the children of `ref-grid`
rank (into {} (map-indexed (fn [i id] [id i])) (:shapes ref-grid))]
(-> grid
(cond-> (some? ref-grid) (with-ref-tracks ref-grid))
(assoc :shapes (->> (:shapes grid)
reverse
(sort-by #(get rank (:shape-ref (get objects %)) ##Inf))
vec))
(ctl/assign-cells objects)
(assoc :shapes (:shapes grid))))
(ctl/assign-cells grid objects)))
(defn- ungroup-shape-changes
@ -298,7 +303,10 @@
(get-in pages-index [target-page-id :objects]))
changes (ungroup-shape-changes it target-page-id target-objects
id options ref-grids)
grid (ungrouped-grid changes target-objects id)]
grid (ungrouped-grid changes target-objects id)
;; the layout update after the ungroup orders the
;; children of a main's grid by their cells
grid (cond-> grid (and grid (nil? options)) (ctl/reorder-grid-children))]
[(cond-> changes-list changes (conj changes))
(cond-> ref-grids grid (assoc (:id grid) grid))]))
[[] {}])

View File

@ -2495,6 +2495,18 @@
:ops [[:variant-property 0 9 3 0] [:switch 4] [:combine 2] [:switch 8] [:style 11 7 1]
[:update-main 2 0 0 0]]}))))
(t/deftest an-ungroup-fills-a-grid-copy-of-a-copy-in-its-references-layer-order
;; a bool ungrouped in a grid main whose copy of a copy holds its own
;; cells: the copy's new shapes take its free cells in the layer order of
;; their references, which keep the order the ungroup gives them
(t/is (map? (cg/run {:world [3 2 {:variants true :rich-variants true :text true :layout :grid :layout-copies true
:layout-children true}]
:ops [[:add-child 0] [:structure 2 0 0] [:add-child 0] [:structure 0 0 0] [:delete 0]
[:restore 0] [:add-child 0] [:structure 3 4 15] [:grid-direction 15 0 0 1]
[:grid-cell 15 0 0 1] [:add-child 0] [:structure 12 0 0] [:add-child 0]
[:structure 13 0 0] [:detach 9 0 0 0] [:add-child 0] [:structure 0 0 0]
[:add-child 0] [:structure 9 2 0]]}))))
(t/deftest a-synced-masked-group-fits-the-mask-its-copy-turned
;; a copy turns the mask of a masked group; a sync gives the group its
;; main's geometry and then fits it to the turned mask again