Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(paste): do not convert url to macro while raw pasting #8815

Merged
merged 4 commits into from
Mar 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 9 additions & 14 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 @@ -197,17 +191,14 @@
(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))))
(when (state/get-input)
(paste-text-or-blocks-aux input e clipboard-data 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)
files (.-files text)
paste-file-if-exist (fn []
(when id
(let [_handled
Expand All @@ -221,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)))))))))
48 changes: 45 additions & 3 deletions src/test/frontend/handler/paste_test.cljs
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
(ns frontend.handler.paste-test
(:require [cljs.test :refer [deftest are]]
(:require [cljs.test :refer [deftest are is testing]]
[frontend.test.helper :as test-helper :include-macros true :refer [deftest-async]]
[goog.object :as gobj]
["/frontend/utils" :as utils]
[frontend.state :as state]
[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 @@ -30,7 +37,7 @@
"[{\"number\": 1234}]" nil nil
;; invalid JSON
"{number: 1234}" nil nil))

(deftest selection-within-link-test
(are [x y] (= (#'paste-handler/selection-within-link? x) y)
{:format :markdown
Expand Down Expand Up @@ -67,4 +74,39 @@
:value "[logseq](https://logseq.com) is developed with [Clojure](https://clojure.org)"
:selection-start 9
:selection-end 76
:selection "https://logseq.com) is developed with [Clojure](https://clojure.org"} false))
:selection "https://logseq.com) is developed with [Clojure](https://clojure.org"} false))

(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"]
(test-helper/with-reset
reset
[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)]
(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))))))
8 changes: 6 additions & 2 deletions src/test/frontend/handler/plugin_config_test.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,9 @@
(plugin-config-handler/open-replace-plugins-modal)
(is (string/starts-with? @error-message "Malformed plugins.edn")
"User sees correct notification"))
(p/finally #(delete-global-config-dir dir))))))
(p/finally #(do
(reset)
(delete-global-config-dir dir)))))))

(deftest-async open-replace-plugins-modal-invalid-edn
(let [dir (create-global-config-dir)
Expand All @@ -89,7 +91,9 @@
(plugin-config-handler/open-replace-plugins-modal)
(is (string/starts-with? @error-message "Invalid plugins.edn")
"User sees correct notification"))
(p/finally #(delete-global-config-dir dir))))))
(p/finally #(do
(reset)
(delete-global-config-dir dir)))))))

(defn- installed-plugins->edn-plugins
"Converts installed plugins state to edn.plugins format"
Expand Down