ui.cljs 48 KB

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