handler.cljs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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 [cljs.reader :refer [read-string]]
  5. [electron.ipc :as ipc]
  6. [electron.listener :as el]
  7. [frontend.components.block :as block]
  8. [frontend.components.editor :as editor]
  9. [frontend.components.page :as page]
  10. [frontend.components.reference :as reference]
  11. [frontend.components.whiteboard :as whiteboard]
  12. [frontend.config :as config]
  13. [frontend.context.i18n :as i18n]
  14. [frontend.db.restore :as db-restore]
  15. [frontend.db.conn :as conn]
  16. [frontend.db.react :as react]
  17. [frontend.error :as error]
  18. [frontend.handler.command-palette :as command-palette]
  19. [frontend.handler.events :as events]
  20. [frontend.handler.file-based.events]
  21. [frontend.handler.file :as file-handler]
  22. [frontend.handler.global-config :as global-config-handler]
  23. [frontend.handler.notification :as notification]
  24. [frontend.handler.page :as page-handler]
  25. [frontend.handler.plugin-config :as plugin-config-handler]
  26. [frontend.handler.repo :as repo-handler]
  27. [frontend.handler.file-based.repo :as file-repo-handler]
  28. [frontend.handler.repo-config :as repo-config-handler]
  29. [frontend.handler.ui :as ui-handler]
  30. [frontend.handler.user :as user-handler]
  31. [frontend.idb :as idb]
  32. [frontend.mobile.util :as mobile-util]
  33. [frontend.modules.instrumentation.core :as instrument]
  34. [frontend.modules.shortcut.core :as shortcut]
  35. [frontend.state :as state]
  36. [frontend.util :as util]
  37. [frontend.util.persist-var :as persist-var]
  38. [goog.object :as gobj]
  39. [lambdaisland.glogi :as log]
  40. [promesa.core :as p]
  41. [frontend.mobile.core :as mobile]
  42. [cljs-bean.core :as bean]
  43. [frontend.handler.test :as test]
  44. [frontend.persist-db.browser :as db-browser]
  45. [frontend.db.async :as db-async]))
  46. (defn- set-global-error-notification!
  47. []
  48. (when-not config/dev?
  49. (set! js/window.onerror
  50. (fn [message, _source, _lineno, _colno, error]
  51. (when-not (error/ignored? message)
  52. (log/error :exception error))))))
  53. (defn- watch-for-date!
  54. []
  55. (let [f (fn []
  56. (let [repo (state/get-current-repo)]
  57. (when (or
  58. (config/db-based-graph? repo)
  59. (and (not (state/nfs-refreshing?))
  60. (not (contains? (:file/unlinked-dirs @state/state)
  61. (config/get-repo-dir repo)))))
  62. ;; Don't create the journal file until user writes something
  63. (page-handler/create-today-journal!))))]
  64. (f)
  65. (js/setInterval f 5000)))
  66. (defn restore-and-setup!
  67. [repo repos]
  68. (when repo
  69. (-> (p/let [_ (db-restore/restore-graph! repo)]
  70. (repo-config-handler/start {:repo repo}))
  71. (p/then
  72. (fn []
  73. ;; try to load custom css only for current repo
  74. (ui-handler/add-style-if-exists!)
  75. (->
  76. (p/do!
  77. (when (config/global-config-enabled?)
  78. (global-config-handler/start {:repo repo}))
  79. (when (config/plugin-config-enabled?)
  80. (plugin-config-handler/start)))
  81. (p/finally
  82. (fn []
  83. ;; install after config is restored
  84. (shortcut/refresh!)
  85. (p/let [files (db-async/<get-files config/demo-repo)]
  86. (cond
  87. (and (not (seq files))
  88. ;; Not native local directory
  89. (not (some config/local-file-based-graph? (map :url repos)))
  90. (not (mobile-util/native-platform?))
  91. (not (config/db-based-graph? repo)))
  92. ;; will execute `(state/set-db-restoring! false)` inside
  93. (file-repo-handler/setup-demo-repo-if-not-exists!)
  94. :else
  95. (state/set-db-restoring! false))))))))
  96. (p/then
  97. (fn []
  98. (js/console.log "db restored, setting up repo hooks")
  99. (state/pub-event! [:modal/nfs-ask-permission])
  100. (page-handler/init-commands!)
  101. (watch-for-date!)
  102. (when (util/electron?) (file-handler/watch-for-current-graph-dir!))
  103. (state/pub-event! [:graph/restored (state/get-current-repo)])))
  104. (p/catch (fn [error]
  105. (log/error :exception error))))))
  106. (defn- handle-connection-change
  107. [e]
  108. (let [online? (= (gobj/get e "type") "online")]
  109. (state/set-online! online?)))
  110. (defn set-network-watcher!
  111. []
  112. (js/window.addEventListener "online" handle-connection-change)
  113. (js/window.addEventListener "offline" handle-connection-change))
  114. (defn enable-datalog-console
  115. "Enables datalog console in browser provided by https://github.com/homebaseio/datalog-console"
  116. []
  117. (js/document.documentElement.setAttribute "__datalog-console-remote-installed__" true)
  118. (.addEventListener js/window "message"
  119. (fn [event]
  120. (let [db (conn/get-db)]
  121. (when-let [devtool-message (gobj/getValueByKeys event "data" ":datalog-console.client/devtool-message")]
  122. (let [msg-type (:type (read-string devtool-message))]
  123. (case msg-type
  124. :datalog-console.client/request-whole-database-as-string
  125. (.postMessage js/window #js {":datalog-console.remote/remote-message" (pr-str db)} "*")
  126. nil)))))))
  127. (defn- register-components-fns!
  128. []
  129. (state/set-page-blocks-cp! page/page)
  130. (state/set-component! :block/linked-references reference/block-linked-references)
  131. (state/set-component! :whiteboard/tldraw-preview whiteboard/tldraw-preview)
  132. (state/set-component! :block/single-block block/single-block-cp)
  133. (state/set-component! :block/container block/block-container)
  134. (state/set-component! :block/embed block/block-embed)
  135. (state/set-component! :block/page-cp block/page-cp)
  136. (state/set-component! :editor/box editor/box)
  137. (command-palette/register-global-shortcut-commands))
  138. (defn- get-system-info
  139. []
  140. (when (util/electron?)
  141. (p/let [info (ipc/ipc :system/info)]
  142. (state/set-state! :system/info (bean/->clj info)))))
  143. (defn start!
  144. [render]
  145. (test/setup-test!)
  146. (get-system-info)
  147. (set-global-error-notification!)
  148. (set! js/window.onhashchange #(state/hide-custom-context-menu!)) ;; close context menu when page navs
  149. (register-components-fns!)
  150. (user-handler/restore-tokens-from-localstorage)
  151. (state/set-db-restoring! true)
  152. (when (util/electron?)
  153. (el/listen!))
  154. (render)
  155. (i18n/start)
  156. (instrument/init)
  157. (state/set-online! js/navigator.onLine)
  158. (set-network-watcher!)
  159. (-> (util/indexeddb-check?)
  160. (p/catch (fn [_e]
  161. (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)
  162. (state/set-indexedb-support! false))))
  163. (idb/start)
  164. (react/run-custom-queries-when-idle!)
  165. (events/run!)
  166. (p/do!
  167. (when (mobile-util/native-platform?)
  168. (mobile/mobile-preinit))
  169. (-> (p/let [_ (db-browser/start-db-worker!)
  170. repos (repo-handler/get-repos)
  171. _ (state/set-repos! repos)
  172. _ (mobile-util/hide-splash) ;; hide splash as early as ui is stable
  173. repo (or (state/get-current-repo) (:url (first repos)))
  174. _ (restore-and-setup! repo repos)]
  175. (when (mobile-util/native-platform?)
  176. (state/restore-mobile-theme!)))
  177. (p/catch (fn [e]
  178. (js/console.error "Error while restoring repos: " e)))
  179. (p/finally (fn []
  180. (state/set-db-restoring! false))))
  181. (util/<app-wake-up-from-sleep-loop (atom false))
  182. (when config/dev?
  183. (enable-datalog-console))
  184. (persist-var/load-vars)))
  185. (defn stop! []
  186. (prn "stop!"))
  187. (defn quit-and-install-new-version!
  188. []
  189. (p/let [_ (ipc/invoke "set-quit-dirty-state" false)]
  190. (ipc/ipc :quitAndInstall)))