Fix pasting file - fixes #8926

Also remove unused mocks in formatted paste and clean up
editor-on-paste!
pull/8949/head
Gabriel Horner 2023-03-28 13:56:03 -04:00 committed by Andelf
parent 37230c229f
commit 07a3adcb52
2 changed files with 37 additions and 21 deletions

View File

@ -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")
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))))]
html (.getData clipboard-data "text/html")
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"

View File

@ -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 "<meta charset='utf-8'><img src=\"https://user-images.githubusercontent.com/38045018/228234385-cbbcc6b2-1168-40da-ab3e-1e506edd5fce.png\"/>"
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)))))