🐛 Give a switched head a grid cell when it joins the grid's flow

A copy of a hidden variant sits in a grid without a cell, as a hidden
child takes no part in the grid's flow. Switching it to a shown variant
put the parent's cells back as they were, so the shown head had no cell
and the next reflow of the grid failed.

The switch now assigns the parent grid's cells again when the new head
enters or leaves the flow, as a sync of the head does.

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

|                             :world | :baseline |    :now |   :mean | :ratio |
|------------------------------------+-----------+---------+---------+--------|
|  deepest-edit-through-three-levels |    35.215 |  19.977 |  21.701 |   0.57 |
| edit-through-a-chain-of-dependents |    68.931 |   39.77 |  41.277 |   0.58 |
|         library-update-into-copies |    83.083 |   52.24 |  53.439 |   0.63 |
|        main-edit-among-many-shapes |     4.837 |   2.095 |   2.487 |   0.43 |
|           main-edit-into-10-copies |      1.79 |   0.962 |   1.002 |   0.54 |
|          main-edit-into-100-copies |    15.415 |   9.465 |  10.586 |   0.61 |
|         main-edit-into-1000-copies |   153.086 | 104.614 | 107.085 |   0.68 |
|         variant-switch-into-copies |    32.052 |  22.023 |  22.427 |   0.69 |

AI-assisted-by: claude-opus-5-5
This commit is contained in:
alonso.torres 2026-11-10 05:35:41 +01:00
parent 88b8a3c697
commit b27745246c
4 changed files with 50 additions and 4 deletions

View File

@ -375,7 +375,7 @@
(reduce #(update %1 %2 turn) objects
(cfh/get-children-ids-with-self objects (:id shape-main)))))))))
(defn- reassign-parent-grid-cells
(defn reassign-parent-grid-cells
"`changes` with the cells of the grid that holds the copy head `id`
assigned again when they take the head in or out of the grid's flow, as
showing or hiding a child does."

View File

@ -793,9 +793,11 @@
;; temporary deletion must not change the parent's placement data.
changes (cond-> changes
(ctsl/grid-layout? parent)
(pcb/update-shapes [(:id parent)]
#(assoc % :layout-grid-cells (:layout-grid-cells parent))
{:ignore-touched true}))]
(-> (pcb/update-shapes [(:id parent)]
#(assoc % :layout-grid-cells (:layout-grid-cells parent))
{:ignore-touched true})
;; a head that enters or leaves the flow takes or frees a cell
(cll/reassign-parent-grid-cells page (:id shape))))]
[new-shape changes (concat all-parents parents-of-swapped)]))
(defn find-switch-target

View File

@ -2005,6 +2005,21 @@
:ops [[:duplicate 0 0 0 0] [:duplicate 0 0 0 0] [:variant-property 0 0 0 0] [:switch 12]
[:duplicate 16 0 0 0] [:make-variant 0] [:switch 12] [:update-main 0 0 0 0]]}))))
(t/deftest switching-a-hidden-grid-child-to-a-shown-variant-gives-it-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 false :grid-empty false :layout-copies true :text true}]
:ops [[:add-variant 0 0 0 0] [:add-variant 2] [:switch 5] [:nest 8 0 0 0] [:delete 0]
[:restore 0] [:style 6 1 1] [:variant-property 0 0 0 0] [:switch 0]]}))))
(t/deftest switching-a-hidden-grid-child-of-a-library-variant-gives-it-a-cell
(t/is (map? (cg/run {:world [2 1 {:variants true :text-styles true :library-variants true :stale-libraries true
:layout :grid :layout-item-margins false :grid-square false :library-depth 1
:library true :rich-variants true :layout-children false :library-text true
:grid-empty false :layout-copies true :text true}]
:ops [[:nest 6 0 0 0] [:style 3 1 1] [:library-variant-property 0 0 0 0] [:library-sync]
[:switch 6]]}))))
(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

@ -198,3 +198,32 @@
(t/is (= 3 (:column cell)))
(t/is (= (:id cell) (:id cell')))
(t/is (= [(:id new-shape)] (:shapes cell')))))
(t/deftest switching-a-hidden-grid-child-to-a-shown-variant-gives-it-a-cell
(let [;; ==== Setup: :m01 is hidden, so its copy in the grid board holds no cell
file (-> (thf/sample-file :file1)
(thv/add-variant :v01 :c01 :m01 :c02 :m02 :variant1-params {:hidden true})
(tho/add-grid-frame :board)
(ths/add-sample-shape :first :parent-label :board)
(thc/instantiate-component :c01 :copy01 :parent-label :board)
(tho/place-in-grid-columns :board 2 {1 :first}))
page (thf/current-page file)
;; ==== Action
[new-shape changes _]
(clv/generate-component-swap-in-place (pcb/empty-changes nil (:id page))
page
(libraries file)
(:data file)
(ths/get-shape file :copy01)
(thi/id :c02)
true)
file' (thf/apply-changes file changes)
new-shape (ths/get-shape-by-id file' (:id new-shape))
cell' (ctl/get-cell-by-shape-id (ths/get-shape file' :board) (:id new-shape))]
;; ==== Check: the shown head takes the free cell
(t/is (:hidden (ths/get-shape file :copy01)))
(t/is (nil? (ctl/get-cell-by-shape-id (ths/get-shape file :board) (thi/id :copy01))))
(t/is (not (:hidden new-shape)))
(t/is (= 2 (:column cell')))))