Prechádzať zdrojové kódy

Add keybinding to open links from search

Gabriel Horner 3 rokov pred
rodič
commit
3772e67ed9

+ 29 - 1
src/main/frontend/components/search.cljs

@@ -4,8 +4,10 @@
             [frontend.components.block :as block]
             [frontend.components.svg :as svg]
             [frontend.handler.route :as route]
+            [frontend.handler.editor :as editor-handler]
             [frontend.handler.page :as page-handler]
             [frontend.handler.block :as block-handler]
+            [frontend.handler.notification :as notification]
             [frontend.db :as db]
             [frontend.db.model :as model]
             [frontend.handler.search :as search-handler]
@@ -83,6 +85,31 @@
 
 (defonce search-timeout (atom nil))
 
+(defn- search-on-chosen-open-link
+  [repo search-q {:keys [data type alias]}]
+  (search-handler/add-search-to-recent! repo search-q)
+  (search-handler/clear-search!)
+  (cond
+    (= :block type)
+    ;; Open the first link in a block's content
+    (let [block-uuid (uuid (:block/uuid data))
+          block (:block/content (db/entity [:block/uuid block-uuid]))
+          link (re-find editor-handler/url-regex block)]
+      (if link
+        (js/window.open link)
+        (notification/show! "No link found on this block." :warning)))
+
+    (= :page type)
+    ;; Open the first link found in a page's properties
+    (let [data (or alias data)
+          page (when data (db/entity [:block/name (util/page-name-sanity-lc data)]))
+          link (some #(re-find editor-handler/url-regex (val %)) (:block/properties page))]
+      (if link
+        (js/window.open link)
+        (notification/show! "No link found on this page's properties." :warning))))
+
+  (state/close-modal!))
+
 (rum/defc ^:large-vars/cleanup-todo search-auto-complete
   [{:keys [pages files blocks has-more?] :as result} search-q all?]
   (let [pages (when-not all? (map (fn [page]
@@ -214,7 +241,8 @@
                                                    (search-result-item "Block"
                                                                        (block-search-result-item repo uuid format content search-q search-mode))])
 
-                                                nil)]))})
+                                                nil)]))
+       :on-chosen-open-link #(search-on-chosen-open-link repo search-q %)})
      (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})

+ 10 - 0
src/main/frontend/handler/ui.cljs

@@ -231,6 +231,16 @@
       ((or on-shift-chosen on-chosen) (nth matched @current-idx) false)
       (and on-enter (on-enter state)))))
 
+(defn auto-complete-open-link
+  [state e]
+  (let [[matched {:keys [on-chosen-open-link]}] (:rum/args state)
+        current-idx (get state :frontend.ui/current-idx)]
+    (util/stop e)
+    (when (and (seq matched)
+             (> (count matched)
+                @current-idx))
+      (on-chosen-open-link (nth matched @current-idx) false))))
+
 ;; date-picker
 ;; TODO: find a better way
 (def *internal-model (rum/cursor state/state :date-picker/date))

+ 7 - 1
src/main/frontend/modules/shortcut/config.cljs

@@ -64,6 +64,10 @@
                                   :binding "shift+enter"
                                   :fn      ui-handler/auto-complete-shift-complete}
 
+   :auto-complete/open-link      {:desc    "Auto-complete: Open selected item in browser"
+                                  :binding "mod+o"
+                                  :fn      ui-handler/auto-complete-open-link}
+
    :cards/toggle-answers         {:desc    "Cards: show/hide answers/clozes"
                                   :binding "s"
                                   :fn      srs/toggle-answers}
@@ -482,7 +486,8 @@
     (build-category-map [:auto-complete/complete
                          :auto-complete/prev
                          :auto-complete/next
-                         :auto-complete/shift-complete])
+                         :auto-complete/shift-complete
+                         :auto-complete/open-link])
 
     :shortcut.handler/cards
     (-> (build-category-map [:cards/toggle-answers
@@ -719,6 +724,7 @@
     :auto-complete/next
     :auto-complete/complete
     :auto-complete/shift-complete
+    :auto-complete/open-link
     :date-picker/prev-day
     :date-picker/next-day
     :date-picker/prev-week