diff --git a/frontend/src/app/main/data/workspace/svg_upload.cljs b/frontend/src/app/main/data/workspace/svg_upload.cljs index 48a84f0b30..3f438739f7 100644 --- a/frontend/src/app/main/data/workspace/svg_upload.cljs +++ b/frontend/src/app/main/data/workspace/svg_upload.cljs @@ -88,15 +88,20 @@ base-id (:parent-id base) - frame-id (or frame-id - (if (or selected-frame? (empty? selected) - (not= :frame (dm/get-in objects [base-id :type]))) - (ctst/top-nested-frame objects position) - base-id)) + keep-parent? (not (or selected-frame? (empty? selected) (nil? base))) - parent-id (if (or selected-frame? (empty? selected) (nil? base)) - frame-id - base-id) + frame-id (or frame-id + (cond + (not keep-parent?) + (ctst/top-nested-frame objects position) + + (= :frame (dm/get-in objects [base-id :type])) + base-id + + :else + (dm/get-in objects [base-id :frame-id]))) + + parent-id (if keep-parent? base-id frame-id) [new-shape new-children] (sb/create-svg-shapes id svg-data position objects frame-id parent-id selected true) diff --git a/frontend/test/frontend_tests/logic/pasting_in_containers_test.cljs b/frontend/test/frontend_tests/logic/pasting_in_containers_test.cljs index 46602ebf2c..3a14c80b4b 100644 --- a/frontend/test/frontend_tests/logic/pasting_in_containers_test.cljs +++ b/frontend/test/frontend_tests/logic/pasting_in_containers_test.cljs @@ -5,6 +5,7 @@ ;; Copyright (c) KALEIDOS SUBSIDIARY SL (ns frontend-tests.logic.pasting-in-containers-test (:require + [app.common.data :as d] [app.common.geom.point :as gpt] [app.common.test-helpers.components :as cthc] [app.common.test-helpers.compositions :as ctho] @@ -16,6 +17,7 @@ [app.common.types.component :as ctk] [app.common.uuid :as uuid] [app.main.data.workspace :as dw] + [app.main.data.workspace.groups :as dwgr] [app.main.data.workspace.selection :as dws] [app.main.data.workspace.shapes :as dwsh] [app.main.data.workspace.svg-upload :as dwsvg] @@ -998,6 +1000,111 @@ (t/is (some? created')) (t/is (= (:id frame-blue) (:parent-id created'))))))))) +(defn- svg-upload-in-group-events + "Group `rect`, select it again inside the group and upload an SVG at + `position`." + [rect position] + (let [svg-data {:name "test.svg" + :attrs {:width 100 :height 100} + :content [{:tag :rect + :attrs {:x "10" :y "10" :width "20" :height "20"}}]}] + [(dws/select-shapes (d/ordered-set (:id rect))) + (dwgr/group-selected) + (dws/select-shapes (d/ordered-set (:id rect))) + (dwsvg/add-svg-shapes nil svg-data position nil)])) + +(defn- check-svg-in-group + [new-state expected-frame-id] + (let [file' (ths/get-file-from-state new-state) + objects (:objects (cthf/current-page file')) + svg' (->> (vals objects) + (filter #(= (:name %) "test")) + first) + parent' (get objects (:parent-id svg'))] + ;; The shape is created inside the group, with the group's frame + (t/is (some? svg')) + (t/is (= :group (:type parent'))) + (t/is (= expected-frame-id (:frame-id svg'))) + (t/is (= (:frame-id parent') (:frame-id svg'))) + (cthf/validate-file! file'))) + +(t/deftest svg-upload-in-group-inside-board-uses-group-frame + "Uploading an SVG with a selection inside a grouped board child keeps the board as frame, even with the cursor outside it" + (t/async + done + (let [;; ==== Setup + file (-> (cthf/sample-file :file1) + (ctho/add-frame :board {:name "board" :x 0 :y 0 :width 500 :height 500}) + (cths/add-sample-shape :rect1 :parent-label :board + :x 10 :y 10 :width 50 :height 50)) + store (ths/setup-store file) + + ;; ==== Action + events (svg-upload-in-group-events (cths/get-shape file :rect1) + (gpt/point 2000 2000))] + + (ths/run-store + store done events + (fn [new-state] + (check-svg-in-group new-state (:id (cths/get-shape file :board)))))))) + +(t/deftest svg-upload-in-group-at-root-uses-group-frame + "Uploading an SVG with a selection inside a group at the root keeps the root as frame, even with the cursor over a board" + (t/async + done + (let [;; ==== Setup + file (-> (cthf/sample-file :file1) + (ctho/add-frame :board {:name "board" :x 1000 :y 1000 :width 500 :height 500}) + (cths/add-sample-shape :rect1 :x 10 :y 10 :width 50 :height 50)) + store (ths/setup-store file) + + ;; ==== Action + events (svg-upload-in-group-events (cths/get-shape file :rect1) + (gpt/point 1200 1200))] + + (ths/run-store + store done events + (fn [new-state] + (check-svg-in-group new-state uuid/zero)))))) + +(t/deftest svg-upload-in-board-keeps-parent-board + "Uploading an SVG with a shape selected directly in a board keeps that board as frame, even with the cursor over another board" + (t/async + done + (let [;; ==== Setup + file (-> (cthf/sample-file :file1) + (ctho/add-frame :board-a {:name "board-a" :x 0 :y 0 :width 500 :height 500}) + (ctho/add-frame :board-b {:name "board-b" :x 1000 :y 1000 :width 500 :height 500}) + (cths/add-sample-shape :rect1 :parent-label :board-a + :x 10 :y 10 :width 50 :height 50)) + store (ths/setup-store file) + + ;; ==== Action + svg-data {:name "test.svg" + :attrs {:width 100 :height 100} + :content [{:tag :rect + :attrs {:x "10" :y "10" :width "20" :height "20"}}]} + + events [(dws/select-shapes (d/ordered-set (:id (cths/get-shape file :rect1)))) + (dwsvg/add-svg-shapes nil svg-data (gpt/point 1200 1200) nil)]] + + (ths/run-store + store done events + (fn [new-state] + (let [;; ==== Get + file' (ths/get-file-from-state new-state) + objects (:objects (cthf/current-page file')) + svg' (->> (vals objects) + (filter #(= (:name %) "test")) + first) + board-a (cths/get-shape file :board-a)] + + ;; ==== Check + (t/is (some? svg')) + (t/is (= (:id board-a) (:frame-id svg'))) + (t/is (= (:id board-a) (:parent-id svg'))) + (cthf/validate-file! file'))))))) + (t/deftest props-paste-with-unloaded-page-is-noop "Pasting props while the page is not loaded is ignored instead of crashing" (t/async