diff --git a/common/src/app/common/logic/variants.cljc b/common/src/app/common/logic/variants.cljc index 666a97329d..fa494d61c5 100644 --- a/common/src/app/common/logic/variants.cljc +++ b/common/src/app/common/logic/variants.cljc @@ -493,6 +493,23 @@ (when-let [parent (get objects parent-id)] (or (d/index-of (:shapes parent) shape-id) 0))) +(defn- sync-restored-position + "A swap back is an ordinary copy of its position, including that + position's content overrides. Sync through the head that owns its ref." + [changes page libraries shape-id] + (let [objects (pcb/get-objects changes) + shape (get objects shape-id) + parent (get objects (:parent-id shape)) + container (ctn/make-container (assoc page :objects objects) :page)] + (if (and (:shape-ref parent) (not (ctk/swapped? shape))) + (let [component (ctf/find-ref-component nil container libraries shape) + head (d/seek #(= (:id component) (:component-id %)) + (ctn/get-parent-copy-heads objects parent))] + (if head + (cll/generate-sync-shape-direct changes nil libraries container (:id head) false) + changes)) + changes))) + (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 @@ -522,6 +539,8 @@ (ctf/find-ref-shape nil page libraries shape :include-deleted? true :with-context? true))) []]) + changes (sync-restored-position changes page libraries (:id new-shape)) + ;; 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/swap_group_test.cljc b/common/test/common_tests/logic/swap_group_test.cljc index e31be06860..3ee293298f 100644 --- a/common/test/common_tests/logic/swap_group_test.cljc +++ b/common/test/common_tests/logic/swap_group_test.cljc @@ -966,3 +966,22 @@ (t/is (= (:fills main-child) (:fills (ths/get-shape-by-id full (:id main-child))))) (t/is (= (thn/normalize-file (tm/file edited)) (thn/normalize-file (tm/file (r/step updated (n/undo)))))))) + +(t/deftest swapping-back-inherits-the-new-positions-content-overrides + (let [initial (tm/make-situation (setup)) + child-id (first (:shapes (tm/resolve-shape initial :card-star))) + edited (-> initial + (r/step (n/change-property :card-star :fills "#333333")) r/settle + (r/step (n/change-property (constantly child-id) :fills "#444444")) r/settle) + swapped (-> edited (r/step (n/swap-shape :copy-star :star)) r/settle) + head (tm/resolve-shape swapped :copy-star) + child (tm/shape-by-id swapped (first (:shapes head))) + file (tm/file swapped) + full (:file (r/sync-cascade file {} (keys (get-in file [:data :components]))))] + (t/is (= "#333333" (get-in head [:fills 0 :fill-color]))) + (t/is (= "#444444" (get-in child [:fills 0 :fill-color]))) + (t/is (empty? (:touched head))) + (t/is (empty? (:touched child))) + (t/is (= (:fills head) (:fills (ths/get-shape-by-id full (:id head))))) + (t/is (= (thn/normalize-file (tm/file edited)) + (thn/normalize-file (tm/file (r/step swapped (n/undo))))))))