right_sidebar.cljs 20 KB

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