From 475d4ce7d9299f94721652074eaeeb66e320dc57 Mon Sep 17 00:00:00 2001 From: Gabriel Horner Date: Tue, 7 Feb 2023 17:03:04 -0500 Subject: [PATCH] Fix command completion firing too often Only fire at the beginning of a line or when starting a new word --- src/main/frontend/handler/editor.cljs | 12 +++++++++--- src/test/frontend/handler/editor_test.cljs | 18 ++++++++++++++++++ 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/src/main/frontend/handler/editor.cljs b/src/main/frontend/handler/editor.cljs index 1c5d78c56..f0a53b61d 100644 --- a/src/main/frontend/handler/editor.cljs +++ b/src/main/frontend/handler/editor.cljs @@ -311,7 +311,7 @@ block (merge block (block/parse-title-and-body uuid format pre-block? (:block/content block))) properties (:block/properties block) - properties (if (and (= format :markdown) + properties (if (and (= format :markdown) (number? (:heading properties))) (dissoc properties :heading) properties) @@ -1810,6 +1810,10 @@ (state/set-editor-op! nil))) 500)))) +(defn- start-of-new-word? + [input pos] + (contains? #{" " "\t"} (get (.-value input) (- pos 2)))) + (defn handle-last-input [] (let [input (state/get-input) pos (cursor/pos input) @@ -1822,8 +1826,10 @@ (cond ;; By default, "/" is also used as namespace separator in Logseq. (and (= last-input-char (state/get-editor-command-trigger)) - (not (contains? #{:page-search-hashtag} (state/sub :editor/action)))) + #_(not (contains? #{:page-search-hashtag} (state/sub :editor/action))) + (or (= 1 pos) (start-of-new-word? input pos))) (do + (prn :NEW-WORD? (or (= 1 pos) (start-of-new-word? input pos))) (state/set-editor-action-data! {:pos (cursor/get-caret-pos input)}) (commands/reinit-matched-commands!) (state/set-editor-show-commands!)) @@ -1856,7 +1862,7 @@ ;; Open "Search page or New page" auto-complete (and (= last-input-char commands/hashtag) ;; Only trigger at beginning of line or before whitespace - (or (= 1 pos) (contains? #{" " "\t"} (get (.-value input) (- pos 2))))) + (or (= 1 pos) (start-of-new-word? input pos))) (do (state/set-editor-action-data! {:pos (cursor/get-caret-pos input)}) (state/set-editor-last-pos! pos) diff --git a/src/test/frontend/handler/editor_test.cljs b/src/test/frontend/handler/editor_test.cljs index 83ceb5faf..6b65613d5 100644 --- a/src/test/frontend/handler/editor_test.cljs +++ b/src/test/frontend/handler/editor_test.cljs @@ -98,6 +98,24 @@ (is (= nil (state/get-editor-action)) "Don't autocomplete properties if typing in a block where properties already exist")) + (testing "Command autocompletion" + (handle-last-input-handler {:value "/"}) + (is (= :commands (state/get-editor-action)) + "Command search if only / has been typed") + + (handle-last-input-handler {:value "some words /"}) + (is (= :commands (state/get-editor-action)) + "Command search on start of new word") + + (handle-last-input-handler {:value "https://"}) + (is (= nil (state/get-editor-action)) + "No command search in middle of a word") + + (handle-last-input-handler {:value "#blah/"}) + (is (= nil (state/get-editor-action)) + "No command search after a tag search to allow for namespace completion") + ) + (testing "Tag autocompletion" (handle-last-input-handler {:value "#" :cursor-pos 1})