container.cljs 43 KB

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