🐛 Give a grid copy its main's tracks when an ungroup fills it

Ungrouping a board in a grid main ungroups its copies alike. A copy with
its own cells placed the shapes the ungroup brought in by its own flow,
which adds tracks along its direction: a copy flowing in columns grew a
row its main did not have, so tracks it never overrode stopped following
its main's.

A copy with its own cells now first takes the tracks the ungroup left in
the grid it refers to, on the axes it does not override, then gives its
free cells to the new shapes in its main's layer order.

Benchmark (bench-components, run alone):

| world                              | baseline |     now |    mean | ratio |
|------------------------------------+----------+---------+---------+-------|
| deepest-edit-through-three-levels  |   35.215 |  22.032 |  22.764 |  0.63 |
| edit-through-a-chain-of-dependents |   68.931 |  43.547 |  51.026 |  0.63 |
| library-update-into-copies         |   83.083 |  52.297 |  57.441 |  0.63 |
| main-edit-among-many-shapes        |    4.837 |   2.474 |   4.362 |  0.51 |
| main-edit-into-10-copies           |    1.79  |   1.091 |   1.162 |  0.61 |
| main-edit-into-100-copies          |   15.415 |  11.465 |  12.265 |  0.74 |
| main-edit-into-1000-copies         |  153.086 | 111.337 | 115.898 |  0.73 |
| variant-switch-into-copies         |   32.052 |  22.186 |  27.148 |  0.69 |

AI-assisted-by: claude-opus-5-5
This commit is contained in:
alonso.torres 2026-11-14 19:52:12 +01:00
parent e1c70cd39e
commit ba37f85ad6
2 changed files with 63 additions and 16 deletions

View File

@ -212,14 +212,32 @@
(recur (concat (rest pending) (map second hits)) (into result hits)))
result))))
(defn- with-ref-tracks
"`grid` with the tracks of `ref-grid` on the axes it does not override,
adding the cells of the tracks it lacks."
[grid ref-grid]
(letfn [(follow [grid attr add-track]
(let [tracks (get ref-grid attr)
own (get grid attr)]
(if (or (ctk/touched-group? grid attr) (< (count tracks) (count own)))
grid
(-> (reduce add-track grid (drop (count own) tracks))
(assoc attr tracks)))))]
(-> grid
(follow :layout-grid-rows ctl/add-grid-row)
(follow :layout-grid-columns ctl/add-grid-column))))
(defn- assign-ungrouped-cells
"`grid` with the cells of the children an ungroup brings into it assigned.
A copy with its own cells 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."
[grid objects]
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."
[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)))
@ -228,8 +246,8 @@
(defn- ungroup-shape-changes
"The changes that ungroup the group, bool or board `shape-id` of the
page `page-id`, whose shapes are `objects`; `options` of the moves and
removals."
[it page-id objects shape-id options]
removals. `ref-grids` holds the grids the ungroups before left, by id."
[it page-id objects shape-id options ref-grids]
(let [shape (get objects shape-id)
changes
(cond
@ -241,9 +259,19 @@
(cond-> changes
(and changes (ctl/grid-layout? objects (:parent-id shape)))
(pcb/update-shapes [(:parent-id shape)] assign-ungrouped-cells
(pcb/update-shapes [(:parent-id shape)]
(fn [grid objects]
(assign-ungrouped-cells grid objects (get ref-grids (:shape-ref grid))))
(merge {:with-objects? true} (select-keys options [:ignore-touched]))))))
(defn- ungrouped-grid
"The grid board that held the shape `shape-id` of `objects`, as `changes`
leave it, or nil."
[changes objects shape-id]
(let [parent-id (:parent-id (get objects shape-id))]
(when (and changes (ctl/grid-layout? objects parent-id))
(get (pcb/get-objects (pcb/apply-changes-local changes)) parent-id))))
(defn generate-ungroup-changes
"The changes that ungroup the groups, bools and boards of `ids`, which
`ungroup-ids` gives. With the file's `pages-index`, the copies of each
@ -253,15 +281,24 @@
(generate-ungroup-changes it page-id objects ids nil))
([it page-id objects ids pages-index]
(let [copies (copies-of pages-index ids)
changes-list (concat
(keep #(ungroup-shape-changes it page-id objects % nil) ids)
(keep (fn [[copy-page-id id]]
(let [copy-objects (if (= copy-page-id page-id)
objects
(get-in pages-index [copy-page-id :objects]))]
(ungroup-shape-changes it copy-page-id copy-objects id
{:allow-altering-copies true :ignore-touched true})))
copies))]
;; mains first, then each copy after the shape it refers to, so a
;; copy's grid finds the one it refers to as its ungroup left it
targets (concat (map (fn [id] [page-id id nil]) ids)
(map (fn [[copy-page-id id]]
[copy-page-id id {:allow-altering-copies true :ignore-touched true}])
copies))
changes-list (->> targets
(reduce (fn [[changes-list ref-grids] [target-page-id id options]]
(let [target-objects (if (= target-page-id page-id)
objects
(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)]
[(cond-> changes-list changes (conj changes))
(cond-> ref-grids grid (assoc (:id grid) grid))]))
[[] {}])
first)]
{:redo-changes (vec (mapcat :redo-changes changes-list))
:undo-changes (vec (mapcat :undo-changes changes-list))
:origin it})))

View File

@ -4308,6 +4308,16 @@
:grid-empty false :layout-copies false :text true}]
:ops [[:switch 11] [:switch 5] [:nest 17] [:add-child 15] [:rotate 15] [:style 12 9 1] [:paste 12]]}))))
(t/deftest generated-an-ungroup-gives-a-grid-copy-with-its-own-cells-its-main-tracks
;; a grid copy with its own cells and direction but its main's tracks
;; takes the tracks an ungroup in the main adds before placing the shapes
;; it brings in
(t/is (map? (cg/run {:world [3 1 {:variants true :rich-variants true :text true :layout :grid
:layout-copies true :layout-children true}]
:ops [[:detach 0 0 0 0] [:detach 17 0 0 0] [:grid-direction 2 0 0 1]
[:grid-cell 2 0 0 1] [:grid-direction 12 0 3 0] [:add-child 0]
[:structure 7 2 0]]}))))
(t/deftest generated-an-ungroup-fills-a-grid-copy-with-its-own-cells-in-main-layer-order
;; shapes an ungroup in a grid main brings into a copy with its own cells
;; take its free cells in the main's layer order, as sync gives them