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 3f4541b commit ace15b1
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 10 deletions.
15 changes: 6 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 @@ -203,8 +197,11 @@
(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)
text* (.getData clipboard-data "text")
files (.-files text*)
text (or (when (gp-util/url? text*)
(wrap-macro-url text*))
text*)
paste-file-if-exist (fn []
(when id
(let [_handled
Expand Down
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 ace15b1

Please sign in to comment.