Skip to content

Commit cfd5ea9

Browse files
megayutiensonqin
authored andcommitted
enhance the / input tolerance
1 parent 813d184 commit cfd5ea9

File tree

4 files changed

+40
-17
lines changed

4 files changed

+40
-17
lines changed

src/main/frontend/commands.cljs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,7 @@
299299
(let [heading (str "Heading " level)]
300300
[heading (->heading level) heading (str "h-" level)])) (range 1 7)))
301301

302+
(defonce *latest-matched-command (atom ""))
302303
(defonce *matched-commands (atom nil))
303304
(defonce *initial-commands (atom nil))
304305

@@ -474,12 +475,18 @@
474475
(defn init-commands!
475476
[get-page-ref-text]
476477
(let [commands (commands-map get-page-ref-text)]
478+
(reset! *latest-matched-command "")
477479
(reset! *initial-commands commands)
478480
(reset! *matched-commands commands)))
479481

482+
(defn set-matched-commands!
483+
[command matched-commands]
484+
(reset! *latest-matched-command command)
485+
(reset! *matched-commands matched-commands))
486+
480487
(defn reinit-matched-commands!
481488
[]
482-
(reset! *matched-commands @*initial-commands))
489+
(set-matched-commands! "" @*initial-commands))
483490

484491
(defn restore-state
485492
[]

src/main/frontend/components/editor.cljs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@
3838
[react-draggable]
3939
[rum.core :as rum]))
4040

41+
(defonce no-matched-coomands [["No matched commands" [[:editor/move-cursor-to-end]]]])
42+
4143
(defn filter-commands
4244
[page? commands]
4345
(if page?
@@ -56,7 +58,7 @@
5658
_ (when (state/get-editor-action)
5759
(reset! *matched matched'))
5860
page? (db/page? (db/entity (:db/id (state/get-edit-block))))
59-
matched (filter-commands page? @*matched)]
61+
matched (or (filter-commands page? @*matched) no-matched-coomands)]
6062
(ui/auto-complete
6163
matched
6264
{:get-group-name

src/main/frontend/handler/editor.cljs

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1773,23 +1773,25 @@
17731773
[property q]
17741774
(search/property-value-search property q))
17751775

1776-
(defn get-matched-commands
1776+
(defn get-last-command
17771777
[input]
17781778
(try
17791779
(let [edit-content (or (gobj/get input "value") "")
17801780
pos (cursor/pos input)
17811781
last-slash-caret-pos (:pos (:pos (state/get-editor-action-data)))
17821782
last-command (and last-slash-caret-pos (subs edit-content last-slash-caret-pos pos))]
1783-
(when (> pos 0)
1784-
(or
1785-
(and (= commands/command-trigger (util/nth-safe edit-content (dec pos)))
1786-
@commands/*initial-commands)
1787-
(and last-command
1788-
(commands/get-matched-commands last-command)))))
1783+
(when (> pos 0) last-command))
17891784
(catch :default e
17901785
(js/console.error e)
17911786
nil)))
17921787

1788+
(defn get-matched-commands
1789+
[command]
1790+
(condp = command
1791+
nil nil
1792+
"" @commands/*initial-commands
1793+
(commands/get-matched-commands command)))
1794+
17931795
(defn auto-complete?
17941796
[]
17951797
(or @*asset-uploading?
@@ -3206,10 +3208,13 @@
32063208
(and (= :commands (state/get-editor-action)) (not= k commands/command-trigger))
32073209
(if (= commands/command-trigger (second (re-find #"(\S+)\s+$" value)))
32083210
(state/clear-editor-action!)
3209-
(let [matched-commands (get-matched-commands input)]
3211+
(let [command (get-last-command input)
3212+
matched-commands (get-matched-commands command)]
32103213
(if (seq matched-commands)
3211-
(reset! commands/*matched-commands matched-commands)
3212-
(state/clear-editor-action!))))
3214+
(commands/set-matched-commands! command matched-commands)
3215+
(if (> (- (count command) (count @commands/*latest-matched-command)) 1)
3216+
(state/clear-editor-action!)
3217+
(reset! commands/*matched-commands nil)))))
32133218

32143219
:else
32153220
(default-case-for-keyup-handler input current-pos k code is-processed?))

src/test/frontend/handler/editor_test.cljs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,10 @@
8282
(state/set-editor-action! action)
8383
;; Default cursor pos to end of line
8484
(let [pos (or cursor-pos (count value))
85-
input #js {:value value}]
86-
(with-redefs [editor/get-matched-commands (constantly commands)
85+
input #js {:value value}
86+
command (subs value 1)]
87+
(with-redefs [editor/get-last-command (constantly command)
88+
editor/get-matched-commands (constantly commands)
8789
;; Ignore as none of its behaviors are tested
8890
editor/default-case-for-keyup-handler (constantly nil)
8991
cursor/pos (constantly pos)]
@@ -93,18 +95,25 @@
9395

9496
(deftest keyup-handler-test
9597
(testing "Command autocompletion"
96-
(keyup-handler {:value "/b"
98+
;; default last matching command is ""
99+
(keyup-handler {:value "/z"
97100
:action :commands
98-
:commands [:fake-command]})
101+
:commands []})
99102
(is (= :commands (state/get-editor-action))
100-
"Completion stays open if there is a matching command")
103+
"Completion stays open if no matches but differs by 1 character from last matching command")
101104

102105
(keyup-handler {:value "/zz"
103106
:action :commands
104107
:commands []})
105108
(is (= nil (state/get-editor-action))
106109
"Completion closed if there no matching commands")
107110

111+
(keyup-handler {:value "/b"
112+
:action :commands
113+
:commands [:fake-command]})
114+
(is (= :commands (state/get-editor-action))
115+
"Completion stays open if there is a matching command")
116+
108117
(keyup-handler {:value "/ " :action :commands})
109118
(is (= nil (state/get-editor-action))
110119
"Completion closed after a space follows /")

0 commit comments

Comments
 (0)