| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471 |
- (ns frontend.handler.events
- "System-component-like ns that defines named events and listens on a
- core.async channel to handle them. Any part of the system can dispatch
- one of these events using state/pub-event!"
- (:refer-clojure :exclude [run!])
- (:require ["@sentry/react" :as Sentry]
- [cljs-bean.core :as bean]
- [clojure.core.async :as async]
- [clojure.core.async.interop :refer [p->c]]
- [clojure.string :as string]
- [frontend.commands :as commands]
- [frontend.components.rtc.indicator :as indicator]
- [frontend.config :as config]
- [frontend.date :as date]
- [frontend.db :as db]
- [frontend.db.async :as db-async]
- [frontend.db.model :as db-model]
- [frontend.db.react :as react]
- [frontend.db.transact :as db-transact]
- [frontend.extensions.fsrs :as fsrs]
- [frontend.fs :as fs]
- [frontend.fs.sync :as sync]
- [frontend.fs.watcher-handler :as fs-watcher]
- [frontend.handler.assets :as assets-handler]
- [frontend.handler.code :as code-handler]
- [frontend.handler.common.page :as page-common-handler]
- [frontend.handler.db-based.property :as db-property-handler]
- [frontend.handler.db-based.rtc :as rtc-handler]
- [frontend.handler.db-based.rtc-flows :as rtc-flows]
- [frontend.handler.editor :as editor-handler]
- [frontend.handler.export :as export]
- [frontend.handler.graph :as graph-handler]
- [frontend.handler.notification :as notification]
- [frontend.handler.page :as page-handler]
- [frontend.handler.plugin :as plugin-handler]
- [frontend.handler.repo :as repo-handler]
- [frontend.handler.repo-config :as repo-config-handler]
- [frontend.handler.route :as route-handler]
- [frontend.handler.search :as search-handler]
- [frontend.handler.shell :as shell-handler]
- [frontend.handler.ui :as ui-handler]
- [frontend.mobile.util :as mobile-util]
- [frontend.modules.instrumentation.posthog :as posthog]
- [frontend.modules.outliner.pipeline :as pipeline]
- [frontend.modules.outliner.ui :as ui-outliner-tx]
- [frontend.modules.shortcut.core :as st]
- [frontend.persist-db :as persist-db]
- [frontend.quick-capture :as quick-capture]
- [frontend.state :as state]
- [frontend.util :as util]
- [frontend.util.persist-var :as persist-var]
- [goog.dom :as gdom]
- [lambdaisland.glogi :as log]
- [logseq.db.frontend.schema :as db-schema]
- [logseq.shui.ui :as shui]
- [promesa.core :as p]))
- ;; TODO: should we move all events here?
- (defmulti handle first)
- (defn file-sync-restart! []
- (async/go (async/<! (p->c (persist-var/load-vars)))
- (async/<! (sync/<sync-stop))
- (some-> (sync/<sync-start) async/<!)))
- (defn file-sync-stop! []
- (async/go (async/<! (p->c (persist-var/load-vars)))
- (async/<! (sync/<sync-stop))))
- (defmethod handle :graph/added [[_ repo {:keys [empty-graph?]}]]
- (search-handler/rebuild-indices!)
- (plugin-handler/hook-plugin-app :graph-after-indexed {:repo repo :empty-graph? empty-graph?})
- (route-handler/redirect-to-home!)
- (when-let [dir-name (and (not (config/db-based-graph? repo)) (config/get-repo-dir repo))]
- (fs/watch-dir! dir-name))
- (file-sync-restart!))
- (defmethod handle :init/commands [_]
- (page-handler/init-commands!))
- (defmethod handle :graph/unlinked [repo current-repo]
- (when (= (:url repo) current-repo)
- (file-sync-restart!)))
- (defn- graph-switch
- [graph]
- (react/clear-query-state!)
- (let [db-based? (config/db-based-graph? graph)]
- (state/set-current-repo! graph)
- (page-handler/init-commands!)
- ;; load config
- (repo-config-handler/restore-repo-config! graph)
- (when-not (= :draw (state/get-current-route))
- (route-handler/redirect-to-home!))
- (when-not db-based?
- ;; graph-switch will trigger a rtc-start automatically
- ;; (rtc-handler/<rtc-start! graph)
- (file-sync-restart!))
- (when-let [dir-name (and (not db-based?) (config/get-repo-dir graph))]
- (fs/watch-dir! dir-name))
- (graph-handler/settle-metadata-to-local! {:last-seen-at (js/Date.now)})))
- ;; Parameters for the `persist-db` function, to show the notification messages
- (defn- graph-switch-on-persisted
- "graph: the target graph to switch to"
- [graph opts]
- (p/do!
- (repo-handler/restore-and-setup-repo! graph)
- (graph-switch graph)
- (state/set-state! :sync-graph/init? false)
- (when (:rtc-download? opts)
- (and (search-handler/rebuild-indices!) true)
- (repo-handler/refresh-repos!))))
- (defmethod handle :graph/switch [[_ graph opts]]
- (export/cancel-db-backup!)
- (persist-db/export-current-graph!)
- (state/set-state! :db/async-queries {})
- (st/refresh!)
- (p/let [writes-finished? (state/<invoke-db-worker :thread-api/file-writes-finished? (state/get-current-repo))
- request-finished? (db-transact/request-finished?)]
- (if (not writes-finished?) ; TODO: test (:sync-graph/init? @state/state)
- (do
- (log/info :graph/switch (cond->
- {:request-finished? request-finished?
- :file-writes-finished? writes-finished?}
- (false? request-finished?)
- (assoc :unfinished-requests? @db-transact/*unfinished-request-ids)))
- (notification/show!
- "Please wait seconds until all changes are saved for the current graph."
- :warning))
- (graph-switch-on-persisted graph opts))))
- (defmethod handle :graph/open-new-window [[_ev target-repo]]
- (ui-handler/open-new-window-or-tab! target-repo))
- (defmethod handle :graph/migrated [[_ _repo]]
- (js/alert "Graph migrated."))
- (defmethod handle :page/create [[_ page-name opts]]
- (if (= page-name (date/today))
- (page-handler/create-today-journal!)
- (page-handler/<create! page-name opts)))
- (defmethod handle :page/deleted [[_ repo page-name file-path tx-meta]]
- (page-common-handler/after-page-deleted! repo page-name file-path tx-meta))
- (defmethod handle :page/renamed [[_ repo data]]
- (page-common-handler/after-page-renamed! repo data))
- (defmethod handle :page/create-today-journal [[_ _repo]]
- (p/let [_ (page-handler/create-today-journal!)]
- (ui-handler/re-render-root!)))
- (defmethod handle :graph/sync-context []
- (let [context {:dev? config/dev?
- :node-test? util/node-test?
- :validate-db-options (:dev/validate-db-options (state/get-config))
- :importing? (:graph/importing @state/state)
- :date-formatter (state/get-date-formatter)
- :journal-file-name-format (or (state/get-journal-file-name-format)
- date/default-journal-filename-formatter)
- :export-bullet-indentation (state/get-export-bullet-indentation)
- :preferred-format (state/get-preferred-format)
- :journals-directory (config/get-journals-directory)
- :whiteboards-directory (config/get-whiteboards-directory)
- :pages-directory (config/get-pages-directory)}]
- (state/<invoke-db-worker :thread-api/set-context context)))
- ;; Hook on a graph is ready to be shown to the user.
- ;; It's different from :graph/restored, as :graph/restored is for window reloaded
- ;; FIXME: config may not be loaded when the graph is ready.
- (defmethod handle :graph/ready
- [[_ repo]]
- (when (config/local-file-based-graph? repo)
- (p/let [dir (config/get-repo-dir repo)
- dir-exists? (fs/dir-exists? dir)]
- (when (and (not dir-exists?)
- (not util/nfs?))
- (state/pub-event! [:graph/dir-gone dir]))))
- (let [db-based? (config/db-based-graph? repo)]
- (p/do!
- (state/pub-event! [:graph/sync-context])
- ;; re-render-root is async and delegated to rum, so we need to wait for main ui to refresh
- (when (util/mobile?)
- (state/pub-event! [:mobile/post-init]))
- ;; FIXME: an ugly implementation for redirecting to page on new window is restored
- (repo-handler/graph-ready! repo)
- (when-not config/publishing?
- (if db-based?
- (export/auto-db-backup! repo {:backup-now? true})
- (fs-watcher/load-graph-files! repo))))))
- (defmethod handle :instrument [[_ {:keys [type payload] :as opts}]]
- (when-not (empty? (dissoc opts :type :payload))
- (js/console.error "instrument data-map should only contains [:type :payload]"))
- (posthog/capture type payload))
- (defmethod handle :capture-error [[_ {:keys [error payload]}]]
- (let [[user-uuid graph-uuid tx-id] @sync/graphs-txid
- payload (merge
- {:schema-version (str db-schema/version)
- :db-schema-version (when-let [db (frontend.db/get-db)]
- (str (:kv/value (frontend.db/entity db :logseq.kv/schema-version))))
- :user-id user-uuid
- :graph-id graph-uuid
- :tx-id tx-id
- :db-based (config/db-based-graph? (state/get-current-repo))}
- payload)]
- (Sentry/captureException error
- (bean/->js {:tags payload}))))
- (defmethod handle :exec-plugin-cmd [[_ {:keys [pid cmd action]}]]
- (commands/exec-plugin-simple-command! pid cmd action))
- (defmethod handle :shortcut-handler-refreshed [[_]]
- (when-not @st/*pending-inited?
- (reset! st/*pending-inited? true)
- (st/consume-pending-shortcuts!)))
- (defmethod handle :mobile/keyboard-will-show [[_ keyboard-height]]
- (let [main-node (util/app-scroll-container-node)]
- (state/set-state! :mobile/show-action-bar? false)
- (when (= (state/sub :editor/record-status) "RECORDING")
- (state/set-state! :mobile/show-recording-bar? true))
- (when-let [^js html (js/document.querySelector ":root")]
- (.setProperty (.-style html) "--ls-native-kb-height" (str keyboard-height "px"))
- (.add (.-classList html) "has-mobile-keyboard")
- (.setProperty (.-style html) "--ls-native-toolbar-opacity" 1))
- (when (mobile-util/native-ios?)
- (reset! util/keyboard-height keyboard-height)
- (set! (.. main-node -style -marginBottom) (str keyboard-height "px")))))
- (defmethod handle :mobile/keyboard-will-hide [[_]]
- (let [main-node (util/app-scroll-container-node)]
- (when (= (state/sub :editor/record-status) "RECORDING")
- (state/set-state! :mobile/show-recording-bar? false))
- (when-let [^js html (js/document.querySelector ":root")]
- (.removeProperty (.-style html) "--ls-native-kb-height")
- (.setProperty (.-style html) "--ls-native-toolbar-opacity" 0)
- (.remove (.-classList html) "has-mobile-keyboard"))
- (when (mobile-util/native-ios?)
- (when-let [card-preview-el (js/document.querySelector ".cards-review")]
- (set! (.. card-preview-el -style -marginBottom) "0px"))
- (set! (.. main-node -style -marginBottom) "0px")
- (when-let [left-sidebar-node (gdom/getElement "left-sidebar")]
- (set! (.. left-sidebar-node -style -bottom) "0px"))
- (when-let [right-sidebar-node (gdom/getElementByClass "sidebar-item-list")]
- (set! (.. right-sidebar-node -style -paddingBottom) "150px")))))
- (defmethod handle :plugin/hook-db-tx [[_ {:keys [blocks tx-data] :as payload}]]
- (when-let [payload (and (seq blocks)
- (merge payload {:tx-data (map #(into [] %) tx-data)}))]
- (plugin-handler/hook-plugin-db :changed payload)
- (plugin-handler/hook-plugin-block-changes payload)))
- (defmethod handle :rebuild-slash-commands-list [[_]]
- (page-handler/rebuild-slash-commands-list!))
- (defmethod handle :shortcut/refresh [[_]]
- (st/refresh!))
- (defmethod handle :editor/set-heading [[_ block heading]]
- (when-let [id (:block/uuid block)]
- (editor-handler/set-heading! id heading)))
- (defmethod handle :graph/restored [[_ graph]]
- (when graph (assets-handler/ensure-assets-dir! graph))
- (rtc-flows/trigger-rtc-start graph)
- (fsrs/update-due-cards-count)
- (when-not (mobile-util/native-ios?)
- (state/pub-event! [:graph/ready graph])))
- (defmethod handle :whiteboard-link [[_ shapes]]
- (route-handler/go-to-search! :whiteboard/link)
- (state/set-state! :whiteboard/linked-shapes shapes))
- (defmethod handle :whiteboard-go-to-link [[_ link]]
- (route-handler/redirect! {:to :page
- :path-params {:name link}}))
- (defmethod handle :graph/save-db-to-disk [[_ _opts]]
- (persist-db/export-current-graph! {:succ-notification? true :force-save? true}))
- (defmethod handle :ui/re-render-root [[_]]
- (ui-handler/re-render-root!))
- (defmethod handle :run/cli-command [[_ command content]]
- (when (and command (not (string/blank? content)))
- (shell-handler/run-cli-command-wrapper! command content)))
- (defmethod handle :editor/quick-capture [[_ args]]
- (quick-capture/quick-capture args))
- (defmethod handle :modal/keymap [[_]]
- (state/open-settings! :keymap))
- (defmethod handle :editor/toggle-own-number-list [[_ blocks]]
- (let [batch? (sequential? blocks)
- blocks (cond->> blocks
- batch?
- (map #(cond-> % (or (uuid? %) (string? %)) (db-model/get-block-by-uuid))))]
- (if (and batch? (> (count blocks) 1))
- (editor-handler/toggle-blocks-as-own-order-list! blocks)
- (when-let [block (cond-> blocks batch? (first))]
- (if (editor-handler/own-order-number-list? block)
- (editor-handler/remove-block-own-order-list-type! block)
- (editor-handler/make-block-as-own-order-list! block))))))
- (defmethod handle :editor/remove-own-number-list [[_ block]]
- (when (some-> block (editor-handler/own-order-number-list?))
- (editor-handler/remove-block-own-order-list-type! block)))
- (defmethod handle :editor/save-current-block [_]
- (editor-handler/save-current-block!))
- (defmethod handle :editor/save-code-editor [_]
- (code-handler/save-code-editor!))
- (defmethod handle :editor/focus-code-editor [[_ editing-block container]]
- (when-let [^js cm (util/get-cm-instance container)]
- (when-not (.hasFocus cm)
- (let [cursor-pos (some-> (:editor/cursor-range @state/state) (deref) (count))
- direction (:block.editing/direction editing-block)
- pos (:block.editing/pos editing-block)
- to-line (case direction
- :up (.lastLine cm)
- (case pos
- :max (.lastLine cm)
- 0))]
- ;; move to friendly cursor
- (doto cm
- (.focus)
- (.setCursor to-line (or cursor-pos 0)))))))
- (defmethod handle :editor/toggle-children-number-list [[_ block]]
- (when-let [blocks (and block (db-model/get-block-immediate-children (state/get-current-repo) (:block/uuid block)))]
- (editor-handler/toggle-blocks-as-own-order-list! blocks)))
- (defmethod handle :editor/upsert-type-block [[_ {:keys [block type lang update-current-block?]}]]
- (p/do!
- (when-not update-current-block?
- (editor-handler/save-current-block!))
- (when-not update-current-block?
- (p/delay 16))
- (let [block (db/entity (:db/id block))
- block-type (:logseq.property.node/display-type block)
- block-title (:block/title block)
- latest-code-lang (or lang
- (:kv/value (db/entity :logseq.kv/latest-code-lang)))
- turn-type! #(if (and (= (keyword type) :code) latest-code-lang)
- (db-property-handler/set-block-properties!
- (:block/uuid %)
- {:logseq.property.node/display-type (keyword type)
- :logseq.property.code/lang latest-code-lang})
- (db-property-handler/set-block-property!
- (:block/uuid %) :logseq.property.node/display-type (keyword type)))]
- (p/let [block (if (or (not (nil? block-type))
- (and (not update-current-block?) (not (string/blank? block-title))))
- (p/let [result (ui-outliner-tx/transact!
- {:outliner-op :insert-blocks}
- ;; insert a new block
- (let [[_p _ block'] (editor-handler/insert-new-block-aux! {} block "")]
- (turn-type! block')))]
- (when-let [id (:block/uuid (first (:blocks result)))]
- (db/entity [:block/uuid id])))
- (p/do!
- (turn-type! block)
- (db/entity [:block/uuid (:block/uuid block)])))]
- (js/setTimeout #(editor-handler/edit-block! block :max) 100)))))
- (defmethod handle :vector-search/sync-state [[_ state]]
- (state/set-state! :vector-search/state state))
- (defmethod handle :rtc/sync-state [[_ state]]
- (state/update-state! :rtc/state (fn [old] (merge old state))))
- (defmethod handle :rtc/log [[_ data]]
- (state/set-state! :rtc/log data))
- (defmethod handle :rtc/remote-graph-gone [_]
- (p/do!
- (notification/show! "This graph has been removed from Logseq Sync." :warning false)
- (rtc-handler/<get-remote-graphs)))
- (defmethod handle :rtc/download-remote-graph [[_ graph-name graph-uuid graph-schema-version]]
- (assert (= (:major (db-schema/parse-schema-version db-schema/version))
- (:major (db-schema/parse-schema-version graph-schema-version)))
- {:app db-schema/version
- :remote-graph graph-schema-version})
- (->
- (p/do!
- (when (util/mobile?)
- (shui/popup-show!
- nil
- (fn []
- [:div.flex.flex-col.items-center.justify-center.mt-8.gap-4
- [:div (str "Downloading " graph-name " ...")]
- (indicator/downloading-logs)])
- {:id :download-rtc-graph}))
- (rtc-handler/<rtc-download-graph! graph-name graph-uuid graph-schema-version 60000)
- (rtc-handler/<get-remote-graphs)
- (when (util/mobile?)
- (shui/popup-hide! :download-rtc-graph)))
- (p/catch (fn [e]
- (println "RTC download graph failed, error:")
- (js/console.error e)
- (when (util/mobile?)
- (shui/popup-hide! :download-rtc-graph)
- ;; TODO: notify error
- )))))
- ;; db-worker -> UI
- (defmethod handle :db/sync-changes [[_ data]]
- (let [retract-datoms (filter (fn [d] (and (= :block/uuid (:a d)) (false? (:added d)))) (:tx-data data))
- retracted-tx-data (map (fn [d] [:db/retractEntity (:e d)]) retract-datoms)
- tx-data (concat (:tx-data data) retracted-tx-data)]
- (pipeline/invoke-hooks (assoc data :tx-data tx-data))
- nil))
- (defmethod handle :db/export-sqlite [_]
- (export/export-repo-as-sqlite-db! (state/get-current-repo))
- nil)
- (defmethod handle :editor/run-query-command [_]
- (editor-handler/run-query-command!))
- (defmethod handle :editor/load-blocks [[_ ids]]
- (when (seq ids)
- ;; not using `<get-blocks` here becuase because we want to
- ;; load all nested children here for copy/export
- (p/all (map (fn [id]
- (db-async/<get-block (state/get-current-repo) id
- {:skip-refresh? false})) ids))))
- (defmethod handle :vector-search/load-model-progress [[_ data]]
- (state/set-state! :vector-search/load-model-progress data))
- (defn run!
- []
- (let [chan (state/get-events-chan)]
- (async/go-loop []
- (let [[payload d] (async/<! chan)]
- (->
- (try
- (p/resolved (handle payload))
- (catch :default error
- (p/rejected error)))
- (p/then (fn [result]
- (p/resolve! d result)))
- (p/catch (fn [error]
- (log/error :event-error error :event (first payload))
- (let [type :handle-system-events/failed]
- (state/pub-event! [:capture-error {:error error
- :payload {:type type
- :payload payload}}])
- (p/reject! d error))))))
- (recur))
- chan))
- (comment
- (let [{:keys [deprecated-app-id current-app-id]} {:deprecated-app-id "AFDADF9A-7466-4ED8-B74F-AAAA0D4565B9", :current-app-id "7563518E-0EFD-4AD2-8577-10CFFD6E4596"}]
- (def deprecated-app-id deprecated-app-id)
- (def current-app-id current-app-id))
- (def deprecated-repo (state/get-current-repo))
- (def new-repo (string/replace deprecated-repo deprecated-app-id current-app-id))
- (update-file-path deprecated-repo new-repo deprecated-app-id current-app-id))
|