Abandon perf fix attempt

pull/10493/head
Ben Yorke 2023-10-09 15:00:35 +02:00
parent 79a11f8b88
commit 6247cc35fe
6 changed files with 142 additions and 57 deletions

View File

@ -6,6 +6,15 @@
[logseq.shui.icon.v2 :as icon]
[logseq.shui.button.v2 :as button]))
(defn to-string [input]
(cond
(string? input) input
(keyword? input) (name input)
(symbol? input) (name input)
(number? input) (str input)
(nil? input) ""
:else (pr-str input)))
(defn print-shortcut-key [key]
(case key
("cmd" "command" "mod" "⌘") "⌘"
@ -25,10 +34,7 @@
(name key)))
(defn normalize-text [app-config text]
(js/console.log "normalize-text" text app-config)
(cond-> (or text "")
(keyword? text) (name)
:stringify (str)
(cond-> (to-string text)
:lower-case (string/lower-case)
:normalize (.normalize "NFKC")
(:feature/enable-search-remove-accents? app-config) (remove-accents)))
@ -36,10 +42,13 @@
(defn split-text-on-highlight [text query normal-text normal-query]
(let [start-index (string/index-of normal-text normal-query)
end-index (+ start-index (count query))
text-string (cond-> (or text "") (keyword? text) name :always str)]
[(or (subs text-string 0 start-index) "")
(or (subs text-string start-index end-index) "")
(or (subs text-string end-index) "")]))
text-string (to-string text)]
(if start-index
[(to-string (subs text-string 0 start-index))
(to-string (subs text-string start-index end-index))
(to-string (subs text-string end-index))]
[text-string "" ""])))
(defn span-with-single-highlight-token [text query normal-text normal-query]
(let [[before-text highlighted-text after-text] (split-text-on-highlight text query normal-text normal-query)]
@ -71,19 +80,20 @@
(recur more)))))
(defn highlight-query* [app-config query text]
(if-not (seq query) [:span text]
(let [normal-text (normalize-text app-config text)
normal-query (normalize-text app-config query)]
(cond
;; When the match is present but is multiple tokens, highlight all tokens
(and (string? query) (re-find #" " query))
(span-with-mutliple-highlight-tokens app-config text query normal-text normal-query)
;; When the match is present and only a single token, highlight that token
(string/includes? normal-text normal-query)
(span-with-single-highlight-token text query normal-text normal-query)
;; Otherwise, just return the text
:else
[:span text]))))
(let [text-string (to-string text)]
(if-not (seq text-string) [:span text-string]
(let [normal-text (normalize-text app-config text-string)
normal-query (normalize-text app-config query)]
(cond
;; When the match is present but is multiple tokens, highlight all tokens
(and (string? query) (re-find #" " query))
(span-with-mutliple-highlight-tokens app-config text-string query normal-text normal-query)
;; When the match is present and only a single token, highlight that token
(string/includes? normal-text normal-query)
(span-with-single-highlight-token text-string query normal-text normal-query)
;; Otherwise, just return the text
:else
[:span text])))))
;; result-item
@ -127,11 +137,11 @@
(when (or value-label value)
[:div {:class "text-xs"}
(when (and value-label value)
[:span.text-gray-11 (str value-label ": ")])
[:span.text-gray-11 (str (to-string value-label) ": ")])
(when (and value-label (not value))
[:span.text-gray-11 (str value-label)])
[:span.text-gray-11 (str (to-string value-label))])
(when value
[:span.text-gray-11 value])])
[:span.text-gray-11 (to-string value)])])
(when shortcut
[:div {:class "flex gap-1"}
(for [[index option] (map-indexed vector (string/split shortcut #" \| "))]
@ -144,7 +154,7 @@
(apply str))]]
(button/root {:theme :gray
:interactive false
:text text
:text (to-string text)
:tiled true}
context))])])]]))
; [:span {:style} (str key)])])])

View File

@ -17,6 +17,7 @@
[frontend.modules.shortcut.core :as shortcut]
[frontend.modules.shortcut.data-helper :as shortcut-helper]
[frontend.search :as search]
[frontend.search.db :as search-db]
[frontend.shui :refer [make-shui-context]]
[frontend.state :as state]
[frontend.ui :as ui]
@ -193,25 +194,25 @@
opts {:limit 100}]
(swap! !results assoc-in [group :status] :loading)
(swap! !results assoc-in [:current-page :status] :loading)
(p/let [blocks (search/block-search repo @!input opts)
items (map #(hash-map :icon "block"
:icon-theme :gray
:text (:block/content %)
:header (some-> % :block/page db/entity :block/name)
:current-page? (some-> % :block/page #{current-page})
:source-block %) blocks)
items-on-other-pages (remove :current-page? items)
items-on-current-page (filter :current-page? items)]
(js/console.log "blocks" (clj->js items) current-page)
; (js/console.log "blocks" (clj->js items)
; (pr-str (map (comp pr-str :block/page) blocks))
; (pr-str (map (comp :block/name :block/page) blocks))
; (pr-str (map (comp :block/name db/entity :block/page) blocks)))
; (js/console.log "load-results/blocks"
; (clj->js blocks)
; (pr-str (first blocks)))
(swap! !results assoc group {:status :success :items items-on-other-pages}
:current-page {:status :success :items items-on-current-page}))))
(p/let [blocks (search/block-search repo @!input opts)])))
; items (map #(hash-map :icon "block"
; :icon-theme :gray
; :text (:block/content %)
; :header (some-> % :block/page db/entity :block/name)
; :current-page? (some-> % :block/page #{current-page})
; :source-block %) blocks)
; items-on-other-pages (remove :current-page? items)
; items-on-current-page (filter :current-page? items)]
; (js/console.log "blocks" (clj->js items) current-page)
; ; (js/console.log "blocks" (clj->js items)
; ; (pr-str (map (comp pr-str :block/page) blocks))
; ; (pr-str (map (comp :block/name :block/page) blocks))
; ; (pr-str (map (comp :block/name db/entity :block/page) blocks)))
; ; (js/console.log "load-results/blocks"
; ; (clj->js blocks)
; ; (pr-str (first blocks)))
; (swap! !results assoc group {:status :success :items items-on-other-pages}
; :current-page {:status :success :items items-on-current-page}))))
; (defmethod load-results :whiteboards [group state]
; (let [!input (::input state)
@ -258,9 +259,9 @@
(load-results :commands state)
(load-results :blocks state)
(load-results :pages state)
; (load-results :whiteboards state)
(load-results :files state)
; (load-results :files state)
(load-results :recents state))))
; ; (load-results :whiteboards state)
; (def search [query]
; (load-results :search-actions state))
@ -604,6 +605,7 @@
(reset! (::keyup-handler state) nil)
state)}
{:did-mount (fn [state]
; (search-db/make-blocks-indice-non-blocking! (state/get-current-repo))
(when-let [ref @(::scroll-container-ref state)]
(js/console.log "scrolling")
(js/setTimeout #(set! (.-scrollTop ref) FILTER-ROW-HEIGHT)))

View File

@ -38,7 +38,7 @@
[frontend.db.model
blocks-count blocks-count-cache delete-blocks get-pre-block
delete-files delete-pages-by-files get-all-block-contents get-all-tagged-pages
delete-files delete-pages-by-files get-all-block-contents get-all-block-contents-non-blocking get-all-tagged-pages
get-all-templates get-block-and-children get-block-by-uuid get-block-children sort-by-left
get-block-parent get-block-parents parents-collapsed? get-block-referenced-blocks get-all-referenced-blocks-uuid
get-block-children-ids get-block-immediate-children get-block-page

View File

@ -1481,22 +1481,69 @@ independent of format as format specific heading characters are stripped"
[?referee-b :block/refs ?refed-b]] db)))
;; block/uuid and block/content
(defn get-single-block-contents [id]
(let [e (db-utils/entity [:block/uuid id])]
(when (and (not (:block/name e))
(not (string/blank? (:block/content e))))
{:db/id (:db/id e)
:block/uuid id
:block/page (:db/id (:block/page e))
:block/content (:block/content e)
:block/format (:block/format e)})))
(defn get-all-block-contents
[]
(when-let [db (conn/get-db)]
(->> (d/datoms db :avet :block/uuid)
(map :v)
(map (fn [id]
(let [e (db-utils/entity [:block/uuid id])]
(when (and (not (:block/name e))
(not (string/blank? (:block/content e))))
{:db/id (:db/id e)
:block/uuid id
:block/page (:db/id (:block/page e))
:block/content (:block/content e)
:block/format (:block/format e)}))))
(map get-single-block-contents)
(remove nil?))))
(defn get-all-block-contents-non-blocking
([]
(when-let [db (conn/get-db)]
(let [ids (->> (d/datoms db :avet :block/uuid)
(map :v)
(remove nil?)
(not-empty))]
(js/Promise.
(fn [resolve-all _]
(-> (fn [promise-acc id]
(.then promise-acc
(fn [acc]
(let [contents (get-single-block-contents id)]
(js/Promise.
(fn [resolve _]
(js/setTimeout
#(if (nil? contents)
(resolve acc)
(resolve (conj acc contents)))
0)))))))
(reduce (js/Promise.resolve (list)) ids)
(.then #(resolve-all %)))))))))
; (apply get-all-block-contents-non-blocking (list))))))
; (map get-single-block-contents)
; (remove nil?))))
; ([acc id & more]
; (let [contents (get-single-block-contents id)
; acc (cond-> acc (seq contents) (conj contents))])
; (js/Promise.
; (fn [resolve reject]
; (let [callback (fn [acc]
; (let [contents (get-single-block-contents id)]
; (js/setTimeout
; #(resolve
; (if (nil? contents)
; acc
; (conj acc contents)))
; 0)))]
; (if (seq more)
; (.then (apply get-all-block-contents-non-blocking more) callback)
; (callback (list))))))))
;; Deprecated?
(defn delete-blocks
[repo-url files _delete-page?]

View File

@ -13,6 +13,8 @@
:or {limit 20}}]
(let [indice (or (get-in @indices [repo :blocks])
(search-db/make-blocks-indice! repo))
; (search-db/make-blocks-indice-non-blocking! repo))
result
(if page
(.search indice
@ -40,7 +42,7 @@
(let [indice (search-db/make-blocks-indice! repo)]
(p/promise indice)))
(transact-blocks! [_this {:keys [blocks-to-remove-set
blocks-to-add]}]
blocks-to-add]}]
(swap! search-db/indices update-in [repo :blocks]
(fn [indice]
(when indice

View File

@ -51,6 +51,15 @@
(remove nil?)
(bean/->js)))
(defn build-blocks-indice-non-blocking
[repo]
(.then (db/get-all-block-contents-non-blocking)
(fn [block-contents]
(->> block-contents
(map block->index)
(remove nil?)
(bean/->js)))))
(defn build-pages-indice
[repo]
(->> (db/get-all-pages repo)
@ -72,6 +81,21 @@
(swap! indices assoc-in [repo :blocks] indice)
indice))
(defn make-blocks-indice-non-blocking!
[repo]
(.then (build-blocks-indice-non-blocking repo)
(fn [blocks]
(let [indice (fuse. blocks
(clj->js {:keys ["uuid" "content" "page"]
:shouldSort true
:tokenize true
:minMatchCharLength 1
:distance 1000
:threshold 0.35}))]
(swap! indices assoc-in [repo :blocks] indice)
indice))))
; (let [blocks (build-blocks-indice-non-blocking repo)]))
(defn original-page-name->index
[p]
(when p