right_sidebar.cljs 22 KB

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