Browse Source

enhance: no need to input space before / and # command triggers

Tienson Qin 7 months ago
parent
commit
4b1c79a931
2 changed files with 17 additions and 24 deletions
  1. 3 7
      src/main/frontend/commands.cljs
  2. 14 17
      src/main/frontend/handler/editor.cljs

+ 3 - 7
src/main/frontend/commands.cljs

@@ -13,12 +13,12 @@
             [frontend.handler.notification :as notification]
             [frontend.handler.plugin :as plugin-handler]
             [frontend.handler.property.file :as property-file]
-            [frontend.util.ref :as ref]
             [frontend.search :as search]
             [frontend.state :as state]
             [frontend.util :as util]
             [frontend.util.cursor :as cursor]
             [frontend.util.file-based.priority :as priority]
+            [frontend.util.ref :as ref]
             [goog.dom :as gdom]
             [goog.object :as gobj]
             [logseq.common.config :as common-config]
@@ -473,7 +473,7 @@
         commands)
 
 ;; Allow user to modify or extend, should specify how to extend.
-      
+
       (state/get-commands)
       (when-let [plugin-commands (seq (some->> (state/get-plugins-slash-commands)
                                                (mapv #(vec (concat % [nil :icon/puzzle])))))]
@@ -748,11 +748,7 @@
                            (string/replace-first (subs edit-content pos)
                                                  (file-based-status/marker-pattern format)
                                                  (str marker " ")))]
-        (state/set-edit-content! input-id new-value)
-        (let [new-pos (compute-pos-delta-when-change-marker
-                       edit-content marker (dec slash-pos))]
-          ;; TODO: any performance issue?
-          (js/setTimeout #(cursor/move-cursor-to current-input new-pos) 10))))))
+        (state/set-edit-content! input-id new-value)))))
 
 (defn- db-based-set-status
   [status]

+ 14 - 17
src/main/frontend/handler/editor.cljs

@@ -1816,14 +1816,10 @@
                             (not (and
                                   (config/db-based-graph? repo)
                                   (re-find #"#\S+" value))))
-                 ; don't auto-save for page's properties block
+                   ;; don't auto-save for page's properties block
                    (save-current-block! {:skip-properties? true})))
                450)))))
 
-(defn- start-of-new-word?
-  [input pos]
-  (contains? #{" " "\t"} (get (.-value input) (- pos 2))))
-
 (defn handle-last-input []
   (let [input           (state/get-input)
         input-id        (state/get-edit-input-id)
@@ -1844,12 +1840,8 @@
       (p/let [_ (state/pub-event! [:editor/toggle-own-number-list edit-block])]
         (state/set-edit-content! input-id ""))
 
-      (and (= last-input-char commands/command-trigger)
-           (or (re-find #"(?m)^/" (str (.-value input))) (start-of-new-word? input pos)))
-      (do
-        (state/set-editor-action-data! {:pos (cursor/get-caret-pos input)})
-        (commands/reinit-matched-commands!)
-        (state/set-editor-show-commands!))
+      (= last-prev-input-char last-input-char commands/command-trigger)
+      (state/clear-editor-action!)
 
       (and (= last-input-char last-prev-input-char commands/colon)
            (or (nil? prev-prev-input-char)
@@ -1880,11 +1872,7 @@
       (state/clear-editor-action!)
 
       ;; Open "Search page or New page" auto-complete
-      (and (= last-input-char commands/hashtag)
-             ;; Only trigger at beginning of a line, before whitespace or after a reference
-           (or (re-find #"(?m)^#" (str (.-value input)))
-               (start-of-new-word? input pos)
-               (and db-based? (= page-ref/right-brackets (common-util/safe-subs (str (.-value input)) (- pos 3) (dec pos))))))
+      (= last-input-char commands/hashtag)
       (do
         (state/set-editor-action-data! {:pos (cursor/get-caret-pos input)})
         (state/set-editor-last-pos! pos)
@@ -2896,6 +2884,15 @@
         (state/set-state! :editor/start-pos pos))
 
       (cond
+        (and (= key commands/command-trigger)
+             ;; FIXME: surround-by? doesn't work on `handle-last-input`
+             (not (or (surround-by? input "http:" "")
+                      (surround-by? input "https:" ""))))
+        (do
+          (state/set-editor-action-data! {:pos (cursor/get-caret-pos input)})
+          (commands/reinit-matched-commands!)
+          (state/set-editor-show-commands!))
+
         (and (= :page-search (state/get-editor-action))
              (= key commands/hashtag))
         (do
@@ -3014,7 +3011,7 @@
         (state/set-editor-action-data! {:pos (cursor/get-caret-pos input)}))
       (when (and (not editor-action) (not non-enter-processed?))
         (cond
-         ;; When you type text inside square brackets
+          ;; When you type text inside square brackets
           (and (not (contains? #{"ArrowDown" "ArrowLeft" "ArrowRight" "ArrowUp" "Escape"} k))
                (wrapped-by? input page-ref/left-brackets page-ref/right-brackets))
           (let [orig-pos (cursor/get-caret-pos input)