diff --git a/src/main/frontend/handler/paste.cljs b/src/main/frontend/handler/paste.cljs
index 3c307d6bd..3193c4354 100644
--- a/src/main/frontend/handler/paste.cljs
+++ b/src/main/frontend/handler/paste.cljs
@@ -181,6 +181,16 @@
(paste-text-in-one-block-at-point))
(paste-copied-blocks-or-text text e html)))
+(defn- paste-file-if-exists [id e]
+ (when id
+ (let [clipboard-data (gobj/get e "clipboardData")
+ files (.-files clipboard-data)]
+ (when-let [file (first files)]
+ (when-let [block (state/get-edit-block)]
+ (editor-handler/upload-asset id #js[file] (:block/format block)
+ editor-handler/*asset-uploading? true)))
+ (util/stop e))))
+
(defn editor-on-paste!
"Pastes with formatting and includes the following features:
- handles internal pastes to correctly paste at the block level
@@ -194,29 +204,19 @@
[id]
(fn [e]
(state/set-state! :editor/on-paste? true)
- (let [input (state/get-input)
- clipboard-data (gobj/get e "clipboardData")
+ (let [clipboard-data (gobj/get e "clipboardData")
html (.getData clipboard-data "text/html")
- text (.getData clipboard-data "text")
- files (.-files text)
- paste-file-if-exist (fn []
- (when id
- (let [_handled
- (let [clipboard-data (gobj/get e "clipboardData")
- files (.-files clipboard-data)]
- (when-let [file (first files)]
- (when-let [block (state/get-edit-block)]
- (editor-handler/upload-asset id #js[file] (:block/format block)
- editor-handler/*asset-uploading? true))))]
- (util/stop e))))]
+ text (.getData clipboard-data "text")]
(cond
- (and (string/blank? text) (string/blank? html)) (paste-file-if-exist)
- (and (seq files) (state/preferred-pasting-file?)) (paste-file-if-exist)
+ (and (string/blank? text) (string/blank? html))
+ (paste-file-if-exists id e)
+ (and (seq (.-files clipboard-data)) (state/preferred-pasting-file?))
+ (paste-file-if-exists id e)
:else
(let [text' (or (when (gp-util/url? text)
(wrap-macro-url text))
text)]
- (paste-text-or-blocks-aux input e text' html))))))
+ (paste-text-or-blocks-aux (state/get-input) e text' html))))))
(defn editor-on-paste-raw!
"Raw pastes without _any_ formatting. Can also replace selected text with a paste"
diff --git a/src/test/frontend/handler/paste_test.cljs b/src/test/frontend/handler/paste_test.cljs
index 65f17f4dd..f3b00e9aa 100644
--- a/src/test/frontend/handler/paste_test.cljs
+++ b/src/test/frontend/handler/paste_test.cljs
@@ -110,8 +110,7 @@
expected-paste "{{video https://www.youtube.com/watch?v=xu9p5ynlhZk}}"]
(test-helper/with-reset
reset
- [utils/getClipText (fn [cb] (cb clipboard))
- state/get-input (constantly #js {:value "block"})
+ [state/get-input (constantly #js {:value "block"})
;; paste-copied-blocks-or-text mocks below
commands/delete-selection! (constantly nil)
commands/simple-insert! (fn [_input text] (p/resolved text))
@@ -128,8 +127,7 @@
block-content "test:: before"]
(test-helper/with-reset
reset
- [utils/getClipText (fn [cb] (cb clipboard))
- state/get-input (constantly #js {:value block-content})
+ [state/get-input (constantly #js {:value block-content})
;; paste-copied-blocks-or-text mocks below
commands/delete-selection! (constantly nil)
commands/simple-insert! (fn [_input text] (p/resolved text))
@@ -139,3 +137,21 @@
#js {:clipboardData #js {:getData (constantly clipboard)}})]
(is (= expected-paste result))
(reset)))))
+
+(deftest-async editor-on-paste-with-file-pasting
+ (let [clipboard ""
+ files [{:name "image.png" :type "image/png" :size 11836}]
+ pasted-file (atom nil)]
+ (test-helper/with-reset
+ reset
+ [state/preferred-pasting-file? (constantly true)
+ ;; paste-file-if-exists mocks below
+ editor-handler/upload-asset (fn [_id file & _]
+ (reset! pasted-file file))
+ util/stop (constantly nil)
+ state/get-edit-block (constantly {})]
+ (p/let [_ ((paste-handler/editor-on-paste! :fake-id)
+ #js {:clipboardData #js {:getData (constantly clipboard)
+ :files files}})]
+ (is (= files (js->clj @pasted-file)))
+ (reset)))))