fix: internal blocks paste

This commit is contained in:
Tienson Qin
2023-03-29 00:29:38 +08:00
committed by Andelf
parent 07a3adcb52
commit bc635eaf4b
9 changed files with 108 additions and 91 deletions

View File

@@ -18,7 +18,8 @@
[cljs.core.match :refer [match]]
[frontend.util.text :as text-util]
[frontend.format.mldoc :as mldoc]
[lambdaisland.glogi :as log]))
[lambdaisland.glogi :as log]
[promesa.core :as p]))
(defn- paste-text-parseable
[format text]
@@ -85,79 +86,78 @@
;; todo: logseq/whiteboard-shapes is now text/html
[text e html]
(util/stop e)
(let [copied-blocks (state/get-copied-blocks)
input (state/get-input)
input-id (state/get-edit-input-id)
text (string/replace text "\r\n" "\n") ;; Fix for Windows platform
replace-text-f (fn [text]
(commands/delete-selection! input-id)
(commands/simple-insert! input-id text nil))
internal-paste? (and
(seq (:copy/blocks copied-blocks))
;; not copied from the external clipboard
(= (string/trimr text)
(string/trimr (:copy/content copied-blocks))))]
(if internal-paste?
(let [blocks (:copy/blocks copied-blocks)]
(when (seq blocks)
(editor-handler/paste-blocks blocks {})))
(let [shape-refs-text (when (and (not (string/blank? html))
(get-whiteboard-tldr-from-text html))
;; text should always be prepared block-ref generated in tldr
text)
{:keys [value selection] :as selection-and-format} (editor-handler/get-selection-and-format)
text-url? (gp-util/url? text)
selection-url? (gp-util/url? selection)]
(cond
(not (string/blank? shape-refs-text))
(commands/simple-insert! input-id shape-refs-text nil)
(p/let [clipboard-items (js/navigator.clipboard.read)
blocks-blob ^js (.getType ^js (first clipboard-items)
"web application/logseq")
blocks-str (when blocks-blob (.text blocks-blob))
copied-blocks (when blocks-str
(gp-util/safe-read-string blocks-str))]
(let [input (state/get-input)
input-id (state/get-edit-input-id)
text (string/replace text "\r\n" "\n") ;; Fix for Windows platform
replace-text-f (fn [text]
(commands/delete-selection! input-id)
(commands/simple-insert! input-id text nil))
internal-paste? (seq copied-blocks)]
(if internal-paste?
(editor-handler/paste-blocks copied-blocks {})
(let [shape-refs-text (when (and (not (string/blank? html))
(get-whiteboard-tldr-from-text html))
;; text should always be prepared block-ref generated in tldr
text)
{:keys [value selection] :as selection-and-format} (editor-handler/get-selection-and-format)
text-url? (gp-util/url? text)
selection-url? (gp-util/url? selection)]
(cond
(not (string/blank? shape-refs-text))
(commands/simple-insert! input-id shape-refs-text nil)
(or (and (or text-url? selection-url?)
(selection-within-link? selection-and-format))
(and text-url? selection-url?))
(replace-text-f text)
(or (and (or text-url? selection-url?)
(selection-within-link? selection-and-format))
(and text-url? selection-url?))
(replace-text-f text)
(and (or text-url?
(and value (gp-util/url? (string/trim value))))
(not (string/blank? (util/get-selected-text))))
(editor-handler/html-link-format! text)
(and (or text-url?
(and value (gp-util/url? (string/trim value))))
(not (string/blank? (util/get-selected-text))))
(editor-handler/html-link-format! text)
(and (block-ref/block-ref? text)
(editor-handler/wrapped-by? input block-ref/left-parens block-ref/right-parens))
(commands/simple-insert! input-id (block-ref/get-block-ref-id text) nil)
(and (block-ref/block-ref? text)
(editor-handler/wrapped-by? input block-ref/left-parens block-ref/right-parens))
(commands/simple-insert! input-id (block-ref/get-block-ref-id text) nil)
:else
;; from external
(let [format (or (db/get-page-format (state/get-current-page)) :markdown)
html-text (let [result (when-not (string/blank? html)
(try
(html-parser/convert format html)
(catch :default e
(log/error :exception e)
nil)))]
(if (string/blank? result) nil result))
text (or html-text text)]
(match [format
(nil? (util/safe-re-find #"(?m)^\s*(?:[-+*]|#+)\s+" text))
(nil? (util/safe-re-find #"(?m)^\s*\*+\s+" text))
(nil? (util/safe-re-find #"(?:\r?\n){2,}" text))]
[:markdown false _ _]
(paste-text-parseable format text)
:else
;; from external
(let [format (or (db/get-page-format (state/get-current-page)) :markdown)
html-text (let [result (when-not (string/blank? html)
(try
(html-parser/convert format html)
(catch :default e
(log/error :exception e)
nil)))]
(if (string/blank? result) nil result))
text (or html-text text)]
(match [format
(nil? (util/safe-re-find #"(?m)^\s*(?:[-+*]|#+)\s+" text))
(nil? (util/safe-re-find #"(?m)^\s*\*+\s+" text))
(nil? (util/safe-re-find #"(?:\r?\n){2,}" text))]
[:markdown false _ _]
(paste-text-parseable format text)
[:org _ false _]
(paste-text-parseable format text)
[:org _ false _]
(paste-text-parseable format text)
[:markdown true _ false]
(paste-segmented-text format text)
[:markdown true _ false]
(paste-segmented-text format text)
[:markdown true _ true]
(replace-text-f text)
[:markdown true _ true]
(replace-text-f text)
[:org _ true false]
(paste-segmented-text format text)
[:org _ true false]
(paste-segmented-text format text)
[:org _ true true]
(replace-text-f text))))))))
[:org _ true true]
(replace-text-f text)))))))))
(defn paste-text-in-one-block-at-point
[]