events.cljs 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124
  1. (ns frontend.handler.events
  2. "System-component-like ns that defines named events and listens on a
  3. core.async channel to handle them. Any part of the system can dispatch
  4. one of these events using state/pub-event!"
  5. (:refer-clojure :exclude [run!])
  6. (:require ["@capacitor/filesystem" :refer [Directory Filesystem]]
  7. ["@sentry/react" :as Sentry]
  8. [cljs-bean.core :as bean]
  9. [clojure.core.async :as async]
  10. [clojure.core.async.interop :refer [p->c]]
  11. [clojure.string :as string]
  12. [frontend.commands :as commands]
  13. [frontend.components.block :as block]
  14. [frontend.components.cmdk.core :as cmdk]
  15. [frontend.components.diff :as diff]
  16. [frontend.components.encryption :as encryption]
  17. [frontend.components.file-based.git :as git-component]
  18. [frontend.components.file-sync :as file-sync]
  19. [frontend.components.plugins :as plugin]
  20. [frontend.components.property.dialog :as property-dialog]
  21. [frontend.components.repo :as repo]
  22. [frontend.components.select :as select]
  23. [frontend.components.selection :as selection]
  24. [frontend.components.settings :as settings]
  25. [frontend.components.shell :as shell]
  26. [frontend.components.user.login :as login]
  27. [frontend.components.whiteboard :as whiteboard]
  28. [frontend.config :as config]
  29. [frontend.context.i18n :refer [t]]
  30. [frontend.date :as date]
  31. [frontend.db :as db]
  32. [frontend.db.conn :as conn]
  33. [frontend.db.model :as db-model]
  34. [frontend.db.persist :as db-persist]
  35. [frontend.db.transact :as db-transact]
  36. [frontend.extensions.fsrs :as fsrs]
  37. [frontend.extensions.srs :as srs]
  38. [frontend.fs :as fs]
  39. [frontend.fs.capacitor-fs :as capacitor-fs]
  40. [frontend.fs.nfs :as nfs]
  41. [frontend.fs.sync :as sync]
  42. [frontend.fs.watcher-handler :as fs-watcher]
  43. [frontend.handler.assets :as assets-handler]
  44. [frontend.handler.code :as code-handler]
  45. [frontend.handler.common.page :as page-common-handler]
  46. [frontend.handler.db-based.property :as db-property-handler]
  47. [frontend.handler.db-based.rtc :as rtc-handler]
  48. [frontend.handler.db-based.rtc-flows :as rtc-flows]
  49. [frontend.handler.editor :as editor-handler]
  50. [frontend.handler.export :as export]
  51. [frontend.handler.file-based.file :as file-handler]
  52. [frontend.handler.file-based.nfs :as nfs-handler]
  53. [frontend.handler.file-sync :as file-sync-handler]
  54. [frontend.handler.graph :as graph-handler]
  55. [frontend.handler.notification :as notification]
  56. [frontend.handler.page :as page-handler]
  57. [frontend.handler.plugin :as plugin-handler]
  58. [frontend.handler.repo :as repo-handler]
  59. [frontend.handler.repo-config :as repo-config-handler]
  60. [frontend.handler.route :as route-handler]
  61. [frontend.handler.search :as search-handler]
  62. [frontend.handler.shell :as shell-handler]
  63. [frontend.handler.ui :as ui-handler]
  64. [frontend.handler.user :as user-handler]
  65. [frontend.mobile.core :as mobile]
  66. [frontend.mobile.graph-picker :as graph-picker]
  67. [frontend.mobile.util :as mobile-util]
  68. [frontend.modules.instrumentation.posthog :as posthog]
  69. [frontend.modules.instrumentation.sentry :as sentry-event]
  70. [frontend.modules.outliner.pipeline :as pipeline]
  71. [frontend.modules.outliner.ui :as ui-outliner-tx]
  72. [frontend.modules.shortcut.core :as st]
  73. [frontend.persist-db :as persist-db]
  74. [frontend.persist-db.browser :as db-browser]
  75. [frontend.quick-capture :as quick-capture]
  76. [frontend.rum :as r]
  77. [frontend.search :as search]
  78. [frontend.state :as state]
  79. [frontend.ui :as ui]
  80. [frontend.util :as util]
  81. [frontend.util.persist-var :as persist-var]
  82. [goog.dom :as gdom]
  83. [lambdaisland.glogi :as log]
  84. [logseq.common.config :as common-config]
  85. [logseq.common.util :as common-util]
  86. [logseq.db :as ldb]
  87. [logseq.shui.ui :as shui]
  88. [promesa.core :as p]
  89. [rum.core :as rum]))
  90. ;; TODO: should we move all events here?
  91. (defmulti handle first)
  92. (defn file-sync-restart! []
  93. (async/go (async/<! (p->c (persist-var/load-vars)))
  94. (async/<! (sync/<sync-stop))
  95. (some-> (sync/<sync-start) async/<!)))
  96. (defn- file-sync-stop! []
  97. (async/go (async/<! (p->c (persist-var/load-vars)))
  98. (async/<! (sync/<sync-stop))))
  99. (defn- enable-beta-features!
  100. []
  101. (when-not (false? (state/enable-sync?)) ; user turns it off
  102. (file-sync-handler/set-sync-enabled! true)))
  103. (defmethod handle :user/fetch-info-and-graphs [[_]]
  104. (state/set-state! [:ui/loading? :login] false)
  105. (async/go
  106. (let [result (async/<! (sync/<user-info sync/remoteapi))]
  107. (cond
  108. (instance? ExceptionInfo result)
  109. nil
  110. (map? result)
  111. (do
  112. (state/set-user-info! result)
  113. (when-let [uid (user-handler/user-uuid)]
  114. (sentry-event/set-user! uid))
  115. (let [status (if (user-handler/alpha-or-beta-user?) :welcome :unavailable)]
  116. (when (and (= status :welcome) (user-handler/logged-in?))
  117. (enable-beta-features!)
  118. (async/<! (p->c (rtc-handler/<get-remote-graphs)))
  119. (async/<! (file-sync-handler/load-session-graphs))
  120. (p/let [repos (repo-handler/refresh-repos!)]
  121. (when-let [repo (state/get-current-repo)]
  122. (when (some #(and (= (:url %) repo)
  123. (vector? (:sync-meta %))
  124. (util/uuid-string? (first (:sync-meta %)))
  125. (util/uuid-string? (second (:sync-meta %)))) repos)
  126. (sync/<sync-start)))))
  127. (ui-handler/re-render-root!)
  128. (file-sync/maybe-onboarding-show status)))))))
  129. (defmethod handle :user/logout [[_]]
  130. (file-sync-handler/reset-session-graphs)
  131. (sync/remove-all-pwd!)
  132. (file-sync-handler/reset-user-state!)
  133. (login/sign-out!))
  134. (defmethod handle :user/login [[_ host-ui?]]
  135. (if (or host-ui? (not util/electron?))
  136. (js/window.open config/LOGIN-URL)
  137. (if (mobile-util/native-platform?)
  138. (route-handler/redirect! {:to :user-login})
  139. (login/open-login-modal!))))
  140. (defmethod handle :graph/added [[_ repo {:keys [empty-graph?]}]]
  141. (search-handler/rebuild-indices!)
  142. (plugin-handler/hook-plugin-app :graph-after-indexed {:repo repo :empty-graph? empty-graph?})
  143. (route-handler/redirect-to-home!)
  144. (when-let [dir-name (and (not (config/db-based-graph? repo)) (config/get-repo-dir repo))]
  145. (fs/watch-dir! dir-name))
  146. (file-sync-restart!))
  147. (defmethod handle :init/commands [_]
  148. (page-handler/init-commands!))
  149. (defmethod handle :graph/unlinked [repo current-repo]
  150. (when (= (:url repo) current-repo)
  151. (file-sync-restart!)))
  152. ;; FIXME(andelf): awful multi-arty function.
  153. ;; Should use a `-impl` function instead of the awful `skip-ios-check?` param with nested callback.
  154. (defn- graph-switch
  155. ([graph]
  156. (graph-switch graph false))
  157. ([graph skip-ios-check?]
  158. (let [db-based? (config/db-based-graph? graph)]
  159. (if (and (mobile-util/native-ios?) (not skip-ios-check?))
  160. (state/pub-event! [:validate-appId graph-switch graph])
  161. (do
  162. (state/set-current-repo! graph)
  163. (page-handler/init-commands!)
  164. ;; load config
  165. (repo-config-handler/restore-repo-config! graph)
  166. (when-not (= :draw (state/get-current-route))
  167. (route-handler/redirect-to-home!))
  168. (when-not db-based?
  169. ;; graph-switch will trigger a rtc-start automatically
  170. ;; (rtc-handler/<rtc-start! graph)
  171. (file-sync-restart!))
  172. (when-let [dir-name (and (not db-based?) (config/get-repo-dir graph))]
  173. (fs/watch-dir! dir-name))
  174. (graph-handler/settle-metadata-to-local! {:last-seen-at (js/Date.now)}))))))
  175. ;; Parameters for the `persist-db` function, to show the notification messages
  176. (defn- graph-switch-on-persisted
  177. "graph: the target graph to switch to"
  178. [graph opts]
  179. (p/do!
  180. (repo-handler/restore-and-setup-repo! graph)
  181. (graph-switch graph)
  182. (state/set-state! :sync-graph/init? false)
  183. (when (:rtc-download? opts)
  184. (and (search-handler/rebuild-indices!) true)
  185. (repo-handler/refresh-repos!))))
  186. (defmethod handle :graph/switch [[_ graph opts]]
  187. (export/cancel-db-backup!)
  188. (persist-db/export-current-graph!)
  189. (state/set-state! :db/async-query-loading #{})
  190. (state/set-state! :db/async-queries {})
  191. (st/refresh!)
  192. (reset! r/*key->atom {})
  193. (let [worker @db-browser/*worker]
  194. (p/let [writes-finished? (when worker (worker :general/file-writes-finished? (state/get-current-repo)))
  195. request-finished? (db-transact/request-finished?)]
  196. (if (not writes-finished?) ; TODO: test (:sync-graph/init? @state/state)
  197. (do
  198. (log/info :graph/switch (cond->
  199. {:request-finished? request-finished?
  200. :file-writes-finished? writes-finished?}
  201. (false? request-finished?)
  202. (assoc :unfinished-requests? @db-transact/*unfinished-request-ids)))
  203. (notification/show!
  204. "Please wait seconds until all changes are saved for the current graph."
  205. :warning))
  206. (graph-switch-on-persisted graph opts)))))
  207. (defmethod handle :graph/pull-down-remote-graph [[_ graph dir-name]]
  208. (if (mobile-util/native-ios?)
  209. (when-let [graph-name (or dir-name (:GraphName graph))]
  210. (let [graph-name (util/safe-sanitize-file-name graph-name)]
  211. (if (string/blank? graph-name)
  212. (notification/show! "Illegal graph folder name.")
  213. ;; Create graph directory under Logseq document folder (local)
  214. (when-let [root (state/get-local-container-root-url)]
  215. (let [graph-path (graph-picker/validate-graph-dirname root graph-name)]
  216. (->
  217. (p/let [exists? (fs/dir-exists? graph-path)]
  218. (let [overwrite? (if exists?
  219. (js/confirm (str "There's already a directory with the name \"" graph-name "\", do you want to overwrite it? Make sure to backup it first if you're not sure about it."))
  220. true)]
  221. (if overwrite?
  222. (p/let [_ (fs/mkdir-if-not-exists graph-path)]
  223. (nfs-handler/ls-dir-files-with-path!
  224. graph-path
  225. {:ok-handler (fn []
  226. (file-sync-handler/init-remote-graph graph-path graph)
  227. (js/setTimeout (fn [] (repo-handler/refresh-repos!)) 200))}))
  228. (let [graph-name (-> (js/prompt "Please specify a new directory name to download the graph:")
  229. str
  230. string/trim)]
  231. (when-not (string/blank? graph-name)
  232. (state/pub-event! [:graph/pull-down-remote-graph graph graph-name]))))))
  233. (p/catch (fn [^js e]
  234. (notification/show! (str e) :error)
  235. (js/console.error e)))))))))
  236. (when (:GraphName graph)
  237. (shui/dialog-open!
  238. (file-sync/pick-dest-to-sync-panel graph)))))
  239. (defmethod handle :graph/pick-page-histories [[_ graph-uuid page-name]]
  240. (shui/dialog-open!
  241. (file-sync/pick-page-histories-panel graph-uuid page-name)
  242. {:id :page-histories :label "modal-page-histories"}))
  243. (defmethod handle :graph/open-new-window [[_ev target-repo]]
  244. (p/let [current-repo (state/get-current-repo)]
  245. (ui-handler/open-new-window-or-tab! current-repo target-repo)))
  246. (defmethod handle :graph/migrated [[_ _repo]]
  247. (js/alert "Graph migrated."))
  248. (defn get-local-repo
  249. []
  250. (when-let [repo (state/get-current-repo)]
  251. (when (config/local-file-based-graph? repo)
  252. repo)))
  253. (defn ask-permission
  254. [repo]
  255. (when
  256. (and (not (util/electron?))
  257. (not (mobile-util/native-platform?)))
  258. (fn [{:keys [close]}]
  259. [:div
  260. ;; TODO: fn translation with args
  261. [:p
  262. "Grant native filesystem permission for directory: "
  263. [:b (config/get-local-dir repo)]]
  264. (ui/button
  265. (t :settings-permission/start-granting)
  266. :class "ui__modal-enter"
  267. :on-click (fn []
  268. (nfs/check-directory-permission! repo)
  269. (close)))])))
  270. (defmethod handle :modal/nfs-ask-permission []
  271. (when-let [repo (get-local-repo)]
  272. (some-> (ask-permission repo)
  273. (shui/dialog-open! {:align :top}))))
  274. (defmethod handle :modal/show-cards [[_ cards-id]]
  275. (let [db-based? (config/db-based-graph? (state/get-current-repo))]
  276. (shui/dialog-open!
  277. (if db-based? (fn [] (fsrs/cards-view cards-id)) srs/global-cards)
  278. {:id :srs
  279. :label "flashcards__cp"})))
  280. (defmethod handle :modal/show-instruction [_]
  281. (shui/dialog-open!
  282. capacitor-fs/instruction
  283. {:id :instruction
  284. :label "instruction__cp"}))
  285. (defmethod handle :modal/show-themes-modal [[_ classic?]]
  286. (if classic?
  287. (plugin/open-select-theme!)
  288. (route-handler/go-to-search! :themes)))
  289. (defmethod handle :ui/toggle-appearance [_]
  290. (let [popup-id "appearance_settings"]
  291. (if (gdom/getElement popup-id)
  292. (shui/popup-hide! popup-id)
  293. (shui/popup-show!
  294. (js/document.querySelector ".toolbar-dots-btn")
  295. (fn []
  296. (settings/appearance))
  297. {:id popup-id
  298. :align :end}))))
  299. (defmethod handle :modal/set-git-username-and-email [[_ _content]]
  300. (shui/dialog-open! git-component/set-git-username-and-email))
  301. (defmethod handle :page/create [[_ page-name opts]]
  302. (if (= page-name (date/today))
  303. (page-handler/create-today-journal!)
  304. (page-handler/<create! page-name opts)))
  305. (defmethod handle :page/deleted [[_ repo page-name file-path tx-meta]]
  306. (page-common-handler/after-page-deleted! repo page-name file-path tx-meta))
  307. (defmethod handle :page/renamed [[_ repo data]]
  308. (page-common-handler/after-page-renamed! repo data))
  309. (defmethod handle :page/create-today-journal [[_ _repo]]
  310. (p/let [_ (page-handler/create-today-journal!)]
  311. (ui-handler/re-render-root!)))
  312. (defmethod handle :file/not-matched-from-disk [[_ path disk-content db-content]]
  313. (when-let [repo (state/get-current-repo)]
  314. (shui/dialog-open!
  315. #(diff/local-file repo path disk-content db-content)
  316. {:label "diff__cp"})))
  317. (defmethod handle :modal/display-file-version-selector [[_ versions path get-content]]
  318. (shui/dialog-open!
  319. #(git-component/file-version-selector versions path get-content)))
  320. (defmethod handle :graph/sync-context []
  321. (let [context {:dev? config/dev?
  322. :node-test? util/node-test?
  323. :validate-db-options (:dev/validate-db-options (state/get-config))
  324. :importing? (:graph/importing @state/state)
  325. :date-formatter (state/get-date-formatter)
  326. :journal-file-name-format (or (state/get-journal-file-name-format)
  327. date/default-journal-filename-formatter)
  328. :export-bullet-indentation (state/get-export-bullet-indentation)
  329. :preferred-format (state/get-preferred-format)
  330. :journals-directory (config/get-journals-directory)
  331. :whiteboards-directory (config/get-whiteboards-directory)
  332. :pages-directory (config/get-pages-directory)}
  333. worker @state/*db-worker]
  334. (when worker
  335. (worker :general/set-context (ldb/write-transit-str context)))))
  336. ;; Hook on a graph is ready to be shown to the user.
  337. ;; It's different from :graph/restored, as :graph/restored is for window reloaded
  338. ;; FIXME: config may not be loaded when the graph is ready.
  339. (defmethod handle :graph/ready
  340. [[_ repo]]
  341. (when (config/local-file-based-graph? repo)
  342. (p/let [dir (config/get-repo-dir repo)
  343. dir-exists? (fs/dir-exists? dir)]
  344. (when (and (not dir-exists?)
  345. (not util/nfs?))
  346. (state/pub-event! [:graph/dir-gone dir]))))
  347. (let [db-based? (config/db-based-graph? repo)]
  348. (p/do!
  349. (state/pub-event! [:graph/sync-context])
  350. ;; re-render-root is async and delegated to rum, so we need to wait for main ui to refresh
  351. (when (mobile-util/native-ios?)
  352. (js/setTimeout #(mobile/mobile-postinit) 1000))
  353. ;; FIXME: an ugly implementation for redirecting to page on new window is restored
  354. (repo-handler/graph-ready! repo)
  355. (if db-based?
  356. (export/auto-db-backup! repo {:backup-now? true})
  357. (fs-watcher/load-graph-files! repo)))))
  358. (defmethod handle :notification/show [[_ {:keys [content status clear?]}]]
  359. (notification/show! content status clear?))
  360. (defmethod handle :command/run [_]
  361. (when (util/electron?)
  362. (shui/dialog-open! shell/shell)))
  363. (defmethod handle :go/search [_]
  364. (shui/dialog-open!
  365. cmdk/cmdk-modal
  366. {:id :ls-dialog-cmdk
  367. :align :top
  368. :content-props {:class "ls-dialog-cmdk"}
  369. :close-btn? false}))
  370. (defmethod handle :go/plugins [_]
  371. (plugin/open-plugins-modal!))
  372. (defmethod handle :go/plugins-waiting-lists [_]
  373. (plugin/open-waiting-updates-modal!))
  374. (defmethod handle :go/plugins-from-file [[_ plugins]]
  375. (plugin/open-plugins-from-file-modal! plugins))
  376. (defmethod handle :go/install-plugin-from-github [[_]]
  377. (shui/dialog-open!
  378. (plugin/install-from-github-release-container)))
  379. (defmethod handle :go/plugins-settings [[_ pid nav? title]]
  380. (when pid
  381. (state/set-state! :plugin/focused-settings pid)
  382. (state/set-state! :plugin/navs-settings? (not (false? nav?)))
  383. (plugin/open-focused-settings-modal! title)))
  384. (defmethod handle :go/proxy-settings [[_ agent-opts]]
  385. (shui/dialog-open!
  386. (plugin/user-proxy-settings-container agent-opts)
  387. {:id :https-proxy-panel :center? true :class "lg:max-w-2xl"}))
  388. (defmethod handle :redirect-to-home [_]
  389. (page-handler/create-today-journal!))
  390. (defmethod handle :instrument [[_ {:keys [type payload] :as opts}]]
  391. (when-not (empty? (dissoc opts :type :payload))
  392. (js/console.error "instrument data-map should only contains [:type :payload]"))
  393. (posthog/capture type payload))
  394. (defmethod handle :capture-error [[_ {:keys [error payload]}]]
  395. (let [[user-uuid graph-uuid tx-id] @sync/graphs-txid
  396. payload (assoc payload
  397. :user-id user-uuid
  398. :graph-id graph-uuid
  399. :tx-id tx-id
  400. :db-based (config/db-based-graph? (state/get-current-repo)))]
  401. (Sentry/captureException error
  402. (bean/->js {:tags payload}))))
  403. (defmethod handle :exec-plugin-cmd [[_ {:keys [pid cmd action]}]]
  404. (commands/exec-plugin-simple-command! pid cmd action))
  405. (defmethod handle :shortcut-handler-refreshed [[_]]
  406. (when-not @st/*pending-inited?
  407. (reset! st/*pending-inited? true)
  408. (st/consume-pending-shortcuts!)))
  409. (defmethod handle :mobile/keyboard-will-show [[_ keyboard-height]]
  410. (let [main-node (util/app-scroll-container-node)]
  411. (state/set-state! :mobile/show-tabbar? false)
  412. (state/set-state! :mobile/show-toolbar? true)
  413. (state/set-state! :mobile/show-action-bar? false)
  414. (when (= (state/sub :editor/record-status) "RECORDING")
  415. (state/set-state! :mobile/show-recording-bar? true))
  416. (when (mobile-util/native-ios?)
  417. (reset! util/keyboard-height keyboard-height)
  418. (set! (.. main-node -style -marginBottom) (str keyboard-height "px"))
  419. (when-let [^js html (js/document.querySelector ":root")]
  420. (.setProperty (.-style html) "--ls-native-kb-height" (str keyboard-height "px"))
  421. (.add (.-classList html) "has-mobile-keyboard"))
  422. (when-let [left-sidebar-node (gdom/getElement "left-sidebar")]
  423. (set! (.. left-sidebar-node -style -bottom) (str keyboard-height "px")))
  424. (when-let [right-sidebar-node (gdom/getElementByClass "sidebar-item-list")]
  425. (set! (.. right-sidebar-node -style -paddingBottom) (str (+ 150 keyboard-height) "px")))
  426. (when-let [card-preview-el (js/document.querySelector ".cards-review")]
  427. (set! (.. card-preview-el -style -marginBottom) (str keyboard-height "px")))
  428. (when-let [card-preview-el (js/document.querySelector ".encryption-password")]
  429. (set! (.. card-preview-el -style -marginBottom) (str keyboard-height "px")))
  430. (js/setTimeout (fn []
  431. (when-let [toolbar (.querySelector main-node "#mobile-editor-toolbar")]
  432. (set! (.. toolbar -style -bottom) (str keyboard-height "px"))))
  433. 100))))
  434. (defmethod handle :mobile/keyboard-will-hide [[_]]
  435. (let [main-node (util/app-scroll-container-node)]
  436. (state/set-state! :mobile/show-toolbar? false)
  437. (state/set-state! :mobile/show-tabbar? true)
  438. (when (= (state/sub :editor/record-status) "RECORDING")
  439. (state/set-state! :mobile/show-recording-bar? false))
  440. (when (mobile-util/native-ios?)
  441. (when-let [^js html (js/document.querySelector ":root")]
  442. (.removeProperty (.-style html) "--ls-native-kb-height")
  443. (.remove (.-classList html) "has-mobile-keyboard"))
  444. (when-let [card-preview-el (js/document.querySelector ".cards-review")]
  445. (set! (.. card-preview-el -style -marginBottom) "0px"))
  446. (when-let [card-preview-el (js/document.querySelector ".encryption-password")]
  447. (set! (.. card-preview-el -style -marginBottom) "0px"))
  448. (set! (.. main-node -style -marginBottom) "0px")
  449. (when-let [left-sidebar-node (gdom/getElement "left-sidebar")]
  450. (set! (.. left-sidebar-node -style -bottom) "0px"))
  451. (when-let [right-sidebar-node (gdom/getElementByClass "sidebar-item-list")]
  452. (set! (.. right-sidebar-node -style -paddingBottom) "150px"))
  453. (when-let [toolbar (.querySelector main-node "#mobile-editor-toolbar")]
  454. (set! (.. toolbar -style -bottom) 0)))))
  455. (defn- get-ios-app-id
  456. [repo-url]
  457. (when repo-url
  458. (let [app-id (-> (first (string/split repo-url "/Documents"))
  459. (string/split "/")
  460. last)]
  461. app-id)))
  462. (defmethod handle :validate-appId [[_ graph-switch-f graph]]
  463. (when-let [deprecated-repo (or graph (state/get-current-repo))]
  464. (if (mobile-util/in-iCloud-container-path? deprecated-repo)
  465. ;; Installation is not changed for iCloud
  466. (when graph-switch-f
  467. (graph-switch-f graph true)
  468. (state/pub-event! [:graph/ready (state/get-current-repo)]))
  469. ;; Installation is changed for App Documents directory
  470. (p/let [deprecated-app-id (get-ios-app-id deprecated-repo)
  471. current-document-url (.getUri Filesystem #js {:path ""
  472. :directory (.-Documents Directory)})
  473. current-app-id (-> (js->clj current-document-url :keywordize-keys true)
  474. get-ios-app-id)]
  475. (if (= deprecated-app-id current-app-id)
  476. (when graph-switch-f (graph-switch-f graph true))
  477. (do
  478. (notification/show! [:div "Migrating from previous App installation..."]
  479. :warning
  480. true)
  481. (prn ::migrate-app-id :from deprecated-app-id :to current-app-id)
  482. (file-sync-stop!)
  483. (.unwatch mobile-util/fs-watcher)
  484. (let [current-repo (string/replace deprecated-repo deprecated-app-id current-app-id)
  485. current-repo-dir (config/get-repo-dir current-repo)]
  486. (try
  487. ;; replace app-id part of repo url
  488. (reset! conn/conns
  489. (update-keys @conn/conns
  490. (fn [key]
  491. (if (string/includes? key deprecated-app-id)
  492. (string/replace key deprecated-app-id current-app-id)
  493. key))))
  494. (db-persist/rename-graph! deprecated-repo current-repo)
  495. (search/remove-db! deprecated-repo)
  496. (state/add-repo! {:url current-repo :nfs? true})
  497. (state/delete-repo! {:url deprecated-repo})
  498. (catch :default e
  499. (js/console.error e)))
  500. (state/set-current-repo! current-repo)
  501. (repo-config-handler/restore-repo-config! current-repo)
  502. (when graph-switch-f (graph-switch-f current-repo true))
  503. (.watch mobile-util/fs-watcher #js {:path current-repo-dir})
  504. (file-sync-restart!))))
  505. (state/pub-event! [:graph/ready (state/get-current-repo)])))))
  506. (defmethod handle :plugin/consume-updates [[_ id prev-pending? updated?]]
  507. (let [downloading? (:plugin/updates-downloading? @state/state)
  508. auto-checking? (plugin-handler/get-auto-checking?)]
  509. (when-let [coming (and (not downloading?)
  510. (get-in @state/state [:plugin/updates-coming id]))]
  511. (let [error-code (:error-code coming)
  512. error-code (if (= error-code (str :no-new-version)) nil error-code)
  513. title (:title coming)]
  514. (when (and prev-pending? (not auto-checking?))
  515. (if-not error-code
  516. (plugin/set-updates-sub-content! (str title "...") 0)
  517. (notification/show!
  518. (str "[Checked]<" title "> " error-code) :error)))))
  519. (if (and updated? downloading?)
  520. ;; try to start consume downloading item
  521. (if-let [next-coming (state/get-next-selected-coming-update)]
  522. (plugin-handler/check-or-update-marketplace-plugin!
  523. (assoc next-coming :only-check false :error-code nil)
  524. (fn [^js e] (js/console.error "[Download Err]" next-coming e)))
  525. (plugin-handler/close-updates-downloading))
  526. ;; try to start consume pending item
  527. (if-let [next-pending (second (first (:plugin/updates-pending @state/state)))]
  528. (do
  529. (println "Updates: take next pending - " (:id next-pending))
  530. (js/setTimeout
  531. #(plugin-handler/check-or-update-marketplace-plugin!
  532. (assoc next-pending :only-check true :auto-check auto-checking? :error-code nil)
  533. (fn [^js e]
  534. (notification/show! (.toString e) :error)
  535. (js/console.error "[Check Err]" next-pending e))) 500))
  536. ;; try to open waiting updates list
  537. (do (when (and prev-pending? (not auto-checking?)
  538. (seq (state/all-available-coming-updates)))
  539. (plugin/open-waiting-updates-modal!))
  540. (plugin-handler/set-auto-checking! false))))))
  541. (defmethod handle :plugin/hook-db-tx [[_ {:keys [blocks tx-data] :as payload}]]
  542. (when-let [payload (and (seq blocks)
  543. (merge payload {:tx-data (map #(into [] %) tx-data)}))]
  544. (plugin-handler/hook-plugin-db :changed payload)
  545. (plugin-handler/hook-plugin-block-changes payload)))
  546. (defmethod handle :plugin/loader-perf-tip [[_ {:keys [^js o _s _e]}]]
  547. (when-let [opts (.-options o)]
  548. (notification/show!
  549. (plugin/perf-tip-content (.-id o) (.-name opts) (.-url opts))
  550. :warning false (.-id o))))
  551. (defmethod handle :mobile-file-watcher/changed [[_ ^js event]]
  552. (let [type (.-event event)
  553. payload (js->clj event :keywordize-keys true)]
  554. (fs-watcher/handle-changed! type payload)
  555. (when (file-sync-handler/enable-sync?)
  556. (sync/file-watch-handler type payload))))
  557. (defmethod handle :rebuild-slash-commands-list [[_]]
  558. (page-handler/rebuild-slash-commands-list!))
  559. (defmethod handle :shortcut/refresh [[_]]
  560. (st/refresh!))
  561. (defn- refresh-cb []
  562. (page-handler/create-today-journal!)
  563. (file-sync-restart!))
  564. (defmethod handle :graph/ask-for-re-fresh [_]
  565. (shui/dialog-open!
  566. [:div {:style {:max-width 700}}
  567. [:p (t :sync-from-local-changes-detected)]
  568. [:div.flex.justify-end
  569. (ui/button
  570. (t :yes)
  571. :autoFocus "on"
  572. :class "ui__modal-enter"
  573. :on-click (fn []
  574. (shui/dialog-close!)
  575. (nfs-handler/refresh! (state/get-current-repo) refresh-cb)))]]))
  576. (defmethod handle :sync/create-remote-graph [[_ current-repo]]
  577. (let [graph-name (js/decodeURI (util/node-path.basename current-repo))]
  578. (async/go
  579. (async/<! (sync/<sync-stop))
  580. (state/set-state! [:ui/loading? :graph/create-remote?] true)
  581. (when-let [GraphUUID (get (async/<! (file-sync-handler/create-graph graph-name)) 2)]
  582. (async/<! (sync/<sync-start))
  583. (state/set-state! [:ui/loading? :graph/create-remote?] false)
  584. ;; update existing repo
  585. (state/set-repos! (map (fn [r]
  586. (if (= (:url r) current-repo)
  587. (assoc r
  588. :GraphUUID GraphUUID
  589. :GraphName graph-name
  590. :remote? true)
  591. r))
  592. (state/get-repos)))))))
  593. (defmethod handle :modal/remote-encryption-input-pw-dialog [[_ repo-url remote-graph-info type opts]]
  594. (shui/dialog-open!
  595. (encryption/input-password
  596. repo-url nil (merge
  597. (assoc remote-graph-info
  598. :type (or type :create-pwd-remote)
  599. :repo repo-url)
  600. opts))
  601. {:center? true :close-btn? false :close-backdrop? false}))
  602. (defmethod handle :journal/insert-template [[_ page-name]]
  603. (let [page-name (util/page-name-sanity-lc page-name)]
  604. (when-let [page (db/get-page page-name)]
  605. (when (db/page-empty? (state/get-current-repo) page-name)
  606. (when-let [template (state/get-default-journal-template)]
  607. (editor-handler/insert-template!
  608. nil
  609. template
  610. {:target page}))))))
  611. (defmethod handle :editor/set-heading [[_ block heading]]
  612. (when-let [id (:block/uuid block)]
  613. (editor-handler/set-heading! id heading)))
  614. (defmethod handle :file-sync-graph/restore-file [[_ graph page-entity content]]
  615. (when (db/get-db graph)
  616. (let [file (:block/file page-entity)]
  617. (when-let [path (:file/path file)]
  618. (when (and (not= content (:file/content file))
  619. (:file/content file))
  620. (sync/add-new-version-file graph path (:file/content file)))
  621. (p/let [_ (file-handler/alter-file graph
  622. path
  623. content
  624. {:re-render-root? true
  625. :skip-compare? true})]
  626. (state/close-modal!)
  627. (route-handler/redirect! {:to :page
  628. :path-params {:name (:block/name page-entity)}}))))))
  629. (defmethod handle :whiteboard/onboarding [[_ opts]]
  630. (shui/dialog-open!
  631. (fn [{:keys [close]}] (whiteboard/onboarding-welcome close))
  632. (merge {:close-btn? false
  633. :center? true
  634. :close-backdrop? false} opts)))
  635. (defmethod handle :file-sync/onboarding-tip [[_ type opts]]
  636. (let [type (keyword type)]
  637. (when-not (config/db-based-graph? (state/get-current-repo))
  638. (shui/dialog-open!
  639. (file-sync/make-onboarding-panel type)
  640. (merge {:close-btn? false
  641. :center? true
  642. :close-backdrop? (not= type :welcome)} opts)))))
  643. (defmethod handle :file-sync/maybe-onboarding-show [[_ type]]
  644. (file-sync/maybe-onboarding-show type))
  645. (defmethod handle :file-sync/storage-exceed-limit [[_]]
  646. (notification/show! "file sync storage exceed limit" :warning false)
  647. (file-sync-stop!))
  648. (defmethod handle :file-sync/graph-count-exceed-limit [[_]]
  649. (notification/show! "file sync graph count exceed limit" :warning false)
  650. (file-sync-stop!))
  651. (defmethod handle :graph/restored [[_ graph]]
  652. (when graph (assets-handler/ensure-assets-dir! graph))
  653. (mobile/init!)
  654. (rtc-flows/trigger-rtc-start graph)
  655. (fsrs/update-due-cards-count)
  656. (when-not (mobile-util/native-ios?)
  657. (state/pub-event! [:graph/ready graph])))
  658. (defmethod handle :whiteboard-link [[_ shapes]]
  659. (route-handler/go-to-search! :whiteboard/link)
  660. (state/set-state! :whiteboard/linked-shapes shapes))
  661. (defmethod handle :whiteboard-go-to-link [[_ link]]
  662. (route-handler/redirect! {:to :page
  663. :path-params {:name link}}))
  664. (defmethod handle :graph/dir-gone [[_ dir]]
  665. (state/pub-event! [:notification/show
  666. {:content (str "The directory " dir " has been renamed or deleted, the editor will be disabled for this graph, you can unlink the graph.")
  667. :status :error
  668. :clear? false}])
  669. (state/update-state! :file/unlinked-dirs (fn [dirs] (conj dirs dir))))
  670. (defmethod handle :graph/dir-back [[_ repo dir]]
  671. (when (contains? (:file/unlinked-dirs @state/state) dir)
  672. (notification/clear-all!)
  673. (state/pub-event! [:notification/show
  674. {:content (str "The directory " dir " has been back, you can edit your graph now.")
  675. :status :success
  676. :clear? true}])
  677. (state/update-state! :file/unlinked-dirs (fn [dirs] (disj dirs dir)))
  678. (when (= dir (config/get-repo-dir repo))
  679. (fs/watch-dir! dir))))
  680. (defmethod handle :ui/notify-skipped-downloading-files [[_ paths]]
  681. (notification/show!
  682. [:div
  683. [:div.mb-4
  684. [:div.font-semibold.mb-4.text-xl "It seems that some of your filenames are in the outdated format."]
  685. [:p
  686. "The files below that have reserved characters can't be saved on this device."]
  687. [:div.overflow-y-auto.max-h-96
  688. [:ol.my-2
  689. (for [path paths]
  690. [:li path])]]
  691. [:div
  692. [:p
  693. "Check " [:a {:href "https://docs.logseq.com/#/page/logseq%20file%20and%20folder%20naming%20rules"
  694. :target "_blank"}
  695. "Logseq file and folder naming rules"]
  696. " for more details."]]]]
  697. :warning
  698. false))
  699. (defmethod handle :graph/setup-a-repo [[_ opts]]
  700. (let [opts' (merge {:picked-root-fn #(state/close-modal!)
  701. :native-icloud? (not (string/blank? (state/get-icloud-container-root-url)))
  702. :logged? (user-handler/logged-in?)} opts)]
  703. (if (mobile-util/native-ios?)
  704. (shui/dialog-open!
  705. #(graph-picker/graph-picker-cp opts')
  706. {:label "graph-setup"})
  707. (page-handler/ls-dir-files! st/refresh! opts'))))
  708. (defmethod handle :graph/new-db-graph [[_ _opts]]
  709. (shui/dialog-open!
  710. repo/new-db-graph
  711. {:id :new-db-graph
  712. :title [:h2 "Create a new graph"]
  713. :style {:max-width "500px"}}))
  714. (defmethod handle :dialog-select/graph-open []
  715. (select/dialog-select! :graph-open))
  716. (defmethod handle :dialog-select/graph-remove []
  717. (select/dialog-select! :graph-remove))
  718. (defmethod handle :dialog-select/db-graph-replace []
  719. (select/dialog-select! :db-graph-replace))
  720. (defmethod handle :graph/save-db-to-disk [[_ _opts]]
  721. (persist-db/export-current-graph! {:succ-notification? true}))
  722. (defmethod handle :class/configure [[_ page]]
  723. (shui/dialog-open!
  724. #(block/block-container {} page)
  725. {:label "page-configure"
  726. :align :top}))
  727. (defmethod handle :file/alter [[_ repo path content]]
  728. (p/let [_ (file-handler/alter-file repo path content {:from-disk? true})]
  729. (ui-handler/re-render-root!)))
  730. (defmethod handle :ui/re-render-root [[_]]
  731. (ui-handler/re-render-root!))
  732. (rum/defcs file-id-conflict-item <
  733. (rum/local false ::resolved?)
  734. [state repo file data]
  735. (let [resolved? (::resolved? state)
  736. id (last (:assertion data))]
  737. [:li {:key file}
  738. [:div
  739. [:a {:on-click #(js/window.apis.openPath file)} file]
  740. (if @resolved?
  741. [:div.flex.flex-row.items-center
  742. (ui/icon "circle-check" {:style {:font-size 20}})
  743. [:div.ml-1 "Resolved"]]
  744. [:div
  745. [:p
  746. (str "It seems that another whiteboard file already has the ID \"" id
  747. "\". You can fix it by changing the ID in this file with another UUID.")]
  748. [:p
  749. "Or, let me"
  750. (ui/button "Fix"
  751. :on-click (fn []
  752. (let [dir (config/get-repo-dir repo)]
  753. (p/let [content (fs/read-file dir file)]
  754. (let [new-content (string/replace content (str id) (str (random-uuid)))]
  755. (p/let [_ (fs/write-file! repo
  756. dir
  757. file
  758. new-content
  759. {})]
  760. (reset! resolved? true))))))
  761. :class "inline mx-1")
  762. "it."]])]]))
  763. (defmethod handle :file/parse-and-load-error [[_ repo parse-errors]]
  764. (state/pub-event! [:notification/show
  765. {:content
  766. [:div
  767. [:h2.title "Oops. These files failed to import to your graph:"]
  768. [:ol.my-2
  769. (for [[file error] parse-errors]
  770. (let [data (ex-data error)]
  771. (cond
  772. (and (common-config/whiteboard? file)
  773. (= :transact/upsert (:error data))
  774. (uuid? (last (:assertion data))))
  775. (rum/with-key (file-id-conflict-item repo file data) file)
  776. :else
  777. (do
  778. (state/pub-event! [:capture-error {:error error
  779. :payload {:type :file/parse-and-load-error}}])
  780. [:li.my-1 {:key file}
  781. [:a {:on-click #(js/window.apis.openPath file)} file]
  782. [:p (.-message error)]]))))]
  783. [:p "Don't forget to re-index your graph when all the conflicts are resolved."]]
  784. :status :error}]))
  785. (defmethod handle :run/cli-command [[_ command content]]
  786. (when (and command (not (string/blank? content)))
  787. (shell-handler/run-cli-command-wrapper! command content)))
  788. (defmethod handle :editor/quick-capture [[_ args]]
  789. (quick-capture/quick-capture args))
  790. (defmethod handle :modal/keymap [[_]]
  791. (state/open-settings! :keymap))
  792. (defmethod handle :editor/toggle-own-number-list [[_ blocks]]
  793. (let [batch? (sequential? blocks)
  794. blocks (cond->> blocks
  795. batch?
  796. (map #(cond-> % (or (uuid? %) (string? %)) (db-model/get-block-by-uuid))))]
  797. (if (and batch? (> (count blocks) 1))
  798. (editor-handler/toggle-blocks-as-own-order-list! blocks)
  799. (when-let [block (cond-> blocks batch? (first))]
  800. (if (editor-handler/own-order-number-list? block)
  801. (editor-handler/remove-block-own-order-list-type! block)
  802. (editor-handler/make-block-as-own-order-list! block))))))
  803. (defmethod handle :editor/remove-own-number-list [[_ block]]
  804. (when (some-> block (editor-handler/own-order-number-list?))
  805. (editor-handler/remove-block-own-order-list-type! block)))
  806. (defmethod handle :editor/save-current-block [_]
  807. (editor-handler/save-current-block!))
  808. (defmethod handle :editor/save-code-editor [_]
  809. (code-handler/save-code-editor!))
  810. (defmethod handle :editor/focus-code-editor [[_ editing-block container]]
  811. (when-let [^js cm (util/get-cm-instance container)]
  812. (when-not (.hasFocus cm)
  813. (let [cursor-pos (some-> (:editor/cursor-range @state/state) (deref) (count))
  814. direction (:block.editing/direction editing-block)
  815. pos (:block.editing/pos editing-block)
  816. to-line (case direction
  817. :up (.lastLine cm)
  818. (case pos
  819. :max (.lastLine cm)
  820. 0))]
  821. ;; move to friendly cursor
  822. (doto cm
  823. (.focus)
  824. (.setCursor to-line (or cursor-pos 0)))))))
  825. (defmethod handle :editor/toggle-children-number-list [[_ block]]
  826. (when-let [blocks (and block (db-model/get-block-immediate-children (state/get-current-repo) (:block/uuid block)))]
  827. (editor-handler/toggle-blocks-as-own-order-list! blocks)))
  828. (defn- editor-new-property [block target {:keys [selected-blocks] :as opts}]
  829. (let [editing-block (state/get-edit-block)
  830. pos (state/get-edit-pos)
  831. edit-block-or-selected (cond
  832. editing-block
  833. [editing-block]
  834. (seq selected-blocks)
  835. selected-blocks
  836. :else
  837. (seq (keep #(db/entity [:block/uuid %]) (state/get-selection-block-ids))))
  838. current-block (when-let [s (state/get-current-page)]
  839. (when (util/uuid-string? s)
  840. (db/entity [:block/uuid (uuid s)])))
  841. blocks (or (when block [block])
  842. edit-block-or-selected
  843. (when current-block [current-block]))
  844. opts' (cond-> opts
  845. editing-block
  846. (assoc :original-block editing-block
  847. :edit-original-block
  848. (fn [{:keys [editing-default-property?]}]
  849. (when editing-block
  850. (let [content (:block/title (db/entity (:db/id editing-block)))
  851. esc? (= "Escape" (state/get-ui-last-key-code))
  852. [content' pos] (cond
  853. esc?
  854. [nil pos]
  855. (and (>= (count content) pos)
  856. (>= pos 2)
  857. (= (util/nth-safe content (dec pos))
  858. (util/nth-safe content (- pos 2))
  859. ";"))
  860. [(str (common-util/safe-subs content 0 (- pos 2))
  861. (common-util/safe-subs content pos))
  862. (- pos 2)]
  863. :else
  864. [nil pos])]
  865. (when content'
  866. (if editing-default-property?
  867. (editor-handler/save-block! (state/get-current-repo) (:block/uuid editing-block) content')
  868. (editor-handler/edit-block! editing-block (or pos :max)
  869. (cond-> {}
  870. content'
  871. (assoc :custom-content content'))))))))))]
  872. (when (seq blocks)
  873. (let [target' (or target
  874. (some-> (state/get-edit-input-id)
  875. (gdom/getElement))
  876. (first (state/get-selection-blocks)))]
  877. (if target'
  878. (shui/popup-show! target'
  879. #(property-dialog/dialog blocks opts')
  880. {:align "start"
  881. :auto-focus? true})
  882. (shui/dialog-open! #(property-dialog/dialog blocks opts')
  883. {:id :property-dialog
  884. :align "start"}))))))
  885. (defmethod handle :editor/new-property [[_ {:keys [block target] :as opts}]]
  886. (when-not config/publishing?
  887. (p/do!
  888. (editor-handler/save-current-block!)
  889. (editor-new-property block target opts))))
  890. (defmethod handle :editor/upsert-type-block [[_ {:keys [block type lang update-current-block?]}]]
  891. (p/do!
  892. (when-not update-current-block?
  893. (editor-handler/save-current-block!))
  894. (when-not update-current-block?
  895. (p/delay 16))
  896. (let [block (db/entity (:db/id block))
  897. block-type (:logseq.property.node/display-type block)
  898. block-title (:block/title block)
  899. latest-code-lang (or lang
  900. (:kv/value (db/entity :logseq.kv/latest-code-lang)))
  901. turn-type! #(if (and (= (keyword type) :code) latest-code-lang)
  902. (db-property-handler/set-block-properties!
  903. (:block/uuid %)
  904. {:logseq.property.node/display-type (keyword type)
  905. :logseq.property.code/lang latest-code-lang})
  906. (db-property-handler/set-block-property!
  907. (:block/uuid %) :logseq.property.node/display-type (keyword type)))]
  908. (p/let [block (if (or (not (nil? block-type))
  909. (and (not update-current-block?) (not (string/blank? block-title))))
  910. (p/let [result (ui-outliner-tx/transact!
  911. {:outliner-op :insert-blocks}
  912. ;; insert a new block
  913. (let [[_p _ block'] (editor-handler/insert-new-block-aux! {} block "")]
  914. (turn-type! block')))
  915. result' (ldb/read-transit-str result)]
  916. (when-let [id (:block/uuid (first (:blocks result')))]
  917. (db/entity [:block/uuid id])))
  918. (p/do!
  919. (turn-type! block)
  920. (db/entity [:block/uuid (:block/uuid block)])))]
  921. (js/setTimeout #(editor-handler/edit-block! block :max) 100)))))
  922. (rum/defc multi-tabs-dialog
  923. []
  924. (let [word (if (util/electron?) "window" "tab")]
  925. [:div.flex.p-4.flex-col.gap-4.h-64
  926. [:span.warning.text-lg
  927. (util/format "Logseq doesn't support multiple %ss access to the same graph yet, please close this %s or switch to another graph."
  928. word word)]
  929. [:div.text-lg
  930. [:p "Switch to another repo: "]
  931. [:div.border.rounded.bg-gray-01.overflow-hidden.w-60
  932. (repo/repos-dropdown {:on-click (fn [e]
  933. (util/stop e)
  934. (state/set-state! :error/multiple-tabs-access-opfs? false)
  935. (shui/dialog-close!))})]]]))
  936. (defmethod handle :show/multiple-tabs-error-dialog [_]
  937. (state/set-state! :error/multiple-tabs-access-opfs? true)
  938. (shui/dialog-open! multi-tabs-dialog))
  939. (defmethod handle :rtc/sync-state [[_ state]]
  940. (state/update-state! :rtc/state (fn [old] (merge old state))))
  941. (defmethod handle :rtc/log [[_ data]]
  942. (state/set-state! :rtc/log data))
  943. (defmethod handle :rtc/download-remote-graph [[_ graph-name graph-uuid graph-schema-version]]
  944. (->
  945. (p/do!
  946. (rtc-handler/<rtc-download-graph! graph-name graph-uuid graph-schema-version 60000))
  947. (p/catch (fn [e]
  948. (println "RTC download graph failed, error:")
  949. (js/console.error e)))))
  950. ;; db-worker -> UI
  951. (defmethod handle :db/sync-changes [[_ data]]
  952. (let [retract-datoms (filter (fn [d] (and (= :block/uuid (:a d)) (false? (:added d)))) (:tx-data data))
  953. retracted-tx-data (map (fn [d] [:db/retractEntity (:e d)]) retract-datoms)
  954. tx-data (concat (:tx-data data) retracted-tx-data)]
  955. (pipeline/invoke-hooks (assoc data :tx-data tx-data))
  956. nil))
  957. (defmethod handle :db/export-sqlite [_]
  958. (export/export-repo-as-sqlite-db! (state/get-current-repo))
  959. nil)
  960. (defmethod handle :editor/run-query-command [_]
  961. (editor-handler/run-query-command!))
  962. (defmethod handle :editor/show-action-bar []
  963. (let [selection (state/get-selection-blocks)
  964. first-visible-block (some #(when (util/el-visible-in-viewport? % true) %) selection)]
  965. (when first-visible-block
  966. (shui/popup-hide! :selection-action-bar)
  967. (shui/popup-show!
  968. first-visible-block
  969. (fn []
  970. (selection/action-bar))
  971. {:id :selection-action-bar
  972. :content-props {:side "top"
  973. :class "!py-0 !px-0 !border-none"}
  974. :auto-side? false
  975. :align :start}))))
  976. (defmethod handle :editor/hide-action-bar []
  977. (shui/popup-hide! :selection-action-bar))
  978. (defn run!
  979. []
  980. (let [chan (state/get-events-chan)]
  981. (async/go-loop []
  982. (let [[payload d] (async/<! chan)]
  983. (->
  984. (try
  985. (p/resolved (handle payload))
  986. (catch :default error
  987. (p/rejected error)))
  988. (p/then (fn [result]
  989. (p/resolve! d result)))
  990. (p/catch (fn [error]
  991. (let [type :handle-system-events/failed]
  992. (state/pub-event! [:capture-error {:error error
  993. :payload {:type type
  994. :payload payload}}])
  995. (p/reject! d error))))))
  996. (recur))
  997. chan))
  998. (comment
  999. (let [{:keys [deprecated-app-id current-app-id]} {:deprecated-app-id "AFDADF9A-7466-4ED8-B74F-AAAA0D4565B9", :current-app-id "7563518E-0EFD-4AD2-8577-10CFFD6E4596"}]
  1000. (def deprecated-app-id deprecated-app-id)
  1001. (def current-app-id current-app-id))
  1002. (def deprecated-repo (state/get-current-repo))
  1003. (def new-repo (string/replace deprecated-repo deprecated-app-id current-app-id))
  1004. (update-file-path deprecated-repo new-repo deprecated-app-id current-app-id))