right_sidebar.cljs 19 KB

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