diff --git a/frontend/src/app/main/data/workspace/clipboard.cljs b/frontend/src/app/main/data/workspace/clipboard.cljs index d9113d8a8b..3911c08436 100644 --- a/frontend/src/app/main/data/workspace/clipboard.cljs +++ b/frontend/src/app/main/data/workspace/clipboard.cljs @@ -906,7 +906,10 @@ variant-props (:variant-properties pdata) - position (deref ms/mouse-position) + ;; Before the pointer has been over the viewport there is + ;; no mouse position, so the paste goes to its center. + position (or (deref ms/mouse-position) + (dsh/get-viewport-center state)) ;; Replace mode is only valid with a single selected shape. ;; In that case we drop the pasted content at its position and @@ -1049,7 +1052,8 @@ (gsh/shape->center frame-object)) :else - (deref ms/mouse-position))) + (or (deref ms/mouse-position) + (dsh/get-viewport-center state)))) (defn- paste-html-text [html] 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..1390af5237 100644 --- a/frontend/test/frontend_tests/logic/pasting_in_containers_test.cljs +++ b/frontend/test/frontend_tests/logic/pasting_in_containers_test.cljs @@ -132,6 +132,32 @@ ;; ==== Check (t/is (= (:parent-id copied-blue1') (:id frame-red'))))))))) +(t/deftest paste-before-the-pointer-reaches-the-viewport-uses-its-center + "With no mouse position yet, a paste with nothing selected lands at the + center of the viewport." + (t/async + done + (let [file (-> (cthf/sample-file :file1) + (ctho/add-frame :frame-red {:name "frame-red"})) + store (ths/setup-store file) + page (cthf/current-page file) + frame (cths/get-shape file :frame-red) + pdata (thp/simulate-copy-shape #{(:id frame)} (:objects page) {(:id file) file} page file #{} 67) + _ (rx/push! ms/mouse-position nil) + events [#(assoc-in % [:workspace-local :vbox] {:x 0 :y 0 :width 1000 :height 800}) + (dws/deselect-all) + (dw/paste-shapes pdata)]] + (ths/run-store + store done events + (fn [new-state] + (let [page' (cthf/current-page (ths/get-file-from-state new-state)) + copied (->> (vals (:objects page')) + (filter #(and (= "frame-red" (:name %)) (not= (:id frame) (:id %)))) + first)] + (t/is (some? copied)) + (t/is (= 500 (:x copied))) + (t/is (= 400 (:y copied))))))))) + (t/deftest copy-shape-to-component "Coping a rect into a component results in a copy of the rect inside the component" (t/async