-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
paste.cljs
224 lines (203 loc) · 9.63 KB
/
paste.cljs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
(ns ^:no-doc frontend.handler.paste
(:require [frontend.state :as state]
[frontend.db :as db]
[frontend.format.block :as block]
[logseq.graph-parser.util :as gp-util]
[logseq.graph-parser.mldoc :as gp-mldoc]
[logseq.graph-parser.block :as gp-block]
[logseq.graph-parser.util.block-ref :as block-ref]
[clojure.string :as string]
[frontend.util :as util]
[frontend.handler.editor :as editor-handler]
[frontend.extensions.html-parser :as html-parser]
[goog.object :as gobj]
[frontend.mobile.util :as mobile-util]
[frontend.util.thingatpt :as thingatpt]
["/frontend/utils" :as utils]
[frontend.commands :as commands]
[cljs.core.match :refer [match]]
[frontend.handler.notification :as notification]
[frontend.util.text :as text-util]
[frontend.format.mldoc :as mldoc]
[lambdaisland.glogi :as log]))
(defn- paste-text-parseable
[format text]
(when-let [editing-block (state/get-edit-block)]
(let [page-id (:db/id (:block/page editing-block))
blocks (block/extract-blocks
(mldoc/->edn text (gp-mldoc/default-config format))
text format
{:page-name (:block/name (db/entity page-id))})
blocks' (gp-block/with-parent-and-left page-id blocks)]
(editor-handler/paste-blocks blocks' {}))))
(defn- paste-segmented-text
[format text]
(let [paragraphs (string/split text #"(?:\r?\n){2,}")
updated-paragraphs
(string/join "\n"
(mapv (fn [p] (->> (string/trim p)
((fn [p]
(if (util/safe-re-find (if (= format :org)
#"\s*\*+\s+"
#"\s*-\s+") p)
p
(str (if (= format :org) "* " "- ") p))))))
paragraphs))]
(paste-text-parseable format updated-paragraphs)))
(defn- wrap-macro-url
[url]
(cond
(boolean (text-util/get-matched-video url))
(util/format "{{video %s}}" url)
(string/includes? url "twitter.com")
(util/format "{{twitter %s}}" url)
:else
(do
(notification/show! (util/format "No macro is available for %s" url) :warning)
nil)))
(defn- try-parse-as-json
"Result is not only to be an Object.
Maybe JSON types like string, number, boolean, null, array"
[text]
(try (js/JSON.parse text)
(catch :default _ #js{})))
(defn- get-whiteboard-tldr-from-text
[text]
(when-let [matched-text (util/safe-re-find #"<whiteboard-tldr>(.*)</whiteboard-tldr>"
(gp-util/safe-decode-uri-component text))]
(try-parse-as-json (second matched-text))))
(defn- selection-within-link?
[selection-and-format]
(let [{:keys [format selection-start selection-end selection value]} selection-and-format]
(and (not= selection-start selection-end)
(->> (case format
:markdown (util/re-pos #"\[.*?\]\(.*?\)" value)
:org (util/re-pos #"\[\[.*?\]\[.*?\]\]" value))
(some (fn [[start-index matched-text]]
(and (<= start-index selection-start)
(>= (+ start-index (count matched-text)) selection-end)
(clojure.string/includes? matched-text selection))))
some?))))
(defn- paste-copied-blocks-or-text
;; 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)
(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 (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)
[:org _ false _]
(paste-text-parseable format text)
[:markdown true _ false]
(paste-segmented-text format text)
[:markdown true _ true]
(replace-text-f text)
[:org _ true false]
(paste-segmented-text format text)
[:org _ true true]
(replace-text-f text))))))))
(defn paste-text-in-one-block-at-point
[]
(utils/getClipText
(fn [clipboard-data]
(when-let [_ (state/get-input)]
(if (gp-util/url? clipboard-data)
(if (string/blank? (util/get-selected-text))
(editor-handler/insert (or (wrap-macro-url clipboard-data) clipboard-data) true)
(editor-handler/html-link-format! clipboard-data))
(editor-handler/insert clipboard-data true))))
(fn [error]
(js/console.error error))))
(defn- paste-text-or-blocks-aux
[input e text html]
(if (or (thingatpt/markdown-src-at-point input)
(thingatpt/org-admonition&src-at-point input))
(when-not (mobile-util/native-ios?)
(util/stop e)
(paste-text-in-one-block-at-point))
(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-let [_ (state/get-input)]
(let [text (or (when (gp-util/url? clipboard-data)
(wrap-macro-url clipboard-data))
clipboard-data)]
(paste-text-or-blocks-aux input e text nil))))
(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 clipboard-data)
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 (paste-text-or-blocks-aux input e text html))))))))