diff --git a/common/src/app/common/logic/libraries.cljc b/common/src/app/common/logic/libraries.cljc index c513a5dbcc..4bc1730eac 100644 --- a/common/src/app/common/logic/libraries.cljc +++ b/common/src/app/common/logic/libraries.cljc @@ -1960,11 +1960,50 @@ data' file-id (changed-components data sync) libraries current-file-id 1))) +(defn- settle-reset-placement + "Place a reset nested head whose geometry was overridden as an ordinary + copy of its position: its subtree takes the placement a sync of the head + that owns its ref gives. Other heads keep the placement they have." + [changes file libraries container shape-id] + (let [objects (pcb/get-objects changes) + shape (get objects shape-id) + parent (get objects (:parent-id shape))] + (if (and (= :page (:type container)) + (:shape-ref parent) + (not (ctk/swapped? shape)) + (ctk/touched-group? (ctn/get-shape container shape-id) :geometry-group)) + (let [container (ctn/make-container (assoc container :objects objects) :page) + component (ctf/find-ref-component file container libraries shape :include-deleted? true) + head (d/seek #(= (:id component) (:component-id %)) + (ctn/get-parent-copy-heads objects parent))] + (if head + (let [file (assoc-in file [:data :pages-index (:id container) :objects] objects) + proposal (generate-sync-shape-direct (-> (pcb/empty-changes) + (pcb/with-container container) + (pcb/with-objects objects)) + file (assoc libraries (:id file) file) + container (:id head) false) + placed (pcb/get-objects proposal)] + (pcb/update-shapes + changes (cfh/get-children-ids-with-self objects shape-id) + (fn [shape] + (let [expected (get placed (:id shape)) + attrs (cond-> placement-geometry-attrs + (= :path (:type shape)) (conj :content))] + (if (= (:type shape) (:type expected)) + (merge shape (select-keys expected attrs)) + shape))) + {:ignore-touched true})) + changes)) + changes))) + (defn generate-reset-component "Reset the copy head to its near main. A swapped head turns back into the - component of the main shape it refers to, keeping its id." + component of the main shape it refers to, keeping its id. A nested head + also takes its position's placement." [changes file-full libraries container id] (-> changes (pcb/with-container container) (pcb/with-objects (:objects container)) - (generate-sync-shape-direct file-full libraries container id true))) + (generate-sync-shape-direct file-full libraries container id true) + (settle-reset-placement file-full libraries container id))) diff --git a/common/test/common_tests/logic/comp_reset_test.cljc b/common/test/common_tests/logic/comp_reset_test.cljc index 3dd2bf348e..c9eb24e5af 100644 --- a/common/test/common_tests/logic/comp_reset_test.cljc +++ b/common/test/common_tests/logic/comp_reset_test.cljc @@ -15,6 +15,7 @@ [app.common.test-helpers.composable.comp.runner :as r] [app.common.test-helpers.composable.comp.setups :as setup] [app.common.test-helpers.composable.core :as tm] + [app.common.test-helpers.composable.gen :as cg] [app.common.test-helpers.compositions :as tho] [app.common.test-helpers.files :as thf] [app.common.test-helpers.ids-map :as thi] @@ -650,3 +651,25 @@ (t/is (gsh/close-attrs? :points (:points child) (:points after) 0.001)) (t/is (= (:rotation child) (:rotation after))) (t/is (nil? (:touched (tm/role-shape reset :copy-instance)))))) + +(t/deftest reset-a-moved-nested-copy-returns-it-to-its-position + (let [initial (setup/nested-component-with-copy) + head (tm/role-shape initial :copy-instance) + child (tm/shape-by-id initial (first (:shapes head))) + moved (-> initial (r/step (n/transform-shape :copy-instance :move [5 -3])) r/settle) + reset (-> moved (r/step (n/reset :copy-instance)) r/settle) + after (tm/role-shape reset :copy-instance)] + (t/is (gsh/close-attrs? :points (:points head) (:points after) 0.001)) + (t/is (gsh/close-attrs? :points (:points child) (:points (tm/shape-by-id reset (:id child))) 0.001)) + (t/is (nil? (:touched after))) + (t/is (= (thn/normalize-file (tm/file moved)) (thn/normalize-file (tm/file (r/step reset (n/undo)))))) + (t/is (map? (cg/run {:world [2 0 {:variants true :rich-variants true :text true :text-styles true + :library true :layout-copies true}] + :ops [[:delete 0 0 0 0] [:nest 4 0 0 0] [:transform 6 0 0 13] + [:edit 4 1 1 :copy-head] [:reset 4]]}))))) + +(t/deftest reset-keeps-an-unmoved-nested-copy-in-its-accepted-placement + ;; The library turned the copy's main; the consumer has not accepted it. + (t/is (map? (cg/run {:world [2 0 {:library true :stale-libraries true + :library-dependency true :library-text true}] + :ops [[:library-transform 14 3 0 0] [:edit 13 1 1 :copy-head] [:reset 13]]}))))