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]]}))))