Split raw and formatted paste to prevent future coupling bugs

This commit is contained in:
Gabriel Horner
2023-03-17 18:15:54 -04:00
committed by Gabriel Horner
parent 72298d23c8
commit 7a905583cf
3 changed files with 53 additions and 39 deletions

View File

@@ -182,38 +182,50 @@
(paste-copied-blocks-or-text text e html)))
(defn editor-on-paste!
([id]
(editor-on-paste! id false))
([id raw-paste?]
(fn [e]
(state/set-state! :editor/on-paste? true)
(let [input (state/get-input)]
(if raw-paste?
(utils/getClipText
(fn [clipboard-data]
(when (state/get-input)
(editor-handler/insert clipboard-data true)))
(fn [error]
(js/console.error error)))
(let [clipboard-data (gobj/get e "clipboardData")
html (when-not raw-paste? (.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))))]
(cond
(and (string/blank? text) (string/blank? html)) (paste-file-if-exist)
(and (seq files) (state/preferred-pasting-file?)) (paste-file-if-exist)
:else
(let [text' (or (when (gp-util/url? text)
(wrap-macro-url text))
text)]
(paste-text-or-blocks-aux input e text' html)))))))))
"Pastes with formatting and includes the following features:
- handles internal pastes to correctly paste at the block level
- formatted paste includes HTML if detected
- special handling for newline and new blocks
- pastes file if it exists
- wraps certain urls with macros
- wraps selected urls with link formatting
- whiteboard friendly pasting
- paste replaces selected text"
[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))))]
(cond
(and (string/blank? text) (string/blank? html)) (paste-file-if-exist)
(and (seq files) (state/preferred-pasting-file?)) (paste-file-if-exist)
:else
(let [text' (or (when (gp-util/url? text)
(wrap-macro-url text))
text)]
(paste-text-or-blocks-aux input e text' html))))))
(defn editor-on-paste-raw!
"Raw pastes without _any_ formatting. Can also replace selected text with a paste"
[]
(state/set-state! :editor/on-paste? true)
(utils/getClipText
(fn [clipboard-data]
(when (state/get-input)
(commands/delete-selection! (state/get-edit-input-id))
(editor-handler/insert clipboard-data true)))
(fn [error]
(js/console.error error))))