container.cljs 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985
  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 (and (state/enable-flashcards? (state/get-current-repo))
  350. (not db-based?))
  351. [:div.flashcards-nav
  352. (flashcards srs-open?)])
  353. (sidebar-item
  354. {:class "graph-view-nav"
  355. :title (t :right-side-bar/graph-view)
  356. :href (rfe/href :graph)
  357. :active (and (not srs-open?) (= route-name :graph))
  358. :icon "hierarchy"
  359. :shortcut :go/graph-view})
  360. (sidebar-item
  361. {:class "all-pages-nav"
  362. :title (t :right-side-bar/all-pages)
  363. :href (rfe/href :all-pages)
  364. :active (and (not srs-open?) (= route-name :all-pages))
  365. :icon "files"})]]
  366. [:div.nav-contents-container.flex.flex-col.gap-1.pt-1
  367. {:on-scroll on-contents-scroll}
  368. (favorites t)
  369. (when (not config/publishing?)
  370. (recent-pages t))]]]
  371. [:span.shade-mask
  372. (cond-> {:on-click close-fn}
  373. (number? offset-ratio)
  374. (assoc :style {:opacity (cond-> offset-ratio
  375. (neg? offset-ratio)
  376. (+ 1))}))]]))
  377. (rum/defc sidebar-resizer
  378. []
  379. (let [*el-ref (rum/use-ref nil)
  380. ^js el-doc js/document.documentElement
  381. adjust-size! (fn [width]
  382. (.setProperty (.-style el-doc) "--ls-left-sidebar-width" width)
  383. (storage/set :ls-left-sidebar-width width))]
  384. ;; restore size
  385. (rum/use-layout-effect!
  386. (fn []
  387. (when-let [width (storage/get :ls-left-sidebar-width)]
  388. (.setProperty (.-style el-doc) "--ls-left-sidebar-width" width)))
  389. [])
  390. ;; draggable handler
  391. (rum/use-effect!
  392. (fn []
  393. (when-let [el (and (fn? js/window.interact) (rum/deref *el-ref))]
  394. (let [^js sidebar-el (.querySelector el-doc "#left-sidebar")]
  395. (-> (js/interact el)
  396. (.draggable
  397. #js {:listeners
  398. #js {:move (fn [^js/MouseEvent e]
  399. (when-let [offset (.-left (.-rect e))]
  400. (let [width (.toFixed (max (min offset 460) 240) 2)]
  401. (adjust-size! (str width "px")))))}})
  402. (.styleCursor false)
  403. (.on "dragstart" (fn []
  404. (.. sidebar-el -classList (add "is-resizing"))
  405. (.. el-doc -classList (add "is-resizing-buf"))))
  406. (.on "dragend" (fn []
  407. (.. sidebar-el -classList (remove "is-resizing"))
  408. (.. el-doc -classList (remove "is-resizing-buf"))))))
  409. #()))
  410. [])
  411. [:span.left-sidebar-resizer {:ref *el-ref}]))
  412. (rum/defcs left-sidebar < rum/reactive
  413. (rum/local false ::closing?)
  414. (rum/local -1 ::close-signal)
  415. (rum/local nil ::touch-state)
  416. [s {:keys [left-sidebar-open? route-match]}]
  417. (let [close-fn #(state/set-left-sidebar-open! false)
  418. *closing? (::closing? s)
  419. *touch-state (::touch-state s)
  420. *close-signal (::close-signal s)
  421. enable-whiteboards? (state/enable-whiteboards?)
  422. touch-point-fn (fn [^js e] (some-> (gobj/get e "touches") (aget 0) (#(hash-map :x (.-clientX %) :y (.-clientY %)))))
  423. srs-open? (= :srs (state/sub :modal/id))
  424. touching-x-offset (and (some-> @*touch-state :after)
  425. (some->> @*touch-state
  426. ((juxt :after :before))
  427. (map :x) (apply -)))
  428. touch-pending? (> (abs touching-x-offset) 20)]
  429. [:div#left-sidebar.cp__sidebar-left-layout
  430. {:class (util/classnames [{:is-open left-sidebar-open?
  431. :is-closing @*closing?
  432. :is-touching touch-pending?}])
  433. :on-touch-start
  434. (fn [^js e]
  435. (reset! *touch-state {:before (touch-point-fn e)}))
  436. :on-touch-move
  437. (fn [^js e]
  438. (when @*touch-state
  439. (some-> *touch-state (swap! assoc :after (touch-point-fn e)))))
  440. :on-touch-end
  441. (fn []
  442. (when touch-pending?
  443. (cond
  444. (and (not left-sidebar-open?) (> touching-x-offset 40))
  445. (state/set-left-sidebar-open! true)
  446. (and left-sidebar-open? (< touching-x-offset -30))
  447. (reset! *close-signal (inc @*close-signal))))
  448. (reset! *touch-state nil))}
  449. ;; sidebar contents
  450. (sidebar-nav route-match close-fn left-sidebar-open? enable-whiteboards? srs-open? *closing?
  451. @*close-signal (and touch-pending? touching-x-offset))
  452. ;; resizer
  453. (sidebar-resizer)]))
  454. (rum/defc recording-bar
  455. []
  456. [:> react-draggable
  457. {:onStart (fn [_event]
  458. (when-let [pos (some-> (state/get-input) cursor/pos)]
  459. (state/set-editor-last-pos! pos)))
  460. :onStop (fn [_event]
  461. (when-let [block (get @(get @state/state :editor/block) :block/uuid)]
  462. (editor-handler/edit-block! block :max)
  463. (when-let [input (state/get-input)]
  464. (when-let [saved-cursor (state/get-editor-last-pos)]
  465. (cursor/move-cursor-to input saved-cursor)))))}
  466. [:div#audio-record-toolbar
  467. {:style {:bottom (+ @util/keyboard-height 45)}}
  468. (footer/audio-record-cp)]])
  469. (rum/defc main <
  470. {:did-mount (fn [state]
  471. (when-let [element (gdom/getElement "main-content-container")]
  472. (dnd/subscribe!
  473. element
  474. :upload-files
  475. {:drop (fn [_e files]
  476. (when-let [id (state/get-edit-input-id)]
  477. (let [format (:block/format (state/get-edit-block))]
  478. (editor-handler/upload-asset! id files format editor-handler/*asset-uploading? true))))})
  479. (common-handler/listen-to-scroll! element)
  480. (when (:margin-less-pages? (first (:rum/args state))) ;; makes sure full screen pages displaying without scrollbar
  481. (set! (.. element -scrollTop) 0)))
  482. state)
  483. :will-unmount (fn [state]
  484. (when-let [el (gdom/getElement "main-content-container")]
  485. (dnd/unsubscribe! el :upload-files))
  486. state)}
  487. [{:keys [route-match margin-less-pages? route-name indexeddb-support? db-restoring? main-content show-action-bar? show-recording-bar?]}]
  488. (let [left-sidebar-open? (state/sub :ui/left-sidebar-open?)
  489. onboarding-and-home? (and (or (nil? (state/get-current-repo)) (config/demo-graph?))
  490. (not config/publishing?)
  491. (= :home route-name))
  492. margin-less-pages? (or (and (mobile-util/native-platform?) onboarding-and-home?) margin-less-pages?)]
  493. [:div#main-container.cp__sidebar-main-layout.flex-1.flex
  494. {:class (util/classnames [{:is-left-sidebar-open left-sidebar-open?}])}
  495. ;; desktop left sidebar layout
  496. (left-sidebar {:left-sidebar-open? left-sidebar-open?
  497. :route-match route-match})
  498. [:div#main-content-container.scrollbar-spacing.w-full.flex.justify-center.flex-row.outline-none.relative
  499. {:tabIndex "-1"
  500. :data-is-margin-less-pages margin-less-pages?}
  501. (when show-action-bar?
  502. (action-bar/action-bar))
  503. [:div.cp__sidebar-main-content
  504. {:data-is-margin-less-pages margin-less-pages?
  505. :data-is-full-width (or margin-less-pages?
  506. (contains? #{:all-files :all-pages :my-publishing} route-name))}
  507. (when show-recording-bar?
  508. (recording-bar))
  509. (mobile-bar)
  510. (footer/footer)
  511. (cond
  512. (not indexeddb-support?)
  513. nil
  514. db-restoring?
  515. (if config/publishing?
  516. [:div.space-y-2
  517. (shui/skeleton {:class "h-8 w-1/3 mb-8 bg-gray-400"})
  518. (shui/skeleton {:class "h-6 w-full bg-gray-400"})
  519. (shui/skeleton {:class "h-6 w-full bg-gray-400"})]
  520. [:div.space-y-2
  521. (shui/skeleton {:class "h-8 w-1/3 mb-8"})
  522. (shui/skeleton {:class "h-6 w-full"})
  523. (shui/skeleton {:class "h-6 w-full"})])
  524. :else
  525. [:div
  526. {:class (if (or onboarding-and-home? margin-less-pages?) "" (util/hiccup->class "mx-auto.pb-24"))
  527. :style {:margin-bottom (cond
  528. margin-less-pages? 0
  529. onboarding-and-home? 0
  530. :else 120)}}
  531. main-content])
  532. (comment
  533. (when onboarding-and-home?
  534. (onboarding/intro onboarding-and-home?)))]]]))
  535. (defonce sidebar-inited? (atom false))
  536. ;; TODO: simplify logic
  537. (rum/defc parsing-progress < rum/static
  538. [state]
  539. (let [finished (or (:finished state) 0)
  540. total (:total state)
  541. width (js/Math.round (* (.toFixed (/ finished total) 2) 100))
  542. display-filename (some-> (:current-parsing-file state)
  543. not-empty
  544. path/filename)
  545. left-label [:div.flex.flex-row.font-bold
  546. (t :parsing-files)
  547. [:div.hidden.md:flex.flex-row
  548. [:span.mr-1 ": "]
  549. [:div.text-ellipsis-wrapper {:style {:max-width 300}}
  550. display-filename]]]]
  551. (ui/progress-bar-with-label width left-label (str finished "/" total))))
  552. (rum/defc main-content < rum/reactive db-mixins/query
  553. {:init (fn [state]
  554. (when-not @sidebar-inited?
  555. (let [current-repo (state/sub :git/current-repo)
  556. default-home (get-default-home-if-valid)
  557. sidebar (:sidebar default-home)
  558. sidebar (if (string? sidebar) [sidebar] sidebar)]
  559. (when-let [pages (->> (seq sidebar)
  560. (remove string/blank?))]
  561. (doseq [page pages]
  562. (let [page (util/safe-page-name-sanity-lc page)
  563. [db-id block-type] (if (= page "contents")
  564. [(or (:db/id (db/get-page page)) "contents") :contents]
  565. [(:db/id (db/get-page page)) :page])]
  566. (state/sidebar-add-block! current-repo db-id block-type)))
  567. (reset! sidebar-inited? true))))
  568. (when (state/mobile?)
  569. (state/set-state! :mobile/show-tabbar? true))
  570. state)}
  571. []
  572. (let [default-home (get-default-home-if-valid)
  573. current-repo (state/sub :git/current-repo)
  574. loading-files? (when current-repo (state/sub [:repo/loading-files? current-repo]))
  575. journals-length (state/sub :journals-length)
  576. latest-journals (db/get-latest-journals (state/get-current-repo) journals-length)
  577. graph-parsing-state (state/sub [:graph/parsing-state current-repo])]
  578. (cond
  579. (or
  580. (:graph-loading? graph-parsing-state)
  581. (not= (:total graph-parsing-state) (:finished graph-parsing-state)))
  582. [:div.flex.items-center.justify-center.full-height-without-header
  583. [:div.flex-1
  584. (parsing-progress graph-parsing-state)]]
  585. :else
  586. [:div
  587. (cond
  588. (and default-home
  589. (= :home (state/get-current-route))
  590. (not (state/route-has-p?))
  591. (:page default-home))
  592. (route-handler/redirect-to-page! (:page default-home))
  593. (and config/publishing?
  594. (not default-home)
  595. (empty? latest-journals))
  596. (route-handler/redirect! {:to :all-pages})
  597. loading-files?
  598. (ui/loading (t :loading-files))
  599. (seq latest-journals)
  600. (journal/journals latest-journals)
  601. ;; FIXME: why will this happen?
  602. :else
  603. [:div])])))
  604. (defn- hide-context-menu-and-clear-selection
  605. [e]
  606. (state/hide-custom-context-menu!)
  607. (when-not (or (gobj/get e "shiftKey")
  608. (util/meta-key? e)
  609. (state/get-edit-input-id)
  610. (= (shui-dialog/get-last-modal-id) :property-dialog)
  611. (some-> (.-target e) (.closest ".ls-block"))
  612. (some-> (.-target e) (.closest "[data-keep-selection]")))
  613. (editor-handler/clear-selection!)))
  614. (rum/defc render-custom-context-menu
  615. [links position]
  616. (let [ref (rum/use-ref nil)]
  617. (rum/use-effect!
  618. #(let [el (rum/deref ref)
  619. {:keys [x y]} (util/calc-delta-rect-offset el js/document.documentElement)]
  620. (set! (.. el -style -transform)
  621. (str "translate3d(" (if (neg? x) x 0) "px," (if (neg? y) (- y 10) 0) "px" ",0)"))))
  622. [:<>
  623. [:div.menu-backdrop {:on-pointer-down (fn [e] (hide-context-menu-and-clear-selection e))}]
  624. [:div#custom-context-menu
  625. {:ref ref
  626. :style {:z-index 999
  627. :left (str (first position) "px")
  628. :top (str (second position) "px")}} links]]))
  629. (rum/defc custom-context-menu < rum/reactive
  630. []
  631. (let [show? (state/sub :custom-context-menu/show?)
  632. links (state/sub :custom-context-menu/links)
  633. position (state/sub :custom-context-menu/position)]
  634. (when (and show? links position)
  635. (render-custom-context-menu links position))))
  636. (rum/defc new-block-mode < rum/reactive
  637. []
  638. (when (state/sub [:document/mode?])
  639. (ui/tippy {:html [:div.p-2
  640. [:p.mb-2 [:b "Document mode"]]
  641. [:ul
  642. [:li
  643. [:div.inline-block.mr-1 (ui/render-keyboard-shortcut (shortcut-dh/gen-shortcut-seq :editor/new-line))]
  644. [:p.inline-block "to create new block"]]
  645. [:li
  646. [:p.inline-block.mr-1 "Click `D` or type"]
  647. [:div.inline-block.mr-1 (ui/render-keyboard-shortcut (shortcut-dh/gen-shortcut-seq :ui/toggle-document-mode))]
  648. [:p.inline-block "to toggle document mode"]]]]}
  649. [:a.block.px-1.text-sm.font-medium.bg-base-2.rounded-md.mx-2
  650. {:on-click state/toggle-document-mode!}
  651. "D"])))
  652. (def help-menu-items
  653. [{:title "Handbook" :icon "book-2" :on-click #(handbooks/toggle-handbooks)}
  654. {:title "Keyboard shortcuts" :icon "command" :on-click #(state/sidebar-add-block! (state/get-current-repo) "shortcut-settings" :shortcut-settings)}
  655. {:title "Documentation" :icon "help" :href "https://docs.logseq.com/"}
  656. :hr
  657. {:title "Report bug" :icon "bug" :on-click #(rfe/push-state :bug-report)}
  658. {:title "Request feature" :icon "git-pull-request" :href "https://discuss.logseq.com/c/feedback/feature-requests/"}
  659. {:title "Submit feedback" :icon "messages" :href "https://discuss.logseq.com/c/feedback/13"}
  660. :hr
  661. {:title "Ask the community" :icon "brand-discord" :href "https://discord.com/invite/KpN4eHY"}
  662. {:title "Support forum" :icon "message" :href "https://discuss.logseq.com/"}
  663. :hr
  664. {:title "Release notes" :icon "asterisk" :href "https://docs.logseq.com/#/page/changelog"}])
  665. (rum/defc help-menu-popup
  666. []
  667. (rum/use-effect!
  668. (fn []
  669. (state/set-state! :ui/handbooks-open? false))
  670. [])
  671. (rum/use-effect!
  672. (fn []
  673. (let [h #(state/set-state! :ui/help-open? false)]
  674. (.addEventListener js/document.body "click" h)
  675. #(.removeEventListener js/document.body "click" h)))
  676. [])
  677. [:div.cp__sidebar-help-menu-popup
  678. [:div.list-wrap
  679. (for [[idx {:keys [title icon href on-click] :as item}] (medley/indexed help-menu-items)]
  680. (case item
  681. :hr
  682. [:hr.my-2 {:key idx}]
  683. ;; default
  684. [:a.it.flex.items-center.px-4.py-1.select-none
  685. {:key title
  686. :on-click (fn []
  687. (cond
  688. (fn? on-click) (on-click)
  689. (string? href) (util/open-url href))
  690. (state/set-state! :ui/help-open? false))}
  691. [:span.flex.items-center.pr-2.opacity-40 (ui/icon icon {:size 20})]
  692. [:strong.font-normal title]]))]
  693. [:div.ft.pl-11.pb-3
  694. [:span.opacity.text-xs.opacity-30 "Logseq " version]]])
  695. (rum/defc help-button < rum/reactive
  696. []
  697. (let [help-open? (state/sub :ui/help-open?)
  698. handbooks-open? (state/sub :ui/handbooks-open?)]
  699. [:<>
  700. [:div.cp__sidebar-help-btn
  701. [:div.inner
  702. {:title (t :help-shortcut-title)
  703. :on-click #(state/toggle! :ui/help-open?)}
  704. [: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"}
  705. [:path {:stroke "none", :d "M0 0h24v24H0z", :fill "none"}]
  706. [:path {:d "M12 16v.01"}]
  707. [:path {:d "M12 13a2 2 0 0 0 .914 -3.782a1.98 1.98 0 0 0 -2.414 .483"}]]]]
  708. (when help-open?
  709. (help-menu-popup))
  710. (when handbooks-open?
  711. (handbooks/handbooks-popup))]))
  712. (rum/defc app-context-menu-observer
  713. < rum/static
  714. (mixins/event-mixin
  715. (fn [state]
  716. ;; fixme: this mixin will register global event listeners on window
  717. ;; which might cause unexpected issues
  718. (mixins/listen state js/window "contextmenu"
  719. (fn [^js e]
  720. (let [target (gobj/get e "target")
  721. block-el (.closest target ".bullet-container[blockid]")
  722. block-id (some-> block-el (.getAttribute "blockid"))
  723. {:keys [block block-ref]} (state/sub :block-ref/context)
  724. {:keys [page page-entity]} (state/sub :page-title/context)]
  725. (let [show!
  726. (fn [content]
  727. (shui/popup-show! e
  728. (fn [{:keys [id]}]
  729. [:div {:on-click #(shui/popup-hide! id)
  730. :data-keep-selection true}
  731. content])
  732. {:on-before-hide state/dom-clear-selection!
  733. :on-after-hide state/state-clear-selection!
  734. :content-props {:class "w-[280px] ls-context-menu-content"}
  735. :as-dropdown? true}))
  736. handled
  737. (cond
  738. (and page (not block-id))
  739. (do
  740. (show! (cp-content/page-title-custom-context-menu-content page-entity))
  741. (state/set-state! :page-title/context nil))
  742. block-ref
  743. (do
  744. (show! (cp-content/block-ref-custom-context-menu-content block block-ref))
  745. (state/set-state! :block-ref/context nil))
  746. ;; block selection
  747. (and (state/selection?) (not (d/has-class? target "bullet")))
  748. (show! (cp-content/custom-context-menu-content))
  749. ;; block bullet
  750. (and block-id (parse-uuid block-id))
  751. (let [block (.closest target ".ls-block")]
  752. (when block
  753. (state/clear-selection!)
  754. (state/conj-selection-block! block :down))
  755. (show! (cp-content/block-context-menu-content target (uuid block-id))))
  756. :else
  757. false)]
  758. (when (not (false? handled))
  759. (util/stop e))))))))
  760. []
  761. nil)
  762. (rum/defcs ^:large-vars/cleanup-todo root-container <
  763. (mixins/modal :modal/show?)
  764. rum/reactive
  765. (mixins/event-mixin
  766. (fn [state]
  767. (mixins/listen state js/window "pointerdown" hide-context-menu-and-clear-selection)
  768. (mixins/listen state js/window "keydown"
  769. (fn [e]
  770. (cond
  771. (= 27 (.-keyCode e))
  772. (if (and (state/modal-opened?)
  773. (not
  774. (and
  775. ;; FIXME: this does not work on CI tests
  776. util/node-test?
  777. (state/editing?))))
  778. (state/close-modal!)
  779. (hide-context-menu-and-clear-selection e)))
  780. (state/set-ui-last-key-code! (.-key e))))
  781. (mixins/listen state js/window "keyup"
  782. (fn [_e]
  783. (state/set-state! :editor/latest-shortcut nil)))))
  784. [state route-match main-content']
  785. (let [{:keys [open-fn]} state
  786. current-repo (state/sub :git/current-repo)
  787. granted? (state/sub [:nfs/user-granted? (state/get-current-repo)])
  788. theme (state/sub :ui/theme)
  789. accent-color (some-> (state/sub :ui/radix-color) (name))
  790. editor-font (some-> (state/sub :ui/editor-font) (name))
  791. system-theme? (state/sub :ui/system-theme?)
  792. light? (= "light" (state/sub :ui/theme))
  793. sidebar-open? (state/sub :ui/sidebar-open?)
  794. settings-open? (state/sub :ui/settings-open?)
  795. left-sidebar-open? (state/sub :ui/left-sidebar-open?)
  796. wide-mode? (state/sub :ui/wide-mode?)
  797. ls-block-hl-colored? (state/sub :pdf/block-highlight-colored?)
  798. onboarding-state (state/sub :file-sync/onboarding-state)
  799. right-sidebar-blocks (state/sub-right-sidebar-blocks)
  800. route-name (get-in route-match [:data :name])
  801. margin-less-pages? (or (boolean (#{:graph} route-name))
  802. (db-model/whiteboard-page? (state/get-current-page)))
  803. db-restoring? (state/sub :db/restoring?)
  804. indexeddb-support? (state/sub :indexeddb/support?)
  805. page? (= :page route-name)
  806. home? (= :home route-name)
  807. native-titlebar? (state/sub [:electron/user-cfgs :window/native-titlebar?])
  808. window-controls? (and (util/electron?) (not util/mac?) (not native-titlebar?))
  809. edit? (state/editing?)
  810. default-home (get-default-home-if-valid)
  811. logged? (user-handler/logged-in?)
  812. fold-button-on-right? (state/enable-fold-button-right?)
  813. show-action-bar? (state/sub :mobile/show-action-bar?)
  814. show-recording-bar? (state/sub :mobile/show-recording-bar?)
  815. preferred-language (state/sub [:preferred-language])]
  816. (theme/container
  817. {:t t
  818. :theme theme
  819. :accent-color accent-color
  820. :editor-font editor-font
  821. :route route-match
  822. :current-repo current-repo
  823. :edit? edit?
  824. :nfs-granted? granted?
  825. :db-restoring? db-restoring?
  826. :sidebar-open? sidebar-open?
  827. :settings-open? settings-open?
  828. :sidebar-blocks-len (count right-sidebar-blocks)
  829. :system-theme? system-theme?
  830. :onboarding-state onboarding-state
  831. :preferred-language preferred-language
  832. :on-click (fn [e]
  833. (editor-handler/unhighlight-blocks!)
  834. (util/fix-open-external-with-shift! e))}
  835. [:main.theme-container-inner#app-container-wrapper
  836. {:class (util/classnames
  837. [{:ls-left-sidebar-open left-sidebar-open?
  838. :ls-right-sidebar-open sidebar-open?
  839. :ls-wide-mode wide-mode?
  840. :ls-window-controls window-controls?
  841. :ls-fold-button-on-right fold-button-on-right?
  842. :ls-hl-colored ls-block-hl-colored?}])
  843. :on-pointer-up (fn []
  844. (when-let [container (gdom/getElement "app-container-wrapper")]
  845. (d/remove-class! container "blocks-selection-mode")
  846. (when (> (count (state/get-selection-blocks)) 1)
  847. (util/clear-selection!))))}
  848. [:button#skip-to-main
  849. {:on-click #(ui/focus-element (ui/main-node))
  850. :on-key-up (fn [e]
  851. (when (= "Enter" (.-key e))
  852. (ui/focus-element (ui/main-node))))}
  853. (t :accessibility/skip-to-main-content)]
  854. [:div.#app-container
  855. [:div#left-container
  856. {:class (if (state/sub :ui/sidebar-open?) "overflow-hidden" "w-full")}
  857. (header/header {:open-fn open-fn
  858. :light? light?
  859. :current-repo current-repo
  860. :logged? logged?
  861. :page? page?
  862. :route-match route-match
  863. :default-home default-home
  864. :new-block-mode new-block-mode})
  865. (when (util/electron?)
  866. (find-in-page/search))
  867. (main {:route-match route-match
  868. :margin-less-pages? margin-less-pages?
  869. :logged? logged?
  870. :home? home?
  871. :route-name route-name
  872. :indexeddb-support? indexeddb-support?
  873. :light? light?
  874. :db-restoring? db-restoring?
  875. :main-content main-content'
  876. :show-action-bar? show-action-bar?
  877. :show-recording-bar? show-recording-bar?})]
  878. (when window-controls?
  879. (window-controls/container))
  880. (right-sidebar/sidebar)
  881. [:div#app-single-container]]
  882. (ui/notification)
  883. (ui/modal)
  884. (ui/sub-modal)
  885. (shui-toaster/install-toaster)
  886. (shui-dialog/install-modals)
  887. (shui-popup/install-popups)
  888. (select/select-modal)
  889. (custom-context-menu)
  890. (plugins/custom-js-installer
  891. {:t t
  892. :current-repo current-repo
  893. :nfs-granted? granted?
  894. :db-restoring? db-restoring?})
  895. (app-context-menu-observer)
  896. [:a#download.hidden]
  897. (when (and (not config/mobile?)
  898. (not config/publishing?))
  899. (help-button))])))