feat(electron): impl quick capture x-callback-url

dev/yarn-workspaces
Andelf 2022-06-15 19:24:28 +08:00
parent 0f30893636
commit 2080481c73
2 changed files with 50 additions and 6 deletions

View File

@ -29,7 +29,7 @@
:payload (str "Failed to open link. Missing graph identifier after `logseq://graph/`.")})))
(defn local-url-handler
"Given a URL with `graph identifier` as path, `page` (optional) and `block-id`
"Given a URL with `graph identifier` as path, `page` (optional) and `block-id`
(optional) as parameters, open the local graphs accordingly.
`graph identifier` is the name of the graph to open, e.g. `lambda`"
[^js win parsed-url force-new-window?]
@ -45,10 +45,10 @@
;; TODO: allow open new window on specific page, without waiting for `graph ready` ipc then redirect to that page
(when (or page-name block-id file)
(let [redirect-f (fn [win' graph-name']
(when (= graph-name graph-name')
(send-to-renderer win' "redirectWhenExists" {:page-name page-name
:block-id block-id
:file file})))]
(when (= graph-name graph-name')
(send-to-renderer win' "redirectWhenExists" {:page-name page-name
:block-id block-id
:file file})))]
(if open-new-window?
(state/set-state! :window/once-graph-ready redirect-f)
(do (win/switch-to-window! window-on-graph)
@ -57,6 +57,22 @@
(send-to-renderer win "openNewWindowOfGraph" graph-name)))
(graph-identifier-error-handler graph-identifier))))
(defn- x-callback-url-handler
[^js parsed-url]
(let [action (.-pathname parsed-url)]
(cond
(= action "/quickCapture")
(let [[url title content] (get-URL-decoded-params parsed-url ["url" "title" "content"])]
(send-to-renderer "quickCapture" {:url url
:title title
:content content}))
:else
(send-to-renderer "notification" {:type "error"
:payload (str "Unimplemented x-callback-url action: `"
action
"`.")}))))
(defn logseq-url-handler
[^js win parsed-url]
(let [url-host (.-host parsed-url)] ;; return "" when no pathname provided
@ -64,6 +80,9 @@
(= "auth-callback" url-host)
(send-to-renderer win "loginCallback" (.get (.-searchParams parsed-url) "code"))
(= "x-callback-url" url-host)
(x-callback-url-handler parsed-url)
;; identifier of graph in local
(= "graph" url-host)
(local-url-handler win parsed-url false)

View File

@ -1,9 +1,12 @@
(ns electron.listener
(:require [frontend.state :as state]
[frontend.context.i18n :refer [t]]
[frontend.date :as date]
[frontend.handler.route :as route-handler]
[frontend.handler.editor :as editor-handler]
[frontend.handler.ui :as ui-handler]
[frontend.config :as config]
[clojure.string :as string]
[cljs-bean.core :as bean]
[frontend.fs.watcher-handler :as watcher-handler]
[frontend.fs.sync :as sync]
@ -34,7 +37,7 @@
(fn [_req]
(persist-dbs!))))
(defn listen-to-electron!
(defn ^:large-vars/cleanup-todo listen-to-electron!
[]
;; TODO: move "file-watcher" to electron.ipc.channels
(js/window.apis.on "file-watcher"
@ -125,6 +128,28 @@
(fn [code]
(user/login-callback code)))
(js/window.apis.on "quickCapture"
(fn [args]
(let [{:keys [url title content]} (bean/->clj args)
page (or (state/get-current-page)
(string/lower-case (date/journal-name)))
format (db/get-page-format page)
time (date/get-current-time)
text (or (and content (not-empty (string/trim content))) "")
link (if (not-empty title) (config/link-format format title url) url)
template (get-in (state/get-config)
[:quick-capture-templates :text]
"**{time}** [[quick capture]]: {text} {url}")
content (-> template
(string/replace "{time}" time)
(string/replace "{url}" link)
(string/replace "{text}" text))]
(if (state/get-edit-block)
(editor-handler/insert content)
(editor-handler/api-insert-new-block! content {:page page
:edit-block? false
:replace-empty-target? true})))))
(js/window.apis.on "openNewWindowOfGraph"
;; Handle open new window in renderer, until the destination graph doesn't rely on setting local storage
;; No db cache persisting ensured. Should be handled by the caller