diff --git a/common/src/app/common/logic/libraries.cljc b/common/src/app/common/logic/libraries.cljc index 330fa596ea..5566ab449e 100644 --- a/common/src/app/common/logic/libraries.cljc +++ b/common/src/app/common/logic/libraries.cljc @@ -2120,26 +2120,26 @@ (reduce (fn [changes child] (generate-duplicate-shape-change changes - (:objects page') - page - unames - update-unames! - ids-map - child - delta - level-delta - libraries - library-data - file-id - (if frame? new-id frame-id) - new-id - duplicating-component? - true - (and remove-swap-slot? + (:objects page') + page + unames + update-unames! + ids-map + child + delta + level-delta + libraries + library-data + file-id + (if frame? new-id frame-id) + new-id + duplicating-component? + true + (and remove-swap-slot? ;; only remove swap slot of children when the current shape ;; is not a subinstance head nor a instance root - (not subinstance-head?) - (not instance-root?)))) + (not subinstance-head?) + (not instance-root?)))) changes (map (d/getf objects) (:shapes obj))))))) @@ -2162,17 +2162,17 @@ changes (->> shapes (reduce #(generate-duplicate-shape-change %1 - all-objects - page - unames - update-unames! - ids-map - %2 - delta - nil - libraries - library-data - file-id) + all-objects + page + unames + update-unames! + ids-map + %2 + delta + nil + libraries + library-data + file-id) changes)) ;; We need to check the changes to get the ids-map diff --git a/common/test/common_tests/logic/comp_remove_swap_slots_test.cljc b/common/test/common_tests/logic/comp_remove_swap_slots_test.cljc index a1e0b4aad1..17c534c22f 100644 --- a/common/test/common_tests/logic/comp_remove_swap_slots_test.cljc +++ b/common/test/common_tests/logic/comp_remove_swap_slots_test.cljc @@ -7,6 +7,8 @@ (ns common-tests.logic.comp-remove-swap-slots-test (:require [app.common.files.changes-builder :as pcb] + [app.common.geom.point :as gpt] + [app.common.logic.libraries :as cll] [app.common.logic.shapes :as cls] [app.common.types.component :as ctk] [app.common.uuid :as uuid] @@ -36,14 +38,14 @@ (-> (thf/sample-file :file1) (tho/add-frame :frame-red) (thc/make-component :red :frame-red) - (tho/add-frame :frame-blue) + (tho/add-frame :frame-blue :name "frame-blue") (thc/make-component :blue :frame-blue) (tho/add-frame :frame-green) (thc/make-component :green :frame-green) (thc/instantiate-component :red :red-copy-green :parent-label :frame-green) (tho/add-frame :frame-b1) (thc/make-component :b1 :frame-b1) - (tho/add-frame :frame-yellow :parent-label :frame-b1) + (tho/add-frame :frame-yellow :parent-label :frame-b1 :name "frame-yellow") (thc/instantiate-component :red :red-copy :parent-label :frame-b1) (thc/component-swap :red-copy :blue :blue1) (thc/instantiate-component :green :green-copy :parent-label :frame-b1 :children-labels [:red-copy-in-green-copy]) @@ -51,7 +53,7 @@ (tho/add-frame :frame-b2) (thc/make-component :b2 :frame-b2))) -(t/deftest test-keep-swap-slot-relocating-blue1-to-root +(t/deftest test-remove-swap-slot-relocating-blue1-to-root (let [;; ==== Setup file (setup-file) @@ -80,7 +82,7 @@ (t/is (some? blue1')) (t/is (nil? (ctk/get-swap-slot blue1'))))) -(t/deftest test-keep-swap-slot-move-blue1-to-root +(t/deftest test-remove-swap-slot-move-blue1-to-root (let [;; ==== Setup file (setup-file) page (thf/current-page file) @@ -110,7 +112,7 @@ (t/is (nil? (ctk/get-swap-slot blue1'))))) -(t/deftest test-keep-swap-slot-relocating-blue1-to-b2 +(t/deftest test-remove-swap-slot-relocating-blue1-to-b2 (let [;; ==== Setup file (setup-file) page (thf/current-page file) @@ -140,7 +142,7 @@ (t/is (some? blue1')) (t/is (nil? (ctk/get-swap-slot blue1'))))) -(t/deftest test-keep-swap-slot-move-blue1-to-b2 +(t/deftest test-remove-swap-slot-move-blue1-to-b2 (let [;; ==== Setup file (setup-file) page (thf/current-page file) @@ -171,7 +173,7 @@ (t/is (some? blue1')) (t/is (nil? (ctk/get-swap-slot blue1'))))) -(t/deftest test-keep-swap-slot-relocating-yellow-to-root +(t/deftest test-remove-swap-slot-relocating-yellow-to-root (let [;; ==== Setup file (setup-file) page (thf/current-page file) @@ -214,7 +216,7 @@ (t/is (some? blue1'')) (t/is (nil? (ctk/get-swap-slot blue1''))))) -(t/deftest test-keep-swap-slot-move-yellow-to-root +(t/deftest test-remove-swap-slot-move-yellow-to-root (let [;; ==== Setup file (setup-file) page (thf/current-page file) @@ -258,7 +260,7 @@ (t/is (nil? (ctk/get-swap-slot blue1''))))) -(t/deftest test-keep-swap-slot-relocating-yellow-to-b2 +(t/deftest test-remove-swap-slot-relocating-yellow-to-b2 (let [;; ==== Setup file (setup-file) page (thf/current-page file) @@ -302,7 +304,7 @@ (t/is (some? blue1'')) (t/is (nil? (ctk/get-swap-slot blue1''))))) -(t/deftest test-keep-swap-slot-move-yellow-to-b2 +(t/deftest test-remove-swap-slot-move-yellow-to-b2 (let [;; ==== Setup file (setup-file) page (thf/current-page file) @@ -346,3 +348,105 @@ ;; blue1 has not swap-id after move (t/is (some? blue1'')) (t/is (nil? (ctk/get-swap-slot blue1''))))) + +(defn- find-duplicated-shape + [original-shape page] + ;; duplicated shape has the same name, the same parent, and doesn't have a label + (->> (vals (:objects page)) + (filter #(and (= (:name %) (:name original-shape)) + (= (:parent-id %) (:parent-id original-shape)) + (nil? (thi/label (:id %))))) + first)) + +(t/deftest test-remove-swap-slot-duplicating-blue1 + (let [;; ==== Setup + file (setup-file) + + page (thf/current-page file) + blue1 (ths/get-shape file :blue1) + + ;; ==== Action + changes (-> (pcb/empty-changes nil) + (cll/generate-duplicate-changes (:objects page) ;; objects + page ;; page + #{(:id blue1)} ;; ids + (gpt/point 0 0) ;; delta + {(:id file) file} ;; libraries + (:data file) ;; library-data + (:id file)) ;; file-id + (cll/generate-duplicate-changes-update-indices (:objects page) ;; objects + #{(:id blue1)})) ;; ids + + + + file' (thf/apply-changes file changes) + + ;; ==== Get + page' (thf/current-page file') + blue1' (ths/get-shape file' :blue1) + duplicated-blue1' (find-duplicated-shape blue1' page')] + + ;; ==== Check + + ;; blue1 has swap-id + (t/is (some? (ctk/get-swap-slot blue1'))) + + ;; duplicated-blue1 has not swap-id + (t/is (some? duplicated-blue1')) + (t/is (nil? (ctk/get-swap-slot duplicated-blue1'))))) + +(t/deftest test-remove-swap-slot-duplicate-yellow + (let [;; ==== Setup + file (setup-file) + page (thf/current-page file) + blue1 (ths/get-shape file :blue1) + yellow (ths/get-shape file :frame-yellow) + + ;; ==== Action + ;; Move blue1 into yellow + changes (cls/generate-move-shapes-to-frame (pcb/empty-changes nil) + #{(:id blue1)} ;; ids + (:id yellow) ;; frame-id + (:id page) ;; page-id + (:objects page) ;; objects + 0 ;; drop-index + nil) ;; cell + + file' (thf/apply-changes file changes) + page' (thf/current-page file') + yellow' (ths/get-shape file' :frame-yellow) + + ;; Duplicate yellow + changes' (-> (pcb/empty-changes nil) + (cll/generate-duplicate-changes (:objects page') ;; objects + page' ;; page + #{(:id yellow')} ;; ids + (gpt/point 0 0) ;; delta + {(:id file') file'} ;; libraries + (:data file') ;; library-data + (:id file')) ;; file-id + (cll/generate-duplicate-changes-update-indices (:objects page') ;; objects + #{(:id yellow')})) ;; ids + + file'' (thf/apply-changes file' changes') + + ;; ==== Get + page'' (thf/current-page file'') + blue1'' (ths/get-shape file'' :blue1) + yellow'' (ths/get-shape file'' :frame-yellow) + + + duplicated-yellow'' (find-duplicated-shape yellow'' page'') + duplicated-blue1-id'' (-> duplicated-yellow'' + :shapes + first) + duplicated-blue1'' (get (:objects page'') duplicated-blue1-id'')] + + ;; ==== Check + + ;; blue1'' has swap-id + (t/is (some? (ctk/get-swap-slot blue1''))) + + ;; duplicated-blue1'' has not swap-id + (t/is (some? duplicated-blue1'')) + (t/is (nil? (ctk/get-swap-slot duplicated-blue1'')))))