From e410278c91564413b9eb51816676de3f9130a862 Mon Sep 17 00:00:00 2001 From: "alonso.torres" Date: Fri, 6 Nov 2026 05:07:28 +0100 Subject: [PATCH] :bug: Delete a switch's unplaced swapped children so undo restores them A switch deletes the old head without its swapped children, which it moves into the new head. A swapped child whose place the new variant lacks found no counterpart and was never moved: it went with the old head's subtree without changes of its own, so undo left it out. The switch keeps out of the deletion only the swapped children whose place, by name and ancestor kinds in their main, the new variant has; the others are deleted with the old head and restored by undo. Benchmark (scripts/bench-components, ms, lower 2.5% quantile): | world | baseline | now | ratio | |------------------------------------+----------+---------+-------| | deepest-edit-through-three-levels | 35.215 | 21.011 | 0.60 | | edit-through-a-chain-of-dependents | 68.931 | 40.794 | 0.59 | | library-update-into-copies | 83.083 | 45.598 | 0.55 | | main-edit-among-many-shapes | 4.837 | 2.087 | 0.43 | | main-edit-into-10-copies | 1.79 | 1.001 | 0.56 | | main-edit-into-100-copies | 15.415 | 10.11 | 0.66 | | main-edit-into-1000-copies | 153.086 | 98.638 | 0.64 | | variant-switch-into-copies | 32.052 | 19.844 | 0.62 | AI-assisted-by: claude-opus-5-5 --- common/src/app/common/logic/swap.cljc | 12 ++++--- common/src/app/common/logic/variants.cljc | 34 ++++++++++++++++++- .../logic/composable_gen_test.cljc | 11 ++++++ 3 files changed, 52 insertions(+), 5 deletions(-) diff --git a/common/src/app/common/logic/swap.cljc b/common/src/app/common/logic/swap.cljc index cd3336a3da..b143828985 100644 --- a/common/src/app/common/logic/swap.cljc +++ b/common/src/app/common/logic/swap.cljc @@ -439,14 +439,18 @@ :ignore-touched true}))])) (defn generate-component-swap + "Replace the copy head `shape` with a copy of `id-new-component`. With + `ignore-swapped?`, a set of the swapped shapes of its subtree that the + caller keeps (or true for all of them), those are not deleted with it." [changes objects shape file page libraries id-new-component index target-cell keep-props-values ignore-swapped?] (let [;; When we keep the touched properties, we can't delete the - ;; swapped children (we will keep them too) + ;; swapped children we keep ignore-swapped-fn - (if ignore-swapped? - #(ctk/swapped? (get objects %)) - (constantly false)) + (cond + (set? ignore-swapped?) #(contains? ignore-swapped? %) + ignore-swapped? #(ctk/swapped? (get objects %)) + :else (constantly false)) ;; In the cases where the swapped shape was the first element of the masked group it would make the group to loose the ;; mask property as part of the sanitization check on generate-delete-shapes, passing "ignore-mask" to prevent this diff --git a/common/src/app/common/logic/variants.cljc b/common/src/app/common/logic/variants.cljc index 551f818694..6f85dc11e0 100644 --- a/common/src/app/common/logic/variants.cljc +++ b/common/src/app/common/logic/variants.cljc @@ -639,6 +639,36 @@ (pcb/update-shapes [(:id new-shape)] #(update % :touched (fnil into #{}) groups) {:ignore-touched true})))) +(defn- kept-swapped-ids + "The swapped shapes in the subtree of the copy head `shape` that a switch + to the component `component-id` of `ldata` keeps: those whose place in + their main, by name and ancestor kinds, the new component has. The others + go with the old head." + [page libraries ldata shape component-id] + (let [objects (:objects page) + container (ctn/make-container page :page) + base-ref (ctf/find-remote-shape container libraries shape {:with-context? true}) + ref-objects (:objects (:container (meta base-ref))) + ref-paths (when base-ref + (into {} (map (juxt :id :shape-path)) + (add-unique-path (reverse (cfh/get-children-with-self ref-objects (:id base-ref))) + ref-objects (:id base-ref)))) + component (ctkl/get-component ldata component-id) + main (ctf/get-component-root ldata component) + main-objects (:objects (ctf/get-component-container ldata component)) + new-paths (into #{} (map :shape-path) + (add-unique-path (reverse (cfh/get-children-with-self main-objects (:id main))) + main-objects (:id main)))] + (into #{} + (comp (filter ctk/swapped?) + (remove #(child-of-swapped? % objects (:id shape))) + (filter (fn [old] + (let [old-ref (find-shape-ref-child-of container libraries old (:id base-ref))] + (and (some? old-ref) + (contains? new-paths (get ref-paths (:id old-ref))))))) + (map :id)) + (rest (cfh/get-children-with-self objects (:id shape)))))) + (defn generate-component-swap-in-place "Swap the copy head `shape` of `page` for a copy of the component `component-id` of library data `ldata`. The new head takes the old one's @@ -658,7 +688,9 @@ [new-shape all-parents changes] (clsw/generate-component-swap changes objects shape ldata page libraries component-id - index target-cell keep-props keep-touched?) + index target-cell keep-props + (when keep-touched? + (kept-swapped-ids page libraries ldata shape component-id))) [changes parents-of-swapped] (if keep-touched? diff --git a/common/test/common_tests/logic/composable_gen_test.cljc b/common/test/common_tests/logic/composable_gen_test.cljc index 12dca7c569..78f9c420d6 100644 --- a/common/test/common_tests/logic/composable_gen_test.cljc +++ b/common/test/common_tests/logic/composable_gen_test.cljc @@ -1219,3 +1219,14 @@ :layout-children false :grid-empty false :layout-copies true :text true}] :ops [[:duplicate 0 0 0 0] [:rotate 16 0 7 0] [:duplicate 0 0 0 0] [:rotate 16 0 7 0] [:switch 12 0 0 0]]})))) + +(t/deftest generated-switch-undo-restores-a-swapped-child-the-new-variant-has-no-place-for + ;; a switched head holds a swapped board whose place the new variant lacks; + ;; the switch deletes it with the old head, and undo brings it back + (t/is (map? (cg/run {:world [2 0 {:variants true :text-styles true :layout nil :layout-item-margins false + :grid-square false :library true :rich-variants true :layout-children false + :grid-empty false :layout-copies true :text true}] + :ops [[:swap 8 0 0 0] [:edit 18 1 1 :copy-head] [:reset 18] [:undo 0 0 0 0] + [:swap 2 0 0 0] [:duplicate 4 0 0 0] [:delete 8] [:restore 8] [:undo 0 0 0 0] + [:text-style 4 0 1] [:delete 0 0 0 0] [:nest 21 0 0 0] [:duplicate 0 0 0 0] + [:swap 16 0 0 0] [:switch 0 0 0 0]]}))))