From 7fd46d516b1eac9d243f7b291bcde120f63ab2cd Mon Sep 17 00:00:00 2001 From: "alonso.torres" Date: Tue, 3 Nov 2026 12:22:44 +0100 Subject: [PATCH] :bug: Keep a detached nested copy child where it is Detaching a copy links each nested copy to the next main up. A nested copy that keeps a size of its own then places its children against the new main's size, so a child with its own center constraint stayed in place at the detach but moved at the next sync of that component, with no change to the main. Detach now marks the geometry of each shape it links to a new main as the copy's own when its next sync would place it elsewhere, so it stays where it is. Swapped heads still take their new place. The model predicts the same override. Benchmark (scripts/bench-components, ms, lower 2.5% quantile): | world | baseline | now | ratio | |------------------------------------+----------+---------+-------| | deepest-edit-through-three-levels | 35.215 | 20.643 | 0.59 | | edit-through-a-chain-of-dependents | 68.931 | 40.331 | 0.59 | | library-update-into-copies | 83.083 | 46.648 | 0.56 | | main-edit-among-many-shapes | 4.837 | 2.336 | 0.48 | | main-edit-into-10-copies | 1.79 | 0.995 | 0.56 | | main-edit-into-100-copies | 15.415 | 9.07 | 0.59 | | main-edit-into-1000-copies | 153.086 | 91.388 | 0.60 | | variant-switch-into-copies | 32.052 | 21.317 | 0.67 | AI-assisted-by: claude-opus-5-5 --- common/src/app/common/logic/instances.cljc | 14 +++++----- common/src/app/common/logic/libraries.cljc | 26 +++++++++++++++++++ .../logic/composable_gen_test.cljc | 7 +++++ 3 files changed, 41 insertions(+), 6 deletions(-) diff --git a/common/src/app/common/logic/instances.cljc b/common/src/app/common/logic/instances.cljc index f54f8c150d..18bd2c21f3 100644 --- a/common/src/app/common/logic/instances.cljc +++ b/common/src/app/common/logic/instances.cljc @@ -126,12 +126,14 @@ objects (pcb/get-objects changes) advanced (into [] (filter (fn [id] - (let [before (get-in container [:objects id]) - after (get objects id)] - (and (ctk/swapped? after) - (not= (:shape-ref before) (:shape-ref after)))))) - (cfh/get-children-ids (:objects container) shape-id))] - (cll/generate-sync-advanced-placement changes container libraries advanced)))) + (not= (get-in container [:objects id :shape-ref]) + (get-in objects [id :shape-ref])))) + (cfh/get-children-ids (:objects container) shape-id)) + swapped? #(ctk/swapped? (get objects %))] + (-> changes + (cll/generate-sync-advanced-placement container libraries (filterv swapped? advanced)) + ;; a nested copy keeps where it is when its new main would place it elsewhere + (cll/generate-keep-advanced-placement container libraries (remove swapped? advanced)))))) (defn- generate-detach-recursive [changes container libraries shape-id first component-root?] diff --git a/common/src/app/common/logic/libraries.cljc b/common/src/app/common/logic/libraries.cljc index 8ef3c97ad3..e4fe694603 100644 --- a/common/src/app/common/logic/libraries.cljc +++ b/common/src/app/common/logic/libraries.cljc @@ -1750,6 +1750,32 @@ changes))) changes ids)) +(defn generate-keep-advanced-placement + "Make authored the geometry of the copy shapes `ids`, whose references + advanced, when their next sync would place them elsewhere: their copy + parent keeps overrides that the new main lacks. Parents come before + their children, so each child is checked in its parent's settled frame." + [changes container libraries ids] + (reduce + (fn [changes id] + (let [objects (pcb/get-objects changes) + container (assoc container :objects objects) + shape (get objects id) + parent (get objects (:parent-id shape)) + main (ctf/find-ref-shape nil container libraries shape + :include-deleted? true :with-context? true) + main-objects (:objects (:container (meta main))) + main-parent (get main-objects (:parent-id main))] + (if (and (some? (:shape-ref parent)) (some? main-parent) + (not (ctk/touched-group? shape :geometry-group)) + (not (close-geometry? shape (get (placed-like main-objects main-parent parent + (authored-constraints objects [id])) + (:id main))))) + (pcb/update-shapes changes [id] #(update % :touched ctk/set-touched-group :geometry-group) + {:ignore-touched true}) + changes))) + changes ids)) + (defn- update-swapped-placement "Synchronizes a swapped copy head with the main shape it replaces, in the attributes that place it in its parent." diff --git a/common/test/common_tests/logic/composable_gen_test.cljc b/common/test/common_tests/logic/composable_gen_test.cljc index 337dfd572c..30bfe76b6a 100644 --- a/common/test/common_tests/logic/composable_gen_test.cljc +++ b/common/test/common_tests/logic/composable_gen_test.cljc @@ -1026,3 +1026,10 @@ (t/is (map? (cg/run {:world [2 0 {:variants true :text-styles true :layout :flex :library true :rich-variants true :layout-copies true :text true}] :ops [[:style 0 23 18 0]]})))) + +(t/deftest generated-detached-copy-child-with-its-own-constraint-stays-in-place + ;; a nested copy child with its own center constraint keeps its place when + ;; the copy around it is detached, as an override of its new main, and a + ;; later sync leaves it there + (t/is (map? (cg/run {:world [2 2 {:text true :text-styles true}] + :ops [[:style 8 9 3 1] [:resize 2 0 0 0] [:detach 3 0 0 0]]}))))