Skip to content

Commit

Permalink
On normal paste wrap link in macro if possible
Browse files Browse the repository at this point in the history
Remove warning for no macro wrapping as that is excessive warning
  • Loading branch information
logseq-cldwalker committed Mar 15, 2023
1 parent dc7480b commit 05f6665
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 10 deletions.
16 changes: 7 additions & 9 deletions src/main/frontend/handler/paste.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
["/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]))
Expand Down Expand Up @@ -54,12 +53,7 @@
(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)))
(util/format "{{twitter %s}}" url)))

(defn- try-parse-as-json
"Result is not only to be an Object.
Expand Down Expand Up @@ -204,7 +198,7 @@
(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)
files (.-files text)
paste-file-if-exist (fn []
(when id
(let [_handled
Expand All @@ -218,4 +212,8 @@
(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))))))))
:else
(let [text' (or (when (gp-util/url? text)
(wrap-macro-url text))
text)]
(paste-text-or-blocks-aux input e text' html)))))))))
23 changes: 22 additions & 1 deletion src/test/frontend/handler/paste_test.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
[frontend.commands :as commands]
[frontend.util :as util]
[promesa.core :as p]
[frontend.extensions.html-parser :as html-parser]
[frontend.handler.paste :as paste-handler]))

(deftest try-parse-as-json-result-parse-test
Expand Down Expand Up @@ -75,7 +76,7 @@
:selection-end 76
:selection "https://logseq.com) is developed with [Clojure](https://clojure.org"} false))

(deftest-async editor-on-paste-raw-link
(deftest-async editor-on-paste-raw-with-link
(testing "Raw paste for link should just paste link"
(let [clipboard "https://www.youtube.com/watch?v=xu9p5ynlhZk"
expected-paste "https://www.youtube.com/watch?v=xu9p5ynlhZk"]
Expand All @@ -89,3 +90,23 @@
(p/let [result ((paste-handler/editor-on-paste! nil true))]
(is (= expected-paste result))
(reset))))))

(deftest-async editor-on-paste-normal-with-link
(testing "Normal paste for link should paste macro wrapped link"
(let [clipboard "https://www.youtube.com/watch?v=xu9p5ynlhZk"
expected-paste "{{video https://www.youtube.com/watch?v=xu9p5ynlhZk}}"]
(test-helper/with-reset
reset
;; These redefs are like above
[utils/getClipText (fn [cb] (cb clipboard))
state/get-input (constantly #js {:value "block"})
commands/delete-selection! (constantly nil)
commands/simple-insert! (fn [_input text] (p/resolved text))
util/get-selected-text (constantly nil)
;; Specific redefs to normal paste
util/stop (constantly nil)
html-parser/convert (constantly nil)]
(p/let [result ((paste-handler/editor-on-paste! nil)
#js {:clipboardData #js {:getData (constantly clipboard)}})]
(is (= expected-paste result))
(reset))))))

0 comments on commit 05f6665

Please sign in to comment.