publishing.cljs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. (ns frontend.publishing
  2. "Entry ns for publishing build. Handles primary publishing app behaviors"
  3. (:require [frontend.state :as state]
  4. [datascript.core :as d]
  5. [frontend.db :as db]
  6. [logseq.db.schema :as db-schema]
  7. [rum.core :as rum]
  8. [frontend.handler.route :as route-handler]
  9. [frontend.page :as page]
  10. [frontend.util :as util]
  11. [frontend.routes :as routes]
  12. [frontend.context.i18n :as i18n]
  13. [reitit.frontend :as rf]
  14. [reitit.frontend.easy :as rfe]
  15. [cljs.reader :as reader]
  16. [frontend.components.block :as block]
  17. [frontend.components.editor :as editor]
  18. [frontend.components.page :as page-component]
  19. [frontend.components.reference :as reference]
  20. [frontend.components.whiteboard :as whiteboard]
  21. [frontend.modules.shortcut.core :as shortcut]
  22. [frontend.handler.events :as events]
  23. [frontend.handler.command-palette :as command-palette]))
  24. ;; The publishing site should be as thin as possible.
  25. ;; Both files and git libraries can be removed.
  26. ;; Maybe we can remove some handlers and components too.
  27. ;; There should be two publishing modes:
  28. ;; 1. Graph version, similar to logseq.com
  29. ;; 2. Traditional blog version, much faster to load
  30. ;; We might host the pages or blocks directly on logseq.com in the future.
  31. ;; How to publish?
  32. ;; 1. When you click a publish button, it'll downloads a zip which includes the
  33. ;; html, css, javascript and other files (image, mp3, etc.), the serialized
  34. ;; data should include all the public pages and blocks.
  35. ;; 2. Built-in sync with GitHub Pages, you should specify a GitHub repo for publishing.
  36. (defn restore-from-transit-str!
  37. []
  38. (state/set-current-repo! "local")
  39. (when-let [data js/window.logseq_db]
  40. (let [data (util/unescape-html data)
  41. db-conn (d/create-conn db-schema/schema)
  42. _ (swap! db/conns assoc "logseq-db/local" db-conn)
  43. db (db/string->db data)]
  44. (reset! db-conn db))))
  45. (defn restore-state!
  46. []
  47. (when-let [data js/window.logseq_state]
  48. (let [data (reader/read-string data)]
  49. (swap! state/state merge data))))
  50. (defn set-router!
  51. []
  52. (rfe/start!
  53. (rf/router routes/routes {})
  54. route-handler/set-route-match!
  55. ;; set to false to enable HistoryAPI
  56. {:use-fragment true}))
  57. (defn start []
  58. (when-let [node (.getElementById js/document "root")]
  59. (set-router!)
  60. (rum/mount (page/current-page) node)))
  61. (defn- register-components-fns!
  62. []
  63. (state/set-page-blocks-cp! page-component/page-blocks-cp)
  64. (state/set-component! :block/linked-references reference/block-linked-references)
  65. (state/set-component! :whiteboard/tldraw-preview whiteboard/tldraw-preview)
  66. (state/set-component! :block/single-block block/single-block-cp)
  67. (state/set-component! :editor/box editor/box)
  68. (command-palette/register-global-shortcut-commands))
  69. (defn ^:export init []
  70. ;; init is called ONCE when the page loads
  71. ;; this is called in the index.html and must be exported
  72. ;; so it is available even in :advanced release builds
  73. (register-components-fns!)
  74. ;; Set :preferred-lang as some components depend on it
  75. (i18n/start)
  76. (restore-from-transit-str!)
  77. (restore-state!)
  78. (shortcut/refresh!)
  79. (events/run!)
  80. ;; actually, there's no persist for publishing
  81. (db/listen-and-persist! (state/get-current-repo))
  82. (start))
  83. (defn stop []
  84. ;; stop is called before any code is reloaded
  85. ;; this is controlled by :before-load in the config
  86. (js/console.log "stop"))