diff --git a/common/src/app/common/logic/libraries.cljc b/common/src/app/common/logic/libraries.cljc index cfe1e01527..221866d576 100644 --- a/common/src/app/common/logic/libraries.cljc +++ b/common/src/app/common/logic/libraries.cljc @@ -555,7 +555,7 @@ [(:shape-ref shape) (d/without-nils (select-keys shape [:constraints-h :constraints-v]))])))) ids)) -(defn- placed-like +(defn placed-like "The main `objects` with the subtree of `shape-main` placed like `placed`, a copy shape expressed in the main's frame: resized to its size, its children following through their constraints and layouts, then turned diff --git a/common/src/app/common/logic/variants.cljc b/common/src/app/common/logic/variants.cljc index 5240863919..487bfb6962 100644 --- a/common/src/app/common/logic/variants.cljc +++ b/common/src/app/common/logic/variants.cljc @@ -581,6 +581,28 @@ changes)) changes))) +(defn- keep-moved-placement + "A switch keeps the place of the copy shape `original-shape` it replaces. + Inside a copy, a first corner that the position of `new-shape` would not + give it needs a geometry override to survive later sync." + [changes original-shape new-shape page libraries] + (let [objects (pcb/get-objects changes) + shape (get objects (:id new-shape)) + parent (get objects (:parent-id shape)) + position (when (and (:shape-ref parent) + (ctk/touched-group? original-shape :geometry-group) + (not (ctk/touched-group? shape :geometry-group))) + (ctf/find-ref-shape nil (assoc page :objects objects) libraries shape + :include-deleted? true :with-context? true)) + position-objects (:objects (:container (meta position))) + position-parent (get position-objects (:parent-id position)) + placed (when position-parent + (get (cll/placed-like position-objects position-parent parent {}) (:id position)))] + (cond-> changes + (and placed (not (gpt/close? (first (:points shape)) (first (:points placed))))) + (pcb/update-shapes [(:id shape)] #(update % :touched ctk/set-touched-group :geometry-group) + {:ignore-touched true})))) + (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 @@ -612,6 +634,10 @@ changes (sync-restored-position changes page libraries (:id new-shape)) + changes (cond-> changes + keep-touched? + (keep-moved-placement shape new-shape page libraries)) + ;; Replacement keeps the head id and its cell. Cleanup during the ;; temporary deletion must not change the parent's placement data. changes (cond-> changes diff --git a/common/test/common_tests/logic/composable_gen_test.cljc b/common/test/common_tests/logic/composable_gen_test.cljc index b60957180d..6b0550c543 100644 --- a/common/test/common_tests/logic/composable_gen_test.cljc +++ b/common/test/common_tests/logic/composable_gen_test.cljc @@ -1063,3 +1063,10 @@ (t/is (map? (cg/run {:world [2 2 {:variants true :text-styles true :variant-diff false :rich-variants true :layout-copies true :text true}] :ops [[:delete 5 0 0 0] [:switch 3 0 0 0] [:transform 0 0 0 3] [:update-main 0 0 0 0]]})))) + +(t/deftest generated-switch-keeps-a-moved-copy-child-in-place-as-its-own + ;; a copy child of a copy moved and switched to another variant keeps the + ;; moved place as its own geometry + (t/is (map? (cg/run {:world [2 0 {:variants true :text-styles true :library true :rich-variants true + :layout-copies true :text true}] + :ops [[:nest 4 0 0 0] [:transform 1 0 0 3] [:switch 3 0 0 0]]}))))