handler.cljs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. (ns frontend.handler
  2. "Main ns that handles application startup. Closest ns that we have to a
  3. system. Contains a couple of small system components"
  4. (:require [electron.ipc :as ipc]
  5. [electron.listener :as el]
  6. [frontend.components.block :as block]
  7. [frontend.components.editor :as editor]
  8. [frontend.components.page :as page]
  9. [frontend.components.reference :as reference]
  10. [frontend.components.whiteboard :as whiteboard]
  11. [frontend.config :as config]
  12. [frontend.context.i18n :as i18n]
  13. [frontend.db.restore :as db-restore]
  14. [frontend.db.react :as react]
  15. [frontend.error :as error]
  16. [frontend.handler.command-palette :as command-palette]
  17. [frontend.handler.events :as events]
  18. [frontend.handler.file-based.events]
  19. [frontend.handler.file :as file-handler]
  20. [frontend.handler.global-config :as global-config-handler]
  21. [frontend.handler.notification :as notification]
  22. [frontend.handler.page :as page-handler]
  23. [frontend.handler.plugin-config :as plugin-config-handler]
  24. [frontend.handler.repo :as repo-handler]
  25. [frontend.handler.repo-config :as repo-config-handler]
  26. [frontend.handler.ui :as ui-handler]
  27. [frontend.handler.user :as user-handler]
  28. [frontend.idb :as idb]
  29. [frontend.mobile.util :as mobile-util]
  30. [frontend.modules.instrumentation.core :as instrument]
  31. [frontend.modules.shortcut.core :as shortcut]
  32. [frontend.state :as state]
  33. [frontend.util :as util]
  34. [frontend.util.persist-var :as persist-var]
  35. [goog.object :as gobj]
  36. [lambdaisland.glogi :as log]
  37. [promesa.core :as p]
  38. [frontend.mobile.core :as mobile]
  39. [cljs-bean.core :as bean]
  40. [frontend.handler.test :as test]
  41. [frontend.persist-db.browser :as db-browser]
  42. [frontend.persist-db :as persist-db]))
  43. (defn- set-global-error-notification!
  44. []
  45. (when-not config/dev?
  46. (set! js/window.onerror
  47. (fn [message, _source, _lineno, _colno, error]
  48. (when-not (error/ignored? message)
  49. (js/console.error message)
  50. (log/error :exception error))))))
  51. (defn- watch-for-date!
  52. []
  53. (let [f (fn []
  54. (let [repo (state/get-current-repo)]
  55. (when (or
  56. (config/db-based-graph? repo)
  57. (and (not (state/nfs-refreshing?))
  58. (not (contains? (:file/unlinked-dirs @state/state)
  59. (config/get-repo-dir repo)))))
  60. ;; Don't create the journal file until user writes something
  61. (page-handler/create-today-journal!))))]
  62. (f)
  63. (js/setInterval f 5000)))
  64. (defn restore-and-setup!
  65. [repo]
  66. (when repo
  67. (-> (p/let [_ (db-restore/restore-graph! repo)]
  68. (repo-config-handler/start {:repo repo}))
  69. (p/then
  70. (fn []
  71. ;; try to load custom css only for current repo
  72. (ui-handler/add-style-if-exists!)
  73. (->
  74. (p/do!
  75. (when (config/global-config-enabled?)
  76. (global-config-handler/start {:repo repo}))
  77. (when (config/plugin-config-enabled?)
  78. (plugin-config-handler/start)))
  79. (p/finally
  80. (fn []
  81. ;; install after config is restored
  82. (shortcut/refresh!)
  83. (state/set-db-restoring! false))))))
  84. (p/then
  85. (fn []
  86. (js/console.log "db restored, setting up repo hooks")
  87. (state/pub-event! [:modal/nfs-ask-permission])
  88. (page-handler/init-commands!)
  89. (watch-for-date!)
  90. (when (util/electron?) (file-handler/watch-for-current-graph-dir!))
  91. (state/pub-event! [:graph/restored (state/get-current-repo)])))
  92. (p/catch (fn [error]
  93. (log/error :exception error))))))
  94. (defn- handle-connection-change
  95. [e]
  96. (let [online? (= (gobj/get e "type") "online")]
  97. (state/set-online! online?)))
  98. (defn set-network-watcher!
  99. []
  100. (js/window.addEventListener "online" handle-connection-change)
  101. (js/window.addEventListener "offline" handle-connection-change))
  102. (defn- register-components-fns!
  103. []
  104. (state/set-page-blocks-cp! page/page-cp)
  105. (state/set-component! :block/linked-references reference/block-linked-references)
  106. (state/set-component! :whiteboard/tldraw-preview whiteboard/tldraw-preview)
  107. (state/set-component! :block/single-block block/single-block-cp)
  108. (state/set-component! :block/container block/block-container)
  109. (state/set-component! :block/reference block/block-reference)
  110. (state/set-component! :block/blocks-container block/blocks-container)
  111. (state/set-component! :block/properties-cp block/db-properties-cp)
  112. (state/set-component! :block/embed block/block-embed)
  113. (state/set-component! :block/page-cp block/page-cp)
  114. (state/set-component! :block/inline-text block/inline-text)
  115. (state/set-component! :editor/box editor/box)
  116. (command-palette/register-global-shortcut-commands))
  117. (defn- get-system-info
  118. []
  119. (when (util/electron?)
  120. (p/let [info (ipc/ipc :system/info)]
  121. (state/set-state! :system/info (bean/->clj info)))))
  122. (defn start!
  123. [render]
  124. (test/setup-test!)
  125. (get-system-info)
  126. (set-global-error-notification!)
  127. (set! js/window.onhashchange #(state/hide-custom-context-menu!)) ;; close context menu when page navs
  128. (register-components-fns!)
  129. (user-handler/restore-tokens-from-localstorage)
  130. (state/set-db-restoring! true)
  131. (when (util/electron?)
  132. (el/listen!))
  133. (render)
  134. (i18n/start)
  135. (instrument/init)
  136. (state/set-online! js/navigator.onLine)
  137. (set-network-watcher!)
  138. (-> (util/indexeddb-check?)
  139. (p/catch (fn [_e]
  140. (notification/show! "Sorry, it seems that your browser doesn't support IndexedDB, we recommend to use latest Chrome(Chromium) or Firefox(Non-private mode)." :error false)
  141. (state/set-indexedb-support! false))))
  142. (idb/start)
  143. (react/run-custom-queries-when-idle!)
  144. (events/run!)
  145. (p/do!
  146. (when (mobile-util/native-platform?)
  147. (mobile/mobile-preinit))
  148. (-> (p/let [_ (db-browser/start-db-worker!)
  149. repos (repo-handler/get-repos)
  150. _ (state/set-repos! repos)
  151. _ (mobile-util/hide-splash) ;; hide splash as early as ui is stable
  152. repo (or (state/get-current-repo) (:url (first repos)))
  153. _ (if (empty? repos)
  154. (repo-handler/new-db! config/demo-repo)
  155. (restore-and-setup! repo))]
  156. (when (util/electron?)
  157. (persist-db/run-export-periodically!))
  158. (when (mobile-util/native-platform?)
  159. (state/restore-mobile-theme!)))
  160. (p/catch (fn [e]
  161. (js/console.error "Error while restoring repos: " e)))
  162. (p/finally (fn []
  163. (state/set-db-restoring! false))))
  164. (util/<app-wake-up-from-sleep-loop (atom false))
  165. (persist-var/load-vars)))
  166. (defn stop! []
  167. (prn "stop!"))
  168. (defn quit-and-install-new-version!
  169. []
  170. (p/let [_ (ipc/invoke "set-quit-dirty-state" false)]
  171. (ipc/ipc :quitAndInstall)))