pipeline.cljs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. (ns frontend.modules.outliner.pipeline
  2. (:require [frontend.config :as config]
  3. [frontend.db :as db]
  4. [frontend.db.react :as react]
  5. [frontend.state :as state]
  6. [frontend.modules.editor.undo-redo :as undo-redo]
  7. [datascript.core :as d]
  8. [frontend.handler.ui :as ui-handler]
  9. [frontend.handler.history :as history]
  10. [logseq.db :as ldb]
  11. [promesa.core :as p]))
  12. (defn store-undo-data!
  13. [{:keys [tx-meta] :as opts}]
  14. (when-not config/test?
  15. (when (or (:outliner/transact? tx-meta)
  16. (:outliner-op tx-meta)
  17. (:whiteboard/transact? tx-meta))
  18. (undo-redo/listen-db-changes! opts))))
  19. (defn- mark-pages-as-loaded!
  20. [repo page-names]
  21. (state/update-state! [repo :unloaded-pages] #(remove page-names %)))
  22. (defn- get-tx-id
  23. [tx-report]
  24. (get-in tx-report [:tempids :db/current-tx]))
  25. (defn- update-current-tx-editor-cursor!
  26. [tx-report]
  27. (let [tx-id (get-tx-id tx-report)
  28. editor-cursor @(:history/tx-before-editor-cursor @state/state)]
  29. (state/update-state! :history/tx->editor-cursor
  30. (fn [m] (assoc-in m [tx-id :before] editor-cursor)))
  31. (state/set-state! :history/tx-before-editor-cursor nil)))
  32. (defn restore-cursor-and-app-state!
  33. [{:keys [editor-cursor app-state]} undo?]
  34. (history/restore-cursor! editor-cursor undo?)
  35. (history/restore-app-state! app-state))
  36. (defn invoke-hooks
  37. [{:keys [request-id tx-meta tx-data deleted-block-uuids affected-keys blocks] :as opts}]
  38. ;; (prn :debug
  39. ;; :request-id request-id
  40. ;; :tx-meta tx-meta
  41. ;; :tx-data tx-data)
  42. (let [{:keys [from-disk? new-graph? local-tx? undo? redo?]} tx-meta
  43. repo (state/get-current-repo)
  44. tx-report {:tx-meta tx-meta
  45. :tx-data tx-data}]
  46. (let [conn (db/get-db repo false)
  47. tx-report (d/transact! conn tx-data tx-meta)]
  48. (when local-tx?
  49. (let [tx-id (get-tx-id tx-report)]
  50. (store-undo-data! (assoc opts :tx-id tx-id))))
  51. (when-not (or undo? redo?)
  52. (update-current-tx-editor-cursor! tx-report)))
  53. (let [new-datoms (filter (fn [datom]
  54. (and
  55. (= :block/uuid (:a datom))
  56. (true? (:added datom)))) tx-data)]
  57. (when (seq new-datoms)
  58. (state/set-state! :editor/new-created-blocks (set (map :v new-datoms)))))
  59. (let [pages (set (keep #(when (= :block/name (:a %)) (:v %)) tx-data))]
  60. (when (seq pages)
  61. (mark-pages-as-loaded! repo pages)))
  62. (if (or from-disk? new-graph?)
  63. (do
  64. (react/clear-query-state!)
  65. (ui-handler/re-render-root!))
  66. (when-not (:graph/importing @state/state)
  67. (react/refresh! repo tx-report affected-keys)
  68. (when-let [state (:ui/restore-cursor-state @state/state)]
  69. (when (or undo? redo?)
  70. (restore-cursor-and-app-state! state undo?)
  71. (state/set-state! :ui/restore-cursor-state nil)))
  72. (state/set-state! :editor/start-pos nil)
  73. (when (and state/lsp-enabled?
  74. (seq blocks)
  75. (<= (count blocks) 1000))
  76. (state/pub-event! [:plugin/hook-db-tx
  77. {:blocks blocks
  78. :deleted-block-uuids deleted-block-uuids
  79. :tx-data (:tx-data tx-report)
  80. :tx-meta (:tx-meta tx-report)}]))))
  81. (when (= (:outliner-op tx-meta) :delete-page)
  82. (state/pub-event! [:page/deleted repo (:deleted-page tx-meta) (:file-path tx-meta) tx-meta]))
  83. (when (= (:outliner-op tx-meta) :rename-page)
  84. (state/pub-event! [:page/renamed repo (:data tx-meta)]))
  85. (when-let [deleting-block-id (:ui/deleting-block @state/state)]
  86. (when (some (fn [datom] (and
  87. (= :block/uuid (:a datom))
  88. (= (:v datom) deleting-block-id)
  89. (true? (:added datom)))) tx-data) ; editing-block was added back (could be undo or from remote sync)
  90. (state/set-state! :ui/deleting-block nil)))
  91. (when request-id
  92. (when-let [deferred (ldb/get-deferred-response request-id)]
  93. (p/resolve! deferred {:tx-meta tx-meta
  94. :tx-data tx-data})
  95. (swap! ldb/*request-id->response dissoc request-id)))))