From 56ef4e88d75a96229e2002f9622aab6eb2f49a4e Mon Sep 17 00:00:00 2001 From: "alonso.torres" Date: Tue, 17 Nov 2026 11:43:15 +0100 Subject: [PATCH] :bug: Paste a copy out of its copy as the component its main swapped in A nested copy pasted out of its copy, where the main the copy refers to had swapped that head, advanced its refs by level through the replaced slot of the old component and passed on the swap, which left an invalid file (`:ref-shape-not-found`, `:misplaced-swap-group`). Users reach it by copying such a copy and pasting it on the page (F590). When the references a pasted head skips include a swapped head, each of its shapes now refers to its counterpart in the main of the component it shows, the first of its content references there, and keeps the overrides of those it skips, without the swap. Found by the generated programs once they paste nested copies (a variant program with a swap in a main). Test (swap-group-test, fails before the fix): `paste-out-of-a-copy-follows-a-swap-in-its-main`. Benchmark (bench-components, alone): | :world | :baseline | :now | :mean | :ratio | |------------------------------------+-----------+---------+---------+--------| | deepest-edit-through-three-levels | 35.215 | 21.672 | 24.681 | 0.62 | | edit-through-a-chain-of-dependents | 68.931 | 43.664 | 46.149 | 0.63 | | library-update-into-copies | 83.083 | 51.625 | 55.322 | 0.62 | | main-edit-among-many-shapes | 4.837 | 2.582 | 3.817 | 0.53 | | main-edit-into-10-copies | 1.79 | 1.063 | 1.179 | 0.59 | | main-edit-into-100-copies | 15.415 | 10.715 | 11.354 | 0.7 | | main-edit-into-1000-copies | 153.086 | 104.404 | 110.116 | 0.68 | | variant-switch-into-copies | 32.052 | 21.823 | 24.319 | 0.68 | AI-assisted-by: claude-opus-5-5 --- common/src/app/common/logic/duplicate.cljc | 39 ++++++++++++++++--- .../common_tests/logic/swap_group_test.cljc | 32 +++++++++++++++ 2 files changed, 66 insertions(+), 5 deletions(-) diff --git a/common/src/app/common/logic/duplicate.cljc b/common/src/app/common/logic/duplicate.cljc index b9acaf490a..d5ede27e37 100644 --- a/common/src/app/common/logic/duplicate.cljc +++ b/common/src/app/common/logic/duplicate.cljc @@ -469,6 +469,34 @@ (assoc % :touched touched) (dissoc % :touched))))) objects))) + ;; a copy whose references skip a swapped head shows that head's + ;; component: each shape refers to its counterpart in the main of the + ;; component, the first of its content references there, and keeps + ;; the overrides of the references it skips + content-reheaded + (fn [objects shape level-delta] + (let [chain (->> (ctf/get-ref-chain-until-target-ref container libraries shape nil) + rest + (take level-delta))] + (when (some ctk/swapped? chain) + (when-let [[file-data component] (ctf/head-component file libraries shape :include-deleted? true)] + (let [main-ids (into #{} (map :id) (ctf/get-component-shapes file-data component)) + unchanged-ids (cli/swapped-descendant-ids objects (:id shape))] + (reduce + (fn [objects child] + (let [chain (rest (ctf/get-ref-chain-until-target-ref container libraries child nil)) + [skipped [target]] (split-with #(not (contains? main-ids (:id %))) chain)] + (if (some? target) + (let [touched (-> (into (or (:touched child) #{}) (mapcat :touched) skipped) + (disj :swap-group))] + (update objects (:id child) + #(cond-> (assoc % :shape-ref (:id target)) + (seq touched) (assoc :touched touched) + (empty? touched) (dissoc :touched)))) + objects))) + objects + (->> (cfh/get-children-with-self objects (:id shape)) + (remove #(contains? unchanged-ids (:id %)))))))))) detach-copy (fn [objects shape] (reduce #(update %1 %2 ctk/detach-shape) objects @@ -487,11 +515,12 @@ (detach-copy objects shape)) :else - (let [unchanged-ids (cli/swapped-descendant-ids objects (:id shape))] - (reduce (partial advance-shape level-delta) - objects - (->> (cfh/get-children-with-self objects (:id shape)) - (remove #(contains? unchanged-ids (:id %)))))))) + (or (content-reheaded objects shape level-delta) + (let [unchanged-ids (cli/swapped-descendant-ids objects (:id shape))] + (reduce (partial advance-shape level-delta) + objects + (->> (cfh/get-children-with-self objects (:id shape)) + (remove #(contains? unchanged-ids (:id %))))))))) objects))] (reduce advance-copy objects diff --git a/common/test/common_tests/logic/swap_group_test.cljc b/common/test/common_tests/logic/swap_group_test.cljc index 960eddffcb..d16020756a 100644 --- a/common/test/common_tests/logic/swap_group_test.cljc +++ b/common/test/common_tests/logic/swap_group_test.cljc @@ -384,6 +384,38 @@ (t/is (ctk/swapped? head)) (t/is (= (thi/id :badge-dot) (:shape-ref (get objects dot-id)))))) +(t/deftest paste-out-of-a-copy-follows-a-swap-in-its-main + ;; {:page-main} [:name Frame1] # [Component :page] + ;; :inner-copy [:name Frame1] @--> [Component :card] :card-main + ;; :inner-star [:name Frame1] @--> [Component :badge] :card-star (swapped) + ;; :outer-copy [:name Frame1] #--> [Component :page] :page-main + ;; :outer-inner [:name Frame1] @--> [Component :card] :inner-copy + ;; :outer-star [:name Frame1] @--> [Component :badge] :inner-star + ;; pasted out of its copy, :outer-star shows the badge the main swapped in: + ;; it becomes a plain copy of :badge, as do its children + (let [file (-> (setup) + (tho/add-simple-component :dot :dot-main :dot-rect) + (tho/add-frame :badge-main) + (thc/instantiate-component :dot :badge-dot :parent-label :badge-main) + (thc/make-component :badge :badge-main) + (tho/add-frame :page-main) + (thc/instantiate-component :card :inner-copy + :parent-label :page-main + :children-labels [:inner-star]) + (tho/swap-component-in-shape :inner-star :badge) + (thc/make-component :page :page-main) + (thc/instantiate-component :page :outer-copy + :children-labels [:outer-inner :outer-star]) + (thf/validate-file!)) + star (ths/get-shape file :outer-star) + objects (copy-objects file :outer-star) + head (get objects (thi/id :outer-star)) + dot (get objects (first (:shapes head)))] + (t/is (= (:id (thc/get-component file :badge)) (:component-id star))) + (t/is (= (thi/id :badge-main) (:shape-ref head))) + (t/is (not (ctk/swapped? head))) + (t/is (= (thi/id :badge-dot) (:shape-ref dot))))) + (t/deftest duplicate-keeps-the-swap (let [file (-> (setup) (tho/duplicate-shape :copy)