ui.cljs 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169
  1. (ns frontend.ui
  2. "Main ns for reusable components"
  3. (:require ["@logseq/react-tweet-embed" :as react-tweet-embed]
  4. ["react-intersection-observer" :as react-intersection-observer]
  5. ["react-resize-context" :as Resize]
  6. ["react-textarea-autosize" :as TextareaAutosize]
  7. ["react-tippy" :as react-tippy]
  8. ["react-transition-group" :refer [CSSTransition TransitionGroup]]
  9. [camel-snake-kebab.core :as csk]
  10. [cljs-bean.core :as bean]
  11. [clojure.string :as string]
  12. [datascript.core :as d]
  13. [electron.ipc :as ipc]
  14. [frontend.components.svg :as svg]
  15. [frontend.config :as config]
  16. [frontend.context.i18n :refer [t]]
  17. [frontend.db-mixins :as db-mixins]
  18. [frontend.handler.notification :as notification]
  19. [frontend.handler.plugin :as plugin-handler]
  20. [frontend.mixins :as mixins]
  21. [frontend.mobile.util :as mobile-util]
  22. [frontend.modules.shortcut.config :as shortcut-config]
  23. [frontend.modules.shortcut.core :as shortcut]
  24. [frontend.modules.shortcut.data-helper :as shortcut-helper]
  25. [frontend.rum :as r]
  26. [frontend.state :as state]
  27. [frontend.storage :as storage]
  28. [frontend.ui.date-picker]
  29. [frontend.util :as util]
  30. [frontend.util.cursor :as cursor]
  31. [goog.dom :as gdom]
  32. [goog.functions :refer [debounce]]
  33. [goog.object :as gobj]
  34. [lambdaisland.glogi :as log]
  35. [medley.core :as medley]
  36. [promesa.core :as p]
  37. [rum.core :as rum]))
  38. (declare icon)
  39. (defonce transition-group (r/adapt-class TransitionGroup))
  40. (defonce css-transition (r/adapt-class CSSTransition))
  41. (defonce textarea (r/adapt-class (gobj/get TextareaAutosize "default")))
  42. (def resize-provider (r/adapt-class (gobj/get Resize "ResizeProvider")))
  43. (def resize-consumer (r/adapt-class (gobj/get Resize "ResizeConsumer")))
  44. (def Tippy (r/adapt-class (gobj/get react-tippy "Tooltip")))
  45. (def ReactTweetEmbed (r/adapt-class react-tweet-embed))
  46. (def useInView (gobj/get react-intersection-observer "useInView"))
  47. (defn reset-ios-whole-page-offset!
  48. []
  49. (and (util/ios?)
  50. (util/safari?)
  51. (js/window.scrollTo 0 0)))
  52. (defonce icon-size (if (mobile-util/native-platform?) 26 20))
  53. (def block-background-colors
  54. ["yellow"
  55. "red"
  56. "pink"
  57. "green"
  58. "blue"
  59. "purple"
  60. "gray"])
  61. (rum/defc menu-background-color
  62. [add-bgcolor-fn rm-bgcolor-fn]
  63. [:div.flex.flex-row.justify-between.py-1.px-2.items-center
  64. [:div.flex.flex-row.justify-between.flex-1.mx-2.mt-2
  65. (for [color block-background-colors]
  66. [:a
  67. {:title (t (keyword "color" color))
  68. :on-click #(add-bgcolor-fn color)}
  69. [:div.heading-bg {:style {:background-color (str "var(--color-" color "-500)")}}]])
  70. [:a
  71. {:title (t :remove-background)
  72. :on-click rm-bgcolor-fn}
  73. [:div.heading-bg.remove "-"]]]])
  74. (rum/defc ls-textarea
  75. < rum/reactive
  76. {:did-mount (fn [state]
  77. (let [^js el (rum/dom-node state)]
  78. ;; Passing aria-label as a prop to TextareaAutosize removes the dash
  79. (.setAttribute el "aria-label" "editing block")
  80. (. el addEventListener "mouseup"
  81. #(let [start (util/get-selection-start el)
  82. end (util/get-selection-end el)]
  83. (when (and start end)
  84. (when-let [e (and (not= start end)
  85. {:caret (cursor/get-caret-pos el)
  86. :start start :end end
  87. :text (. (.-value el) substring start end)
  88. :point {:x (.-x %) :y (.-y %)}})]
  89. (plugin-handler/hook-plugin-editor :input-selection-end (bean/->js e)))))))
  90. state)}
  91. [{:keys [on-change] :as props}]
  92. (let [skip-composition? (state/sub :editor/action)
  93. on-composition (fn [e]
  94. (if skip-composition?
  95. (on-change e)
  96. (case e.type
  97. "compositionend" (do
  98. (state/set-editor-in-composition! false)
  99. (on-change e))
  100. (state/set-editor-in-composition! true))))
  101. props (assoc props
  102. :on-change (fn [e] (when-not (state/editor-in-composition?)
  103. (on-change e)))
  104. :on-composition-start on-composition
  105. :on-composition-update on-composition
  106. :on-composition-end on-composition)]
  107. (textarea props)))
  108. (rum/defc dropdown-content-wrapper
  109. < {:did-mount (fn [state]
  110. (let [k (inc (count (state/sub :modal/dropdowns)))
  111. args (:rum/args state)]
  112. (state/set-state! [:modal/dropdowns k] (second args))
  113. (assoc state ::k k)))
  114. :will-unmount (fn [state]
  115. (state/update-state! :modal/dropdowns #(dissoc % (::k state)))
  116. state)}
  117. [dropdown-state _close-fn content class style-opts]
  118. (let [class (or class
  119. (util/hiccup->class "origin-top-right.absolute.right-0.mt-2"))]
  120. [:div.dropdown-wrapper
  121. {:style style-opts
  122. :class (str class " "
  123. (case dropdown-state
  124. "entering" "transition ease-out duration-100 transform opacity-0 scale-95"
  125. "entered" "transition ease-out duration-100 transform opacity-100 scale-100"
  126. "exiting" "transition ease-in duration-75 transform opacity-100 scale-100"
  127. "exited" "transition ease-in duration-75 transform opacity-0 scale-95"))}
  128. content]))
  129. ;; public exports
  130. (rum/defcs dropdown < (mixins/modal :open?)
  131. [state content-fn modal-content-fn
  132. & [{:keys [modal-class z-index trigger-class]
  133. :or {z-index 999}}]]
  134. (let [{:keys [open?]} state
  135. modal-content (modal-content-fn state)
  136. close-fn (:close-fn state)]
  137. [:div.relative.ui__dropdown-trigger {:class trigger-class}
  138. (content-fn state)
  139. (css-transition
  140. {:in @open? :timeout 0}
  141. (fn [dropdown-state]
  142. (when @open?
  143. (dropdown-content-wrapper dropdown-state close-fn modal-content modal-class {:z-index z-index}))))]))
  144. ;; `sequence` can be a list of symbols, a list of strings, or a string
  145. (defn render-keyboard-shortcut [sequence]
  146. (let [sequence (if (string? sequence)
  147. (-> sequence ;; turn string into sequence
  148. (string/trim)
  149. (string/lower-case)
  150. (string/split #" |\+"))
  151. sequence)]
  152. [:span.keyboard-shortcut
  153. (map-indexed (fn [i key]
  154. [:code {:key i}
  155. ;; Display "cmd" rather than "meta" to the user to describe the Mac
  156. ;; mod key, because that's what the Mac keyboards actually say.
  157. (if (or (= :meta key) (= "meta" key))
  158. (util/meta-key-name)
  159. (name key))])
  160. sequence)]))
  161. (rum/defc menu-link
  162. [options child shortcut]
  163. (if (:only-child? options)
  164. [:div.menu-link
  165. (dissoc options :only-child?) child]
  166. [:a.flex.justify-between.px-4.py-2.text-sm.transition.ease-in-out.duration-150.cursor.menu-link
  167. options
  168. [:span.flex-1 child]
  169. (when shortcut
  170. [:span.ml-1 (render-keyboard-shortcut shortcut)])]))
  171. (rum/defc dropdown-with-links
  172. [content-fn links
  173. {:keys [outer-header outer-footer links-header links-footer] :as opts}]
  174. (dropdown
  175. content-fn
  176. (fn [{:keys [close-fn]}]
  177. (let [links-children
  178. (let [links (if (fn? links) (links) links)
  179. links (remove nil? links)]
  180. (for [{:keys [options title icon key hr hover-detail item _as-link?]} links]
  181. (let [new-options
  182. (merge options
  183. (cond->
  184. {:title hover-detail
  185. :on-click (fn [e]
  186. (when-not (false? (when-let [on-click-fn (:on-click options)]
  187. (on-click-fn e)))
  188. (close-fn)))}
  189. key
  190. (assoc :key key)))
  191. child (if hr
  192. nil
  193. (or item
  194. [:div.flex.items-center
  195. (when icon icon)
  196. [:div.title-wrap {:style {:margin-right "8px"
  197. :margin-left "4px"}} title]]))]
  198. (if hr
  199. [:hr.menu-separator {:key "dropdown-hr"}]
  200. (rum/with-key
  201. (menu-link new-options child nil)
  202. title)))))
  203. wrapper-children
  204. [:.menu-links-wrapper
  205. (when links-header links-header)
  206. links-children
  207. (when links-footer links-footer)]]
  208. (if (or outer-header outer-footer)
  209. [:.menu-links-outer
  210. outer-header wrapper-children outer-footer]
  211. wrapper-children)))
  212. opts))
  213. (rum/defc notification-content
  214. [state content status uid]
  215. (when (and content status)
  216. (let [svg
  217. (if (keyword? status)
  218. (case status
  219. :success
  220. (icon "circle-check" {:class "text-success" :size "20"})
  221. :warning
  222. (icon "alert-circle" {:class "text-warning" :size "20"})
  223. :error
  224. (icon "circle-x" {:class "text-error" :size "20"})
  225. (icon "info-circle" {:class "text-indigo-500" :size "20"}))
  226. status)]
  227. [:div.ui__notifications-content
  228. {:style
  229. (when (or (= state "exiting")
  230. (= state "exited"))
  231. {:z-index -1})}
  232. [:div.max-w-sm.w-full.shadow-lg.rounded-lg.pointer-events-auto.notification-area
  233. {:class (case state
  234. "entering" "transition ease-out duration-300 transform opacity-0 translate-y-2 sm:translate-x-0"
  235. "entered" "transition ease-out duration-300 transform translate-y-0 opacity-100 sm:translate-x-0"
  236. "exiting" "transition ease-in duration-100 opacity-100"
  237. "exited" "transition ease-in duration-100 opacity-0")}
  238. [:div.rounded-lg.shadow-xs {:style {:max-height "calc(100vh - 200px)"
  239. :overflow-y "auto"
  240. :overflow-x "hidden"}}
  241. [:div.p-4
  242. [:div.flex.items-start
  243. [:div.flex-shrink-0
  244. svg]
  245. [:div.ml-3.w-0.flex-1
  246. [:div.text-sm.leading-5.font-medium.whitespace-pre-line {:style {:margin 0}}
  247. content]]
  248. [:div.ml-4.flex-shrink-0.flex
  249. [:button.inline-flex.text-gray-400.focus:outline-none.focus:text-gray-500.transition.ease-in-out.duration-150.notification-close-button
  250. {:aria-label "Close"
  251. :on-click (fn []
  252. (notification/clear! uid))}
  253. (icon "x" {:fill "currentColor"})]]]]]]])))
  254. (declare button)
  255. (rum/defc notification-clear-all
  256. []
  257. [:div.ui__notifications-content
  258. [:div.pointer-events-auto
  259. (button (t :notification/clear-all)
  260. :intent "logseq"
  261. :on-click (fn []
  262. (notification/clear-all!)))]])
  263. (rum/defc notification < rum/reactive
  264. []
  265. (let [contents (state/sub :notification/contents)]
  266. (transition-group
  267. {:class-name "notifications ui__notifications"}
  268. (let [notifications (map (fn [el]
  269. (let [k (first el)
  270. v (second el)]
  271. (css-transition
  272. {:timeout 100
  273. :key (name k)}
  274. (fn [state]
  275. (notification-content state (:content v) (:status v) k)))))
  276. contents)
  277. clear-all (when (> (count contents) 1)
  278. (css-transition
  279. {:timeout 100
  280. :k "clear-all"}
  281. (fn [_state]
  282. (notification-clear-all))))
  283. items (if clear-all (cons clear-all notifications) notifications)]
  284. (doall items)))))
  285. (rum/defc humanity-time-ago
  286. [input opts]
  287. (let [time-fn (fn []
  288. (try
  289. (util/time-ago input)
  290. (catch :default e
  291. (js/console.error e)
  292. input)))
  293. [time set-time] (rum/use-state (time-fn))]
  294. (rum/use-effect!
  295. (fn []
  296. (let [timer (js/setInterval
  297. #(set-time (time-fn)) (* 1000 30))]
  298. #(js/clearInterval timer)))
  299. [])
  300. [:span.ui__humanity-time (merge {} opts) time]))
  301. (defn checkbox
  302. [option]
  303. [:input.form-checkbox.h-4.w-4.transition.duration-150.ease-in-out
  304. (merge {:type "checkbox"} option)])
  305. (defn main-node
  306. []
  307. (gdom/getElement "main-content-container"))
  308. (defn focus-element
  309. [element]
  310. (when-let [element ^js (gdom/getElement element)]
  311. (.focus element)))
  312. (defn get-scroll-top []
  313. (.-scrollTop (main-node)))
  314. (defn get-dynamic-style-node
  315. []
  316. (js/document.getElementById "dynamic-style-scope"))
  317. (defn inject-document-devices-envs!
  318. []
  319. (let [^js cl (.-classList js/document.documentElement)]
  320. (when config/publishing? (.add cl "is-publish-mode"))
  321. (when util/mac? (.add cl "is-mac"))
  322. (when util/win32? (.add cl "is-win32"))
  323. (when util/linux? (.add cl "is-linux"))
  324. (when (util/electron?) (.add cl "is-electron"))
  325. (when (util/ios?) (.add cl "is-ios"))
  326. (when (util/mobile?) (.add cl "is-mobile"))
  327. (when (util/safari?) (.add cl "is-safari"))
  328. (when (mobile-util/native-ios?) (.add cl "is-native-ios"))
  329. (when (mobile-util/native-android?) (.add cl "is-native-android"))
  330. (when (mobile-util/native-iphone?) (.add cl "is-native-iphone"))
  331. (when (mobile-util/native-iphone-without-notch?) (.add cl "is-native-iphone-without-notch"))
  332. (when (mobile-util/native-ipad?) (.add cl "is-native-ipad"))
  333. (when (util/electron?)
  334. (doseq [[event function]
  335. [["persist-zoom-level" #(storage/set :zoom-level %)]
  336. ["restore-zoom-level" #(when-let [zoom-level (storage/get :zoom-level)] (js/window.apis.setZoomLevel zoom-level))]
  337. ["full-screen" #(do (js-invoke cl (if (= % "enter") "add" "remove") "is-fullscreen")
  338. (state/set-state! :electron/window-fullscreen? (= % "enter")))]
  339. ["maximize" #(state/set-state! :electron/window-maximized? %)]]]
  340. (.on js/window.apis event function))
  341. (p/then (ipc/ipc :getAppBaseInfo) #(let [{:keys [isFullScreen isMaximized]} (js->clj % :keywordize-keys true)]
  342. (when isFullScreen ((.add cl "is-fullscreen")
  343. (state/set-state! :electron/window-fullscreen? true)))
  344. (when isMaximized (state/set-state! :electron/window-maximized? true)))))))
  345. (defn inject-dynamic-style-node!
  346. []
  347. (let [style (get-dynamic-style-node)]
  348. (if (nil? style)
  349. (let [node (js/document.createElement "style")]
  350. (set! (.-id node) "dynamic-style-scope")
  351. (.appendChild js/document.head node))
  352. style)))
  353. (defn apply-custom-theme-effect! [theme]
  354. (when config/lsp-enabled?
  355. (when-let [custom-theme (state/sub [:ui/custom-theme (keyword theme)])]
  356. ;; If the name is nil, the user has not set a custom theme (initially {:mode light/dark}).
  357. ;; The url is not used because the default theme does not have an url.
  358. (if (some? (:name custom-theme))
  359. (js/LSPluginCore.selectTheme (bean/->js custom-theme)
  360. (bean/->js {:emit false}))
  361. (state/set-state! :plugin/selected-theme (:url custom-theme))))))
  362. (defn setup-system-theme-effect!
  363. []
  364. (let [^js schemaMedia (js/window.matchMedia "(prefers-color-scheme: dark)")]
  365. (try (.addEventListener schemaMedia "change" state/sync-system-theme!)
  366. (catch :default _error
  367. (.addListener schemaMedia state/sync-system-theme!)))
  368. (state/sync-system-theme!)
  369. #(try (.removeEventListener schemaMedia "change" state/sync-system-theme!)
  370. (catch :default _error
  371. (.removeListener schemaMedia state/sync-system-theme!)))))
  372. (defn set-global-active-keystroke [val]
  373. (.setAttribute js/document.body "data-active-keystroke" val))
  374. (defn setup-active-keystroke! []
  375. (let [active-keystroke (atom #{})
  376. heads #{:shift :alt :meta :control}
  377. handle-global-keystroke (fn [down? e]
  378. (let [handler (if down? conj disj)
  379. keystroke e.key]
  380. (swap! active-keystroke handler keystroke))
  381. (when (contains? heads (keyword (util/safe-lower-case e.key)))
  382. (set-global-active-keystroke (string/join "+" @active-keystroke))))
  383. keydown-handler (partial handle-global-keystroke true)
  384. keyup-handler (partial handle-global-keystroke false)
  385. clear-all #(do (set-global-active-keystroke "")
  386. (reset! active-keystroke #{}))]
  387. (.addEventListener js/window "keydown" keydown-handler)
  388. (.addEventListener js/window "keyup" keyup-handler)
  389. (.addEventListener js/window "blur" clear-all)
  390. (.addEventListener js/window "visibilitychange" clear-all)
  391. (fn []
  392. (.removeEventListener js/window "keydown" keydown-handler)
  393. (.removeEventListener js/window "keyup" keyup-handler)
  394. (.removeEventListener js/window "blur" clear-all)
  395. (.removeEventListener js/window "visibilitychange" clear-all))))
  396. (defn setup-viewport-listeners! []
  397. (when-let [^js vw (gobj/get js/window "visualViewport")]
  398. (let [handler #(state/set-state! :ui/viewport {:width (.-width vw) :height (.-height vw) :scale (.-scale vw)})]
  399. (.addEventListener js/window.visualViewport "resize" handler)
  400. (handler)
  401. #(.removeEventListener js/window.visualViewport "resize" handler))))
  402. (defonce last-scroll-top (atom 0))
  403. (defn scroll-down?
  404. []
  405. (let [scroll-top (get-scroll-top)
  406. down? (>= scroll-top @last-scroll-top)]
  407. (reset! last-scroll-top scroll-top)
  408. down?))
  409. (defn bottom-reached?
  410. [node threshold]
  411. (let [full-height (gobj/get node "scrollHeight")
  412. scroll-top (gobj/get node "scrollTop")
  413. client-height (gobj/get node "clientHeight")]
  414. (<= (- full-height scroll-top client-height) threshold)))
  415. (defn on-scroll
  416. [node {:keys [on-load on-top-reached threshold bottom-reached]
  417. :or {threshold 500}}]
  418. (let [scroll-top (gobj/get node "scrollTop")
  419. bottom-reached? (if (fn? bottom-reached)
  420. (bottom-reached)
  421. (bottom-reached? node threshold))
  422. top-reached? (= scroll-top 0)
  423. down? (scroll-down?)]
  424. (when (and bottom-reached? on-load)
  425. (on-load))
  426. (when (and (not down?) top-reached? on-top-reached)
  427. (on-top-reached))))
  428. (defn attach-listeners
  429. "Attach scroll and resize listeners."
  430. [state]
  431. (let [list-element-id (first (:rum/args state))
  432. opts (-> state :rum/args (nth 2))
  433. node (js/document.getElementById list-element-id)
  434. debounced-on-scroll (debounce #(on-scroll node opts) 100)]
  435. (mixins/listen state node :scroll debounced-on-scroll)))
  436. (rum/defcs infinite-list <
  437. (mixins/event-mixin attach-listeners)
  438. "Render an infinite list."
  439. [state _list-element-id body {:keys [on-load has-more more more-class]
  440. :or {more-class "text-sm"}}]
  441. [:div
  442. body
  443. (when has-more
  444. [:div.w-full.p-4
  445. [:a.fade-link.text-link.font-bold
  446. {:on-click on-load
  447. :class more-class}
  448. (or more (t :page/earlier))]])])
  449. (rum/defcs auto-complete <
  450. (rum/local 0 ::current-idx)
  451. (shortcut/mixin :shortcut.handler/auto-complete)
  452. [state
  453. matched
  454. {:keys [on-chosen
  455. on-shift-chosen
  456. get-group-name
  457. empty-placeholder
  458. item-render
  459. class
  460. header]}]
  461. (let [*current-idx (get state ::current-idx)]
  462. [:div#ui__ac {:class class}
  463. (if (seq matched)
  464. [:div#ui__ac-inner.hide-scrollbar
  465. (when header header)
  466. (for [[idx item] (medley/indexed matched)]
  467. [:<>
  468. {:key idx}
  469. (let [item-cp
  470. [:div.menu-link-wrap
  471. {:key idx
  472. ;; mouse-move event to indicate that cursor moved by user
  473. :on-mouse-move #(reset! *current-idx idx)}
  474. (let [chosen? (= @*current-idx idx)]
  475. (menu-link
  476. {:id (str "ac-" idx)
  477. :class (when chosen? "chosen")
  478. :on-mouse-down (fn [e]
  479. (util/stop e)
  480. (if (and (gobj/get e "shiftKey") on-shift-chosen)
  481. (on-shift-chosen item)
  482. (on-chosen item)))}
  483. (if item-render (item-render item chosen?) item) nil))]]
  484. (if get-group-name
  485. (if-let [group-name (get-group-name item)]
  486. [:div
  487. [:div.ui__ac-group-name group-name]
  488. item-cp]
  489. item-cp)
  490. item-cp))])]
  491. (when empty-placeholder
  492. empty-placeholder))]))
  493. (def datepicker frontend.ui.date-picker/date-picker)
  494. (defn toggle
  495. ([on? on-click] (toggle on? on-click false))
  496. ([on? on-click small?]
  497. [:a.ui__toggle {:on-click on-click
  498. :class (if small? "is-small" "")}
  499. [:span.wrapper.transition-colors.ease-in-out.duration-200
  500. {:aria-checked (if on? "true" "false"), :tab-index "0", :role "checkbox"
  501. :class (if on? "bg-indigo-600" "bg-gray-300")}
  502. [:span.switcher.transform.transition.ease-in-out.duration-200
  503. {:class (if on? (if small? "translate-x-4" "translate-x-5") "translate-x-0")
  504. :aria-hidden "true"}]]]))
  505. (defn keyboard-shortcut-from-config [shortcut-name]
  506. (let [default-binding (:binding (get shortcut-config/all-default-keyboard-shortcuts shortcut-name))
  507. custom-binding (when (state/shortcuts) (get (state/shortcuts) shortcut-name))
  508. binding (or custom-binding default-binding)]
  509. (shortcut-helper/decorate-binding binding)))
  510. (rum/defc modal-overlay
  511. [state close-fn close-backdrop?]
  512. [:div.ui__modal-overlay
  513. {:class (case state
  514. "entering" "ease-out duration-300 opacity-0"
  515. "entered" "ease-out duration-300 opacity-100"
  516. "exiting" "ease-in duration-200 opacity-100"
  517. "exited" "ease-in duration-200 opacity-0")
  518. :on-click #(when close-backdrop? (close-fn))}
  519. [:div.absolute.inset-0.opacity-75]])
  520. (rum/defc modal-panel-content <
  521. mixins/component-editing-mode
  522. [panel-content close-fn]
  523. (panel-content close-fn))
  524. (rum/defc modal-panel
  525. [show? panel-content transition-state close-fn fullscreen? close-btn?]
  526. [:div.ui__modal-panel.transform.transition-all.sm:min-w-lg.sm
  527. {:class (case transition-state
  528. "entering" "ease-out duration-300 opacity-0 translate-y-4 sm:translate-y-0 sm:scale-95"
  529. "entered" "ease-out duration-300 opacity-100 translate-y-0 sm:scale-100"
  530. "exiting" "ease-in duration-200 opacity-100 translate-y-0 sm:scale-100"
  531. "exited" "ease-in duration-200 opacity-0 translate-y-4 sm:translate-y-0 sm:scale-95")}
  532. [:div.ui__modal-close-wrap
  533. (when-not (false? close-btn?)
  534. [:a.ui__modal-close
  535. {:aria-label "Close"
  536. :type "button"
  537. :on-click close-fn}
  538. [:svg.h-6.w-6
  539. {:stroke "currentColor", :view-box "0 0 24 24", :fill "none"}
  540. [:path
  541. {:d "M6 18L18 6M6 6l12 12"
  542. :stroke-width "2"
  543. :stroke-linejoin "round"
  544. :stroke-linecap "round"}]]])]
  545. (when show?
  546. [:div {:class (if fullscreen? "" "panel-content")}
  547. (modal-panel-content panel-content close-fn)])])
  548. (rum/defc modal < rum/reactive
  549. (mixins/event-mixin
  550. (fn [state]
  551. (mixins/hide-when-esc-or-outside
  552. state
  553. :on-hide (fn []
  554. (some->
  555. (.querySelector (rum/dom-node state) "button.ui__modal-close")
  556. (.click)))
  557. :outside? false)
  558. (mixins/on-key-down
  559. state
  560. {;; enter
  561. 13 (fn [state _e]
  562. (some->
  563. (.querySelector (rum/dom-node state) "button.ui__modal-enter")
  564. (.click)))})))
  565. []
  566. (let [modal-panel-content (state/sub :modal/panel-content)
  567. fullscreen? (state/sub :modal/fullscreen?)
  568. close-btn? (state/sub :modal/close-btn?)
  569. close-backdrop? (state/sub :modal/close-backdrop?)
  570. show? (state/sub :modal/show?)
  571. label (state/sub :modal/label)
  572. close-fn (fn []
  573. (state/close-modal!)
  574. (state/close-settings!))
  575. modal-panel-content (or modal-panel-content (fn [_close] [:div]))]
  576. [:div.ui__modal
  577. {:style {:z-index (if show? 999 -1)}
  578. :label label}
  579. (css-transition
  580. {:in show? :timeout 0}
  581. (fn [state]
  582. (modal-overlay state close-fn close-backdrop?)))
  583. (css-transition
  584. {:in show? :timeout 0}
  585. (fn [state]
  586. (modal-panel show? modal-panel-content state close-fn fullscreen? close-btn?)))]))
  587. (defn make-confirm-modal
  588. [{:keys [tag title sub-title sub-checkbox? on-cancel on-confirm]
  589. :or {on-cancel #()}}]
  590. (fn [close-fn]
  591. (let [*sub-checkbox-selected (and sub-checkbox? (atom []))]
  592. [:div.ui__confirm-modal
  593. {:class (str "is-" tag)}
  594. [:div.sm:flex.sm:items-start
  595. [:div.mx-auto.flex-shrink-0.flex.items-center.justify-center.h-12.w-12.rounded-full.bg-error.sm:mx-0.sm:h-10.sm:w-10
  596. [:svg.h-6.w-6.text-error
  597. {:stroke "currentColor", :view-box "0 0 24 24", :fill "none"}
  598. [:path
  599. {:d
  600. "M12 9v2m0 4h.01m-6.938 4h13.856c1.54 0 2.502-1.667 1.732-3L13.732 4c-.77-1.333-2.694-1.333-3.464 0L3.34 16c-.77 1.333.192 3 1.732 3z"
  601. :stroke-width "2"
  602. :stroke-linejoin "round"
  603. :stroke-linecap "round"}]]]
  604. [:div.mt-3.text-center.sm:mt-0.sm:ml-4.sm:text-left
  605. [:h2.headline.text-lg.leading-6.font-medium
  606. (if (keyword? title) (t title) title)]
  607. [:label.sublabel
  608. (when sub-checkbox?
  609. (checkbox
  610. {:default-value false
  611. :on-change (fn [e]
  612. (let [checked (.. e -target -checked)]
  613. (reset! *sub-checkbox-selected [checked])))}))
  614. [:h3.subline.text-gray-400
  615. (if (keyword? sub-title)
  616. (t sub-title)
  617. sub-title)]]]]
  618. [:div.mt-5.sm:mt-4.sm:flex.sm:flex-row-reverse
  619. [:span.flex.w-full.rounded-md.shadow-sm.sm:ml-3.sm:w-auto
  620. [:button.inline-flex.justify-center.w-full.rounded-md.border.border-transparent.px-4.py-2.bg-indigo-600.text-base.leading-6.font-medium.text-white.shadow-sm.hover:bg-indigo-500.focus:outline-none.focus:border-indigo-700.focus:shadow-outline-indigo.transition.ease-in-out.duration-150.sm:text-sm.sm:leading-5
  621. {:type "button"
  622. :autoFocus "on"
  623. :class "ui__modal-enter"
  624. :on-click #(and (fn? on-confirm)
  625. (on-confirm % {:close-fn close-fn
  626. :sub-selected (and *sub-checkbox-selected @*sub-checkbox-selected)}))}
  627. (t :yes)]]
  628. [:span.mt-3.flex.w-full.rounded-md.shadow-sm.sm:mt-0.sm:w-auto
  629. [:button.inline-flex.justify-center.w-full.rounded-md.border.border-gray-300.px-4.py-2.bg-white.text-base.leading-6.font-medium.text-gray-700.shadow-sm.hover:text-gray-500.focus:outline-none.focus:border-blue-300.focus:shadow-outline-blue.transition.ease-in-out.duration-150.sm:text-sm.sm:leading-5
  630. {:type "button"
  631. :on-click (comp on-cancel close-fn)}
  632. (t :cancel)]]]])))
  633. (rum/defc sub-modal < rum/reactive
  634. []
  635. (when-let [modals (seq (state/sub :modal/subsets))]
  636. (for [[idx modal] (medley/indexed modals)]
  637. (let [id (:modal/id modal)
  638. modal-panel-content (:modal/panel-content modal)
  639. close-btn? (:modal/close-btn? modal)
  640. close-backdrop? (:modal/close-backdrop? modal)
  641. show? (:modal/show? modal)
  642. label (:modal/label modal)
  643. close-fn (fn []
  644. (state/close-sub-modal! id))
  645. modal-panel-content (or modal-panel-content (fn [_close] [:div]))]
  646. [:div.ui__modal.is-sub-modal
  647. {:style {:z-index (if show? (+ 999 idx) -1)}
  648. :label label}
  649. (css-transition
  650. {:in show? :timeout 0}
  651. (fn [state]
  652. (modal-overlay state close-fn close-backdrop?)))
  653. (css-transition
  654. {:in show? :timeout 0}
  655. (fn [state]
  656. (modal-panel show? modal-panel-content state close-fn false close-btn?)))]))))
  657. (defn loading
  658. ([content] (loading content nil))
  659. ([content opts]
  660. [:div.flex.flex-row.items-center.inline
  661. [:span.icon.flex.items-center (svg/loader-fn opts)
  662. (when-not (string/blank? content)
  663. [:span.text.pl-2 content])]]))
  664. (rum/defc rotating-arrow
  665. [collapsed?]
  666. [:span
  667. {:class (if collapsed? "rotating-arrow collapsed" "rotating-arrow not-collapsed")}
  668. (svg/caret-right)])
  669. (rum/defcs foldable-title <
  670. (rum/local false ::control?)
  671. [state {:keys [on-mouse-down header title-trigger? collapsed?]}]
  672. (let [control? (get state ::control?)]
  673. [:div.content
  674. [:div.flex-1.flex-row.foldable-title (cond->
  675. {:on-mouse-over #(reset! control? true)
  676. :on-mouse-out #(reset! control? false)}
  677. title-trigger?
  678. (assoc :on-mouse-down on-mouse-down
  679. :class "cursor"))
  680. [:div.flex.flex-row.items-center
  681. (when-not (mobile-util/native-platform?)
  682. [:a.block-control.opacity-50.hover:opacity-100.mr-2
  683. (cond->
  684. {:style {:width 14
  685. :height 16
  686. :margin-left -30}}
  687. (not title-trigger?)
  688. (assoc :on-mouse-down on-mouse-down))
  689. [:span {:class (if (or @control? @collapsed?) "control-show cursor-pointer" "control-hide")}
  690. (rotating-arrow @collapsed?)]])
  691. (if (fn? header)
  692. (header @collapsed?)
  693. header)]]]))
  694. (rum/defcs foldable < db-mixins/query rum/reactive
  695. (rum/local false ::collapsed?)
  696. {:will-mount (fn [state]
  697. (let [args (:rum/args state)]
  698. (when (true? (:default-collapsed? (last args)))
  699. (reset! (get state ::collapsed?) true)))
  700. state)
  701. :did-mount (fn [state]
  702. (when-let [f (:init-collapsed (last (:rum/args state)))]
  703. (f (::collapsed? state)))
  704. state)}
  705. [state header content {:keys [title-trigger? on-mouse-down
  706. _default-collapsed? _init-collapsed]}]
  707. (let [collapsed? (get state ::collapsed?)
  708. on-mouse-down (fn [e]
  709. (util/stop e)
  710. (swap! collapsed? not)
  711. (when on-mouse-down
  712. (on-mouse-down @collapsed?)))]
  713. [:div.flex.flex-col
  714. (foldable-title {:on-mouse-down on-mouse-down
  715. :header header
  716. :title-trigger? title-trigger?
  717. :collapsed? collapsed?})
  718. [:div {:class (if @collapsed? "hidden" "initial")
  719. :on-mouse-down (fn [e] (.stopPropagation e))}
  720. (if (fn? content)
  721. (if (not @collapsed?) (content) nil)
  722. content)]]))
  723. (rum/defc admonition
  724. [type content]
  725. (let [type (name type)]
  726. (when-let [icon (case (string/lower-case type)
  727. "note" svg/note
  728. "tip" svg/tip
  729. "important" svg/important
  730. "caution" svg/caution
  731. "warning" svg/warning
  732. "pinned" svg/pinned
  733. nil)]
  734. [:div.flex.flex-row.admonitionblock.align-items {:class type}
  735. [:div.pr-4.admonition-icon.flex.flex-col.justify-center
  736. {:title (string/capitalize type)} (icon)]
  737. [:div.ml-4.text-lg
  738. content]])))
  739. (rum/defcs catch-error
  740. < {:did-catch
  741. (fn [state error _info]
  742. (log/error :exception error)
  743. (assoc state ::error error))}
  744. [{error ::error, c :rum/react-component} error-view view]
  745. (if (some? error)
  746. error-view
  747. view))
  748. (rum/defcs catch-error-and-notify
  749. < {:did-catch
  750. (fn [state error _info]
  751. (log/error :exception error)
  752. (notification/show!
  753. (str "Error caught by UI!\n " error)
  754. :error)
  755. (assoc state ::error error))}
  756. [{error ::error, c :rum/react-component} error-view view]
  757. (if (some? error)
  758. error-view
  759. view))
  760. (rum/defc block-error
  761. "Well styled error message for blocks"
  762. [title {:keys [content section-attrs]}]
  763. [:section.border.mt-1.p-1.cursor-pointer.block-content-fallback-ui
  764. section-attrs
  765. [:div.flex.justify-between.items-center.px-1
  766. [:h5.text-error.pb-1 title]
  767. [:a.text-xs.opacity-50.hover:opacity-80
  768. {:href "https://github.com/logseq/logseq/issues/new?labels=from:in-app&template=bug_report.yaml"
  769. :target "_blank"} "report issue"]]
  770. (when content [:pre.m-0.text-sm content])])
  771. (def component-error
  772. "Well styled error message for higher level components. Currently same as
  773. block-error but this could change"
  774. block-error)
  775. (rum/defc select
  776. ([options on-change]
  777. (select options on-change nil))
  778. ([options on-change class]
  779. [:select.pl-6.block.text-base.leading-6.border-gray-300.focus:outline-none.focus:shadow-outline-blue.focus:border-blue-300.sm:text-sm.sm:leading-5
  780. {:class (or class "form-select")
  781. :on-change (fn [e]
  782. (let [value (util/evalue e)]
  783. (on-change e value)))}
  784. (for [{:keys [label value selected disabled]
  785. :or {selected false disabled false}} options]
  786. [:option (cond->
  787. {:key label
  788. :value (or value label)} ;; NOTE: value might be an empty string, `or` is safe here
  789. disabled
  790. (assoc :disabled disabled)
  791. selected
  792. (assoc :selected selected))
  793. label])]))
  794. (rum/defc radio-list
  795. [options on-change class]
  796. [:div.ui__radio-list
  797. {:class class}
  798. (for [{:keys [label value selected]} options]
  799. [:label
  800. {:key (str "radio-list-" label)}
  801. [:input.form-radio
  802. {:value value
  803. :type "radio"
  804. :on-change #(on-change (util/evalue %))
  805. :checked selected}]
  806. label])])
  807. (rum/defc checkbox-list
  808. [options on-change class]
  809. (let [checked-vals
  810. (->> options (filter :selected) (map :value) (into #{}))
  811. on-item-change
  812. (fn [^js e]
  813. (let [^js target (.-target e)
  814. checked? (.-checked target)
  815. value (.-value target)]
  816. (on-change
  817. (into []
  818. (if checked?
  819. (conj checked-vals value)
  820. (disj checked-vals value))))))]
  821. [:div.ui__checkbox-list
  822. {:class class}
  823. (for [{:keys [label value selected]} options]
  824. [:label
  825. {:key (str "check-list-" label)}
  826. [:input.form-checkbox
  827. {:value value
  828. :type "checkbox"
  829. :on-change on-item-change
  830. :checked selected}]
  831. label])]))
  832. (rum/defcs tippy < rum/reactive
  833. (rum/local false ::mounted?)
  834. [state {:keys [fixed-position? open? in-editor? html] :as opts} child]
  835. (let [*mounted? (::mounted? state)
  836. manual (not= open? nil)
  837. edit-id (ffirst (state/sub :editor/editing?))
  838. editing-node (when edit-id (gdom/getElement edit-id))
  839. editing? (some? editing-node)
  840. scrolling? (state/sub :ui/scrolling?)
  841. open? (if manual open? @*mounted?)
  842. disabled? (boolean
  843. (or
  844. (and in-editor?
  845. ;; editing in non-preview containers or scrolling
  846. (not (util/rec-get-tippy-container editing-node))
  847. (or editing? scrolling?))
  848. (not (state/enable-tooltip?))))]
  849. (Tippy (->
  850. (merge {:arrow true
  851. :sticky true
  852. :delay 600
  853. :theme "customized"
  854. :disabled disabled?
  855. :unmountHTMLWhenHide true
  856. :open (if disabled? false open?)
  857. :trigger (if manual "manual" "mouseenter focus")
  858. ;; See https://github.com/tvkhoa/react-tippy/issues/13
  859. :popperOptions {:modifiers {:flip {:enabled (not fixed-position?)}
  860. :hide {:enabled false}
  861. :preventOverflow {:enabled false}}}
  862. :onShow #(reset! *mounted? true)
  863. :onHide #(reset! *mounted? false)}
  864. opts)
  865. (assoc :html (or
  866. (when open?
  867. (try
  868. (when html
  869. (if (fn? html)
  870. (html)
  871. [:div.px-2.py-1
  872. html]))
  873. (catch :default e
  874. (log/error :exception e)
  875. [:div])))
  876. [:div {:key "tippy"} ""])))
  877. (rum/fragment {:key "tippy-children"} child))))
  878. (rum/defc slider
  879. [default-value {:keys [min max on-change]}]
  880. [:input.cursor-pointer
  881. {:type "range"
  882. :value (int default-value)
  883. :min min
  884. :max max
  885. :style {:width "100%"}
  886. :on-change #(let [value (util/evalue %)]
  887. (on-change value))}])
  888. (rum/defcs tweet-embed < (rum/local true :loading?)
  889. [state id]
  890. (let [*loading? (:loading? state)]
  891. [:div [(when @*loading? [:span.flex.items-center [svg/loading " ... loading"]])
  892. (ReactTweetEmbed
  893. {:id id
  894. :class "contents"
  895. :options {:theme (when (= (state/sub :ui/theme) "dark") "dark")}
  896. :on-tweet-load-success #(reset! *loading? false)})]]))
  897. (def get-adapt-icon-class
  898. (memoize (fn [klass] (r/adapt-class klass))))
  899. (rum/defc icon
  900. ([name] (icon name nil))
  901. ([name {:keys [extension? font? class] :as opts}]
  902. (when-not (string/blank? name)
  903. (let [^js jsTablerIcons (gobj/get js/window "tablerIcons")]
  904. (if (or extension? font? (not jsTablerIcons))
  905. [:span.ui__icon (merge {:class
  906. (util/format
  907. (str "%s-" name
  908. (when (:class opts)
  909. (str " " (string/trim (:class opts)))))
  910. (if extension? "tie tie" "ti ti"))}
  911. (dissoc opts :class :extension? :font?))]
  912. ;; tabler svg react
  913. (when-let [klass (gobj/get js/tablerIcons (str "Icon" (csk/->PascalCase name)))]
  914. (let [f (get-adapt-icon-class klass)]
  915. [:span.ui__icon.ti
  916. {:class (str "ls-icon-" name " " class)}
  917. (f (merge {:size 18} (r/map-keys->camel-case (dissoc opts :class))))])))))))
  918. (rum/defc button
  919. [text & {:keys [background href class intent on-click small? large? title icon icon-props disabled?]
  920. :or {small? false large? false}
  921. :as option}]
  922. (let [klass (if-not intent ".bg-indigo-600.hover:bg-indigo-700.focus:border-indigo-700.active:bg-indigo-700.text-center" intent)
  923. klass (if background (string/replace klass "indigo" background) klass)
  924. klass (if small? (str klass ".px-2.py-1") klass)
  925. klass (if large? (str klass ".text-base") klass)
  926. klass (if disabled? (str klass "disabled:opacity-75") klass)]
  927. [:button.ui__button
  928. (merge
  929. {:type "button"
  930. :title title
  931. :disabled disabled?
  932. :class (str (util/hiccup->class klass) " " class)}
  933. (dissoc option :background :class :small? :large? :disabled?)
  934. (when href
  935. {:on-click (fn []
  936. (util/open-url href)
  937. (when (fn? on-click) (on-click)))}))
  938. (when icon (frontend.ui/icon icon (merge icon-props {:class (when-not (empty? text) "mr-1")})))
  939. text]))
  940. (rum/defc point
  941. ([] (point "bg-red-600" 5 nil))
  942. ([klass size {:keys [class style] :as opts}]
  943. [:span.ui__point.overflow-hidden.rounded-full.inline-block
  944. (merge {:class (str (util/hiccup->class klass) " " class)
  945. :style (merge {:width size :height size} style)}
  946. (dissoc opts :style :class))]))
  947. (rum/defc type-icon
  948. [{:keys [name class title extension?]}]
  949. [:.type-icon {:class class
  950. :title title}
  951. (icon name {:extension? extension?})])
  952. (rum/defc with-shortcut < rum/reactive
  953. < {:key-fn (fn [key pos] (str "shortcut-" key pos))}
  954. [shortcut-key position content]
  955. (let [tooltip? (state/sub :ui/shortcut-tooltip?)]
  956. (if tooltip?
  957. (tippy
  958. {:html [:div.text-sm.font-medium (keyboard-shortcut-from-config shortcut-key)]
  959. :interactive true
  960. :position position
  961. :theme "monospace"
  962. :delay [1000, 100]
  963. :arrow true}
  964. content)
  965. content)))
  966. (rum/defc progress-bar
  967. [width]
  968. {:pre (integer? width)}
  969. [:div.w-full.bg-indigo-200.rounded-full.h-2.5.animate-pulse
  970. [:div.bg-indigo-600.h-2.5.rounded-full {:style {:width (str width "%")}
  971. :transition "width 1s"}]])
  972. (rum/defc progress-bar-with-label
  973. [width label-left label-right]
  974. {:pre (integer? width)}
  975. [:div
  976. [:div.flex.justify-between.mb-1
  977. [:span.text-base
  978. label-left]
  979. [:span.text-sm.font-medium
  980. label-right]]
  981. (progress-bar width)])
  982. (rum/defc lazy-loading-placeholder
  983. [height]
  984. [:div.shadow.rounded-md.p-4.w-full.mx-auto.mb-5.fade-in {:style {:height height}}
  985. [:div.animate-pulse.flex.space-x-4
  986. [:div.flex-1.space-y-3.py-1
  987. [:div.h-2.bg-base-4.rounded]
  988. [:div.space-y-3
  989. [:div.grid.grid-cols-3.gap-4
  990. [:div.h-2.bg-base-4.rounded.col-span-2]
  991. [:div.h-2.bg-base-4.rounded.col-span-1]]
  992. [:div.h-2.bg-base-4.rounded]]]]])
  993. (rum/defc lazy-visible-inner
  994. [visible? content-fn ref]
  995. (let [[set-ref rect] (r/use-bounding-client-rect)
  996. placeholder-height (or (when rect (.-height rect)) 88)]
  997. [:div.lazy-visibility {:ref ref}
  998. [:div {:ref set-ref}
  999. (if visible?
  1000. (when (fn? content-fn)
  1001. [:div.fade-enter
  1002. {:ref #(when-let [^js cls (and % (.-classList %))]
  1003. (.add cls "fade-enter-active"))}
  1004. (content-fn)])
  1005. (lazy-loading-placeholder placeholder-height))]]))
  1006. (rum/defc lazy-visible
  1007. ([content-fn]
  1008. (lazy-visible content-fn nil))
  1009. ([content-fn {:keys [trigger-once? _debug-id]
  1010. :or {trigger-once? false}}]
  1011. (let [[visible? set-visible!] (rum/use-state false)
  1012. root-margin 100
  1013. inViewState (useInView #js {:rootMargin (str root-margin "px")
  1014. :triggerOnce trigger-once?
  1015. :onChange (fn [in-view? entry]
  1016. (let [self-top (.-top (.-boundingClientRect entry))]
  1017. (when (or (and (not visible?) in-view?)
  1018. ;; hide only the components below the current top for better ux
  1019. (and visible? (not in-view?) (> self-top root-margin)))
  1020. (set-visible! in-view?))))})
  1021. ref (.-ref inViewState)]
  1022. (lazy-visible-inner visible? content-fn ref))))
  1023. (rum/defc portal
  1024. ([children]
  1025. (portal children {:attach-to (fn [] js/document.body)
  1026. :prepend? false}))
  1027. ([children {:keys [attach-to prepend?]}]
  1028. (let [[portal-anchor set-portal-anchor] (rum/use-state nil)]
  1029. (rum/use-effect!
  1030. (fn []
  1031. (let [div (js/document.createElement "div")
  1032. attached (or (if (fn? attach-to) (attach-to) attach-to) js/document.body)]
  1033. (.setAttribute div "data-logseq-portal" (str (d/squuid)))
  1034. (if prepend? (.prepend attached div) (.append attached div))
  1035. (set-portal-anchor div)
  1036. #(.remove div)))
  1037. [])
  1038. (when portal-anchor
  1039. (rum/portal (rum/fragment children) portal-anchor)))))
  1040. (rum/defc menu-heading
  1041. ([add-heading-fn auto-heading-fn rm-heading-fn]
  1042. (menu-heading nil add-heading-fn auto-heading-fn rm-heading-fn))
  1043. ([heading add-heading-fn auto-heading-fn rm-heading-fn]
  1044. [:div.flex.flex-row.justify-between.pb-2.pt-1.px-2.items-center
  1045. [:div.flex.flex-row.justify-between.flex-1.px-1
  1046. (for [i (range 1 7)]
  1047. (button
  1048. ""
  1049. :disabled? (and (some? heading) (= heading i))
  1050. :icon (str "h-" i)
  1051. :title (t :heading i)
  1052. :class "to-heading-button"
  1053. :on-click #(add-heading-fn i)
  1054. :intent "link"
  1055. :small? true))
  1056. (button
  1057. ""
  1058. :icon "h-auto"
  1059. :disabled? (and (some? heading) (true? heading))
  1060. :icon-props {:extension? true}
  1061. :class "to-heading-button"
  1062. :title (t :auto-heading)
  1063. :on-click auto-heading-fn
  1064. :intent "link"
  1065. :small? true)
  1066. (button
  1067. ""
  1068. :icon "heading-off"
  1069. :disabled? (and (some? heading) (not heading))
  1070. :icon-props {:extension? true}
  1071. :class "to-heading-button"
  1072. :title (t :remove-heading)
  1073. :on-click rm-heading-fn
  1074. :intent "link"
  1075. :small? true)]]))