🐛 Restore the main children an update main deleted in their order

A copy swapped two adjacent children of its main, and update-main
deleted both main children and added their replacements. Undoing it
put the main's children back out of order. Each restore was recorded at
the shape's position in the original state, as the update-main builder
does not apply its changes locally; the undo restores in reverse order,
so the second deleted sibling came back at a position counted with the
first still in place.

Without the builder's local objects, a restore now takes the position
the shape had when the batch deleted it, as the earlier changes leave
its parent.

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

|                             :world | :baseline |    :now |   :mean | :ratio |
|------------------------------------+-----------+---------+---------+--------|
|  deepest-edit-through-three-levels |    35.215 |  22.018 |   22.34 |   0.63 |
| edit-through-a-chain-of-dependents |    68.931 |  42.507 |    43.3 |   0.62 |
|         library-update-into-copies |    83.083 |  47.544 |  49.173 |   0.57 |
|        main-edit-among-many-shapes |     4.837 |   2.058 |   2.319 |   0.43 |
|           main-edit-into-10-copies |      1.79 |   0.977 |   1.075 |   0.55 |
|          main-edit-into-100-copies |    15.415 |   9.676 |  10.209 |   0.63 |
|         main-edit-into-1000-copies |   153.086 | 104.546 | 108.771 |   0.68 |
|         variant-switch-into-copies |    32.052 |  19.974 |  22.679 |   0.62 |

AI-assisted-by: claude-opus-5-5
This commit is contained in:
alonso.torres 2026-11-10 00:57:10 +01:00
parent c56c4cd4e7
commit 88b8a3c697
3 changed files with 81 additions and 3 deletions

View File

@ -1318,22 +1318,63 @@
(map set-link updated-shapes)
(map #(set-link (ctn/get-shape container (:id %))) updated-shapes)))))
(defn- position-in-batch
"The position of the shape `id` of `container` among the children of its
parent, as the changes in `changes` so far leave them."
[changes container id]
(let [objects (:objects container)
parent-id (:parent-id (get objects id))
page? (cfh/page? container)
;; a change on the objects of `container`
in-container? (fn [change]
(= (:id container) (if page? (:page-id change) (:component-id change))))
insert (fn [shapes index ids]
(if (some? index)
(d/insert-at-index shapes index ids)
(into (filterv (complement (set ids)) shapes) ids)))
children (reduce (fn [shapes change]
(if-not (in-container? change)
shapes
(case (:type change)
:del-obj (filterv #(not= % (:id change)) shapes)
:add-obj (cond-> shapes
(= parent-id (:parent-id change))
(insert (:index change) [(:id change)]))
:mov-objects (let [moved (:shapes change)]
(if (= parent-id (:parent-id change))
(insert shapes (:index change) moved)
(filterv (complement (set moved)) shapes)))
:reorder-children (cond-> shapes
(= parent-id (:parent-id change))
(-> (:shapes change) (vec)))
shapes)))
(vec (get-in objects [parent-id :shapes]))
(:redo-changes changes))]
(d/index-of children id)))
(defn- remove-shape
"Delete `shape` of `container` with its children. Each undo restores a
shape at the position it had when the changes so far deleted it, so
siblings that one batch deletes come back in their order."
[changes shape container]
(shape-log :info (:id shape) container
:msg (str "REMOVE-SHAPE " (shape-label shape container)))
(let [objects (get container :objects)
parents (cfh/get-parent-ids objects (:id shape))
;; from the bottom up
ids (reverse (into [(:id shape)] (cfh/get-children-ids objects (:id shape))))]
ids (reverse (into [(:id shape)] (cfh/get-children-ids objects (:id shape))))
;; the builder's objects, when it applies its changes as it goes
local (pcb/get-objects changes)]
(pcb/add-changes changes container
(conj (mapv del-obj-change ids)
{:type :reg-objects :shapes (vec parents)})
;; the parents are recalculated after the shapes are restored
(cons {:type :reg-objects :shapes (vec parents)}
(map #(add-obj-change (get objects %)
(or (cfh/get-position-on-parent (pcb/get-objects changes) %)
(cfh/get-position-on-parent objects %)))
(or (cfh/get-position-on-parent local %)
(if (= % (:id shape))
(position-in-batch changes container %)
(cfh/get-position-on-parent objects %))))
ids)))))
(defn- longest-increasing

View File

@ -1998,6 +1998,13 @@
: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 undoing-an-update-main-restores-the-main-children-it-replaced-in-order
(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
:layout-children false :grid-empty false :layout-copies true :text true}]
: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 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

@ -512,6 +512,36 @@
(t/is (= (:id (thc/get-component file :heart)) (:component-id other)))
(t/is (= "#222222" (tho/bottom-fill-color file :copy2)))))
(t/deftest undoing-an-update-main-that-replaces-two-children-keeps-their-order
;; {:row-main} [:name Frame1] # [Component :row]
;; :row-s1..:row-s4 [:name Frame1] @--> [Component :star] :star-main
;; :copy [:name Frame1] #--> [Component :row] :row-main
;; :copy-s1, :copy-s2 @--> [Component :heart] (swapped)
;; :copy-s3, :copy-s4 @--> [Component :star]
(let [file (-> (thf/sample-file :file1)
(tho/add-simple-component :star :star-main :star-rect)
(tho/add-simple-component :heart :heart-main :heart-rect)
(tho/add-frame :row-main)
(thc/instantiate-component :star :row-s1 :parent-label :row-main)
(thc/instantiate-component :star :row-s2 :parent-label :row-main)
(thc/instantiate-component :star :row-s3 :parent-label :row-main)
(thc/instantiate-component :star :row-s4 :parent-label :row-main)
(thc/make-component :row :row-main)
(thc/instantiate-component :row :copy
:children-labels [:copy-s1 :copy-s2 :copy-s3 :copy-s4])
(tho/swap-component-in-shape :copy-s1 :heart)
(tho/swap-component-in-shape :copy-s2 :heart))
page (thf/current-page file)
container (ctn/make-container page :page)
changes (-> (pcb/empty-changes)
(pcb/with-container container)
(cll/generate-sync-shape-inverse {(:id file) file} container (thi/id :copy)))
updated (thf/apply-changes file changes :validate? false)
undone (thf/apply-undo-changes updated changes)]
(t/is (= 4 (count (:shapes (ths/get-shape updated :row-main)))))
(t/is (= (:shapes (ths/get-shape file :row-main))
(:shapes (ths/get-shape undone :row-main))))))
(t/deftest update-main-keeps-a-deeper-swap-as-a-swap
(let [file (-> (setup-two-levels)
(update-main :outer-copy :page))