Преглед изворни кода

ux: show page content in cmd k menu

Junyi Du пре 3 година
родитељ
комит
310e1a39f6

+ 3 - 3
src/electron/electron/search.cljs

@@ -288,9 +288,8 @@
             ;; the 2nd column in pages_fts (content)
             ;; pfts_2lqh is a key for retrieval
             ;; highlight and snippet only works for some matching with high rank
-            highlight-aux "highlight(pages_fts, 1, '$pfts_2lqh>$', '$<pfts_2lqh$')"
             snippet-aux "snippet(pages_fts, 1, '$pfts_2lqh>$', '$<pfts_2lqh$', '...', 32)"
-            select (str "select rowid, uuid, " highlight-aux ", " snippet-aux " from pages_fts where ")
+            select (str "select rowid, uuid, content, " snippet-aux " from pages_fts where ")
             match-sql (str select
                            " content match ? order by rank limit ?")
             non-match-sql (str select
@@ -299,7 +298,8 @@
          (concat
           (search-pages-aux database match-sql match-input limit)
           (search-pages-aux database non-match-sql non-match-input limit))
-         (distinct)
+         ;; distinct by :id
+         (group-by :id) (vals) (map first)
          (take limit)
          (vec))))))
 

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

@@ -62,12 +62,64 @@
                              (conj result [:span content])))]
             [:p {:class "m-0"} elements]))))))
 
+;; (def fts-mark)
+
+;; (defn highlight-page-content-query
+;;   [content q]
+;;   (if (or (string/blank? content) (string/blank? q))
+;;     content
+;;     (if (and (string/includes? )
+;;              (not (util/safe-re-find #" " q)))
+;;       (let [i (string/index-of lc-content lc-q)
+;;             [before after] [(subs content 0 i) (subs content (+ i (count q)))]]
+;;         [:div
+;;          (when-not (string/blank? before)
+;;            [:span before])
+;;          [:mark.p-0.rounded-none (subs content i (+ i (count q)))]
+;;          (when-not (string/blank? after)
+;;            [:span after])])
+;;       (let [elements (loop [words q-words
+;;                             content content
+;;                             result []]
+;;                        (if (and (seq words) content)
+;;                          (let [word (first words)
+;;                                lc-word (util/search-normalize word (state/enable-search-remove-accents?))
+;;                                lc-content (util/search-normalize content (state/enable-search-remove-accents?))]
+;;                            (if-let [i (string/index-of lc-content lc-word)]
+;;                              (recur (rest words)
+;;                                     (subs content (+ i (count word)))
+;;                                     (vec
+;;                                      (concat result
+;;                                              [[:span (subs content 0 i)]
+;;                                               [:mark.p-0.rounded-none (subs content i (+ i (count word)))]])))
+;;                              (recur nil
+;;                                     content
+;;                                     result)))
+;;                          (conj result [:span content])))]
+;;         [:p {:class "m-0"} elements]))))
+
 (rum/defc search-result-item
   [icon content]
   [:.search-result
    (ui/type-icon icon)
    [:.self-center content]])
 
+(rum/defc page-content-search-result-item
+  [repo uuid snippet _q search-mode]
+  [:div
+   (when (not= search-mode :page)
+     [:div {:class "mb-1" :key "parents"}
+      (block/breadcrumb {:id "block-search-block-parent"
+                         :block? true
+                         :search? true}
+                        repo
+                        (clojure.core/uuid uuid)
+                        {:indent? false})])
+   [:div {:class "font-medium" :key "content"}
+    ;; (highlight-page-content-query snippet q)
+    snippet
+    ]])
+
 (rum/defc block-search-result-item
   [repo uuid format content q search-mode]
   (let [content (search-handler/sanity-search-content format content)]
@@ -254,10 +306,23 @@
                                 (do (log/error "search result with non-existing uuid: " data)
                                     (str "Cache is outdated. Please click the 'Re-index' button in the graph's dropdown menu."))))])
 
+       :page-content
+       (let [{:block/keys [snippet uuid]} data  ;; content here is normalized
+             repo (state/sub :git/current-repo)
+             page (model/query-block-by-uuid uuid)] ;; it's actually a page
+         [:span {:data-block-ref uuid}
+          (search-result-item {:name "page"
+                               :title (t :search-item/page)
+                               :extension? true}
+                              (if page
+                                (page-content-search-result-item repo uuid snippet search-q search-mode)
+                                (do (log/error "search result with non-existing uuid: " data)
+                                    (str "Cache is outdated. Please click the 'Re-index' button in the graph's dropdown menu."))))])
+
        nil)]))
 
 (rum/defc search-auto-complete
-  [{:keys [engine pages files blocks has-more?] :as result} search-q all?]
+  [{:keys [engine pages files pages-content blocks has-more?] :as result} search-q all?]
   (let [pages (when-not all? (map (fn [page]
                                     (let [alias (model/get-redirect-page-name page)]
                                       (cond->
@@ -270,6 +335,7 @@
                                   (remove nil? pages)))
         files (when-not all? (map (fn [file] {:type :file :data file}) files))
         blocks (map (fn [block] {:type :block :data block}) blocks)
+        pages-content (map (fn [pages-content] {:type :page-content :data pages-content}) pages-content)
         search-mode (state/sub :search/mode)
         new-page (if (or
                       (some? engine)
@@ -284,13 +350,13 @@
                      [{:type :new-page}]))
         result (cond
                  config/publishing?
-                 (concat pages files blocks)
+                 (concat pages files blocks) ;; Browser doesn't have page content FTS
 
                  (= :whiteboard/link search-mode)
-                 (concat pages blocks)
+                 (concat pages blocks pages-content)
 
                  :else
-                 (concat new-page pages files blocks))
+                 (concat new-page pages files blocks pages-content))
         result (if (= search-mode :graph)
                  [{:type :graph-add-filter}]
                  result)

+ 1 - 0
src/main/frontend/db/model.cljs

@@ -274,6 +274,7 @@
   (db-utils/entity [:block/uuid (if (uuid? id) id (uuid id))]))
 
 (defn query-block-by-uuid
+  "Return block or page entity, depends on the uuid"
   [id]
   (db-utils/pull [:block/uuid (if (uuid? id) id (uuid id))]))
 

+ 5 - 2
src/main/frontend/handler/search.cljs

@@ -44,12 +44,15 @@
                         (:db/id (db/entity repo [:block/name (util/page-name-sanity-lc page-db-id)]))
                         page-db-id)
            opts (if page-db-id (assoc opts :page (str page-db-id)) opts)]
-       (p/let [blocks (search/block-search repo q opts)]
+       (p/let [blocks (search/block-search repo q opts)
+               pages-content (search/page-content-search repo q opts)
+               _ (prn pages-content)]
          (let [result (merge
                        {:blocks blocks
                         :has-more? (= limit (count blocks))}
                        (when-not page-db-id
-                         {:pages (search/page-search q)
+                         {:pages-content pages-content
+                          :pages (search/page-search q)
                           :files (search/file-search q)}))
                search-key (if more? :search/more-result :search/result)]
            (swap! state/state assoc search-key result)

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

@@ -4,8 +4,7 @@
             [frontend.db :as db]
             [frontend.state :as state]
             [frontend.util :as util]
-            ["fuse.js" :as fuse]
-            [frontend.db.utils :as db-util]))
+            ["fuse.js" :as fuse]))
 
 ;; Notice: When breaking changes happen, bump version in src/electron/electron/search.cljs
 

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

@@ -23,8 +23,7 @@
       (keep (fn [{:keys [content snippet uuid]}]
               (when-not (> (count content) (* 10 (state/block-content-max-length repo)))
                 {:block/uuid uuid
-                 :snippet snippet
-                 :block/content content})) result)))
+                 :block/snippet snippet})) result)))
   (rebuild-blocks-indice! [_this]
     (let [blocks-indice (search-db/build-blocks-indice repo)
           pages-indice  (search-db/build-pages-indice repo)]

+ 1 - 1
src/main/frontend/state.cljs

@@ -51,7 +51,7 @@
      :journals-length                       3
 
      :search/q                              ""
-     :search/mode                           :global
+     :search/mode                           :global  ;; inner page or full graph? {:page :global}
      :search/result                         nil
      :search/graph-filters                  []
      :search/engines                        {}