瀏覽代碼

feat: introduce :block/content-max-length for better performance for

both searching and editing.
Tienson Qin 3 年之前
父節點
當前提交
9ac8bf98e1

+ 4 - 1
src/main/frontend/components/block.cljs

@@ -1968,7 +1968,7 @@
 
 (defn- block-content-on-mouse-down
   [e block block-id content edit-input-id]
-  (when-not (util/base64-image-included? content)
+  (when-not (> (count content) (state/block-content-max-length (state/get-current-repo)))
     (.stopPropagation e)
     (let [target (gobj/get e "target")
           button (gobj/get e "buttons")
@@ -2098,6 +2098,9 @@
        (merge attrs))
 
      [:<>
+      (when (> (count content) (state/block-content-max-length (state/get-current-repo)))
+        [:div.warning.text-sm
+         "Large block will not be editable or searchable to not slow down the app, please use another editor to edit this block."])
       [:div.flex.flex-row.justify-between.block-content-inner
        [:div.flex-1.w-full
         (cond

+ 1 - 1
src/main/frontend/search/db.cljs

@@ -14,7 +14,7 @@
   "Convert a block to the index for searching"
   [{:block/keys [uuid page content] :as block}]
   (when-let [content (util/search-normalize content (state/enable-search-remove-accents?))]
-    (when-not (util/base64-image-included? content)
+    (when-not (> (count content) (state/block-content-max-length (state/get-current-repo)))
       {:id (:db/id block)
        :uuid (str uuid)
        :page page

+ 2 - 2
src/main/frontend/search/node.cljs

@@ -4,7 +4,7 @@
             [frontend.search.db :as search-db]
             [frontend.search.protocol :as protocol]
             [promesa.core :as p]
-            [frontend.util :as util]))
+            [frontend.state :as state]))
 
 (defrecord Node [repo]
   protocol/Engine
@@ -12,7 +12,7 @@
     (p/let [result (ipc/ipc "search-blocks" repo q opts)
             result (bean/->clj result)]
       (keep (fn [{:keys [content uuid page]}]
-              (when-not (util/base64-image-included? content)
+              (when-not (> (count content) (state/block-content-max-length repo))
                 {:block/uuid uuid
                  :block/content content
                  :block/page page})) result)))

+ 44 - 34
src/main/frontend/state.cljs

@@ -857,40 +857,9 @@
   []
   (or (not @publishing?) (:publishing/enable-editing? (get-config))))
 
-(defn set-editing!
-  ([edit-input-id content block cursor-range]
-   (set-editing! edit-input-id content block cursor-range true))
-  ([edit-input-id content block cursor-range move-cursor?]
-   (when (and edit-input-id block
-              (or
-                (publishing-enable-editing?)
-                (not @publishing?)))
-     (let [block-element (gdom/getElement (string/replace edit-input-id "edit-block" "ls-block"))
-           container (util/get-block-container block-element)
-           block (if container
-                   (assoc block
-                     :block/container (gobj/get container "id"))
-                   block)
-           content (string/trim (or content ""))]
-       (swap! state
-              (fn [state]
-                (-> state
-                    (assoc-in [:editor/content edit-input-id] content)
-                    (assoc
-                     :editor/block block
-                     :editor/editing? {edit-input-id true}
-                     :editor/last-key-code nil
-                     :cursor-range cursor-range))))
-       (when-let [input (gdom/getElement edit-input-id)]
-         (let [pos (count cursor-range)]
-           (when content
-             (util/set-change-value input content))
-
-           (when move-cursor?
-             (cursor/move-cursor-to input pos))
-
-           (when (or (util/mobile?) (mobile-util/native-platform?))
-             (set-state! :mobile/show-action-bar? false))))))))
+(defn block-content-max-length
+  [repo]
+  (or (:block/content-max-length (get (sub-config) repo)) 5000))
 
 (defn clear-edit!
   []
@@ -1543,6 +1512,47 @@
    (clear-edit!)
    (set-selection-blocks! blocks direction)))
 
+(defn set-editing!
+  ([edit-input-id content block cursor-range]
+   (set-editing! edit-input-id content block cursor-range true))
+  ([edit-input-id content block cursor-range move-cursor?]
+   (if (> (count content)
+          (block-content-max-length (get-current-repo)))
+     (let [elements (array-seq (js/document.getElementsByClassName (:block/uuid block)))]
+       (when (first elements)
+         (util/scroll-to-element (gobj/get (first elements) "id")))
+       (exit-editing-and-set-selected-blocks! elements))
+     (when (and edit-input-id block
+               (or
+                (publishing-enable-editing?)
+                (not @publishing?)))
+      (let [block-element (gdom/getElement (string/replace edit-input-id "edit-block" "ls-block"))
+            container (util/get-block-container block-element)
+            block (if container
+                    (assoc block
+                           :block/container (gobj/get container "id"))
+                    block)
+            content (string/trim (or content ""))]
+        (swap! state
+               (fn [state]
+                 (-> state
+                     (assoc-in [:editor/content edit-input-id] content)
+                     (assoc
+                      :editor/block block
+                      :editor/editing? {edit-input-id true}
+                      :editor/last-key-code nil
+                      :cursor-range cursor-range))))
+        (when-let [input (gdom/getElement edit-input-id)]
+          (let [pos (count cursor-range)]
+            (when content
+              (util/set-change-value input content))
+
+            (when move-cursor?
+              (cursor/move-cursor-to input pos))
+
+            (when (or (util/mobile?) (mobile-util/native-platform?))
+              (set-state! :mobile/show-action-bar? false)))))))))
+
 (defn remove-watch-state [key]
   (remove-watch state key))
 

+ 0 - 5
src/main/frontend/util.cljc

@@ -1343,8 +1343,3 @@
                Math/floor
                int
                (#(str % " " (:name unit) (when (> % 1) "s") " ago"))))))))
-
-#?(:cljs
-   (defn base64-image-included?
-     [content]
-     (safe-re-find #"data:image/[^;]+;base64" content)))

+ 4 - 0
templates/config.edn

@@ -119,6 +119,10 @@
  ;; Set this to `true` so that it's the same behaviour as the usual outliner mode.
  :shortcut/doc-mode-enter-for-new-block? false
 
+ ;; Block content larger than `search/text-max-length` will not be searchable
+ ;; or editable for performance.
+ :block/content-max-length 5000
+
  ;; Whether to show command doc on hover
  :ui/show-command-doc? true