Browse Source

enhance: block search performance

Tienson Qin 4 years ago
parent
commit
510096541c

+ 13 - 24
src/main/frontend/components/editor.cljs

@@ -413,6 +413,7 @@
         false
         *slash-caret-pos)))])
 
+(def search-timeout (atom nil))
 (rum/defcs box < rum/reactive
   (mixins/event-mixin
    (fn [state]
@@ -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])
@@ -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

+ 4 - 2
src/main/frontend/components/search.cljs

@@ -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
@@ -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"

+ 4 - 3
src/main/frontend/db/model.cljs

@@ -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

+ 26 - 0
src/main/frontend/handler/editor.cljs

@@ -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))))

+ 4 - 4
yarn.lock

@@ -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"