right_sidebar.cljs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  1. (ns frontend.components.right-sidebar
  2. (:require [cljs-bean.core :as bean]
  3. [clojure.string :as string]
  4. [frontend.components.block :as block]
  5. [frontend.components.cmdk.core :as cmdk]
  6. [frontend.components.icon :as icon]
  7. [frontend.components.onboarding :as onboarding]
  8. [frontend.components.page :as page]
  9. [frontend.components.profiler :as profiler]
  10. [frontend.components.shortcut-help :as shortcut-help]
  11. [frontend.config :as config]
  12. [frontend.context.i18n :refer [t]]
  13. [frontend.date :as date]
  14. [frontend.db :as db]
  15. [frontend.db.async :as db-async]
  16. [frontend.db.rtc.debug-ui :as rtc-debug-ui]
  17. [frontend.handler.editor :as editor-handler]
  18. [frontend.handler.route :as route-handler]
  19. [frontend.handler.ui :as ui-handler]
  20. [frontend.state :as state]
  21. [frontend.ui :as ui]
  22. [frontend.util :as util]
  23. [logseq.db :as ldb]
  24. [logseq.shui.hooks :as hooks]
  25. [logseq.shui.ui :as shui]
  26. [medley.core :as medley]
  27. [promesa.core :as p]
  28. [rum.core :as rum]))
  29. (rum/defc toggle
  30. []
  31. (when-not (util/sm-breakpoint?)
  32. (ui/with-shortcut :ui/toggle-right-sidebar "left"
  33. (shui/button-ghost-icon :layout-sidebar-right
  34. {:title (t :right-side-bar/toggle-right-sidebar)
  35. :class "toggle-right-sidebar"
  36. :on-click ui-handler/toggle-right-sidebar!}))))
  37. (rum/defc block-cp < rum/reactive
  38. [repo idx block]
  39. (let [id (:block/uuid block)]
  40. [:div.mt-2
  41. (page/page-cp {:parameters {:path {:name (str id)}}
  42. :sidebar? true
  43. :sidebar/idx idx
  44. :repo repo})]))
  45. (defn get-scrollable-container
  46. []
  47. (js/document.querySelector ".sidebar-item-list"))
  48. (rum/defc page-cp < rum/reactive
  49. [repo page-name]
  50. (page/page-cp {:parameters {:path {:name page-name}}
  51. :sidebar? true
  52. :scroll-container (get-scrollable-container)
  53. :repo repo}))
  54. (rum/defc shortcut-settings
  55. []
  56. [:div.contents.flex-col.flex.ml-3
  57. (shortcut-help/shortcut-page {:show-title? false})])
  58. (defn- block-with-breadcrumb
  59. [repo block idx sidebar-key ref?]
  60. (when-let [block-id (:block/uuid block)]
  61. [[:.flex.items-center {:class (when ref? "ml-2")}
  62. (block/breadcrumb {:id "block-parent"
  63. :block? true
  64. :sidebar-key sidebar-key} repo block-id {:indent? false})]
  65. (block-cp repo idx block)]))
  66. (rum/defc search-title < rum/reactive
  67. [*input]
  68. (let [input (rum/react *input)
  69. input' (if (string/blank? input) "Blank input" input)]
  70. [:span.overflow-hidden.text-ellipsis input']))
  71. (rum/defc sidebar-search
  72. [repo block-type init-key input *input]
  73. (rum/with-key
  74. (cmdk/cmdk-block {:initial-input input
  75. :sidebar? true
  76. :on-input-change (fn [new-value]
  77. (reset! *input new-value))
  78. :on-input-blur (fn [new-value]
  79. (state/sidebar-replace-block! [repo input block-type]
  80. [repo new-value block-type]))})
  81. (str init-key)))
  82. (defn- <build-sidebar-item
  83. [repo idx db-id block-type *db-id init-key]
  84. (->
  85. (p/do!
  86. (when-not (contains? #{:contents :search} block-type)
  87. (db-async/<get-block repo db-id))
  88. (let [lookup (cond
  89. (integer? db-id) db-id
  90. (uuid? db-id) [:block/uuid db-id]
  91. :else nil)
  92. entity (when lookup (db/entity repo lookup))
  93. page? (ldb/page? entity)
  94. block-render (fn []
  95. (when entity
  96. (if page?
  97. [[:.flex.items-center.page-title.gap-1
  98. (icon/get-node-icon-cp entity {:class "text-md"})
  99. [:span.overflow-hidden.text-ellipsis (:block/title entity)]]
  100. (page-cp repo (str (:block/uuid entity)))]
  101. (block-with-breadcrumb repo entity idx [repo db-id block-type] false))))]
  102. (case (keyword block-type)
  103. :contents
  104. (when-let [page (db/get-page "Contents")]
  105. [[:.flex.items-center (ui/icon "list-details" {:class "text-md mr-2"}) (t :right-side-bar/contents)]
  106. (page-cp repo (str (:block/uuid page)))])
  107. :help
  108. [[:.flex.items-center (ui/icon "help" {:class "text-md mr-2"}) (t :right-side-bar/help)] (onboarding/help)]
  109. :page-graph
  110. [[:.flex.items-center (ui/icon "hierarchy" {:class "text-md mr-2"}) (t :right-side-bar/page-graph)]
  111. (page/page-graph)]
  112. :block-ref
  113. (let [lookup (if (integer? db-id) db-id [:block/uuid db-id])]
  114. (when-let [block (db/entity repo lookup)]
  115. [(t :right-side-bar/block-ref)
  116. (block-with-breadcrumb repo block idx [repo db-id block-type] true)]))
  117. :block
  118. (block-render)
  119. :page
  120. (block-render)
  121. :search
  122. [[:.flex.items-center.page-title
  123. (ui/icon "search" {:class "text-md mr-2"})
  124. (search-title *db-id)]
  125. (sidebar-search repo block-type init-key db-id *db-id)]
  126. :shortcut-settings
  127. [[:.flex.items-center (ui/icon "command" {:class "text-md mr-2"}) (t :help/shortcuts)]
  128. (shortcut-settings)]
  129. :rtc
  130. [[:.flex.items-center (ui/icon "cloud" {:class "text-md mr-2"}) "(Dev) RTC"]
  131. (rtc-debug-ui/rtc-debug-ui)]
  132. :profiler
  133. [[:.flex.items-center (ui/icon "cloud" {:class "text-md mr-2"}) "(Dev) Profiler"]
  134. (profiler/profiler)]
  135. ["" [:span]])))
  136. (p/catch (fn [error]
  137. (js/console.error error)))))
  138. (defonce *drag-to
  139. (atom nil))
  140. (defonce *drag-from
  141. (atom nil))
  142. (rum/defc actions-menu-content
  143. [db-id idx type collapsed? block-count]
  144. (let [multi-items? (> block-count 1)
  145. menu-item shui/dropdown-menu-item
  146. block (when (integer? db-id) (db/entity db-id))
  147. page? (or (contains? #{:page :contents} type) (ldb/page? block))]
  148. [:<>
  149. (menu-item {:on-click #(state/sidebar-remove-block! idx)} (t :right-side-bar/pane-close))
  150. (when multi-items? (menu-item {:on-click #(state/sidebar-remove-rest! db-id)} (t :right-side-bar/pane-close-others)))
  151. (when multi-items? (menu-item {:on-click (fn []
  152. (state/clear-sidebar-blocks!)
  153. (state/hide-right-sidebar!))} (t :right-side-bar/pane-close-all)))
  154. (when (and (not collapsed?) multi-items?) [:hr.menu-separator])
  155. (when-not collapsed? (menu-item {:on-click #(state/sidebar-block-toggle-collapse! db-id)} (t :right-side-bar/pane-collapse)))
  156. (when multi-items? (menu-item {:on-click #(state/sidebar-block-collapse-rest! db-id)} (t :right-side-bar/pane-collapse-others)))
  157. (when multi-items? (menu-item {:on-click #(state/sidebar-block-set-collapsed-all! true)} (t :right-side-bar/pane-collapse-all)))
  158. (when (and collapsed? multi-items?) [:hr.menu-separator])
  159. (when collapsed? (menu-item {:on-click #(state/sidebar-block-toggle-collapse! db-id)} (t :right-side-bar/pane-expand)))
  160. (when multi-items? (menu-item {:on-click #(state/sidebar-block-set-collapsed-all! false)} (t :right-side-bar/pane-expand-all)))
  161. (when page? [:hr.menu-separator])
  162. (when page?
  163. (menu-item {:on-click (fn [] (route-handler/redirect-to-page! (:block/uuid block)))}
  164. (t :right-side-bar/pane-open-as-page)))]))
  165. (rum/defc drop-indicator
  166. [idx drag-to]
  167. [:.sidebar-drop-indicator {:on-drag-enter #(when drag-to (reset! *drag-to idx))
  168. :on-drag-over util/stop
  169. :class (when (= idx drag-to) "drag-over")}])
  170. (rum/defc drop-area
  171. [idx]
  172. [:.sidebar-item-drop-area
  173. {:on-drag-over util/stop}
  174. [:.sidebar-item-drop-area-overlay.top
  175. {:on-drag-enter #(reset! *drag-to (dec idx))}]
  176. [:.sidebar-item-drop-area-overlay.bottom
  177. {:on-drag-enter #(reset! *drag-to idx)}]])
  178. (rum/defc inner-component <
  179. {:should-update (fn [_prev-state state] (last (:rum/args state)))}
  180. [component _should-update?]
  181. component)
  182. (rum/defc sidebar-item-inner
  183. [db-id {:keys [repo idx block-type collapsed? drag-from drag-to block-count *db-id init-key]}]
  184. (let [[item set-item!] (hooks/use-state nil)]
  185. (hooks/use-effect!
  186. (fn []
  187. (p/let [item (<build-sidebar-item repo idx db-id block-type *db-id init-key)]
  188. (set-item! item)))
  189. [])
  190. (when item
  191. [:<>
  192. (when (zero? idx) (drop-indicator (dec idx) drag-to))
  193. [:div.flex.sidebar-item.content.color-level.rounded-md.shadow-lg
  194. {:class [(str "item-type-" (name block-type))
  195. (when collapsed? "collapsed")]}
  196. (let [[title component] item]
  197. [:div.flex.flex-col.w-full.relative
  198. [:.flex.flex-row.justify-between.sidebar-item-header.color-level.rounded-t-md
  199. {:class (when collapsed? "rounded-b-md")
  200. :draggable true
  201. :on-context-menu (fn [e]
  202. (util/stop e)
  203. (shui/popup-show! e
  204. (actions-menu-content db-id idx block-type collapsed? block-count)
  205. {:as-dropdown? true
  206. :content-props {:on-click (fn [] (shui/popup-hide!))}}))
  207. :on-drag-start (fn [event]
  208. (editor-handler/block->data-transfer! (:block/name (db/entity db-id)) event true)
  209. (reset! *drag-from idx))
  210. :on-drag-end (fn [_event]
  211. (when drag-to (state/sidebar-move-block! idx drag-to))
  212. (reset! *drag-to nil)
  213. (reset! *drag-from nil))
  214. :on-pointer-up (fn [event]
  215. (when (= (.-which (.-nativeEvent event)) 2)
  216. (state/sidebar-remove-block! idx)))}
  217. [:button.flex.flex-row.px-2.items-center.w-full.overflow-hidden
  218. {:aria-expanded (str (not collapsed?))
  219. :id (str "sidebar-panel-header-" idx)
  220. :aria-controls (str "sidebar-panel-content-" idx)
  221. :on-click (fn [event]
  222. (util/stop event)
  223. (state/sidebar-block-toggle-collapse! db-id))}
  224. [:span.opacity-50.hover:opacity-100.flex.items-center.pr-1
  225. (ui/rotating-arrow collapsed?)]
  226. [:div.ml-1.font-medium.text-sm.overflow-hidden.whitespace-nowrap
  227. title]]
  228. [:.item-actions.flex.items-center
  229. (shui/button
  230. {:title (t :right-side-bar/pane-more)
  231. :class "px-2 py-2 h-8 w-8 text-muted-foreground"
  232. :variant :ghost
  233. :on-click #(shui/popup-show!
  234. (.-target %)
  235. (actions-menu-content db-id idx block-type collapsed? block-count)
  236. {:as-dropdown? true
  237. :content-props {:on-click (fn [] (shui/popup-hide!))}})}
  238. (ui/icon "dots"))
  239. (shui/button
  240. {:title (t :right-side-bar/pane-close)
  241. :variant :ghost
  242. :class "px-2 py-2 h-8 w-8 text-muted-foreground"
  243. :on-click #(state/sidebar-remove-block! idx)}
  244. (ui/icon "x"))]]
  245. [:div {:role "region"
  246. :id (str "sidebar-panel-content-" idx)
  247. :aria-labelledby (str "sidebar-panel-header-" idx)
  248. :class (util/classnames [{:hidden collapsed?
  249. :initial (not collapsed?)
  250. :sidebar-panel-content true
  251. :px-2 (not (contains? #{:search :shortcut-settings} block-type))}])}
  252. (inner-component component (not drag-from))]
  253. (when drag-from (drop-area idx))])]
  254. (drop-indicator idx drag-to)])))
  255. (rum/defcs sidebar-item < rum/reactive
  256. {:init (fn [state] (assoc state
  257. ::db-id (atom (nth (:rum/args state) 2))
  258. ::init-key (random-uuid)))}
  259. [state repo idx db-id block-type block-count]
  260. (let [drag-from (rum/react *drag-from)
  261. drag-to (rum/react *drag-to)]
  262. (let [collapsed? (state/sub [:ui/sidebar-collapsed-blocks db-id])]
  263. (sidebar-item-inner db-id {:repo repo
  264. :idx idx
  265. :block-type block-type
  266. :collapsed? collapsed?
  267. :drag-from drag-from
  268. :drag-to drag-to
  269. :block-count block-count
  270. :*db-id (::db-id state)
  271. :init-key (::init-key state)}))))
  272. (defn- get-page
  273. [match]
  274. (let [route-name (get-in match [:data :name])
  275. page (case route-name
  276. :page
  277. (get-in match [:path-params :name])
  278. :file
  279. (get-in match [:path-params :path])
  280. (date/journal-name))]
  281. (when page
  282. (string/lower-case page))))
  283. (defn get-current-page
  284. []
  285. (let [match (:route-match @state/state)]
  286. (get-page match)))
  287. (rum/defc sidebar-resizer
  288. [sidebar-open? sidebar-id handler-position]
  289. (let [el-ref (rum/use-ref nil)
  290. min-px-width 320 ; Custom window controls width
  291. min-ratio 0.1
  292. max-ratio 0.7
  293. keyboard-step 5
  294. add-resizing-class #(.. js/document.documentElement -classList (add "is-resizing-buf"))
  295. remove-resizing-class (fn []
  296. (.. js/document.documentElement -classList (remove "is-resizing-buf"))
  297. (reset! ui-handler/*right-sidebar-resized-at (js/Date.now)))
  298. set-width! (fn [ratio]
  299. (when el-ref
  300. (let [value (* ratio 100)
  301. width (str value "%")]
  302. (.setAttribute (rum/deref el-ref) "aria-valuenow" value)
  303. (ui-handler/persist-right-sidebar-width! width))))]
  304. (hooks/use-effect!
  305. (fn []
  306. (when-let [el (and (fn? js/window.interact) (rum/deref el-ref))]
  307. (-> (js/interact el)
  308. (.draggable
  309. (bean/->js
  310. {:listeners
  311. {:move
  312. (fn [^js/MouseEvent e]
  313. (let [width js/document.documentElement.clientWidth
  314. min-ratio (max min-ratio (/ min-px-width width))
  315. sidebar-el (js/document.getElementById sidebar-id)
  316. offset (.-pageX e)
  317. ratio (.toFixed (/ offset width) 6)
  318. ratio (if (= handler-position :west) (- 1 ratio) ratio)
  319. cursor-class (str "cursor-" (first (name handler-position)) "-resize")]
  320. (if (= (.getAttribute el "data-expanded") "true")
  321. (cond
  322. (< ratio (/ min-ratio 2))
  323. (state/hide-right-sidebar!)
  324. (< ratio min-ratio)
  325. (.. js/document.documentElement -classList (add cursor-class))
  326. (and (< ratio max-ratio) sidebar-el)
  327. (when sidebar-el
  328. (#(.. js/document.documentElement -classList (remove cursor-class))
  329. (set-width! ratio)))
  330. :else
  331. #(.. js/document.documentElement -classList (remove cursor-class)))
  332. (when (> ratio (/ min-ratio 2)) (state/open-right-sidebar!)))))}}))
  333. (.styleCursor false)
  334. (.on "dragstart" add-resizing-class)
  335. (.on "dragend" remove-resizing-class)
  336. (.on "keydown" (fn [e]
  337. (when-let [sidebar-el (js/document.getElementById sidebar-id)]
  338. (let [width js/document.documentElement.clientWidth
  339. min-ratio (max min-ratio (/ min-px-width width))
  340. keyboard-step (case (.-code e)
  341. "ArrowLeft" (- keyboard-step)
  342. "ArrowRight" keyboard-step
  343. 0)
  344. offset (+ (.-x (.getBoundingClientRect sidebar-el)) keyboard-step)
  345. ratio (.toFixed (/ offset width) 6)
  346. ratio (if (= handler-position :west) (- 1 ratio) ratio)]
  347. (when (and (> ratio min-ratio) (< ratio max-ratio) (not (zero? keyboard-step)))
  348. (do (add-resizing-class)
  349. (set-width! ratio)))))))
  350. (.on "keyup" remove-resizing-class)))
  351. #())
  352. [])
  353. (hooks/use-effect!
  354. (fn []
  355. ;; sidebar animation duration
  356. (js/setTimeout
  357. #(reset! ui-handler/*right-sidebar-resized-at (js/Date.now)) 300))
  358. [sidebar-open?])
  359. [:.resizer
  360. {:ref el-ref
  361. :role "separator"
  362. :aria-orientation "vertical"
  363. :aria-label (t :right-side-bar/separator)
  364. :aria-valuemin (* min-ratio 100)
  365. :aria-valuemax (* max-ratio 100)
  366. :aria-valuenow 50
  367. :tabIndex "0"
  368. :data-expanded sidebar-open?}]))
  369. (rum/defcs sidebar-inner <
  370. (rum/local false ::anim-finished?)
  371. {:will-mount (fn [state]
  372. (js/setTimeout (fn [] (reset! (get state ::anim-finished?) true)) 300)
  373. state)}
  374. [state repo t blocks]
  375. (let [*anim-finished? (get state ::anim-finished?)
  376. block-count (count blocks)]
  377. [:div.cp__right-sidebar-inner.flex.flex-col.h-full#right-sidebar-container
  378. [:div.cp__right-sidebar-scrollable
  379. {:on-drag-over util/stop}
  380. [:div.cp__right-sidebar-topbar.flex.flex-row.justify-between.items-center
  381. [:div.cp__right-sidebar-settings.hide-scrollbar.gap-1 {:key "right-sidebar-settings"}
  382. [:div.text-sm
  383. [:button.button.cp__right-sidebar-settings-btn {:on-click (fn [_e]
  384. (state/sidebar-add-block! repo "contents" :contents))}
  385. (t :right-side-bar/contents)]]
  386. [:div.text-sm
  387. [:button.button.cp__right-sidebar-settings-btn {:on-click (fn []
  388. (when-let [page (get-current-page)]
  389. (state/sidebar-add-block!
  390. repo
  391. page
  392. :page-graph)))}
  393. (t :right-side-bar/page-graph)]]
  394. [:div.text-sm
  395. [:button.button.cp__right-sidebar-settings-btn {:on-click (fn [_e]
  396. (state/sidebar-add-block! repo "help" :help))}
  397. (t :right-side-bar/help)]]
  398. (when (and (state/sub [:ui/developer-mode?]) (not config/publishing?))
  399. [:div.text-sm
  400. [:button.button.cp__right-sidebar-settings-btn {:on-click (fn [_e]
  401. (state/sidebar-add-block! repo "rtc" :rtc))}
  402. "(Dev) RTC"]])
  403. (when (state/sub [:ui/developer-mode?])
  404. [:div.text-sm
  405. [:button.button.cp__right-sidebar-settings-btn {:on-click (fn [_e]
  406. (state/sidebar-add-block! repo "profiler" :profiler))}
  407. "(Dev) Profiler"]])]]
  408. [:.sidebar-item-list.flex-1.scrollbar-spacing.px-2
  409. (if @*anim-finished?
  410. (for [[idx [repo db-id block-type]] (medley/indexed blocks)]
  411. (rum/with-key
  412. (sidebar-item repo idx db-id block-type block-count)
  413. (str "sidebar-block-" db-id)))
  414. [:div.p-4
  415. [:span.font-medium.opacity-50 "Loading ..."]])]]]))
  416. (rum/defcs sidebar < rum/reactive
  417. [state]
  418. (let [blocks (state/sub-right-sidebar-blocks)
  419. blocks (if (empty? blocks)
  420. [[(state/get-current-repo) "contents" :contents nil]]
  421. blocks)
  422. sidebar-open? (state/sub :ui/sidebar-open?)
  423. width (state/sub :ui/sidebar-width)
  424. repo (state/sub :git/current-repo)]
  425. [:div#right-sidebar.cp__right-sidebar.h-screen
  426. {:class (if sidebar-open? "open" "closed")
  427. :style {:width width}}
  428. (sidebar-resizer sidebar-open? "right-sidebar" :west)
  429. (when sidebar-open?
  430. (sidebar-inner repo t blocks))]))