diff --git a/common/src/app/common/logic/variants.cljc b/common/src/app/common/logic/variants.cljc index 0bbcca249c..260985de52 100644 --- a/common/src/app/common/logic/variants.cljc +++ b/common/src/app/common/logic/variants.cljc @@ -506,10 +506,11 @@ old-ref (if root? orig-base-ref-shape (find-shape-ref-child-of container libraries old (:id orig-base-ref-shape))) - new (get new-shapes-map (get o-ref-shapes-p-map (:id old-ref)))] - (when new - {:old old :old-ref old-ref :new new - :swapped? (and (not root?) (ctk/swapped? old))}))) + new (get new-shapes-map (get o-ref-shapes-p-map (:id old-ref))) + swapped? (and (not root?) (ctk/swapped? old))] + ;; a swap is kept only where the new variant holds a copy + (when (and new (or (not swapped?) (ctk/instance-head? new))) + {:old old :old-ref old-ref :new new :swapped? swapped?}))) orig-touched) ;; A swapped child is kept as it is, and the others carry their @@ -657,8 +658,8 @@ (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." + their main, by name and ancestor kinds, the new component holds a copy + in. The others go with the old head." [page libraries ldata shape component-id] (let [objects (:objects page) container (ctn/make-container page :page) @@ -671,7 +672,8 @@ 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) + ;; the paths of the new main where it holds a copy + new-paths (into #{} (comp (filter ctk/instance-head?) (map :shape-path)) (add-unique-path (reverse (cfh/get-children-with-self main-objects (:id main))) main-objects (:id main)))] (into #{} diff --git a/common/test/common_tests/logic/composable_gen_test.cljc b/common/test/common_tests/logic/composable_gen_test.cljc index 4ae8249ad2..ed4687d6f9 100644 --- a/common/test/common_tests/logic/composable_gen_test.cljc +++ b/common/test/common_tests/logic/composable_gen_test.cljc @@ -1849,6 +1849,14 @@ [:delete 7 0 0 0] [:make-variant 8 0 0 0] [:transform 13 3 7 0] [:combine 7] [:switch 7] [:resize 10 0 0 0] [:combine 1] [:switch 10]]})))) +(t/deftest switching-a-copy-drops-a-swap-where-the-new-variant-holds-a-plain-board + (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 [[:combine 0] [:switch 1] [:make-variant 1 0 0 0] [:add-variant 4] [:switch 8] + [:make-variant 0 0 0 0] [:detach 0 0 0 0] [:variant-property 0 0 0 0] + [:switch 12]]})))) + (t/deftest random-variant-compositions (let [coverage (atom {}) failure (atom nil)] diff --git a/common/test/common_tests/logic/variants_switch_test.cljc b/common/test/common_tests/logic/variants_switch_test.cljc index 35d142abe3..cfe7b932dd 100644 --- a/common/test/common_tests/logic/variants_switch_test.cljc +++ b/common/test/common_tests/logic/variants_switch_test.cljc @@ -12,6 +12,7 @@ [app.common.geom.point :as gpt] [app.common.geom.rect :as grc] [app.common.geom.shapes :as gsh] + [app.common.logic.instances :as cli] [app.common.logic.libraries :as cll] [app.common.logic.shapes :as cls] [app.common.math :as mth] @@ -3572,6 +3573,27 @@ (t/is (gpt/close? (offset-in-root file :m02 :m02-icon) (offset-in-root file' :copy02 :copy-icon-b))))) +(t/deftest test-switch-drops-a-swap-the-new-variant-holds-no-copy-for + (let [;; ==== Setup: the second variant's icon is detached into a plain board + file (setup-variant-with-swapped-icon) + page (thf/current-page file) + file (thf/apply-changes file (cli/generate-detach-instance (-> (pcb/empty-changes nil) + (pcb/with-page page) + (pcb/with-objects (:objects page))) + page + {(:id file) file} + (thi/id :m02-icon))) + + ;; ==== Action + file' (tho/swap-component-in-shape file :copy01 :c02 {:new-shape-label :copy02 + :keep-touched? true}) + icon' (ths/get-shape-by-id file' (-> (ths/get-shape file' :copy02) :shapes first))] + + ;; ==== Check + (t/is (= (:id (ths/get-shape file :m02-icon)) (:shape-ref icon'))) + (t/is (not (ctk/instance-head? icon'))) + (t/is (nil? (ths/get-shape file' :copy-icon-b))))) + (defn- resize-width [shape width] (gsh/transform-shape shape (ctm/change-size shape width nil)))