container.cljs 42 KB

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