🐛 Order a switched grid copy's children by the cells it keeps

A nested grid copy with its own cells, switched to a variant with an
identical grid, carries those cells onto the new copy. Its children
kept the new main's order, so they no longer followed the flow of the
carried cells, as the children of a grid copy with its own cells do.

After carrying the cells, the switch reorders the new copy's children
by them, as the sync does for a copy with its own cells.

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

|                             :world | :baseline |   :now |   :mean | :ratio |
|------------------------------------+-----------+--------+---------+--------|
|  deepest-edit-through-three-levels |    35.215 | 20.163 |  20.973 |   0.57 |
| edit-through-a-chain-of-dependents |    68.931 | 39.635 |  41.769 |   0.57 |
|         library-update-into-copies |    83.083 | 49.886 |  50.993 |    0.6 |
|        main-edit-among-many-shapes |     4.837 |   2.05 |   2.334 |   0.42 |
|           main-edit-into-10-copies |      1.79 |   0.95 |   1.003 |   0.53 |
|          main-edit-into-100-copies |    15.415 |  9.468 |  10.043 |   0.61 |
|         main-edit-into-1000-copies |   153.086 | 98.029 | 103.338 |   0.64 |
|         variant-switch-into-copies |    32.052 | 21.263 |  22.422 |   0.66 |

AI-assisted-by: claude-opus-5-5
This commit is contained in:
alonso.torres 2026-11-09 22:16:52 +01:00
parent 777b489381
commit ceb142f7a7
3 changed files with 48 additions and 6 deletions

View File

@ -435,7 +435,8 @@
identical grids: the same tracks and the same cells holding
corresponding children, and they differ from the cells `new` shows.
Otherwise the new variant's cells win. `counterpart` gives the child of
`new` that relates to a child of `old`."
`new` that relates to a child of `old`. The children of `new` then follow
the flow of the carried cells."
[changes old old-ref new old-path new-path counterpart]
(let [identical? (and (= (:layout-grid-rows old-ref) (:layout-grid-rows new))
(= (:layout-grid-columns old-ref) (:layout-grid-columns new))
@ -458,11 +459,15 @@
(= (set (map #(dissoc % :id) (vals cells)))
(set (map #(dissoc % :id) (vals (:layout-grid-cells new))))))]
(if (and cells (not (shown? cells)))
(pcb/update-shapes changes [(:id new)]
#(-> %
(assoc :layout-grid-cells cells)
(update :touched (fnil conj #{}) :layout-grid-cells))
{:ignore-touched true})
(-> changes
(pcb/update-shapes [(:id new)]
#(-> %
(assoc :layout-grid-cells cells)
(update :touched (fnil conj #{}) :layout-grid-cells))
{:ignore-touched true})
(pcb/apply-changes-local)
;; the children follow the flow of the carried cells
(pcb/reorder-grid-children [(:id new)]))
changes)))
(defn generate-keep-touched

View File

@ -1991,6 +1991,13 @@
[:duplicate 1 0 0 0] [:rotate 7 0 0 0] [:variant-property 0 19 4 0]
[:switch 20]]}))))
(t/deftest switching-a-grid-copy-orders-its-children-as-its-kept-cells-flow
(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 [[:grid-direction 11 0 0 1] [:grid-cell 11 0 0 1] [:swap 16 0 0 0] [:make-variant 4]
[:switch 10] [:add-variant 4] [:switch 9]]}))))
(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

@ -4067,6 +4067,36 @@
(t/is (= [1 2] (item-cell file' copy' "Item 2")))
(t/is (ctk/touched-group? copy' :layout-grid-cells))))
(t/deftest a-switch-between-identical-grids-orders-the-children-as-the-kept-cells-flow
(let [file (-> (thf/sample-file :file1)
(ths/add-sample-shape :v01 :type :frame :is-variant-container true)
(add-grid-member :v01 :blue :blue-root "Blue" 3 [[1 1] [1 2] [1 3]])
(add-grid-member :v01 :green :green-root "Green" 3 [[1 1] [1 2] [1 3]])
(thc/instantiate-component :blue :copy01))
page (thf/current-page file)
copy (ths/get-shape file :copy01)
named (fn [item-name] (d/seek #(= item-name (:name (get (:objects page) %))) (:shapes copy)))
;; each item takes the cell of the one before it, Item 1 the cell of Item 3
traded {(named "Item 1") (named "Item 3") (named "Item 2") (named "Item 1") (named "Item 3") (named "Item 2")}
changes (-> (pcb/empty-changes nil (:id page))
(pcb/with-objects (:objects page))
(cls/generate-update-shapes #{(:id copy)}
(fn [shape]
(update shape :layout-grid-cells update-vals
(fn [cell] (update cell :shapes (partial mapv #(get traded % %))))))
(:objects page) {})
(pcb/apply-changes-local)
(pcb/reorder-grid-children [(:id copy)]))
file (thf/apply-changes file changes)
copy (ths/get-shape file :copy01)
file' (tho/swap-component-in-shape file :copy01 :green {:new-shape-label :copy02 :keep-touched? true})
copy' (ths/get-shape file' :copy02)]
(t/is (= [1 2] (item-cell file copy "Item 1")))
(t/is (= (:shapes copy) (:shapes (ctl/reorder-grid-children copy))))
(t/is (= [1 2] (item-cell file' copy' "Item 1")))
(t/is (= [1 1] (item-cell file' copy' "Item 3")))
(t/is (= (:shapes copy') (:shapes (ctl/reorder-grid-children copy'))))))
(t/deftest a-switch-to-a-different-grid-takes-the-new-variants-cells
(let [file (grid-variants-with-moved-copy)
file' (tho/swap-component-in-shape file :copy01 :wide {:new-shape-label :copy02 :keep-touched? true})