Skip to content

Commit

Permalink
enhance: block search performance
Browse files Browse the repository at this point in the history
  • Loading branch information
tiensonqin committed Feb 27, 2021
1 parent 8ea96a0 commit 5100965
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 33 deletions.
37 changes: 13 additions & 24 deletions src/main/frontend/components/editor.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,7 @@
false
*slash-caret-pos)))])

(def search-timeout (atom nil))
(rum/defcs box < rum/reactive
(mixins/event-mixin
(fn [state]
Expand Down Expand Up @@ -661,7 +662,8 @@
:else
(reset! *matched-block-commands matched-block-commands))
(reset! *show-block-commands false))))
(editor-handler/close-autocomplete-if-outside input))))))))
(when (nil? @search-timeout)
(editor-handler/close-autocomplete-if-outside input)))))))))
{:did-mount (fn [state]
(let [[{:keys [dummy? format block-parent-id]} id] (:rum/args state)
content (get-in @state/state [:editor/content id])
Expand Down Expand Up @@ -746,29 +748,16 @@
(state/set-edit-pos! current-pos)
(editor-handler/close-autocomplete-if-outside input)))
:on-change (fn [e]
(let [value (util/evalue e)
current-pos (:pos (util/get-caret-pos (gdom/getElement id)))]
(state/set-edit-content! id value false)
(state/set-edit-pos! current-pos)
(when-let [repo (or (:block/repo block)
(state/get-current-repo))]
(state/set-editor-last-input-time! repo (util/time-ms))
(db/clear-repo-persistent-job! repo))
(let [input (gdom/getElement id)
native-e (gobj/get e "nativeEvent")
last-input-char (util/nth-safe value (dec current-pos))]
(case last-input-char
"/"
;; TODO: is it cross-browser compatible?
(when (not= (gobj/get native-e "inputType") "insertFromPaste")
(when-let [matched-commands (seq (editor-handler/get-matched-commands input))]
(reset! *slash-caret-pos (util/get-caret-pos input))
(reset! *show-commands true)))
"<"
(when-let [matched-commands (seq (editor-handler/get-matched-block-commands input))]
(reset! *angle-bracket-caret-pos (util/get-caret-pos input))
(reset! *show-block-commands true))
nil))))
(if (state/sub :editor/show-block-search?)
(let [blocks-count (or (db/blocks-count) 0)
timeout (if (> blocks-count 2000) 300 100)]
(when @search-timeout
(js/clearTimeout @search-timeout))
(reset! search-timeout
(js/setTimeout
#(editor-handler/edit-box-on-change! e block id)
timeout)))
(editor-handler/edit-box-on-change! e block id)))
:on-paste (fn [e]
(when-let [handled
(let [pick-one-allowed-item
Expand Down
6 changes: 4 additions & 2 deletions src/main/frontend/components/search.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,9 @@
[state]
(let [search-result (state/sub :search/result)
search-q (state/sub :search/q)
show-result? (boolean (seq search-result))]
show-result? (boolean (seq search-result))
blocks-count (or (db/blocks-count) 0)
timeout (if (> blocks-count 2000) 300 100)]
(rum/with-context [[t] i18n/*tongue-context*]
[:div#search.flex-1.flex
[:div.inner
Expand Down Expand Up @@ -271,7 +273,7 @@
(reset! search-timeout
(js/setTimeout
#(search-handler/search value)
200))))))}]
timeout))))))}]
(when-not (string/blank? search-q)
(ui/css-transition
{:class-names "fade"
Expand Down
7 changes: 4 additions & 3 deletions src/main/frontend/db/model.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -1131,9 +1131,10 @@
([cache?]
(if (and cache? @blocks-count-cache)
@blocks-count-cache
(let [n (count (d/datoms (conn/get-conn) :avet :block/uuid))]
(reset! blocks-count-cache n)
n))))
(when-let [conn (conn/get-conn)]
(let [n (count (d/datoms conn :avet :block/uuid))]
(reset! blocks-count-cache n)
n)))))

;; block/uuid and block/content
(defn get-all-block-contents
Expand Down
26 changes: 26 additions & 0 deletions src/main/frontend/handler/editor.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -2257,3 +2257,29 @@
value (:block/content block)
new-value (string/replace value full_text new-full-text)]
(save-block-aux! block new-value (:block/format block) {})))

(defn edit-box-on-change!
[e block id]
(let [value (util/evalue e)
current-pos (:pos (util/get-caret-pos (gdom/getElement id)))]
(state/set-edit-content! id value false)
(state/set-edit-pos! current-pos)
(when-let [repo (or (:block/repo block)
(state/get-current-repo))]
(state/set-editor-last-input-time! repo (util/time-ms))
(db/clear-repo-persistent-job! repo))
(let [input (gdom/getElement id)
native-e (gobj/get e "nativeEvent")
last-input-char (util/nth-safe value (dec current-pos))]
(case last-input-char
"/"
;; TODO: is it cross-browser compatible?
(when (not= (gobj/get native-e "inputType") "insertFromPaste")
(when-let [matched-commands (seq (get-matched-commands input))]
(reset! commands/*slash-caret-pos (util/get-caret-pos input))
(reset! commands/*show-commands true)))
"<"
(when-let [matched-commands (seq (get-matched-block-commands input))]
(reset! commands/*angle-bracket-caret-pos (util/get-caret-pos input))
(reset! commands/*show-block-commands true))
nil))))
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3906,10 +3906,10 @@ mkdirp@^0.5.4, mkdirp@~0.5.1:
dependencies:
minimist "^1.2.5"

mldoc@^0.4.1:
version "0.4.1"
resolved "https://registry.yarnpkg.com/mldoc/-/mldoc-0.4.1.tgz#e815773f0c82e425ba66da84230c168ea84fed3d"
integrity sha512-YXMX7QHXTahZeURifwoVwQq4Fxwf4PE3EvaLyS5Gp2rcBQEqFC8BNt3n3Fdc2Zu/pQQ5BFlXxfvWWaFNjCwDUQ==
mldoc@^0.4.8:
version "0.4.8"
resolved "https://registry.yarnpkg.com/mldoc/-/mldoc-0.4.8.tgz#d52ad35378fdde1cdf7aa527a530488e39db3437"
integrity sha512-BZ8zXo2wgdnYOUXbZtpX5ocegtMIVRz1b+132xGKWlksu0VUEE0QJS7MjZYOMCnKVT7jmr/flpYENX/JkuUunw==
dependencies:
yargs "^12.0.2"

Expand Down

0 comments on commit 5100965

Please sign in to comment.