Browse Source

Add basic add to today functionality

Gabriel Horner 2 years ago
parent
commit
7d0f089704
1 changed files with 91 additions and 50 deletions
  1. 91 50
      src/main/frontend/components/search.cljs

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

@@ -83,7 +83,26 @@
      [:div {:class "font-medium" :key "content"}
      [:div {:class "font-medium" :key "content"}
       (highlight-exact-query content q)]]))
       (highlight-exact-query content q)]]))
 
 
-(defonce search-timeout (atom nil))
+(defn- search-has-a-match?
+  [pages search-q result]
+  (or
+   (and (seq pages)
+        (= (util/safe-page-name-sanity-lc search-q)
+           (util/safe-page-name-sanity-lc (first pages))))
+   (nil? result)))
+
+(defn- transform-pages
+  [pages]
+  (map (fn [page]
+         (let [alias (model/get-redirect-page-name page)]
+           (cond->
+            {:type :page
+             :data page}
+            (and alias
+                 (not= (util/page-name-sanity-lc page)
+                       (util/page-name-sanity-lc alias)))
+            (assoc :alias alias))))
+       (remove nil? pages)))
 
 
 (defn- search-on-chosen-open-link
 (defn- search-on-chosen-open-link
   [repo search-q {:keys [data type alias]}]
   [repo search-q {:keys [data type alias]}]
@@ -113,7 +132,8 @@
 
 
 (defn- search-on-chosen
 (defn- search-on-chosen
   [repo search-q {:keys [type data alias]}]
   [repo search-q {:keys [type data alias]}]
-  (search-handler/add-search-to-recent! repo search-q)
+  (when-not (contains? #{:new-page :add-to-todays-journal} type)
+    (search-handler/add-search-to-recent! repo search-q))
   (search-handler/clear-search!)
   (search-handler/clear-search!)
   (case type
   (case type
     :graph-add-filter
     :graph-add-filter
@@ -125,6 +145,11 @@
     :new-whiteboard
     :new-whiteboard
     (whiteboard-handler/create-new-whiteboard-and-redirect! search-q)
     (whiteboard-handler/create-new-whiteboard-and-redirect! search-q)
 
 
+    :add-to-todays-journal
+    (editor-handler/api-insert-new-block!
+     search-q
+     {:page (date/today)})
+
     :page
     :page
     (let [data (or alias data)]
     (let [data (or alias data)]
       (cond
       (cond
@@ -209,6 +234,12 @@
        :graph-add-filter
        :graph-add-filter
        [:b search-q]
        [:b search-q]
 
 
+       :add-to-todays-journal
+       (search-result-item {:name "block"
+                            :title (t :search-item/block)
+                            :extension? true}
+                           "Add new block to today's journal")
+
        :new-page
        :new-page
        (create-item-render "new-page" (t :new-page) (str "\"" (string/trim search-q) "\""))
        (create-item-render "new-page" (t :new-page) (str "\"" (string/trim search-q) "\""))
 
 
@@ -250,29 +281,21 @@
 
 
 (rum/defc search-auto-complete
 (rum/defc search-auto-complete
   [{:keys [pages files blocks has-more?] :as result} search-q all?]
   [{:keys [pages files 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->
-                                       {:type :page
-                                        :data page}
-                                        (and alias
-                                             (not= (util/page-name-sanity-lc page)
-                                                   (util/page-name-sanity-lc alias)))
-                                        (assoc :alias alias))))
-                                  (remove nil? pages)))
+  (let [pages (when-not all? (transform-pages pages))
         files (when-not all? (map (fn [file] {:type :file :data file}) files))
         files (when-not all? (map (fn [file] {:type :file :data file}) files))
         blocks (map (fn [block] {:type :block :data block}) blocks)
         blocks (map (fn [block] {:type :block :data block}) blocks)
         search-mode (state/sub :search/mode)
         search-mode (state/sub :search/mode)
-        new-page (if (or
-                      (and (seq pages)
-                           (= (util/safe-page-name-sanity-lc search-q)
-                              (util/safe-page-name-sanity-lc (:data (first pages)))))
-                      (nil? result)
-                      all?)
-                   []
-                   (if (state/enable-whiteboards?)
-                     [{:type :new-page} {:type :new-whiteboard}]
-                     [{:type :new-page}]))
+        create-results
+        (if (or (search-has-a-match? (:pages result) search-q result)
+                all?)
+          []
+          (cond-> []
+                  (= search-mode :global)
+                  (conj {:type :add-to-todays-journal :group "Create"})
+                  true
+                  (conj {:type :new-page})
+                  (state/enable-whiteboards?)
+                  (conj {:type :new-whiteboard})))
         result (cond
         result (cond
                  config/publishing?
                  config/publishing?
                  (concat pages files blocks)
                  (concat pages files blocks)
@@ -281,7 +304,11 @@
                  (concat pages blocks)
                  (concat pages blocks)
 
 
                  :else
                  :else
-                 (concat new-page pages files blocks))
+                 (concat create-results
+                         (map-indexed
+                          (fn [idx result]
+                            (if (= 0 idx) (assoc result :group "Search") result))
+                          (concat pages files blocks))))
         result (if (= search-mode :graph)
         result (if (= search-mode :graph)
                  [{:type :graph-add-filter}]
                  [{:type :graph-add-filter}]
                  result)
                  result)
@@ -290,6 +317,7 @@
      (ui/auto-complete
      (ui/auto-complete
       result
       result
       {:class "search-results"
       {:class "search-results"
+       :get-group-name :group
        :on-chosen #(search-on-chosen repo search-q %)
        :on-chosen #(search-on-chosen repo search-q %)
        :on-shift-chosen #(search-on-shift-chosen repo search-q %)
        :on-shift-chosen #(search-on-shift-chosen repo search-q %)
        :item-render #(search-item-render search-q %)
        :item-render #(search-item-render search-q %)
@@ -308,6 +336,8 @@
 (rum/defc recent-search-and-pages
 (rum/defc recent-search-and-pages
   [in-page-search?]
   [in-page-search?]
   [:div.recent-search
   [:div.recent-search
+  (when-not in-page-search?
+    [:div.px-2.font-medium.opacity-50.uppercase "Search"])
    [:div.wrap.px-4.py-2.text-sm.opacity-70.flex.flex-row.justify-between.align-items.mx-1.sm:mx-0
    [:div.wrap.px-4.py-2.text-sm.opacity-70.flex.flex-row.justify-between.align-items.mx-1.sm:mx-0
     [:div "Recent search:"]
     [:div "Recent search:"]
     (ui/with-shortcut :go/search-in-page "bottom"
     (ui/with-shortcut :go/search-in-page "bottom"
@@ -341,7 +371,7 @@
                     (case type
                     (case type
                       :page
                       :page
                       (do (route/redirect-to-page! data)
                       (do (route/redirect-to-page! data)
-                          (state/close-modal!))
+                        (state/close-modal!))
                       :search
                       :search
                       (let [q data]
                       (let [q data]
                         (state/set-q! q)
                         (state/set-q! q)
@@ -392,6 +422,32 @@
     :else
     :else
     (t :search)))
     (t :search)))
 
 
+(defonce search-timeout (atom nil))
+
+(defn- input-on-change
+  [e]
+  (when @search-timeout
+    (js/clearTimeout @search-timeout))
+  (let [timeout 300
+        value (util/evalue e)
+        is-composing? (util/onchange-event-is-composing? e)] ;; #3199
+    (if (and (string/blank? value) (not is-composing?))
+      (search-handler/clear-search! false)
+      (let [search-mode (state/get-search-mode)
+            opts (if (= :page search-mode)
+                   (when-let [current-page (or (state/get-current-page)
+                                               (date/today))]
+                     {:page-db-id (:db/id (db/entity [:block/name (util/page-name-sanity-lc current-page)]))})
+                   {})]
+        (state/set-q! value)
+        (reset! search-timeout
+                (js/setTimeout
+                 (fn []
+                   (if (= :page search-mode)
+                     (search-handler/search (state/get-current-repo) value opts)
+                     (search-handler/search (state/get-current-repo) value)))
+                 timeout))))))
+
 (rum/defcs search-modal < rum/reactive
 (rum/defcs search-modal < rum/reactive
   (shortcut/disable-all-shortcuts)
   (shortcut/disable-all-shortcuts)
   (mixins/event-mixin
   (mixins/event-mixin
@@ -404,9 +460,14 @@
   (let [search-result (state/sub :search/result)
   (let [search-result (state/sub :search/result)
         search-q (state/sub :search/q)
         search-q (state/sub :search/q)
         search-mode (state/sub :search/mode)
         search-mode (state/sub :search/mode)
-        timeout 300
         in-page-search? (= search-mode :page)]
         in-page-search? (= search-mode :page)]
     [:div.cp__palette.cp__palette-main
     [:div.cp__palette.cp__palette-main
+     (when (= :global search-mode)
+       [:div.pt-2.pl-4.header-wrap
+        (when (seq search-q)
+          (if (search-has-a-match? (:pages search-result) search-q search-result)
+            [:div [:span.mr-2 (ui/icon "search")] "Search"]
+            [:div [:span.mr-2 (ui/icon "plus")] "Quick Capture"]))])
      [:div.ls-search
      [:div.ls-search
       [:div.input-wrap
       [:div.input-wrap
       [:input.cp__palette-input.w-full
       [:input.cp__palette-input.w-full
@@ -420,31 +481,11 @@
                          (default-placeholder search-mode))
                          (default-placeholder search-mode))
         :auto-complete (if (util/chrome?) "chrome-off" "off") ; off not working here
         :auto-complete (if (util/chrome?) "chrome-off" "off") ; off not working here
         :value         search-q
         :value         search-q
-        :on-change     (fn [e]
-                         (when @search-timeout
-                           (js/clearTimeout @search-timeout))
-                         (let [value (util/evalue e)
-                               is-composing? (util/onchange-event-is-composing? e)] ;; #3199
-                           (if (and (string/blank? value) (not is-composing?))
-                             (search-handler/clear-search! false)
-                             (let [search-mode (state/get-search-mode)
-                                   opts (if (= :page search-mode)
-                                          (when-let [current-page (or (state/get-current-page)
-                                                                      (date/today))]
-                                            {:page-db-id (:db/id (db/entity [:block/name (util/page-name-sanity-lc current-page)]))})
-                                          {})]
-                               (state/set-q! value)
-                               (reset! search-timeout
-                                       (js/setTimeout
-                                        (fn []
-                                          (if (= :page search-mode)
-                                            (search-handler/search (state/get-current-repo) value opts)
-                                            (search-handler/search (state/get-current-repo) value)))
-                                        timeout))))))}]]
-      [:div.search-results-wrap
-       (if (seq search-result)
-         (search-auto-complete search-result search-q false)
-         (recent-search-and-pages in-page-search?))]]]))
+        :on-change     input-on-change}]]
+     [:div.search-results-wrap
+      (if (seq search-result)
+        (search-auto-complete search-result search-q false)
+        (recent-search-and-pages in-page-search?))]]]))
 
 
 (rum/defc more < rum/reactive
 (rum/defc more < rum/reactive
   [route]
   [route]