🐛 Give a copy its main's cells when its direction change moves none

Changing the direction of a grid copy without cell overrides reflowed its
auto cells and left the layout to pin those after a gap in the new order,
without making them an override. Changing the direction back kept the
pins, so the copy differed from its main with no override, and the next
sync of its main silently changed it.

A direction change that moves no item of such a copy gives it the cells
its main gives it, laid out as sync lays them out in the new direction,
so changing the direction back restores its main's grid; one that moves
items makes the cells an override, as before. The frontend and the test
operation set the cells first and then the direction, the only override
the change makes otherwise.

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

| world                              | baseline |     now | ratio |
|------------------------------------+----------+---------+-------|
| deepest-edit-through-three-levels  |   35.215 |  21.653 |  0.61 |
| edit-through-a-chain-of-dependents |   68.931 |  40.646 |  0.59 |
| library-update-into-copies         |   83.083 |  49.282 |  0.59 |
| main-edit-among-many-shapes        |    4.837 |   2.415 |  0.50 |
| main-edit-into-10-copies           |     1.79 |   1.072 |  0.60 |
| main-edit-into-100-copies          |   15.415 |   9.683 |  0.63 |
| main-edit-into-1000-copies         |  153.086 | 103.559 |  0.68 |
| variant-switch-into-copies         |   32.052 |  21.623 |  0.67 |

AI-assisted-by: claude-opus-5-5
This commit is contained in:
alonso.torres 2026-11-06 09:52:06 +01:00
parent 189be55a15
commit 9c539b173f
3 changed files with 93 additions and 46 deletions

View File

@ -1920,6 +1920,54 @@
:layout-grid-columns (:layout-grid-columns target))
(ctsl/fit-cells-to-tracks)))))
(defn copy-grid-cells
"The grid copy `shape-copy` with the cells of its main `shape-main`, its
own children in place of the main's, laid out in its own direction: the
cells of a copy without cell overrides."
[shape-copy shape-main objects]
(let [ids-map
(into {}
(comp
(map #(get objects %))
(keep
(fn [copy-shape]
;; Paired children refer to this near main, which can
;; belong to another component when resetting nested copies.
(when-let [ref (:shape-ref copy-shape)]
[ref (:id copy-shape)]))))
(:shapes shape-copy))]
(-> shape-copy
(assoc :layout-grid-cells (-> shape-main (ctsl/remap-grid-cells ids-map) :layout-grid-cells))
(ctsl/assign-cells objects))))
(defn grid-direction-cells
"The update that gives a grid shape of `page` the cells it has once its
direction changes to `dir`: its single-span auto cells reflow in the new
order. In a copy without cell overrides, a reflow that moves items makes
the cells an override, and one that moves none leaves the cells its
main gives it, laid out as sync lays them out in the new direction, so a
copy that changes its direction back has its main's cells again."
[file page libraries dir]
(let [container (ctn/make-container page :page)
objects (:objects page)]
(fn [shape]
(let [from-dir (ctsl/grid-direction shape)]
(if (or (not= :grid (:layout shape)) (= from-dir dir))
shape
(let [reflowed (ctsl/reflow-grid-auto-items-for-direction shape from-dir dir)
main (when (and (:shape-ref shape)
(not (ctk/touched-group? shape :layout-grid-cells)))
(ctf/find-ref-shape file container libraries shape :include-deleted? true))]
(cond
(not= :grid (:layout main))
reflowed
(not= (:layout-grid-cells reflowed) (:layout-grid-cells shape))
(update reflowed :touched ctk/set-touched-group :layout-grid-cells)
:else
(copy-grid-cells (assoc shape :layout-grid-dir dir) main objects))))))))
(defn- update-grid-copy-attrs
"Synchronizes the `layout-grid-cells` property from the main shape to the copies"
[changes shape-before shape-main omit-touched?]
@ -1928,35 +1976,22 @@
(pcb/update-shapes
[(:id shape-before)]
(fn [shape-copy objects]
(let [ids-map
(into {}
(comp
(map #(get objects %))
(keep
(fn [copy-shape]
;; Paired children refer to this near main, which can
;; belong to another component when resetting nested copies.
(when-let [ref (:shape-ref copy-shape)]
[ref (:id copy-shape)]))))
(:shapes shape-copy))
remove-orphan-cells
(let [remove-orphan-cells
(fn [cells {:keys [shapes]}]
(let [child? (set shapes)]
(-> cells
(update-vals
(fn [cell]
(update cell :shapes #(filterv child? %)))))))
;; Take cells from main and remap the shapes to assign it to the copy
copy-cells (-> shape-copy :layout-grid-cells (remove-orphan-cells shape-copy))
main-cells (-> shape-main (ctsl/remap-grid-cells ids-map) :layout-grid-cells)
keep-cells? (and omit-touched? (contains? (:touched shape-copy) :layout-grid-cells))]
(let [shape-copy (assoc shape-copy :layout-grid-cells (if keep-cells? copy-cells main-cells))]
(if keep-cells?
(-> shape-copy
(add-inherited-grid-cells shape-before)
(assign-inherited-grid-cells shape-before objects))
(ctsl/assign-cells shape-copy objects)))))
(if keep-cells?
(-> shape-copy
(assoc :layout-grid-cells copy-cells)
(add-inherited-grid-cells shape-before)
(assign-inherited-grid-cells shape-before objects))
;; 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})))
(defn- update-grid-main-attrs

View File

@ -1240,3 +1240,18 @@
:grid-empty false :layout-copies true :text true}]
:ops [[:library-transform 9 3 4 0] [:delete 5] [:restore 5] [:library-transform 9 4 0 0]
[:library-transform 4 3 0 0] [:library-sync 0 0 0 0] [:switch 13 0 0 0]]}))))
(t/deftest generated-direction-change-in-a-copy-lays-out-its-mains-cells
;; a copy without cell overrides takes its main's cells in the direction
;; it changes to, pins included; changing the direction back gives the
;; main's grid again
(t/is (map? (cg/run {:world [3 0 {:text true :text-styles true :layout :grid :layout-children false
:layout-item-margins false :grid-square true :grid-empty false}]
:ops [[:grid-direction 1 0 1 0] [:add-child 5 0 0 0] [:grid-direction 0 0 0 1]
[:grid-cell 0 0 0 1]]})))
(t/is (map? (cg/run {:world [2 0 {:text true :text-styles true :layout :grid :layout-children true
:layout-item-margins false :grid-square false :grid-empty false}]
:ops [[:delete 0 0 0 0] [:restore 0 0 0 0] [:delete 0 0 0 0] [:grid-direction 0 0 0 1]
[:grid-cell 0 0 0 1] [:restore 0 0 0 0] [:delete 0 0 0 0] [:grid-direction 5 0 3 0]
[:add-child 1 0 0 0] [:update-main 0 0 0 0] [:grid-direction 0 0 0 0]
[:grid-direction 0 0 3 0]]}))))

View File

@ -13,6 +13,7 @@
[app.common.files.shapes-helpers :as cfsh]
[app.common.geom.point :as gpt]
[app.common.logic.duplicate :as cld]
[app.common.logic.libraries :as cll]
[app.common.logic.shapes :as cls]
[app.common.types.color :as clr]
[app.common.types.component :as ctc]
@ -292,34 +293,30 @@
(rx/of (with-meta event (meta it)))))))))
(defn change-grid-direction
"Set grid direction and reflow single-span auto items in one undo step."
"Set grid direction and lay out the cells for it in one undo step. A copy
without cell overrides takes its main's cells laid out in the new
direction; only the direction becomes its override."
[ids dir]
(ptk/reify ::change-grid-direction
ptk/WatchEvent
(watch [_ _ _]
(let [undo-id (js/Symbol)]
(rx/of (dwu/start-undo-transaction undo-id)
(dwsh/update-shapes
ids
(fn [shape]
(let [from-dir (ctl/grid-direction shape)]
(cond
(not= :grid (:layout shape))
shape
(not (contains? #{:row :column} dir))
shape
(= from-dir dir)
(assoc shape :layout-grid-dir dir)
:else
(ctl/reflow-grid-auto-items-for-direction shape from-dir dir))))
;; Auto cells are already reflowed, so skip generic reassignment
;; and child reordering.
{:skip-grid-reassignment? true})
(ptk/data-event :layout/update {:ids ids})
(dwu/commit-undo-transaction undo-id))))))
(watch [_ state _]
(if (contains? #{:row :column} dir)
(let [undo-id (js/Symbol)
cells (cll/grid-direction-cells (dsh/lookup-file state) (dsh/lookup-page state)
(dsh/lookup-libraries state) dir)]
(rx/of (dwu/start-undo-transaction undo-id)
;; Auto cells are already laid out, so skip generic reassignment
;; and child reordering.
(dwsh/update-shapes ids cells {:attrs #{:layout-grid-cells :touched}
:ignore-touched true
:skip-grid-reassignment? true})
(dwsh/update-shapes ids
#(cond-> % (= :grid (:layout %)) (assoc :layout-grid-dir dir))
{:attrs #{:layout-grid-dir}
:skip-grid-reassignment? true})
(ptk/data-event :layout/update {:ids ids})
(dwu/commit-undo-transaction undo-id)))
(rx/empty)))))
(defn update-layout
([ids changes] (update-layout ids changes nil))