container.cljs 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984
  1. (ns frontend.components.container
  2. (:require [cljs-drag-n-drop.core :as dnd]
  3. [clojure.string :as string]
  4. [frontend.version :refer [version]]
  5. [frontend.components.find-in-page :as find-in-page]
  6. [frontend.components.header :as header]
  7. [frontend.components.journal :as journal]
  8. [frontend.components.plugins :as plugins]
  9. [frontend.components.repo :as repo]
  10. [frontend.components.right-sidebar :as right-sidebar]
  11. [frontend.components.select :as select]
  12. [frontend.components.theme :as theme]
  13. [frontend.components.dnd :as dnd-component]
  14. [frontend.components.icon :as icon]
  15. [frontend.components.handbooks :as handbooks]
  16. [frontend.components.block :as block]
  17. [dommy.core :as d]
  18. [frontend.components.content :as cp-content]
  19. [frontend.components.title :as title]
  20. [frontend.config :as config]
  21. [frontend.context.i18n :refer [t]]
  22. [frontend.db :as db]
  23. [electron.ipc :as ipc]
  24. [frontend.db-mixins :as db-mixins]
  25. [frontend.db.model :as db-model]
  26. [frontend.extensions.pdf.utils :as pdf-utils]
  27. [frontend.storage :as storage]
  28. [frontend.handler.common :as common-handler]
  29. [frontend.handler.editor :as editor-handler]
  30. [frontend.handler.page :as page-handler]
  31. [frontend.util.page :as page-util]
  32. [frontend.handler.route :as route-handler]
  33. [frontend.handler.user :as user-handler]
  34. [frontend.handler.whiteboard :as whiteboard-handler]
  35. [frontend.handler.recent :as recent-handler]
  36. [frontend.mixins :as mixins]
  37. [frontend.mobile.action-bar :as action-bar]
  38. [frontend.mobile.footer :as footer]
  39. [frontend.mobile.mobile-bar :refer [mobile-bar]]
  40. [frontend.mobile.util :as mobile-util]
  41. [frontend.modules.shortcut.data-helper :as shortcut-dh]
  42. [frontend.modules.shortcut.utils :as shortcut-utils]
  43. [frontend.state :as state]
  44. [frontend.ui :as ui]
  45. [logseq.shui.ui :as shui]
  46. [logseq.shui.toaster.core :as shui-toaster]
  47. [logseq.shui.dialog.core :as shui-dialog]
  48. [logseq.shui.popup.core :as shui-popup]
  49. [frontend.util :as util]
  50. [frontend.util.cursor :as cursor]
  51. [frontend.components.window-controls :as window-controls]
  52. [medley.core :as medley]
  53. [goog.dom :as gdom]
  54. [goog.object :as gobj]
  55. [logseq.common.path :as path]
  56. [react-draggable]
  57. [reitit.frontend.easy :as rfe]
  58. [rum.core :as rum]
  59. [logseq.db :as ldb]
  60. [frontend.extensions.fsrs :as fsrs]
  61. [logseq.common.util.namespace :as ns-util]))
  62. (rum/defc nav-content-item < rum/reactive
  63. [name {:keys [class count]} child]
  64. (let [collapsed? (state/sub [:ui/navigation-item-collapsed? class])]
  65. [:div.nav-content-item
  66. {:class (util/classnames [class {:is-expand (not collapsed?)
  67. :has-children (and (number? count) (> count 0))}])}
  68. [:div.nav-content-item-inner
  69. [:div.header.items-center
  70. {:on-click (fn [^js/MouseEvent _e]
  71. (state/toggle-navigation-item-collapsed! class))}
  72. [:div.a name]
  73. [:div.b (ui/icon "chevron-left" {:class "more" :size 14})]]
  74. (when child [:div.bd child])]]))
  75. (rum/defc page-name
  76. [page icon recent?]
  77. (let [repo (state/get-current-repo)
  78. db-based? (config/db-based-graph? repo)
  79. page (or (db/get-alias-source-page repo (:db/id page)) page)
  80. title (:block/title page)
  81. untitled? (db-model/untitled-page? title)
  82. name (:block/name page)
  83. file-rpath (when (util/electron?) (page-util/get-page-file-rpath name))
  84. ctx-icon #(shui/tabler-icon %1 {:class "scale-90 pr-1 opacity-80"})
  85. open-in-sidebar #(state/sidebar-add-block!
  86. (state/get-current-repo)
  87. (:db/id page)
  88. :page)
  89. x-menu-content (fn []
  90. (let [x-menu-item shui/dropdown-menu-item
  91. x-menu-shortcut shui/dropdown-menu-shortcut]
  92. [:<>
  93. (when-not recent?
  94. (x-menu-item
  95. {:on-click #(page-handler/<unfavorite-page! (if db-based? (str (:block/uuid page)) title))}
  96. (ctx-icon "star-off")
  97. (t :page/unfavorite)
  98. (x-menu-shortcut (when-let [binding (shortcut-dh/shortcut-binding :command/toggle-favorite)]
  99. (some-> binding
  100. (first)
  101. (shortcut-utils/decorate-binding))))))
  102. (when-let [page-fpath (and (util/electron?) file-rpath
  103. (config/get-repo-fpath (state/get-current-repo) file-rpath))]
  104. [:<>
  105. (x-menu-item
  106. {:on-click #(ipc/ipc :openFileInFolder page-fpath)}
  107. (ctx-icon "folder")
  108. (t :page/open-in-finder))
  109. (x-menu-item
  110. {:on-click #(js/window.apis.openPath page-fpath)}
  111. (ctx-icon "file")
  112. (t :page/open-with-default-app))])
  113. (x-menu-item
  114. {:on-click open-in-sidebar}
  115. (ctx-icon "layout-sidebar-right")
  116. (t :content/open-in-sidebar)
  117. (x-menu-shortcut (shortcut-utils/decorate-binding "shift+click")))]))]
  118. ;; TODO: move to standalone component
  119. [:a.flex.items-center.justify-between.relative.group.h-6
  120. (cond->
  121. {:on-click
  122. (fn [e]
  123. (if (gobj/get e "shiftKey")
  124. (open-in-sidebar)
  125. (route-handler/redirect-to-page! (:block/uuid page) {:click-from-recent? recent?})))
  126. :on-context-menu (fn [^js e]
  127. (shui/popup-show! e (x-menu-content)
  128. {:as-dropdown? true
  129. :content-props {:on-click (fn [] (shui/popup-hide!))
  130. :class "w-60"}})
  131. (util/stop e))}
  132. (ldb/object? page)
  133. (assoc :title (title/block-unique-title page)))
  134. [:span.page-icon.ml-3.justify-center icon]
  135. [:span.page-title {:class (when untitled? "opacity-50")
  136. :style {:display "ruby"}}
  137. (cond
  138. (not (db/page? page))
  139. (block/inline-text :markdown (:block/title page))
  140. untitled? (t :untitled)
  141. :else (let [title' (pdf-utils/fix-local-asset-pagename title)
  142. parent (:logseq.property/parent page)]
  143. (if (and parent (not (ldb/class? page)))
  144. (str (:block/title parent) ns-util/parent-char title')
  145. title')))]
  146. ;; dots trigger
  147. (shui/button
  148. {:size :sm
  149. :variant :ghost
  150. :class "absolute right-2 top-0 px-1.5 scale-75 opacity-30 hidden group-hover:block hover:opacity-80 active:opacity-100"
  151. :on-click #(do
  152. (shui/popup-show! (.-target %) (x-menu-content)
  153. {:as-dropdown? true
  154. :content-props {:on-click (fn [] (shui/popup-hide!))
  155. :class "w-60"}})
  156. (util/stop %))}
  157. [:i.relative {:style {:top "1px"}} (shui/tabler-icon "dots")])]))
  158. ;; Fall back to default if icon is undefined or empty
  159. (rum/defc favorites < rum/reactive
  160. [t]
  161. (let [_favorites-updated? (state/sub :favorites/updated?)
  162. favorite-entities (page-handler/get-favorites)]
  163. (nav-content-item
  164. [:a.flex.items-center.text-sm.font-medium.rounded-md.wrap-th
  165. (ui/icon "star" {:size 16})
  166. [:strong.flex-1.ml-2 (string/upper-case (t :left-side-bar/nav-favorites))]]
  167. {:class "favorites"
  168. :count (count favorite-entities)
  169. :edit-fn
  170. (fn [e]
  171. (rfe/push-state :page {:name "Favorites"})
  172. (util/stop e))}
  173. (when (seq favorite-entities)
  174. (let [favorite-items (map
  175. (fn [e]
  176. (let [icon (icon/get-node-icon-cp e {})]
  177. {:id (str (:db/id e))
  178. :value (:block/uuid e)
  179. :content [:li.favorite-item (page-name e icon false)]}))
  180. favorite-entities)]
  181. (dnd-component/items favorite-items
  182. {:on-drag-end (fn [favorites']
  183. (page-handler/<reorder-favorites! favorites'))
  184. :parent-node :ul.favorites.text-sm}))))))
  185. (rum/defc recent-pages < rum/reactive db-mixins/query
  186. [t]
  187. (let [pages (recent-handler/get-recent-pages)]
  188. (nav-content-item
  189. [:a.flex.items-center.text-sm.font-medium.rounded-md.wrap-th
  190. (ui/icon "history" {:size 16})
  191. [:strong.flex-1.ml-2
  192. (string/upper-case (t :left-side-bar/nav-recent-pages))]]
  193. {:class "recent"
  194. :count (count pages)}
  195. [:ul.text-sm
  196. (for [page pages]
  197. [:li.recent-item.select-none
  198. {:key (str "recent-" (:db/id page))
  199. :title (title/block-unique-title page)
  200. :draggable true
  201. :on-drag-start (fn [event] (editor-handler/block->data-transfer! (:block/name page) event true))
  202. :data-ref name}
  203. (page-name page (icon/get-node-icon-cp page {}) true)])])))
  204. (rum/defcs flashcards < db-mixins/query rum/reactive
  205. {:did-mount (fn [state]
  206. (fsrs/update-due-cards-count)
  207. state)}
  208. [_state srs-open?]
  209. (let [num (state/sub :srs/cards-due-count)]
  210. [:a.item.group.flex.items-center.px-2.py-2.text-sm.font-medium.rounded-md
  211. {:class (util/classnames [{:active srs-open?}])
  212. :on-click #(do
  213. (fsrs/update-due-cards-count)
  214. (state/pub-event! [:modal/show-cards]))}
  215. (ui/icon "infinity")
  216. [:span.flex-1 (t :right-side-bar/flashcards)]
  217. [:span.ml-1 (ui/render-keyboard-shortcut
  218. (ui/keyboard-shortcut-from-config :go/flashcards
  219. {:pick-first? true}))]
  220. (when (and num (not (zero? num)))
  221. [:span.ml-1.inline-block.py-0.5.px-3.text-xs.font-medium.rounded-full.fade-in num])]))
  222. (defn get-default-home-if-valid
  223. []
  224. (when-let [default-home (state/get-default-home)]
  225. (let [page (:page default-home)
  226. page (when (and (string? page)
  227. (not (string/blank? page)))
  228. (db/get-page page))]
  229. (if page
  230. default-home
  231. (dissoc default-home :page)))))
  232. (defn sidebar-item
  233. [{:keys [on-click-handler class title icon icon-extension? active href shortcut]}]
  234. [:div
  235. {:class class}
  236. [:a.item.group.flex.items-center.text-sm.font-medium.rounded-md
  237. {:on-click on-click-handler
  238. :class (when active "active")
  239. :href href}
  240. (ui/icon (str icon) {:extension? icon-extension?})
  241. [:span.flex-1 title]
  242. (when shortcut
  243. [:span.ml-1 (ui/render-keyboard-shortcut (ui/keyboard-shortcut-from-config shortcut))])]])
  244. (rum/defc ^:large-vars/cleanup-todo sidebar-nav
  245. [route-match close-modal-fn left-sidebar-open? enable-whiteboards? srs-open?
  246. *closing? close-signal touching-x-offset]
  247. (let [[local-closing? set-local-closing?] (rum/use-state false)
  248. [el-rect set-el-rect!] (rum/use-state nil)
  249. ref-el (rum/use-ref nil)
  250. ref-open? (rum/use-ref left-sidebar-open?)
  251. db-based? (config/db-based-graph? (state/get-current-repo))
  252. default-home (get-default-home-if-valid)
  253. route-name (get-in route-match [:data :name])
  254. on-contents-scroll #(when-let [^js el (.-target %)]
  255. (let [top (.-scrollTop el)
  256. cls (.-classList el)
  257. cls' "is-scrolled"]
  258. (if (> top 2)
  259. (.add cls cls')
  260. (.remove cls cls'))))
  261. close-fn #(set-local-closing? true)
  262. touching-x-offset (when (number? touching-x-offset)
  263. (if-not left-sidebar-open?
  264. (when (> touching-x-offset 0)
  265. (min touching-x-offset (:width el-rect)))
  266. (when (< touching-x-offset 0)
  267. (max touching-x-offset (- 0 (:width el-rect))))))
  268. offset-ratio (and (number? touching-x-offset)
  269. (some->> (:width el-rect)
  270. (/ touching-x-offset)))]
  271. (rum/use-effect!
  272. #(js/setTimeout
  273. (fn [] (some-> (rum/deref ref-el)
  274. (.getBoundingClientRect)
  275. (.toJSON)
  276. (js->clj :keywordize-keys true)
  277. (set-el-rect!)))
  278. 16)
  279. [])
  280. (rum/use-layout-effect!
  281. (fn []
  282. (when (and (rum/deref ref-open?) local-closing?)
  283. (reset! *closing? true))
  284. (rum/set-ref! ref-open? left-sidebar-open?)
  285. #())
  286. [local-closing? left-sidebar-open?])
  287. (rum/use-effect!
  288. (fn []
  289. (when-not (neg? close-signal)
  290. (close-fn)))
  291. [close-signal])
  292. [:<>
  293. [:div.left-sidebar-inner.flex-1.flex.flex-col.min-h-0
  294. {:ref ref-el
  295. :style (cond-> {}
  296. (and (number? offset-ratio)
  297. (> touching-x-offset 0))
  298. (assoc :transform (str "translate3d(calc(" touching-x-offset "px - 100%), 0, 0)"))
  299. (and (number? offset-ratio)
  300. (< touching-x-offset 0))
  301. (assoc :transform (str "translate3d(" (* offset-ratio 100) "%, 0, 0)")))
  302. :on-transition-end (fn []
  303. (when local-closing?
  304. (reset! *closing? false)
  305. (set-local-closing? false)
  306. (close-modal-fn)))
  307. :on-click #(when-let [^js target (and (util/sm-breakpoint?) (.-target %))]
  308. (when (some (fn [sel] (boolean (.closest target sel)))
  309. [".favorites .bd" ".recent .bd" ".dropdown-wrapper" ".nav-header"])
  310. (close-fn)))}
  311. [:div.flex.flex-col.wrap.gap-1.relative
  312. [:nav.px-4.flex.flex-col.gap-1.cp__menubar-repos
  313. {:aria-label "Navigation menu"}
  314. (repo/repos-dropdown)
  315. [:div.nav-header.flex.flex-col.mt-1
  316. (let [page (:page default-home)]
  317. (if (and page (not (state/enable-journals? (state/get-current-repo))))
  318. (sidebar-item
  319. {:class "home-nav"
  320. :title page
  321. :on-click-handler route-handler/redirect-to-home!
  322. :active (and (not srs-open?)
  323. (= route-name :page)
  324. (= page (get-in route-match [:path-params :name])))
  325. :icon "home"
  326. :shortcut :go/home})
  327. (sidebar-item
  328. {:class "journals-nav"
  329. :active (and (not srs-open?)
  330. (or (= route-name :all-journals) (= route-name :home)))
  331. :title (t :left-side-bar/journals)
  332. :on-click-handler (fn [e]
  333. (if (gobj/get e "shiftKey")
  334. (route-handler/sidebar-journals!)
  335. (route-handler/go-to-journals!)))
  336. :icon "calendar"
  337. :shortcut :go/journals})))
  338. (when enable-whiteboards?
  339. (when (or config/dev? (not db-based?))
  340. (sidebar-item
  341. {:class "whiteboard"
  342. :title (t :right-side-bar/whiteboards)
  343. :href (rfe/href :whiteboards)
  344. :on-click-handler (fn [_e] (whiteboard-handler/onboarding-show))
  345. :active (and (not srs-open?) (#{:whiteboard :whiteboards} route-name))
  346. :icon "whiteboard"
  347. :icon-extension? true
  348. :shortcut :go/whiteboards})))
  349. (when (state/enable-flashcards? (state/get-current-repo))
  350. [:div.flashcards-nav
  351. (flashcards srs-open?)])
  352. (sidebar-item
  353. {:class "graph-view-nav"
  354. :title (t :right-side-bar/graph-view)
  355. :href (rfe/href :graph)
  356. :active (and (not srs-open?) (= route-name :graph))
  357. :icon "hierarchy"
  358. :shortcut :go/graph-view})
  359. (sidebar-item
  360. {:class "all-pages-nav"
  361. :title (t :right-side-bar/all-pages)
  362. :href (rfe/href :all-pages)
  363. :active (and (not srs-open?) (= route-name :all-pages))
  364. :icon "files"})]]
  365. [:div.nav-contents-container.flex.flex-col.gap-1.pt-1
  366. {:on-scroll on-contents-scroll}
  367. (favorites t)
  368. (when (not config/publishing?)
  369. (recent-pages t))]]]
  370. [:span.shade-mask
  371. (cond-> {:on-click close-fn}
  372. (number? offset-ratio)
  373. (assoc :style {:opacity (cond-> offset-ratio
  374. (neg? offset-ratio)
  375. (+ 1))}))]]))
  376. (rum/defc sidebar-resizer
  377. []
  378. (let [*el-ref (rum/use-ref nil)
  379. ^js el-doc js/document.documentElement
  380. adjust-size! (fn [width]
  381. (.setProperty (.-style el-doc) "--ls-left-sidebar-width" width)
  382. (storage/set :ls-left-sidebar-width width))]
  383. ;; restore size
  384. (rum/use-layout-effect!
  385. (fn []
  386. (when-let [width (storage/get :ls-left-sidebar-width)]
  387. (.setProperty (.-style el-doc) "--ls-left-sidebar-width" width)))
  388. [])
  389. ;; draggable handler
  390. (rum/use-effect!
  391. (fn []
  392. (when-let [el (and (fn? js/window.interact) (rum/deref *el-ref))]
  393. (let [^js sidebar-el (.querySelector el-doc "#left-sidebar")]
  394. (-> (js/interact el)
  395. (.draggable
  396. #js {:listeners
  397. #js {:move (fn [^js/MouseEvent e]
  398. (when-let [offset (.-left (.-rect e))]
  399. (let [width (.toFixed (max (min offset 460) 240) 2)]
  400. (adjust-size! (str width "px")))))}})
  401. (.styleCursor false)
  402. (.on "dragstart" (fn []
  403. (.. sidebar-el -classList (add "is-resizing"))
  404. (.. el-doc -classList (add "is-resizing-buf"))))
  405. (.on "dragend" (fn []
  406. (.. sidebar-el -classList (remove "is-resizing"))
  407. (.. el-doc -classList (remove "is-resizing-buf"))))))
  408. #()))
  409. [])
  410. [:span.left-sidebar-resizer {:ref *el-ref}]))
  411. (rum/defcs left-sidebar < rum/reactive
  412. (rum/local false ::closing?)
  413. (rum/local -1 ::close-signal)
  414. (rum/local nil ::touch-state)
  415. [s {:keys [left-sidebar-open? route-match]}]
  416. (let [close-fn #(state/set-left-sidebar-open! false)
  417. *closing? (::closing? s)
  418. *touch-state (::touch-state s)
  419. *close-signal (::close-signal s)
  420. enable-whiteboards? (state/enable-whiteboards?)
  421. touch-point-fn (fn [^js e] (some-> (gobj/get e "touches") (aget 0) (#(hash-map :x (.-clientX %) :y (.-clientY %)))))
  422. srs-open? (= :srs (state/sub :modal/id))
  423. touching-x-offset (and (some-> @*touch-state :after)
  424. (some->> @*touch-state
  425. ((juxt :after :before))
  426. (map :x) (apply -)))
  427. touch-pending? (> (abs touching-x-offset) 20)]
  428. [:div#left-sidebar.cp__sidebar-left-layout
  429. {:class (util/classnames [{:is-open left-sidebar-open?
  430. :is-closing @*closing?
  431. :is-touching touch-pending?}])
  432. :on-touch-start
  433. (fn [^js e]
  434. (reset! *touch-state {:before (touch-point-fn e)}))
  435. :on-touch-move
  436. (fn [^js e]
  437. (when @*touch-state
  438. (some-> *touch-state (swap! assoc :after (touch-point-fn e)))))
  439. :on-touch-end
  440. (fn []
  441. (when touch-pending?
  442. (cond
  443. (and (not left-sidebar-open?) (> touching-x-offset 40))
  444. (state/set-left-sidebar-open! true)
  445. (and left-sidebar-open? (< touching-x-offset -30))
  446. (reset! *close-signal (inc @*close-signal))))
  447. (reset! *touch-state nil))}
  448. ;; sidebar contents
  449. (sidebar-nav route-match close-fn left-sidebar-open? enable-whiteboards? srs-open? *closing?
  450. @*close-signal (and touch-pending? touching-x-offset))
  451. ;; resizer
  452. (sidebar-resizer)]))
  453. (rum/defc recording-bar
  454. []
  455. [:> react-draggable
  456. {:onStart (fn [_event]
  457. (when-let [pos (some-> (state/get-input) cursor/pos)]
  458. (state/set-editor-last-pos! pos)))
  459. :onStop (fn [_event]
  460. (when-let [block (get @(get @state/state :editor/block) :block/uuid)]
  461. (editor-handler/edit-block! block :max)
  462. (when-let [input (state/get-input)]
  463. (when-let [saved-cursor (state/get-editor-last-pos)]
  464. (cursor/move-cursor-to input saved-cursor)))))}
  465. [:div#audio-record-toolbar
  466. {:style {:bottom (+ @util/keyboard-height 45)}}
  467. (footer/audio-record-cp)]])
  468. (rum/defc main <
  469. {:did-mount (fn [state]
  470. (when-let [element (gdom/getElement "main-content-container")]
  471. (dnd/subscribe!
  472. element
  473. :upload-files
  474. {:drop (fn [_e files]
  475. (when-let [id (state/get-edit-input-id)]
  476. (let [format (:block/format (state/get-edit-block))]
  477. (editor-handler/upload-asset! id files format editor-handler/*asset-uploading? true))))})
  478. (common-handler/listen-to-scroll! element)
  479. (when (:margin-less-pages? (first (:rum/args state))) ;; makes sure full screen pages displaying without scrollbar
  480. (set! (.. element -scrollTop) 0)))
  481. state)
  482. :will-unmount (fn [state]
  483. (when-let [el (gdom/getElement "main-content-container")]
  484. (dnd/unsubscribe! el :upload-files))
  485. state)}
  486. [{:keys [route-match margin-less-pages? route-name indexeddb-support? db-restoring? main-content show-action-bar? show-recording-bar?]}]
  487. (let [left-sidebar-open? (state/sub :ui/left-sidebar-open?)
  488. onboarding-and-home? (and (or (nil? (state/get-current-repo)) (config/demo-graph?))
  489. (not config/publishing?)
  490. (= :home route-name))
  491. margin-less-pages? (or (and (mobile-util/native-platform?) onboarding-and-home?) margin-less-pages?)]
  492. [:div#main-container.cp__sidebar-main-layout.flex-1.flex
  493. {:class (util/classnames [{:is-left-sidebar-open left-sidebar-open?}])}
  494. ;; desktop left sidebar layout
  495. (left-sidebar {:left-sidebar-open? left-sidebar-open?
  496. :route-match route-match})
  497. [:div#main-content-container.scrollbar-spacing.w-full.flex.justify-center.flex-row.outline-none.relative
  498. {:tabIndex "-1"
  499. :data-is-margin-less-pages margin-less-pages?}
  500. (when show-action-bar?
  501. (action-bar/action-bar))
  502. [:div.cp__sidebar-main-content
  503. {:data-is-margin-less-pages margin-less-pages?
  504. :data-is-full-width (or margin-less-pages?
  505. (contains? #{:all-files :all-pages :my-publishing} route-name))}
  506. (when show-recording-bar?
  507. (recording-bar))
  508. (mobile-bar)
  509. (footer/footer)
  510. (cond
  511. (not indexeddb-support?)
  512. nil
  513. db-restoring?
  514. (if config/publishing?
  515. [:div.space-y-2
  516. (shui/skeleton {:class "h-8 w-1/3 mb-8 bg-gray-400"})
  517. (shui/skeleton {:class "h-6 w-full bg-gray-400"})
  518. (shui/skeleton {:class "h-6 w-full bg-gray-400"})]
  519. [:div.space-y-2
  520. (shui/skeleton {:class "h-8 w-1/3 mb-8"})
  521. (shui/skeleton {:class "h-6 w-full"})
  522. (shui/skeleton {:class "h-6 w-full"})])
  523. :else
  524. [:div
  525. {:class (if (or onboarding-and-home? margin-less-pages?) "" (util/hiccup->class "mx-auto.pb-24"))
  526. :style {:margin-bottom (cond
  527. margin-less-pages? 0
  528. onboarding-and-home? 0
  529. :else 120)}}
  530. main-content])
  531. (comment
  532. (when onboarding-and-home?
  533. (onboarding/intro onboarding-and-home?)))]]]))
  534. (defonce sidebar-inited? (atom false))
  535. ;; TODO: simplify logic
  536. (rum/defc parsing-progress < rum/static
  537. [state]
  538. (let [finished (or (:finished state) 0)
  539. total (:total state)
  540. width (js/Math.round (* (.toFixed (/ finished total) 2) 100))
  541. display-filename (some-> (:current-parsing-file state)
  542. not-empty
  543. path/filename)
  544. left-label [:div.flex.flex-row.font-bold
  545. (t :parsing-files)
  546. [:div.hidden.md:flex.flex-row
  547. [:span.mr-1 ": "]
  548. [:div.text-ellipsis-wrapper {:style {:max-width 300}}
  549. display-filename]]]]
  550. (ui/progress-bar-with-label width left-label (str finished "/" total))))
  551. (rum/defc main-content < rum/reactive db-mixins/query
  552. {:init (fn [state]
  553. (when-not @sidebar-inited?
  554. (let [current-repo (state/sub :git/current-repo)
  555. default-home (get-default-home-if-valid)
  556. sidebar (:sidebar default-home)
  557. sidebar (if (string? sidebar) [sidebar] sidebar)]
  558. (when-let [pages (->> (seq sidebar)
  559. (remove string/blank?))]
  560. (doseq [page pages]
  561. (let [page (util/safe-page-name-sanity-lc page)
  562. [db-id block-type] (if (= page "contents")
  563. [(or (:db/id (db/get-page page)) "contents") :contents]
  564. [(:db/id (db/get-page page)) :page])]
  565. (state/sidebar-add-block! current-repo db-id block-type)))
  566. (reset! sidebar-inited? true))))
  567. (when (state/mobile?)
  568. (state/set-state! :mobile/show-tabbar? true))
  569. state)}
  570. []
  571. (let [default-home (get-default-home-if-valid)
  572. current-repo (state/sub :git/current-repo)
  573. loading-files? (when current-repo (state/sub [:repo/loading-files? current-repo]))
  574. journals-length (state/sub :journals-length)
  575. latest-journals (db/get-latest-journals (state/get-current-repo) journals-length)
  576. graph-parsing-state (state/sub [:graph/parsing-state current-repo])]
  577. (cond
  578. (or
  579. (:graph-loading? graph-parsing-state)
  580. (not= (:total graph-parsing-state) (:finished graph-parsing-state)))
  581. [:div.flex.items-center.justify-center.full-height-without-header
  582. [:div.flex-1
  583. (parsing-progress graph-parsing-state)]]
  584. :else
  585. [:div
  586. (cond
  587. (and default-home
  588. (= :home (state/get-current-route))
  589. (not (state/route-has-p?))
  590. (:page default-home))
  591. (route-handler/redirect-to-page! (:page default-home))
  592. (and config/publishing?
  593. (not default-home)
  594. (empty? latest-journals))
  595. (route-handler/redirect! {:to :all-pages})
  596. loading-files?
  597. (ui/loading (t :loading-files))
  598. (seq latest-journals)
  599. (journal/journals latest-journals)
  600. ;; FIXME: why will this happen?
  601. :else
  602. [:div])])))
  603. (defn- hide-context-menu-and-clear-selection
  604. [e]
  605. (state/hide-custom-context-menu!)
  606. (when-not (or (gobj/get e "shiftKey")
  607. (util/meta-key? e)
  608. (state/get-edit-input-id)
  609. (= (shui-dialog/get-last-modal-id) :property-dialog)
  610. (some-> (.-target e) (.closest ".ls-block"))
  611. (some-> (.-target e) (.closest "[data-keep-selection]")))
  612. (editor-handler/clear-selection!)))
  613. (rum/defc render-custom-context-menu
  614. [links position]
  615. (let [ref (rum/use-ref nil)]
  616. (rum/use-effect!
  617. #(let [el (rum/deref ref)
  618. {:keys [x y]} (util/calc-delta-rect-offset el js/document.documentElement)]
  619. (set! (.. el -style -transform)
  620. (str "translate3d(" (if (neg? x) x 0) "px," (if (neg? y) (- y 10) 0) "px" ",0)"))))
  621. [:<>
  622. [:div.menu-backdrop {:on-pointer-down (fn [e] (hide-context-menu-and-clear-selection e))}]
  623. [:div#custom-context-menu
  624. {:ref ref
  625. :style {:z-index 999
  626. :left (str (first position) "px")
  627. :top (str (second position) "px")}} links]]))
  628. (rum/defc custom-context-menu < rum/reactive
  629. []
  630. (let [show? (state/sub :custom-context-menu/show?)
  631. links (state/sub :custom-context-menu/links)
  632. position (state/sub :custom-context-menu/position)]
  633. (when (and show? links position)
  634. (render-custom-context-menu links position))))
  635. (rum/defc new-block-mode < rum/reactive
  636. []
  637. (when (state/sub [:document/mode?])
  638. (ui/tippy {:html [:div.p-2
  639. [:p.mb-2 [:b "Document mode"]]
  640. [:ul
  641. [:li
  642. [:div.inline-block.mr-1 (ui/render-keyboard-shortcut (shortcut-dh/gen-shortcut-seq :editor/new-line))]
  643. [:p.inline-block "to create new block"]]
  644. [:li
  645. [:p.inline-block.mr-1 "Click `D` or type"]
  646. [:div.inline-block.mr-1 (ui/render-keyboard-shortcut (shortcut-dh/gen-shortcut-seq :ui/toggle-document-mode))]
  647. [:p.inline-block "to toggle document mode"]]]]}
  648. [:a.block.px-1.text-sm.font-medium.bg-base-2.rounded-md.mx-2
  649. {:on-click state/toggle-document-mode!}
  650. "D"])))
  651. (def help-menu-items
  652. [{:title "Handbook" :icon "book-2" :on-click #(handbooks/toggle-handbooks)}
  653. {:title "Keyboard shortcuts" :icon "command" :on-click #(state/sidebar-add-block! (state/get-current-repo) "shortcut-settings" :shortcut-settings)}
  654. {:title "Documentation" :icon "help" :href "https://docs.logseq.com/"}
  655. :hr
  656. {:title "Report bug" :icon "bug" :on-click #(rfe/push-state :bug-report)}
  657. {:title "Request feature" :icon "git-pull-request" :href "https://discuss.logseq.com/c/feedback/feature-requests/"}
  658. {:title "Submit feedback" :icon "messages" :href "https://discuss.logseq.com/c/feedback/13"}
  659. :hr
  660. {:title "Ask the community" :icon "brand-discord" :href "https://discord.com/invite/KpN4eHY"}
  661. {:title "Support forum" :icon "message" :href "https://discuss.logseq.com/"}
  662. :hr
  663. {:title "Release notes" :icon "asterisk" :href "https://docs.logseq.com/#/page/changelog"}])
  664. (rum/defc help-menu-popup
  665. []
  666. (rum/use-effect!
  667. (fn []
  668. (state/set-state! :ui/handbooks-open? false))
  669. [])
  670. (rum/use-effect!
  671. (fn []
  672. (let [h #(state/set-state! :ui/help-open? false)]
  673. (.addEventListener js/document.body "click" h)
  674. #(.removeEventListener js/document.body "click" h)))
  675. [])
  676. [:div.cp__sidebar-help-menu-popup
  677. [:div.list-wrap
  678. (for [[idx {:keys [title icon href on-click] :as item}] (medley/indexed help-menu-items)]
  679. (case item
  680. :hr
  681. [:hr.my-2 {:key idx}]
  682. ;; default
  683. [:a.it.flex.items-center.px-4.py-1.select-none
  684. {:key title
  685. :on-click (fn []
  686. (cond
  687. (fn? on-click) (on-click)
  688. (string? href) (util/open-url href))
  689. (state/set-state! :ui/help-open? false))}
  690. [:span.flex.items-center.pr-2.opacity-40 (ui/icon icon {:size 20})]
  691. [:strong.font-normal title]]))]
  692. [:div.ft.pl-11.pb-3
  693. [:span.opacity.text-xs.opacity-30 "Logseq " version]]])
  694. (rum/defc help-button < rum/reactive
  695. []
  696. (let [help-open? (state/sub :ui/help-open?)
  697. handbooks-open? (state/sub :ui/handbooks-open?)]
  698. [:<>
  699. [:div.cp__sidebar-help-btn
  700. [:div.inner
  701. {:title (t :help-shortcut-title)
  702. :on-click #(state/toggle! :ui/help-open?)}
  703. [:svg.scale-125 {:stroke "currentColor", :fill "none", :stroke-linejoin "round", :width "24", :view-box "0 0 24 24", :xmlns "http://www.w3.org/2000/svg", :stroke-linecap "round", :stroke-width "2", :class "icon icon-tabler icon-tabler-help-small", :height "24"}
  704. [:path {:stroke "none", :d "M0 0h24v24H0z", :fill "none"}]
  705. [:path {:d "M12 16v.01"}]
  706. [:path {:d "M12 13a2 2 0 0 0 .914 -3.782a1.98 1.98 0 0 0 -2.414 .483"}]]]]
  707. (when help-open?
  708. (help-menu-popup))
  709. (when handbooks-open?
  710. (handbooks/handbooks-popup))]))
  711. (rum/defc app-context-menu-observer
  712. < rum/static
  713. (mixins/event-mixin
  714. (fn [state]
  715. ;; fixme: this mixin will register global event listeners on window
  716. ;; which might cause unexpected issues
  717. (mixins/listen state js/window "contextmenu"
  718. (fn [^js e]
  719. (let [target (gobj/get e "target")
  720. block-el (.closest target ".bullet-container[blockid]")
  721. block-id (some-> block-el (.getAttribute "blockid"))
  722. {:keys [block block-ref]} (state/sub :block-ref/context)
  723. {:keys [page page-entity]} (state/sub :page-title/context)]
  724. (let [show!
  725. (fn [content]
  726. (shui/popup-show! e
  727. (fn [{:keys [id]}]
  728. [:div {:on-click #(shui/popup-hide! id)
  729. :data-keep-selection true}
  730. content])
  731. {:on-before-hide state/dom-clear-selection!
  732. :on-after-hide state/state-clear-selection!
  733. :content-props {:class "w-[280px] ls-context-menu-content"}
  734. :as-dropdown? true}))
  735. handled
  736. (cond
  737. (and page (not block-id))
  738. (do
  739. (show! (cp-content/page-title-custom-context-menu-content page-entity))
  740. (state/set-state! :page-title/context nil))
  741. block-ref
  742. (do
  743. (show! (cp-content/block-ref-custom-context-menu-content block block-ref))
  744. (state/set-state! :block-ref/context nil))
  745. ;; block selection
  746. (and (state/selection?) (not (d/has-class? target "bullet")))
  747. (show! (cp-content/custom-context-menu-content))
  748. ;; block bullet
  749. (and block-id (parse-uuid block-id))
  750. (let [block (.closest target ".ls-block")]
  751. (when block
  752. (state/clear-selection!)
  753. (state/conj-selection-block! block :down))
  754. (show! (cp-content/block-context-menu-content target (uuid block-id))))
  755. :else
  756. false)]
  757. (when (not (false? handled))
  758. (util/stop e))))))))
  759. []
  760. nil)
  761. (rum/defcs ^:large-vars/cleanup-todo root-container <
  762. (mixins/modal :modal/show?)
  763. rum/reactive
  764. (mixins/event-mixin
  765. (fn [state]
  766. (mixins/listen state js/window "pointerdown" hide-context-menu-and-clear-selection)
  767. (mixins/listen state js/window "keydown"
  768. (fn [e]
  769. (cond
  770. (= 27 (.-keyCode e))
  771. (if (and (state/modal-opened?)
  772. (not
  773. (and
  774. ;; FIXME: this does not work on CI tests
  775. util/node-test?
  776. (state/editing?))))
  777. (state/close-modal!)
  778. (hide-context-menu-and-clear-selection e)))
  779. (state/set-ui-last-key-code! (.-key e))))
  780. (mixins/listen state js/window "keyup"
  781. (fn [_e]
  782. (state/set-state! :editor/latest-shortcut nil)))))
  783. [state route-match main-content']
  784. (let [{:keys [open-fn]} state
  785. current-repo (state/sub :git/current-repo)
  786. granted? (state/sub [:nfs/user-granted? (state/get-current-repo)])
  787. theme (state/sub :ui/theme)
  788. accent-color (some-> (state/sub :ui/radix-color) (name))
  789. editor-font (some-> (state/sub :ui/editor-font) (name))
  790. system-theme? (state/sub :ui/system-theme?)
  791. light? (= "light" (state/sub :ui/theme))
  792. sidebar-open? (state/sub :ui/sidebar-open?)
  793. settings-open? (state/sub :ui/settings-open?)
  794. left-sidebar-open? (state/sub :ui/left-sidebar-open?)
  795. wide-mode? (state/sub :ui/wide-mode?)
  796. ls-block-hl-colored? (state/sub :pdf/block-highlight-colored?)
  797. onboarding-state (state/sub :file-sync/onboarding-state)
  798. right-sidebar-blocks (state/sub-right-sidebar-blocks)
  799. route-name (get-in route-match [:data :name])
  800. margin-less-pages? (or (boolean (#{:graph} route-name))
  801. (db-model/whiteboard-page? (state/get-current-page)))
  802. db-restoring? (state/sub :db/restoring?)
  803. indexeddb-support? (state/sub :indexeddb/support?)
  804. page? (= :page route-name)
  805. home? (= :home route-name)
  806. native-titlebar? (state/sub [:electron/user-cfgs :window/native-titlebar?])
  807. window-controls? (and (util/electron?) (not util/mac?) (not native-titlebar?))
  808. edit? (state/editing?)
  809. default-home (get-default-home-if-valid)
  810. logged? (user-handler/logged-in?)
  811. fold-button-on-right? (state/enable-fold-button-right?)
  812. show-action-bar? (state/sub :mobile/show-action-bar?)
  813. show-recording-bar? (state/sub :mobile/show-recording-bar?)
  814. preferred-language (state/sub [:preferred-language])]
  815. (theme/container
  816. {:t t
  817. :theme theme
  818. :accent-color accent-color
  819. :editor-font editor-font
  820. :route route-match
  821. :current-repo current-repo
  822. :edit? edit?
  823. :nfs-granted? granted?
  824. :db-restoring? db-restoring?
  825. :sidebar-open? sidebar-open?
  826. :settings-open? settings-open?
  827. :sidebar-blocks-len (count right-sidebar-blocks)
  828. :system-theme? system-theme?
  829. :onboarding-state onboarding-state
  830. :preferred-language preferred-language
  831. :on-click (fn [e]
  832. (editor-handler/unhighlight-blocks!)
  833. (util/fix-open-external-with-shift! e))}
  834. [:main.theme-container-inner#app-container-wrapper
  835. {:class (util/classnames
  836. [{:ls-left-sidebar-open left-sidebar-open?
  837. :ls-right-sidebar-open sidebar-open?
  838. :ls-wide-mode wide-mode?
  839. :ls-window-controls window-controls?
  840. :ls-fold-button-on-right fold-button-on-right?
  841. :ls-hl-colored ls-block-hl-colored?}])
  842. :on-pointer-up (fn []
  843. (when-let [container (gdom/getElement "app-container-wrapper")]
  844. (d/remove-class! container "blocks-selection-mode")
  845. (when (> (count (state/get-selection-blocks)) 1)
  846. (util/clear-selection!))))}
  847. [:button#skip-to-main
  848. {:on-click #(ui/focus-element (ui/main-node))
  849. :on-key-up (fn [e]
  850. (when (= "Enter" (.-key e))
  851. (ui/focus-element (ui/main-node))))}
  852. (t :accessibility/skip-to-main-content)]
  853. [:div.#app-container
  854. [:div#left-container
  855. {:class (if (state/sub :ui/sidebar-open?) "overflow-hidden" "w-full")}
  856. (header/header {:open-fn open-fn
  857. :light? light?
  858. :current-repo current-repo
  859. :logged? logged?
  860. :page? page?
  861. :route-match route-match
  862. :default-home default-home
  863. :new-block-mode new-block-mode})
  864. (when (util/electron?)
  865. (find-in-page/search))
  866. (main {:route-match route-match
  867. :margin-less-pages? margin-less-pages?
  868. :logged? logged?
  869. :home? home?
  870. :route-name route-name
  871. :indexeddb-support? indexeddb-support?
  872. :light? light?
  873. :db-restoring? db-restoring?
  874. :main-content main-content'
  875. :show-action-bar? show-action-bar?
  876. :show-recording-bar? show-recording-bar?})]
  877. (when window-controls?
  878. (window-controls/container))
  879. (right-sidebar/sidebar)
  880. [:div#app-single-container]]
  881. (ui/notification)
  882. (ui/modal)
  883. (ui/sub-modal)
  884. (shui-toaster/install-toaster)
  885. (shui-dialog/install-modals)
  886. (shui-popup/install-popups)
  887. (select/select-modal)
  888. (custom-context-menu)
  889. (plugins/custom-js-installer
  890. {:t t
  891. :current-repo current-repo
  892. :nfs-granted? granted?
  893. :db-restoring? db-restoring?})
  894. (app-context-menu-observer)
  895. [:a#download.hidden]
  896. (when (and (not config/mobile?)
  897. (not config/publishing?))
  898. (help-button))])))