ui.cljs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. (ns frontend.handler.events.ui
  2. "UI events"
  3. (:require [clojure.core.async :as async]
  4. [clojure.core.async.interop :refer [p->c]]
  5. [frontend.components.block :as block]
  6. [frontend.components.cmdk.core :as cmdk]
  7. [frontend.components.file-sync :as file-sync]
  8. [frontend.components.page :as component-page]
  9. [frontend.components.plugins :as plugin]
  10. [frontend.components.property.dialog :as property-dialog]
  11. [frontend.components.repo :as repo]
  12. [frontend.components.select :as select]
  13. [frontend.components.selection :as selection]
  14. [frontend.components.settings :as settings]
  15. [frontend.components.shell :as shell]
  16. [frontend.components.user.login :as login]
  17. [frontend.components.whiteboard :as whiteboard]
  18. [frontend.config :as config]
  19. [frontend.context.i18n :refer [t]]
  20. [frontend.db :as db]
  21. [frontend.extensions.fsrs :as fsrs]
  22. [frontend.extensions.srs :as srs]
  23. [frontend.fs.capacitor-fs :as capacitor-fs]
  24. [frontend.fs.nfs :as nfs]
  25. [frontend.fs.sync :as sync]
  26. [frontend.handler.db-based.rtc :as rtc-handler]
  27. [frontend.handler.editor :as editor-handler]
  28. [frontend.handler.events :as events]
  29. [frontend.handler.file-based.nfs :as nfs-handler]
  30. [frontend.handler.file-sync :as file-sync-handler]
  31. [frontend.handler.notification :as notification]
  32. [frontend.handler.page :as page-handler]
  33. [frontend.handler.plugin :as plugin-handler]
  34. [frontend.handler.repo :as repo-handler]
  35. [frontend.handler.route :as route-handler]
  36. [frontend.handler.user :as user-handler]
  37. [frontend.mobile.util :as mobile-util]
  38. [frontend.modules.instrumentation.sentry :as sentry-event]
  39. [frontend.state :as state]
  40. [frontend.ui :as ui]
  41. [frontend.util :as util]
  42. [goog.dom :as gdom]
  43. [logseq.common.util :as common-util]
  44. [logseq.shui.ui :as shui]
  45. [promesa.core :as p]
  46. [rum.core :as rum]))
  47. (defmethod events/handle :class/configure [[_ page]]
  48. (shui/dialog-open!
  49. #(block/block-container {} page)
  50. {:label "page-configure"
  51. :align :top}))
  52. (defmethod events/handle :go/search [_]
  53. (shui/dialog-open!
  54. cmdk/cmdk-modal
  55. {:id :ls-dialog-cmdk
  56. :align :top
  57. :content-props {:class "ls-dialog-cmdk"}
  58. :close-btn? false}))
  59. (defmethod events/handle :command/run [_]
  60. (when (util/electron?)
  61. (shui/dialog-open! shell/shell)))
  62. (defmethod events/handle :notification/show [[_ {:keys [content status clear?]}]]
  63. (notification/show! content status clear?))
  64. (defmethod events/handle :command/run [_]
  65. (when (util/electron?)
  66. (shui/dialog-open! shell/shell)))
  67. (defmethod events/handle :go/plugins [_]
  68. (plugin/open-plugins-modal!))
  69. (defmethod events/handle :go/plugins-waiting-lists [_]
  70. (plugin/open-waiting-updates-modal!))
  71. (defmethod events/handle :go/plugins-from-file [[_ plugins]]
  72. (plugin/open-plugins-from-file-modal! plugins))
  73. (defmethod events/handle :go/install-plugin-from-github [[_]]
  74. (shui/dialog-open!
  75. (plugin/install-from-github-release-container)))
  76. (defmethod events/handle :go/plugins-settings [[_ pid nav? title]]
  77. (when pid
  78. (state/set-state! :plugin/focused-settings pid)
  79. (state/set-state! :plugin/navs-settings? (not (false? nav?)))
  80. (plugin/open-focused-settings-modal! title)))
  81. (defmethod events/handle :go/proxy-settings [[_ agent-opts]]
  82. (shui/dialog-open!
  83. (plugin/user-proxy-settings-container agent-opts)
  84. {:id :https-proxy-panel :center? true :class "lg:max-w-2xl"}))
  85. (defmethod events/handle :redirect-to-home [_]
  86. (page-handler/create-today-journal!))
  87. (defmethod events/handle :page/show-delete-dialog [[_ selected-rows ok-handler]]
  88. (shui/dialog-open!
  89. (component-page/batch-delete-dialog
  90. selected-rows false
  91. ok-handler)))
  92. (defn ask-permission
  93. [repo]
  94. (when
  95. (and (not (util/electron?))
  96. (not (mobile-util/native-platform?)))
  97. (fn [{:keys [close]}]
  98. [:div
  99. ;; TODO: fn translation with args
  100. [:p
  101. "Grant native filesystem permission for directory: "
  102. [:b (config/get-local-dir repo)]]
  103. (ui/button
  104. (t :settings-permission/start-granting)
  105. :class "ui__modal-enter"
  106. :on-click (fn []
  107. (nfs/check-directory-permission! repo)
  108. (close)))])))
  109. (defn get-local-repo
  110. []
  111. (when-let [repo (state/get-current-repo)]
  112. (when (config/local-file-based-graph? repo)
  113. repo)))
  114. (defmethod events/handle :modal/nfs-ask-permission []
  115. (when-let [repo (get-local-repo)]
  116. (some-> (ask-permission repo)
  117. (shui/dialog-open! {:align :top}))))
  118. (defmethod events/handle :modal/show-cards [[_ cards-id]]
  119. (let [db-based? (config/db-based-graph? (state/get-current-repo))]
  120. (shui/dialog-open!
  121. (if db-based? (fn [] (fsrs/cards-view cards-id)) srs/global-cards)
  122. {:id :srs
  123. :label "flashcards__cp"})))
  124. (defmethod events/handle :modal/show-instruction [_]
  125. (shui/dialog-open!
  126. capacitor-fs/instruction
  127. {:id :instruction
  128. :label "instruction__cp"}))
  129. (defmethod events/handle :modal/show-themes-modal [[_ classic?]]
  130. (if classic?
  131. (plugin/open-select-theme!)
  132. (route-handler/go-to-search! :themes)))
  133. (defmethod events/handle :ui/toggle-appearance [_]
  134. (let [popup-id "appearance_settings"]
  135. (if (gdom/getElement popup-id)
  136. (shui/popup-hide! popup-id)
  137. (shui/popup-show!
  138. (js/document.querySelector ".toolbar-dots-btn")
  139. (fn []
  140. (settings/appearance))
  141. {:id popup-id
  142. :align :end}))))
  143. (defmethod events/handle :plugin/consume-updates [[_ id prev-pending? updated?]]
  144. (let [downloading? (:plugin/updates-downloading? @state/state)
  145. auto-checking? (plugin-handler/get-auto-checking?)]
  146. (when-let [coming (and (not downloading?)
  147. (get-in @state/state [:plugin/updates-coming id]))]
  148. (let [error-code (:error-code coming)
  149. error-code (if (= error-code (str :no-new-version)) nil error-code)
  150. title (:title coming)]
  151. (when (and prev-pending? (not auto-checking?))
  152. (if-not error-code
  153. (plugin/set-updates-sub-content! (str title "...") 0)
  154. (notification/show!
  155. (str "[Checked]<" title "> " error-code) :error)))))
  156. (if (and updated? downloading?)
  157. ;; try to start consume downloading item
  158. (if-let [next-coming (state/get-next-selected-coming-update)]
  159. (plugin-handler/check-or-update-marketplace-plugin!
  160. (assoc next-coming :only-check false :error-code nil)
  161. (fn [^js e] (js/console.error "[Download Err]" next-coming e)))
  162. (plugin-handler/close-updates-downloading))
  163. ;; try to start consume pending item
  164. (if-let [next-pending (second (first (:plugin/updates-pending @state/state)))]
  165. (do
  166. (println "Updates: take next pending - " (:id next-pending))
  167. (js/setTimeout
  168. #(plugin-handler/check-or-update-marketplace-plugin!
  169. (assoc next-pending :only-check true :auto-check auto-checking? :error-code nil)
  170. (fn [^js e]
  171. (notification/show! (.toString e) :error)
  172. (js/console.error "[Check Err]" next-pending e))) 500))
  173. ;; try to open waiting updates list
  174. (do (when (and prev-pending? (not auto-checking?)
  175. (seq (state/all-available-coming-updates)))
  176. (plugin/open-waiting-updates-modal!))
  177. (plugin-handler/set-auto-checking! false))))))
  178. (defmethod events/handle :plugin/loader-perf-tip [[_ {:keys [^js o _s _e]}]]
  179. (when-let [opts (.-options o)]
  180. (notification/show!
  181. (plugin/perf-tip-content (.-id o) (.-name opts) (.-url opts))
  182. :warning false (.-id o))))
  183. (defn- refresh-cb []
  184. (page-handler/create-today-journal!)
  185. (events/file-sync-restart!))
  186. (defmethod events/handle :graph/ask-for-re-fresh [_]
  187. (shui/dialog-open!
  188. [:div {:style {:max-width 700}}
  189. [:p (t :sync-from-local-changes-detected)]
  190. [:div.flex.justify-end
  191. (ui/button
  192. (t :yes)
  193. :autoFocus "on"
  194. :class "ui__modal-enter"
  195. :on-click (fn []
  196. (shui/dialog-close!)
  197. (nfs-handler/refresh! (state/get-current-repo) refresh-cb)))]]))
  198. (defn- editor-new-property [block target {:keys [selected-blocks] :as opts}]
  199. (let [editing-block (state/get-edit-block)
  200. pos (state/get-edit-pos)
  201. edit-block-or-selected (cond
  202. editing-block
  203. [editing-block]
  204. (seq selected-blocks)
  205. selected-blocks
  206. :else
  207. (seq (keep #(db/entity [:block/uuid %]) (state/get-selection-block-ids))))
  208. current-block (when-let [s (state/get-current-page)]
  209. (when (util/uuid-string? s)
  210. (db/entity [:block/uuid (uuid s)])))
  211. blocks (or (when block [block])
  212. edit-block-or-selected
  213. (when current-block [current-block]))
  214. opts' (cond-> opts
  215. editing-block
  216. (assoc :original-block editing-block
  217. :edit-original-block
  218. (fn [{:keys [editing-default-property?]}]
  219. (when editing-block
  220. (let [content (:block/title (db/entity (:db/id editing-block)))
  221. esc? (= "Escape" (state/get-ui-last-key-code))
  222. [content' pos] (cond
  223. esc?
  224. [nil pos]
  225. (and (>= (count content) pos)
  226. (>= pos 2)
  227. (= (util/nth-safe content (dec pos))
  228. (util/nth-safe content (- pos 2))
  229. ";"))
  230. [(str (common-util/safe-subs content 0 (- pos 2))
  231. (common-util/safe-subs content pos))
  232. (- pos 2)]
  233. :else
  234. [nil pos])]
  235. (when content'
  236. (if editing-default-property?
  237. (editor-handler/save-block! (state/get-current-repo) (:block/uuid editing-block) content')
  238. (editor-handler/edit-block! editing-block (or pos :max)
  239. (cond-> {}
  240. content'
  241. (assoc :custom-content content'))))))))))]
  242. (when (seq blocks)
  243. (let [target' (or target
  244. (some-> (state/get-edit-input-id)
  245. (gdom/getElement))
  246. (first (state/get-selection-blocks)))]
  247. (if target'
  248. (shui/popup-show! target'
  249. #(property-dialog/dialog blocks opts')
  250. {:align "start"
  251. :auto-focus? true})
  252. (shui/dialog-open! #(property-dialog/dialog blocks opts')
  253. {:id :property-dialog
  254. :align "start"}))))))
  255. (defmethod events/handle :editor/new-property [[_ {:keys [block target] :as opts}]]
  256. (when-not config/publishing?
  257. (p/do!
  258. (editor-handler/save-current-block!)
  259. (editor-new-property block target opts))))
  260. (defmethod events/handle :graph/new-db-graph [[_ _opts]]
  261. (shui/dialog-open!
  262. repo/new-db-graph
  263. {:id :new-db-graph
  264. :title [:h2 "Create a new graph"]
  265. :style {:max-width "500px"}}))
  266. (defmethod events/handle :dialog-select/graph-open []
  267. (select/dialog-select! :graph-open))
  268. (defmethod events/handle :dialog-select/graph-remove []
  269. (select/dialog-select! :graph-remove))
  270. (defmethod events/handle :dialog-select/db-graph-replace []
  271. (select/dialog-select! :db-graph-replace))
  272. (rum/defc multi-tabs-dialog
  273. []
  274. (let [word (if (util/electron?) "window" "tab")]
  275. [:div.flex.p-4.flex-col.gap-4.h-64
  276. [:span.warning.text-lg
  277. (util/format "Logseq doesn't support multiple %ss access to the same graph yet, please close this %s or switch to another graph."
  278. word word)]
  279. [:div.text-lg
  280. [:p "Switch to another repo: "]
  281. [:div.border.rounded.bg-gray-01.overflow-hidden.w-60
  282. (repo/repos-dropdown {:on-click (fn [e]
  283. (util/stop e)
  284. (state/set-state! :error/multiple-tabs-access-opfs? false)
  285. (shui/dialog-close!))})]]]))
  286. (defmethod events/handle :show/multiple-tabs-error-dialog [_]
  287. (state/set-state! :error/multiple-tabs-access-opfs? true)
  288. (shui/dialog-open! multi-tabs-dialog))
  289. (defmethod events/handle :editor/show-action-bar []
  290. (let [selection (state/get-selection-blocks)
  291. first-visible-block (some #(when (util/el-visible-in-viewport? % true) %) selection)]
  292. (when first-visible-block
  293. (shui/popup-hide! :selection-action-bar)
  294. (shui/popup-show!
  295. first-visible-block
  296. (fn []
  297. (selection/action-bar))
  298. {:id :selection-action-bar
  299. :content-props {:side "top"
  300. :class "!py-0 !px-0 !border-none"}
  301. :auto-side? false
  302. :align :start}))))
  303. (defmethod events/handle :editor/hide-action-bar []
  304. (shui/popup-hide! :selection-action-bar))
  305. (defmethod events/handle :user/logout [[_]]
  306. (file-sync-handler/reset-session-graphs)
  307. (sync/remove-all-pwd!)
  308. (file-sync-handler/reset-user-state!)
  309. (login/sign-out!))
  310. (defmethod events/handle :user/login [[_ host-ui?]]
  311. (if (or host-ui? (not util/electron?))
  312. (js/window.open config/LOGIN-URL)
  313. (if (mobile-util/native-platform?)
  314. (route-handler/redirect! {:to :user-login})
  315. (login/open-login-modal!))))
  316. (defmethod events/handle :whiteboard/onboarding [[_ opts]]
  317. (shui/dialog-open!
  318. (fn [{:keys [close]}] (whiteboard/onboarding-welcome close))
  319. (merge {:close-btn? false
  320. :center? true
  321. :close-backdrop? false} opts)))
  322. (defn- enable-beta-features!
  323. []
  324. (when-not (false? (state/enable-sync?)) ; user turns it off
  325. (file-sync-handler/set-sync-enabled! true)))
  326. ;; TODO: separate rtc and file-based implementation
  327. (defmethod events/handle :user/fetch-info-and-graphs [[_]]
  328. (state/set-state! [:ui/loading? :login] false)
  329. (async/go
  330. (let [result (async/<! (sync/<user-info sync/remoteapi))]
  331. (cond
  332. (instance? ExceptionInfo result)
  333. nil
  334. (map? result)
  335. (do
  336. (state/set-user-info! result)
  337. (when-let [uid (user-handler/user-uuid)]
  338. (sentry-event/set-user! uid))
  339. (let [status (if (user-handler/alpha-or-beta-user?) :welcome :unavailable)]
  340. (when (and (= status :welcome) (user-handler/logged-in?))
  341. (enable-beta-features!)
  342. (async/<! (p->c (rtc-handler/<get-remote-graphs)))
  343. (async/<! (file-sync-handler/load-session-graphs))
  344. (p/let [repos (repo-handler/refresh-repos!)]
  345. (when-let [repo (state/get-current-repo)]
  346. (when (some #(and (= (:url %) repo)
  347. (vector? (:sync-meta %))
  348. (util/uuid-string? (first (:sync-meta %)))
  349. (util/uuid-string? (second (:sync-meta %)))) repos)
  350. (sync/<sync-start)))))
  351. (file-sync/maybe-onboarding-show status)))))))
  352. (defmethod events/handle :file-sync/onboarding-tip [[_ type opts]]
  353. (let [type (keyword type)]
  354. (when-not (config/db-based-graph? (state/get-current-repo))
  355. (shui/dialog-open!
  356. (file-sync/make-onboarding-panel type)
  357. (merge {:close-btn? false
  358. :center? true
  359. :close-backdrop? (not= type :welcome)} opts)))))