Просмотр исходного кода

enhance(ux): convert a url to markdown/org link when pasting

Tienson Qin 3 лет назад
Родитель
Сommit
6a8ef4f4f8
2 измененных файлов с 44 добавлено и 42 удалено
  1. 1 1
      src/main/frontend/handler/editor.cljs
  2. 43 41
      src/main/frontend/handler/paste.cljs

+ 1 - 1
src/main/frontend/handler/editor.cljs

@@ -62,7 +62,7 @@
 (defonce *asset-uploading-process (atom 0))
 (defonce *selected-text (atom nil))
 
-(defn- get-selection-and-format
+(defn get-selection-and-format
   []
   (when-let [block (state/get-edit-block)]
     (when (:block/uuid block)

+ 43 - 41
src/main/frontend/handler/paste.cljs

@@ -89,47 +89,49 @@
           (when (seq blocks)
             (state/set-copied-full-blocks! blocks)
             (editor-handler/paste-blocks blocks {}))))
-      (cond
-        (and (gp-util/url? text)
-             (not (string/blank? (util/get-selected-text))))
-        (do
-          (util/stop e)
-          (editor-handler/html-link-format! text))
-
-        (and (text/block-ref? text)
-             (editor-handler/wrapped-by? input "((" "))"))
-        (do
-          (util/stop e)
-          (commands/simple-insert! (state/get-edit-input-id) (text/get-block-ref text) nil))
-
-        :else
-        ;; from external
-        (let [format (or (db/get-page-format (state/get-current-page)) :markdown)
-              text (or (when-not (string/blank? html)
-                         (html-parser/convert format html))
-                       text)]
-          (util/stop e)
-          (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]
-            (commands/simple-insert! (state/get-edit-input-id) text nil)
-
-            [:org _ true false]
-            (paste-segmented-text format text)
-
-            [:org _ true true]
-            (commands/simple-insert! (state/get-edit-input-id) text nil)))))))
+      (let [{:keys [value]} (editor-handler/get-selection-and-format)]
+        (cond
+          (and (or (gp-util/url? text)
+                   (and value (gp-util/url? (string/trim value))))
+               (not (string/blank? (util/get-selected-text))))
+          (do
+            (util/stop e)
+            (editor-handler/html-link-format! text))
+
+          (and (text/block-ref? text)
+               (editor-handler/wrapped-by? input "((" "))"))
+          (do
+            (util/stop e)
+            (commands/simple-insert! (state/get-edit-input-id) (text/get-block-ref text) nil))
+
+          :else
+          ;; from external
+          (let [format (or (db/get-page-format (state/get-current-page)) :markdown)
+                text (or (when-not (string/blank? html)
+                           (html-parser/convert format html))
+                         text)]
+            (util/stop e)
+            (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]
+              (commands/simple-insert! (state/get-edit-input-id) text nil)
+
+              [:org _ true false]
+              (paste-segmented-text format text)
+
+              [:org _ true true]
+              (commands/simple-insert! (state/get-edit-input-id) text nil))))))))
 
 (defn paste-text-in-one-block-at-point
   []