Browse Source

enhance: full search results

Tienson Qin 4 years ago
parent
commit
422fd8353b

+ 4 - 0
resources/css/common.css

@@ -816,3 +816,7 @@ a.page-op {
         transform: scale(0.9);
     }
 }
+
+.search-more {
+    background: var(--ls-a-chosen-bg);
+}

+ 1 - 0
src/electron/electron/handler.cljs

@@ -106,6 +106,7 @@
   true)
 
 (defmethod handle :search-blocks [window [_ repo q limit]]
+  (prn {:limit limti})
   (search/search-blocks repo q limit))
 
 (defmethod handle :rebuild-blocks-indice [window [_ repo data]]

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

@@ -130,7 +130,7 @@
 ;;       (js->clj (.all ^object stmt q) :keywordize-keys true))))
 
 (defn search-blocks
-  [repo q limit]
+  [repo q]
   (when-let [database (get-db repo)]
     (when-not (string/blank? q)
       (let [limit (or limit 100)

+ 2 - 1
src/main/frontend/components/page.cljs

@@ -353,7 +353,8 @@
                                :top 20}}
                  [:div.flex.flex-row
                   [:a.opacity-30.hover:opacity-100.page-op.mr-2
-                   {:on-click #(route-handler/go-to-search! :page)}
+                   {:title "Search in current page"
+                    :on-click #(route-handler/go-to-search! :page)}
                    svg/search]
                   (when (not config/mobile?)
                     (presentation repo page))

+ 50 - 26
src/main/frontend/components/search.cljs

@@ -17,7 +17,8 @@
             [goog.dom :as gdom]
             [medley.core :as medley]
             [frontend.context.i18n :as i18n]
-            [frontend.date :as date]))
+            [frontend.date :as date]
+            [reitit.frontend.easy :as rfe]))
 
 (defn- partition-between
   "Split `coll` at positions where `pred?` is true."
@@ -138,13 +139,13 @@
 (defonce search-timeout (atom nil))
 
 (rum/defc search-auto-complete
-  [{:keys [pages files blocks] :as result} search-q]
+  [{:keys [pages files blocks has-more?] :as result} search-q all?]
   (rum/with-context [[t] i18n/*tongue-context*]
     (let [new-file (when-let [ext (util/get-file-ext search-q)]
                      (when (contains? config/mldoc-support-formats (keyword (string/lower-case ext)))
                        [{:type :new-file}]))
-          pages (map (fn [page] {:type :page :data page}) pages)
-          files (map (fn [file] {:type :file :data file}) files)
+          pages (when-not all? (map (fn [page] {:type :page :data page}) pages))
+          files (when-not all? (map (fn [file] {:type :file :data file}) files))
           blocks (map (fn [block] {:type :block :data block}) blocks)
           search-mode (state/get-search-mode)
           new-page (if (or
@@ -152,17 +153,19 @@
                              (= (string/lower-case search-q)
                                 (string/lower-case (:data (first pages)))))
                         (nil? result)
-                        (not= :global search-mode))
+                        (not= :global search-mode)
+                        all?)
                      []
                      [{:type :new-page}])
           result (if config/publishing?
                    (concat pages files blocks)
                    (concat new-page pages new-file files blocks))]
-      [:div.absolute.rounded-md.shadow-lg
+      [:div.rounded-md.shadow-lg
        {:style (merge
                 {:top 48
                  :left 32
-                 :width 700})}
+                 :width 700})
+        :class (if all? "search-all" "absolute")}
        (ui/auto-complete
         result
         {:class "search-results"
@@ -214,29 +217,38 @@
          :item-render (fn [{:keys [type data]}]
                         (let [search-mode (state/get-search-mode)]
                           [:div {:class "py-2"} (case type
-                                                 :new-page
-                                                 [:div.text.font-bold (str (t :new-page) ": ")
-                                                  [:span.ml-1 (str "\"" search-q "\"")]]
+                                                  :new-page
+                                                  [:div.text.font-bold (str (t :new-page) ": ")
+                                                   [:span.ml-1 (str "\"" search-q "\"")]]
 
-                                                 :new-file
-                                                 [:div.text.font-bold (str (t :new-file) ": ")
-                                                  [:span.ml-1 (str "\"" search-q "\"")]]
+                                                  :new-file
+                                                  [:div.text.font-bold (str (t :new-file) ": ")
+                                                   [:span.ml-1 (str "\"" search-q "\"")]]
 
-                                                 :page
-                                                 (search-result-item "Page" (highlight-exact-query data search-q))
+                                                  :page
+                                                  (search-result-item "Page" (highlight-exact-query data search-q))
 
-                                                 :file
-                                                 (search-result-item "File" (highlight-exact-query data search-q))
+                                                  :file
+                                                  (search-result-item "File" (highlight-exact-query data search-q))
 
-                                                 :block
-                                                 (let [{:block/keys [page content uuid]} data
-                                                       page (or (:page/original-name page)
-                                                                (:page/name page))
-                                                       repo (state/sub :git/current-repo)
-                                                       format (db/get-page-format page)]
-                                                   (search-result-item "Block" (block-search-result-item repo uuid format content search-q search-mode)))
+                                                  :block
+                                                  (let [{:block/keys [page content uuid]} data
+                                                        page (or (:page/original-name page)
+                                                                 (:page/name page))
+                                                        repo (state/sub :git/current-repo)
+                                                        format (db/get-page-format page)]
+                                                    (search-result-item "Block" (block-search-result-item repo uuid format content search-q search-mode)))
 
-                                                 nil)]))})])))
+                                                  nil)]))})
+       (when (and has-more? (util/electron?) (not all?))
+         [:div.px-2.py-4.search-more
+          [:a.text-sm.font-medium {:href (rfe/href :search {:q search-q})
+                                   :on-click (fn []
+                                               (when-not (string/blank? search-q)
+                                                 (search-handler/search (state/get-current-repo) search-q {:limit 1000
+                                                                                                           :more? true})
+                                                 (search-handler/clear-search!)))}
+           (t :more)]])])))
 
 (rum/defcs search < rum/reactive
   (rum/local false ::inside-box?)
@@ -298,4 +310,16 @@
             {:class-names "fade"
              :timeout {:enter 500
                        :exit 300}}
-            (search-auto-complete search-result search-q)))]]])))
+            (search-auto-complete search-result search-q false)))]]])))
+
+(rum/defc more < rum/reactive
+  [route]
+  (let [search-q (get-in route [:path-params :q])
+        search-result (state/sub :search/more-result)]
+    (rum/with-context [[t] i18n/*tongue-context*]
+      [:div#search.flex-1.flex
+       [:div.inner
+        [:h1.title "Search result for " [:i search-q]]
+        [:div#search-wrapper.relative.w-full.text-gray-400.focus-within:text-gray-600
+         (when-not (string/blank? search-q)
+           (search-auto-complete search-result search-q true))]]])))

+ 4 - 0
src/main/frontend/dicts.cljs

@@ -153,6 +153,8 @@ title: How to take dummy notes?
         :select-block-below "Select Block Below"
         :select-all-blocks "Select All Blocks"
         :general "General"
+        :more "More"
+        :search/result-for "Search result for "
         :help/toggle "Toggle help"
         :help/git-commit-message "Git commit message"
         :help/full-text-search "Full Text Search"
@@ -883,6 +885,8 @@ title: How to take dummy notes?
            :select-block-below "选择下方的块"
            :select-all-blocks "选择所有块"
            :general "常规​​​​​"
+           :more "更多"
+           :search/result-for "更多搜索结果 "
            :help/toggle "显示/关闭帮助"
            :help/git-commit-message "提交消息"
            :help/full-text-search "全文搜索"

+ 10 - 6
src/main/frontend/handler/search.cljs

@@ -12,10 +12,12 @@
 (defn search
   ([repo q]
    (search repo q nil))
-  ([repo q {:keys [page-db-id]
-            :or {page-db-id nil}
+  ([repo q {:keys [page-db-id limit more?]
+            :or {page-db-id nil
+                 limit 20}
             :as opts}]
-   (let [page-db-id (if (string? page-db-id)
+   (let [limit (:limit opts)
+         page-db-id (if (string? page-db-id)
                       (:db/id (db/entity repo [:page/name (string/lower-case page-db-id)]))
                       page-db-id)]
      (p/let [blocks (search/block-search repo q opts)]
@@ -23,11 +25,13 @@
                      (filter (fn [block] (= (get-in block [:block/page :db/id]) page-db-id)) blocks)
                      blocks)
             result (merge
-                    {:blocks blocks}
+                    {:blocks blocks
+                     :has-more? (= limit (count blocks))}
                     (when-not page-db-id
                       {:pages (search/page-search q)
-                       :files (search/file-search q)}))]
-        (swap! state/state assoc :search/result result))))))
+                       :files (search/file-search q)}))
+            search-key (if more? :search/more-result :search/result)]
+        (swap! state/state assoc search-key result))))))
 
 (defn clear-search!
   []

+ 5 - 0
src/main/frontend/routes.cljs

@@ -5,6 +5,7 @@
             [frontend.components.page :as page]
             [frontend.components.diff :as diff]
             [frontend.components.journal :as journal]
+            [frontend.components.search :as search]
             [frontend.components.settings :as settings]
             [frontend.components.external :as external]
             [frontend.components.publishing :as publishing]))
@@ -31,6 +32,10 @@
     {:name :file
      :view file/file}]
 
+   ["/search/:q"
+    {:name :search
+     :view search/more}]
+
    ["/new-page"
     {:name :new-page
      :view page/new}]

+ 4 - 0
src/main/frontend/ui.css

@@ -12,6 +12,10 @@
   }
 }
 
+.search-all #ui__ac-inner {
+    max-height: none;
+}
+
 .ui__notifications {
   position: fixed;
   z-index: 99;