浏览代码

On normal paste wrap link in macro if possible

Remove warning for no macro wrapping as that is excessive warning
Gabriel Horner 2 年之前
父节点
当前提交
05f6665d12
共有 2 个文件被更改,包括 29 次插入10 次删除
  1. 7 9
      src/main/frontend/handler/paste.cljs
  2. 22 1
      src/test/frontend/handler/paste_test.cljs

+ 7 - 9
src/main/frontend/handler/paste.cljs

@@ -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]))
@@ -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.
@@ -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
@@ -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)))))))))

+ 22 - 1
src/test/frontend/handler/paste_test.cljs

@@ -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
@@ -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"]
@@ -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))))))