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