block.cljs 98 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581
  1. (ns frontend.components.block
  2. (:refer-clojure :exclude [range])
  3. (:require ["/frontend/utils" :as utils]
  4. [cljs-bean.core :as bean]
  5. [cljs.core.match :refer [match]]
  6. [cljs.reader :as reader]
  7. [clojure.string :as string]
  8. [clojure.walk :as walk]
  9. [datascript.core :as dc]
  10. [dommy.core :as d]
  11. [frontend.commands :as commands]
  12. [frontend.components.datetime :as datetime-comp]
  13. [frontend.components.lazy-editor :as lazy-editor]
  14. [frontend.components.svg :as svg]
  15. [frontend.components.macro :as macro]
  16. [frontend.config :as config]
  17. [frontend.context.i18n :as i18n]
  18. [frontend.date :as date]
  19. [frontend.db :as db]
  20. [frontend.db.utils :as db-utils]
  21. [frontend.db-mixins :as db-mixins]
  22. [frontend.db.model :as model]
  23. [frontend.db.query-dsl :as query-dsl]
  24. [frontend.extensions.highlight :as highlight]
  25. [frontend.extensions.latex :as latex]
  26. [frontend.extensions.sci :as sci]
  27. [frontend.format.block :as block]
  28. [frontend.format.mldoc :as mldoc]
  29. [frontend.handler.block :as block-handler]
  30. [frontend.handler.dnd :as dnd]
  31. [frontend.handler.editor :as editor-handler]
  32. [frontend.handler.repeated :as repeated]
  33. [frontend.handler.route :as route-handler]
  34. [frontend.handler.ui :as ui-handler]
  35. [frontend.handler.query :as query-handler]
  36. [frontend.handler.common :as common-handler]
  37. [frontend.modules.outliner.tree :as tree]
  38. [frontend.search :as search]
  39. [frontend.security :as security]
  40. [frontend.state :as state]
  41. [frontend.template :as template]
  42. [frontend.text :as text]
  43. [frontend.ui :as ui]
  44. [frontend.util :as util]
  45. [frontend.util.property :as property]
  46. [goog.dom :as gdom]
  47. [goog.object :as gobj]
  48. [lambdaisland.glogi :as log]
  49. [medley.core :as medley]
  50. [promesa.core :as p]
  51. [reitit.frontend.easy :as rfe]
  52. [rum.core :as rum]
  53. [shadow.loader :as loader]
  54. [frontend.components.query-table :as query-table]))
  55. ;; TODO: remove rum/with-context because it'll make reactive queries not working
  56. (defn safe-read-string
  57. ([s]
  58. (safe-read-string s true))
  59. ([s warn?]
  60. (try
  61. (reader/read-string s)
  62. (catch js/Error e
  63. (println "read-string error:")
  64. (js/console.error e)
  65. (when warn?
  66. [:div.warning {:title "read-string failed"}
  67. s])))))
  68. ;; local state
  69. (defonce *dragging?
  70. (atom false))
  71. (defonce *dragging-block
  72. (atom nil))
  73. (defonce *drag-to-block
  74. (atom nil))
  75. (def *move-to (atom nil))
  76. ;; TODO: dynamic
  77. (defonce max-blocks-per-page 200)
  78. (defonce *blocks-container-id (atom 0))
  79. ;; TODO:
  80. ;; add `key`
  81. (defn- remove-nils
  82. [col]
  83. (remove nil? col))
  84. (defn vec-cat
  85. [& args]
  86. (->> (apply concat args)
  87. remove-nils
  88. vec))
  89. (defn ->elem
  90. ([elem items]
  91. (->elem elem nil items))
  92. ([elem attrs items]
  93. (let [elem (keyword elem)]
  94. (if attrs
  95. (vec
  96. (cons elem
  97. (cons attrs
  98. (seq items))))
  99. (vec
  100. (cons elem
  101. (seq items)))))))
  102. (defn- join-lines
  103. [l]
  104. (string/trim (apply str l)))
  105. (defn- string-of-url
  106. [url]
  107. (match url
  108. ["File" s]
  109. (string/replace s "file://" "")
  110. ["Complex" m]
  111. (let [{:keys [link protocol]} m]
  112. (if (= protocol "file")
  113. link
  114. (str protocol "://" link)))))
  115. (defn- get-file-absolute-path
  116. [config path]
  117. (let [path (string/replace path "file:" "")
  118. block-id (:block/uuid config)
  119. current-file (and block-id
  120. (:file/path (:block/file (:block/page (db/entity [:block/uuid block-id])))))]
  121. (when current-file
  122. (let [parts (string/split current-file #"/")
  123. parts-2 (string/split path #"/")
  124. current-dir (string/join "/" (drop-last 1 parts))]
  125. (cond
  126. (if util/win32? (utils/win32 path) (util/starts-with? path "/"))
  127. path
  128. (and (not (util/starts-with? path ".."))
  129. (not (util/starts-with? path ".")))
  130. (str current-dir "/" path)
  131. :else
  132. (let [parts (loop [acc []
  133. parts (reverse parts)
  134. col (reverse parts-2)]
  135. (if (empty? col)
  136. acc
  137. (let [[part parts] (case (first col)
  138. ".."
  139. [(first parts) (rest parts)]
  140. "."
  141. ["" parts]
  142. [(first col) (rest parts)])]
  143. (recur (conj acc part)
  144. parts
  145. (rest col)))))
  146. parts (remove #(string/blank? %) parts)]
  147. (string/join "/" (reverse parts))))))))
  148. (defonce *resizing-image? (atom false))
  149. (rum/defcs resizable-image <
  150. (rum/local nil ::size)
  151. {:will-unmount (fn [state]
  152. (reset! *resizing-image? false)
  153. state)}
  154. [state config title src metadata full_text local?]
  155. (rum/with-context [[t] i18n/*tongue-context*]
  156. (let [size (get state ::size)]
  157. (ui/resize-provider
  158. (ui/resize-consumer
  159. (cond->
  160. {:className "resize image-resize"
  161. :onSizeChanged (fn [value]
  162. (when (and (not @*resizing-image?)
  163. (some? @size)
  164. (not= value @size))
  165. (reset! *resizing-image? true))
  166. (reset! size value))
  167. :onMouseUp (fn []
  168. (when (and @size @*resizing-image?)
  169. (when-let [block-id (:block/uuid config)]
  170. (let [size (bean/->clj @size)]
  171. (editor-handler/resize-image! block-id metadata full_text size))))
  172. (when @*resizing-image?
  173. ;; TODO: need a better way to prevent the clicking to edit current block
  174. (js/setTimeout #(reset! *resizing-image? false) 200)))
  175. :onClick (fn [e]
  176. (when @*resizing-image? (util/stop e)))}
  177. (and (:width metadata) (not (util/mobile?)))
  178. (assoc :style {:width (:width metadata)}))
  179. [:div.asset-container
  180. [:img.rounded-sm.shadow-xl.relative
  181. (merge
  182. {:loading "lazy"
  183. :src src
  184. :title title}
  185. metadata)]
  186. [:span.ctl
  187. [:a.delete
  188. {:title "Delete this image"
  189. :on-click
  190. (fn [e]
  191. (when-let [block-id (:block/uuid config)]
  192. (let [confirm-fn (ui/make-confirm-modal
  193. {:title (t :asset/confirm-delete (.toLocaleLowerCase (t :text/image)))
  194. :sub-title (if local? :asset/physical-delete "")
  195. :sub-checkbox? local?
  196. :on-confirm (fn [e {:keys [close-fn sub-selected]}]
  197. (close-fn)
  198. (editor-handler/delete-asset-of-block!
  199. {:block-id block-id
  200. :local? local?
  201. :delete-local? (first sub-selected)
  202. :repo (state/get-current-repo)
  203. :href src
  204. :title title
  205. :full-text full_text}))})]
  206. (state/set-modal! confirm-fn)
  207. (util/stop e))))}
  208. svg/trash-sm]]])))))
  209. (rum/defcs asset-link < rum/reactive
  210. (rum/local nil ::src)
  211. [state config title href label metadata full_text]
  212. (let [src (::src state)
  213. granted? (state/sub [:nfs/user-granted? (state/get-current-repo)])
  214. href (config/get-local-asset-absolute-path href)]
  215. (when (or granted? (util/electron?))
  216. (p/then (editor-handler/make-asset-url href) #(reset! src %)))
  217. (when @src
  218. (resizable-image config title @src metadata full_text true))))
  219. ;; TODO: safe encoding asciis
  220. ;; TODO: image link to another link
  221. (defn image-link [config url href label metadata full_text]
  222. (let [metadata (if (string/blank? metadata)
  223. nil
  224. (safe-read-string metadata false))
  225. title (second (first label))]
  226. (ui/catch-error
  227. [:span.warning full_text]
  228. (if (and (config/local-asset? href)
  229. (config/local-db? (state/get-current-repo)))
  230. (asset-link config title href label metadata full_text)
  231. (let [href (cond
  232. (util/starts-with? href "http")
  233. href
  234. config/publishing?
  235. (subs href 1)
  236. :else
  237. (get-file-absolute-path config href))]
  238. (resizable-image config title href metadata full_text false))))))
  239. (defn repetition-to-string
  240. [[[kind] [duration] n]]
  241. (let [kind (case kind
  242. "Dotted" "."
  243. "Plus" "+"
  244. "DoublePlus" "++")]
  245. (str kind n (string/lower-case (str (first duration))))))
  246. (defn timestamp-to-string
  247. [{:keys [active date time repetition wday active]}]
  248. (let [{:keys [year month day]} date
  249. {:keys [hour min]} time
  250. [open close] (if active ["<" ">"] ["[" "]"])
  251. repetition (if repetition
  252. (str " " (repetition-to-string repetition))
  253. "")
  254. hour (if hour (util/zero-pad hour))
  255. min (if min (util/zero-pad min))
  256. time (cond
  257. (and hour min)
  258. (util/format " %s:%s" hour min)
  259. hour
  260. (util/format " %s" hour)
  261. :else
  262. "")]
  263. (util/format "%s%s-%s-%s %s%s%s%s"
  264. open
  265. (str year)
  266. (util/zero-pad month)
  267. (util/zero-pad day)
  268. wday
  269. time
  270. repetition
  271. close)))
  272. (defn timestamp [{:keys [active date time repetition wday] :as t} kind]
  273. (let [prefix (case kind
  274. "Scheduled"
  275. [:i {:class "fa fa-calendar"
  276. :style {:margin-right 3.5}}]
  277. "Deadline"
  278. [:i {:class "fa fa-calendar-times-o"
  279. :style {:margin-right 3.5}}]
  280. "Date"
  281. nil
  282. "Closed"
  283. nil
  284. "Started"
  285. [:i {:class "fa fa-clock-o"
  286. :style {:margin-right 3.5}}]
  287. "Start"
  288. "From: "
  289. "Stop"
  290. "To: "
  291. nil)]
  292. (let [class (if (= kind "Closed")
  293. "line-through")]
  294. [:span.timestamp (cond-> {:active (str active)}
  295. class
  296. (assoc :class class))
  297. prefix
  298. (timestamp-to-string t)])))
  299. (defn range [{:keys [start stop]} stopped?]
  300. [:div {:class "timestamp-range"
  301. :stopped stopped?}
  302. (timestamp start "Start")
  303. (timestamp stop "Stop")])
  304. (declare map-inline)
  305. (declare markup-element-cp)
  306. (declare markup-elements-cp)
  307. (declare page-reference)
  308. (rum/defc page-inner
  309. [config page-name href redirect-page-name page-entity contents-page? children html-export? label]
  310. (let [tag? (:tag? config)]
  311. [:a
  312. {:class (if tag? "tag" "page-ref")
  313. :data-ref page-name
  314. :href href
  315. :on-click (fn [e]
  316. (util/stop e)
  317. (let [create-first-block! (fn []
  318. (when-not (editor-handler/add-default-title-property-if-needed! redirect-page-name)
  319. (editor-handler/insert-first-page-block-if-not-exists! redirect-page-name)))]
  320. (if (gobj/get e "shiftKey")
  321. (do
  322. (js/setTimeout create-first-block! 310)
  323. (when-let [page-entity (db/entity [:block/name redirect-page-name])]
  324. (state/sidebar-add-block!
  325. (state/get-current-repo)
  326. (:db/id page-entity)
  327. :page
  328. {:page page-entity})))
  329. (do
  330. (create-first-block!)
  331. (route-handler/redirect! {:to :page
  332. :path-params {:name redirect-page-name}}))))
  333. (when (and contents-page?
  334. (state/get-left-sidebar-open?))
  335. (ui-handler/close-left-sidebar!)))}
  336. (if (and (coll? children) (seq children))
  337. (for [child children]
  338. (if (= (first child) "Label")
  339. (last child)
  340. (let [{:keys [content children]} (last child)
  341. page-name (subs content 2 (- (count content) 2))]
  342. (rum/with-key (page-reference html-export? page-name (assoc config :children children) nil) page-name))))
  343. (cond
  344. (and label
  345. (string? label)
  346. (not (string/blank? label))) ; alias
  347. label
  348. (coll? label)
  349. (->elem :span (map-inline config label))
  350. :else
  351. (let [s (get page-entity :block/original-name page-name)]
  352. (if tag? (str "#" s) s))))]))
  353. (defn- use-delayed-open [open? page-name]
  354. "A react hook to debounce open? value.
  355. If open? changed from false to open, there will be a `timeout` delay.
  356. Otherwise, the value will be changed to false immediately"
  357. (let [[deval set-deval!] (rum/use-state nil)]
  358. (rum/use-effect!
  359. (fn []
  360. (if open? (let [timer (js/setTimeout #(set-deval! open?) 1000)]
  361. #(js/clearTimeout timer))
  362. (set-deval! open?))) ;; immediately change
  363. [open? page-name])
  364. deval))
  365. (rum/defc page-preview-trigger
  366. [{:keys [children sidebar? tippy-position tippy-distance fixed-position? open? manual?] :as config} page-name]
  367. (let [redirect-page-name (or (model/get-redirect-page-name page-name (:block/alias? config))
  368. page-name)
  369. page-original-name (model/get-page-original-name redirect-page-name)
  370. debounced-open? (use-delayed-open open? page-name)
  371. html-template (fn []
  372. (when redirect-page-name
  373. [:div.tippy-wrapper.overflow-y-auto.p-4
  374. {:style {:width 600
  375. :text-align "left"
  376. :font-weight 500
  377. :max-height 600
  378. :padding-bottom 64}}
  379. (if (and (string? page-original-name) (string/includes? page-original-name "/"))
  380. [:div.my-2
  381. (->>
  382. (for [page (string/split page-original-name #"/")]
  383. (when (and (string? page) page)
  384. (page-reference false page {} nil)))
  385. (interpose [:span.mx-2.opacity-30 "/"]))]
  386. [:h2.font-bold.text-lg (if (= page-name redirect-page-name)
  387. page-original-name
  388. [:span
  389. [:span.text-sm.mr-2 "Alias:"]
  390. page-original-name])])
  391. (let [page (db/entity [:block/name (string/lower-case redirect-page-name)])]
  392. (editor-handler/insert-first-page-block-if-not-exists! redirect-page-name)
  393. (when-let [f (state/get-page-blocks-cp)]
  394. (f (state/get-current-repo) page {:sidebar? sidebar? :preview? true})))]))]
  395. (if (or (not manual?) open?)
  396. (ui/tippy {:html html-template
  397. :interactive true
  398. :open? debounced-open?
  399. :delay [1000, 100]
  400. :fixed-position? fixed-position?
  401. :position (or tippy-position "top")
  402. :distance (or tippy-distance 10)}
  403. children)
  404. children)))
  405. (rum/defc page-cp
  406. [{:keys [html-export? label children contents-page? sidebar? preview?] :as config} page]
  407. (when-let [page-name (:block/name page)]
  408. (let [page-name (string/lower-case page-name)
  409. page-entity (db/entity [:block/name page-name])
  410. redirect-page-name (model/get-redirect-page-name page-name (:block/alias? config))
  411. href (if html-export?
  412. (util/encode-str page-name)
  413. (rfe/href :page {:name redirect-page-name}))
  414. inner (page-inner config
  415. page-name
  416. href redirect-page-name page-entity contents-page? children html-export? label)]
  417. (if (and (not (util/mobile?)) (not preview?))
  418. (page-preview-trigger (assoc config :children inner) page-name)
  419. inner))))
  420. (rum/defc asset-reference
  421. [title path]
  422. (let [repo-path (config/get-repo-dir (state/get-current-repo))
  423. full-path (.. util/node-path (join repo-path (config/get-local-asset-absolute-path path)))]
  424. [:div
  425. [:a.asset-ref {:target "_blank" :href full-path} (or title path)]
  426. (case (util/get-file-ext full-path)
  427. "pdf"
  428. [:iframe {:src full-path
  429. :class "pdf-preview"
  430. :fullscreen true
  431. :height 800}]
  432. ;; https://en.wikipedia.org/wiki/HTML5_video
  433. ("mp4" "ogg" "webm")
  434. [:video {:src full-path
  435. :controls true}]
  436. nil)]))
  437. (defonce excalidraw-loaded? (atom false))
  438. (rum/defc excalidraw < rum/reactive
  439. {:init (fn [state]
  440. (p/let [_ (loader/load :excalidraw)]
  441. (reset! excalidraw-loaded? true))
  442. state)}
  443. [file]
  444. (let [loaded? (rum/react excalidraw-loaded?)
  445. draw-component (if loaded?
  446. (resolve 'frontend.extensions.excalidraw/draw))]
  447. (when draw-component
  448. (draw-component {:file file}))))
  449. (rum/defc page-reference < rum/reactive
  450. [html-export? s config label]
  451. (let [show-brackets? (state/show-brackets?)
  452. nested-link? (:nested-link? config)
  453. contents-page? (= "contents" (string/lower-case (str (:id config))))
  454. draw? (string/ends-with? s ".excalidraw")]
  455. (if (string/ends-with? s ".excalidraw")
  456. [:div.draw {:on-click (fn [e]
  457. (.stopPropagation e))}
  458. (excalidraw s)]
  459. [:span.page-reference
  460. {:data-ref s}
  461. (when (and (or show-brackets? nested-link?)
  462. (not html-export?)
  463. (not contents-page?))
  464. [:span.text-gray-500.bracket "[["])
  465. (let [s (string/trim s)]
  466. (page-cp (assoc config
  467. :label (mldoc/plain->text label)
  468. :contents-page? contents-page?)
  469. {:block/name s}))
  470. (when (and (or show-brackets? nested-link?)
  471. (not html-export?)
  472. (not contents-page?))
  473. [:span.text-gray-500.bracket "]]"])])))
  474. (defn- latex-environment-content
  475. [name option content]
  476. (if (= (string/lower-case name) "equation")
  477. content
  478. (util/format "\\begin%s\n%s\\end{%s}"
  479. (str "{" name "}" option)
  480. content
  481. name)))
  482. (declare blocks-container)
  483. (defn- edit-parent-block [e config]
  484. (when-not (state/editing?)
  485. (.stopPropagation e)
  486. (editor-handler/edit-block! config :max (:block/format config) (:block/uuid config))))
  487. (rum/defc block-embed < rum/reactive db-mixins/query
  488. [config id]
  489. (let [blocks (db/get-block-and-children (state/get-current-repo) id)]
  490. [:div.color-level.embed-block.bg-base-2
  491. {:style {:z-index 2}
  492. :on-double-click #(edit-parent-block % config)
  493. :on-mouse-down (fn [e] (.stopPropagation e))}
  494. [:div.px-3.pt-1.pb-2
  495. (blocks-container blocks (assoc config
  496. :id (str id)
  497. :embed-id id
  498. :embed? true
  499. :ref? false))]]))
  500. (rum/defc page-embed < rum/reactive db-mixins/query
  501. [config page-name]
  502. (let [page-name (string/trim (string/lower-case page-name))
  503. current-page (state/get-current-page)]
  504. [:div.color-level.embed.embed-page.bg-base-2
  505. {:class (if (:sidebar? config) "in-sidebar")
  506. :on-double-click #(edit-parent-block % config)
  507. :on-mouse-down #(.stopPropagation %)}
  508. [:section.flex.items-center.p-1.embed-header
  509. [:div.mr-3 svg/page]
  510. (page-cp config {:block/name page-name})]
  511. (when (and
  512. (not= (string/lower-case (or current-page ""))
  513. page-name)
  514. (not= (string/lower-case (get config :id ""))
  515. page-name))
  516. (let [blocks (db/get-page-blocks (state/get-current-repo) page-name)]
  517. (blocks-container blocks (assoc config
  518. :id page-name
  519. :embed? true
  520. :page-embed? true
  521. :ref? false))))]))
  522. (defn- get-label-text
  523. [label]
  524. (and (= 1 (count label))
  525. (let [label (first label)]
  526. (string? (last label))
  527. (last label))))
  528. (defn- get-page
  529. [label]
  530. (when-let [label-text (get-label-text label)]
  531. (db/entity [:block/name (string/lower-case label-text)])))
  532. (defn- macro->text
  533. [name arguments]
  534. (if (and (seq arguments)
  535. (not= arguments ["null"]))
  536. (util/format "{{{%s %s}}}" name (string/join ", " arguments))
  537. (util/format "{{{%s}}}" name)))
  538. (declare block-content)
  539. (declare block-container)
  540. (declare block-parents)
  541. (rum/defc block-reference < rum/reactive
  542. [config id label]
  543. (when (and
  544. (not (string/blank? id))
  545. (util/uuid-string? id))
  546. (let [block-id (uuid id)
  547. block (db/pull-block block-id)
  548. repo (state/get-current-repo)]
  549. (if block
  550. [:div.block-ref-wrap.inline
  551. {:on-mouse-down
  552. (fn [e]
  553. (util/stop e)
  554. (if (gobj/get e "shiftKey")
  555. (state/sidebar-add-block!
  556. (state/get-current-repo)
  557. (:db/id block)
  558. :block-ref
  559. {:block block})
  560. (route-handler/redirect! {:to :page
  561. :path-params {:name id}})))}
  562. (let [title (let [title (:block/title block)]
  563. [:span.block-ref
  564. (block-content (assoc config :block-ref? true)
  565. block nil (:block/uuid block)
  566. (:slide? config))])
  567. inner (if label
  568. (->elem
  569. :span.block-ref
  570. (map-inline config label))
  571. title)]
  572. (if (and (not (util/mobile?)) (not (:preview? config)))
  573. (ui/tippy {:html (fn []
  574. [:div.tippy-wrapper.overflow-y-auto.p-4
  575. {:style {:width 735
  576. :text-align "left"
  577. :max-height 600}}
  578. [(block-parents config repo block-id (:block/format config))
  579. (blocks-container
  580. (db/get-block-and-children repo block-id)
  581. (assoc config :id (str id) :preview? true))]])
  582. :interactive true
  583. :delay [1000, 100]} inner)
  584. inner))]
  585. [:span.warning.mr-1 {:title "Block ref invalid"}
  586. (util/format "((%s))" id)]))))
  587. (defn inline-text
  588. [format v]
  589. (when (string? v)
  590. (let [inline-list (mldoc/inline->edn v (mldoc/default-config format))]
  591. [:div.inline.mr-1 (map-inline {} inline-list)])))
  592. (defn selection-range-in-block? []
  593. (and (= "Range" (. (js/window.getSelection) -type))
  594. (-> (js/window.getSelection)
  595. (.-anchorNode)
  596. (.-parentNode)
  597. (.closest ".block-content"))))
  598. (defn- render-macro
  599. [config name arguments macro-content format]
  600. (if macro-content
  601. (let [ast (->> (mldoc/->edn macro-content (mldoc/default-config format))
  602. (map first))
  603. block? (mldoc/block-with-title? (ffirst ast))]
  604. (if block?
  605. [:div
  606. (markup-elements-cp (assoc config :block/format format) ast)]
  607. (inline-text format macro-content)))
  608. [:span.warning {:title (str "Unsupported macro name: " name)}
  609. (macro->text name arguments)]))
  610. (rum/defc nested-link < rum/reactive
  611. [config html-export? link]
  612. (let [show-brackets? (state/show-brackets?)
  613. {:keys [content children]} link]
  614. [:span.page-reference.nested
  615. (when (and show-brackets?
  616. (not html-export?)
  617. (not (= (:id config) "contents")))
  618. [:span.text-gray-500 "[["])
  619. (let [page-name (subs content 2 (- (count content) 2))]
  620. (page-cp (assoc config
  621. :children children
  622. :nested-link? true) {:block/name page-name}))
  623. (when (and show-brackets?
  624. (not html-export?)
  625. (not (= (:id config) "contents")))
  626. [:span.text-gray-500 "]]"])]))
  627. (rum/defcs tutorial-video <
  628. (rum/local true)
  629. [state]
  630. (let [lite-mode? (:rum/local state)]
  631. [:div.tutorial-video-container.relative
  632. {:style {:height 367 :width 653}}
  633. (if @lite-mode?
  634. [:div
  635. [:img.w-full.h-full.absolute
  636. {:src (if (util/electron?)
  637. "img/tutorial-thumb.jpg"
  638. "https://img.youtube.com/vi/Afmqowr0qEQ/maxresdefault.jpg")}]
  639. [:button
  640. {:class "absolute bg-red-300 w-16 h-16 -m-8 top-1/2 left-1/2 rounded-full"
  641. :on-click (fn [_] (swap! lite-mode? not))}
  642. (svg/play)]]
  643. [:iframe.w-full.h-full
  644. {:allow-full-screen "allowfullscreen"
  645. :allow
  646. "accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope"
  647. :frame-border "0"
  648. :src "https://www.youtube.com/embed/Afmqowr0qEQ?autoplay=1"}])]))
  649. (declare custom-query)
  650. (defn- show-link?
  651. [config metadata s full-text]
  652. (let [img-formats (set (map name (config/img-formats)))
  653. metadata-show (:show (safe-read-string metadata))
  654. format (get-in config [:block :block/format])]
  655. (or
  656. (and
  657. (= :org format)
  658. (or
  659. (and
  660. (nil? metadata-show)
  661. (or
  662. (config/local-asset? s)
  663. (text/image-link? img-formats s)))
  664. (true? (boolean metadata-show))))
  665. ;; markdown
  666. (string/starts-with? (string/triml full-text) "!")
  667. ;; image http link
  668. (and (or (string/starts-with? full-text "http://")
  669. (string/starts-with? full-text "https://"))
  670. (text/image-link? img-formats s)))))
  671. (defn- relative-assets-path->absolute-path
  672. [path]
  673. (if (util/absolute-path? path)
  674. path
  675. (.. util/node-path
  676. (join (config/get-repo-dir (state/get-current-repo))
  677. (config/get-local-asset-absolute-path path)))))
  678. (defn inline
  679. [{:keys [html-export?] :as config} item]
  680. (match item
  681. ["Plain" s]
  682. s
  683. ["Spaces" s]
  684. s
  685. ["Superscript" l]
  686. (->elem :sup (map-inline config l))
  687. ["Subscript" l]
  688. (->elem :sub (map-inline config l))
  689. ["Tag" s]
  690. (when-let [s (block/get-tag item)]
  691. (let [s (text/page-ref-un-brackets! s)]
  692. (page-cp (assoc config :tag? true) {:block/name s})))
  693. ["Emphasis" [[kind] data]]
  694. (let [elem (case kind
  695. "Bold" :b
  696. "Italic" :i
  697. "Underline" :ins
  698. "Strike_through" :del
  699. "Highlight" :mark)]
  700. (->elem elem (map-inline config data)))
  701. ["Entity" e]
  702. [:span {:dangerouslySetInnerHTML
  703. {:__html (:html e)}}]
  704. ["Latex_Fragment" ["Displayed" s]]
  705. (if html-export?
  706. (latex/html-export s false true)
  707. (latex/latex (str (dc/squuid)) s false true))
  708. ["Latex_Fragment" ["Inline" s]]
  709. (if html-export?
  710. (latex/html-export s false true)
  711. (latex/latex (str (dc/squuid)) s false false))
  712. ["Target" s]
  713. [:a {:id s} s]
  714. ["Radio_Target" s]
  715. [:a {:id s} s]
  716. ["Email" address]
  717. (let [{:keys [local_part domain]} address
  718. address (str local_part "@" domain)]
  719. [:a {:href (str "mainto:" address)}
  720. address])
  721. ;; ["Block_reference" id]
  722. ;; ;; FIXME: alert when self block reference
  723. ;; (block-reference (assoc config :reference? true) id nil)
  724. ["Nested_link" link]
  725. (nested-link config html-export? link)
  726. ["Link" link]
  727. (let [{:keys [url label title metadata full_text]} link]
  728. (match url
  729. ["Block_ref" id]
  730. (let [label* (if (seq (mldoc/plain->text label)) label nil)]
  731. (block-reference (assoc config :reference? true) id label*))
  732. ["Page_ref" page]
  733. (let [format (get-in config [:block :block/format])]
  734. (if (and (= format :org)
  735. (show-link? config nil page page)
  736. (not (contains? #{"pdf" "mp4" "ogg" "webm"} (util/get-file-ext page))))
  737. (image-link config url page nil nil page)
  738. (let [label* (if (seq (mldoc/plain->text label)) label nil)]
  739. (if (and (string? page) (string/blank? page))
  740. [:span (util/format "[[%s]]" page)]
  741. (page-reference (:html-export? config) page config label*)))))
  742. ["Search" s]
  743. (cond
  744. (string/blank? s)
  745. [:span.warning {:title "Invalid link"} full_text]
  746. (= \# (first s))
  747. (->elem :a {:on-click #(route-handler/jump-to-anchor! (mldoc/anchorLink (subs s 1)))} (subs s 1))
  748. ;; FIXME: same headline, see more https://orgmode.org/manual/Internal-Links.html
  749. (and (= \* (first s))
  750. (not= \* (last s)))
  751. (->elem :a {:on-click #(route-handler/jump-to-anchor! (mldoc/anchorLink (subs s 1)))} (subs s 1))
  752. (util/safe-re-find #"(?i)^http[s]?://" s)
  753. (->elem :a {:href s
  754. :data-href s
  755. :target "_blank"}
  756. (map-inline config label))
  757. ;; image
  758. (and (show-link? config metadata s full_text)
  759. (not (contains? #{"pdf" "mp4" "ogg" "webm"} (util/get-file-ext s))))
  760. (image-link config url s label metadata full_text)
  761. (and (util/electron?)
  762. (show-link? config metadata s full_text))
  763. (asset-reference (second (first label)) s)
  764. ;; open file externally if s is "../assets/<...>"
  765. (and (util/electron?)
  766. (config/local-asset? s))
  767. (let [path (relative-assets-path->absolute-path s)]
  768. (->elem
  769. :a
  770. (cond->
  771. {:href (str "file://" path)
  772. :data-href path
  773. :target "_blank"}
  774. title
  775. (assoc :title title))
  776. (map-inline config label)))
  777. :else
  778. (page-reference (:html-export? config) s config label))
  779. :else
  780. (let [href (string-of-url url)
  781. protocol (or
  782. (and (= "Complex" (first url))
  783. (:protocol (second url)))
  784. (and (= "File" (first url))
  785. "file"))]
  786. (cond
  787. (and (= (get-in config [:block :block/format]) :org)
  788. (= "Complex" (first url))
  789. (= (string/lower-case protocol) "id")
  790. (string? (:link (second url)))
  791. (util/uuid-string? (:link (second url)))) ; org mode id
  792. (let [id (uuid (:link (second url)))
  793. block (db/entity [:block/uuid id])]
  794. (if (:block/pre-block? block)
  795. (let [page (:block/page block)]
  796. (page-reference html-export? (:block/name page) config label))
  797. (block-reference config (:link (second url)) label)))
  798. (= protocol "file")
  799. (if (show-link? config metadata href full_text)
  800. (image-link config url href label metadata full_text)
  801. (let [label-text (get-label-text label)
  802. page (if (string/blank? label-text)
  803. {:block/name (db/get-file-page (string/replace href "file:" ""))}
  804. (get-page label))]
  805. (if (and page
  806. (when-let [ext (util/get-file-ext href)]
  807. (config/mldoc-support? ext)))
  808. [:span.page-reference
  809. [:span.text-gray-500 "[["]
  810. (page-cp config page)
  811. [:span.text-gray-500 "]]"]]
  812. (let [href*
  813. (if (util/electron?)
  814. (relative-assets-path->absolute-path href)
  815. href)]
  816. (->elem
  817. :a
  818. (cond->
  819. {:href (str "file://" href*)
  820. :data-href href*
  821. :target "_blank"}
  822. title
  823. (assoc :title title))
  824. (map-inline config label))))))
  825. ;; image
  826. (show-link? config metadata href full_text)
  827. (image-link config url href label metadata full_text)
  828. :else
  829. (->elem
  830. :a.external-link
  831. (cond->
  832. {:href href
  833. :target "_blank"}
  834. title
  835. (assoc :title title))
  836. (map-inline config label))))))
  837. ["Verbatim" s]
  838. [:code s]
  839. ["Code" s]
  840. [:code s]
  841. ["Inline_Source_Block" x]
  842. [:code (:code x)]
  843. ["Export_Snippet" "html" s]
  844. (when (not html-export?)
  845. [:span {:dangerouslySetInnerHTML
  846. {:__html s}}])
  847. ;; String to hiccup
  848. ["Inline_Hiccup" s]
  849. (ui/catch-error
  850. [:div.warning {:title "Invalid hiccup"} s]
  851. (-> (safe-read-string s)
  852. (security/remove-javascript-links-in-href)))
  853. ["Inline_Html" s]
  854. (when (not html-export?)
  855. ;; TODO: how to remove span and only export the content of `s`?
  856. [:span {:dangerouslySetInnerHTML
  857. {:__html s}}])
  858. ["Break_Line"]
  859. [:br]
  860. ["Hard_Break_Line"]
  861. [:br]
  862. ["Timestamp" ["Scheduled" t]]
  863. nil
  864. ["Timestamp" ["Deadline" t]]
  865. nil
  866. ["Timestamp" ["Date" t]]
  867. (timestamp t "Date")
  868. ["Timestamp" ["Closed" t]]
  869. (timestamp t "Closed")
  870. ["Timestamp" ["Range" t]]
  871. (range t false)
  872. ["Timestamp" ["Clock" ["Stopped" t]]]
  873. (range t true)
  874. ["Timestamp" ["Clock" ["Started" t]]]
  875. (timestamp t "Started")
  876. ["Cookie" ["Percent" n]]
  877. [:span {:class "cookie-percent"}
  878. (util/format "[d%%]" n)]
  879. ["Cookie" ["Absolute" current total]]
  880. [:span {:class "cookie-absolute"}
  881. (util/format "[%d/%d]" current total)]
  882. ["Footnote_Reference" options]
  883. (let [{:keys [id name]} options
  884. encode-name (util/url-encode name)]
  885. [:sup.fn
  886. [:a {:id (str "fnr." encode-name)
  887. :class "footref"
  888. :on-click #(route-handler/jump-to-anchor! (str "fn." encode-name))}
  889. name]])
  890. ["Macro" options]
  891. (let [{:keys [name arguments]} options
  892. arguments (if (and
  893. (>= (count arguments) 2)
  894. (and (string/starts-with? (first arguments) "[[")
  895. (string/ends-with? (last arguments) "]]"))) ; page reference
  896. (let [title (string/join ", " arguments)]
  897. [title])
  898. arguments)]
  899. (cond
  900. (= name "query")
  901. [:div.dsl-query
  902. (let [query (string/join ", " arguments)]
  903. (custom-query (assoc config :dsl-query? true)
  904. {:title [:span.font-medium.px-2.py-1.query-title.text-sm.rounded-md.shadow-xs
  905. (str "Query: " query)]
  906. :query query}))]
  907. (= name "function")
  908. (or
  909. (when (:query-result config)
  910. (when-let [query-result (rum/react (:query-result config))]
  911. (let [fn-string (-> (util/format "(fn [result] %s)" (first arguments))
  912. (common-handler/safe-read-string "failed to parse function")
  913. (query-handler/normalize-query-function query-result)
  914. (str))
  915. f (sci/eval-string fn-string)]
  916. (when (fn? f)
  917. (try (f query-result)
  918. (catch js/Error e
  919. (js/console.error e)))))))
  920. [:span.warning
  921. (util/format "{{function %s}}" (first arguments))])
  922. (= name "youtube")
  923. (when-let [url (first arguments)]
  924. (let [YouTube-regex #"^((?:https?:)?//)?((?:www|m).)?((?:youtube.com|youtu.be))(/(?:[\w-]+\?v=|embed/|v/)?)([\w-]+)(\S+)?$"]
  925. (when-let [youtube-id (cond
  926. (== 11 (count url)) url
  927. :else
  928. (nth (util/safe-re-find YouTube-regex url) 5))]
  929. (when-not (string/blank? youtube-id)
  930. (let [width (min (- (util/get-width) 96)
  931. 560)
  932. height (int (* width (/ 315 560)))]
  933. [:iframe
  934. {:allow-full-screen "allowfullscreen"
  935. :allow
  936. "accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope"
  937. :frame-border "0"
  938. :src (str "https://www.youtube.com/embed/" youtube-id)
  939. :height height
  940. :width width}])))))
  941. (= name "tutorial-video")
  942. (tutorial-video)
  943. (= name "vimeo")
  944. (when-let [url (first arguments)]
  945. (let [Vimeo-regex #"^((?:https?:)?//)?((?:www).)?((?:player.vimeo.com|vimeo.com)?)((?:/video/)?)([\w-]+)(\S+)?$"]
  946. (when-let [vimeo-id (nth (util/safe-re-find Vimeo-regex url) 5)]
  947. (when-not (string/blank? vimeo-id)
  948. (let [width (min (- (util/get-width) 96)
  949. 560)
  950. height (int (* width (/ 315 560)))]
  951. [:iframe
  952. {:allow-full-screen "allowfullscreen"
  953. :allow
  954. "accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope"
  955. :frame-border "0"
  956. :src (str "https://player.vimeo.com/video/" vimeo-id)
  957. :height height
  958. :width width}])))))
  959. ;; TODO: support fullscreen mode, maybe we need a fullscreen dialog?
  960. (= name "bilibili")
  961. (when-let [url (first arguments)]
  962. (let [id-regex #"https?://www\.bilibili\.com/video/([^? ]+)"]
  963. (when-let [id (cond
  964. (<= (count url) 15) url
  965. :else
  966. (last (util/safe-re-find id-regex url)))]
  967. (when-not (string/blank? id)
  968. (let [width (min (- (util/get-width) 96)
  969. 560)
  970. height (int (* width (/ 315 560)))]
  971. [:iframe
  972. {:allowfullscreen true
  973. :framespacing "0"
  974. :frameborder "no"
  975. :border "0"
  976. :scrolling "no"
  977. :src (str "https://player.bilibili.com/player.html?bvid=" id "&high_quality=1")
  978. :width width
  979. :height (max 500 height)}])))))
  980. (= name "embed")
  981. (let [a (first arguments)]
  982. (cond
  983. (nil? a) ; empty embed
  984. nil
  985. (and (string/starts-with? a "[[")
  986. (string/ends-with? a "]]"))
  987. (let [page-name (-> (string/replace a "[[" "")
  988. (string/replace "]]" "")
  989. string/trim)]
  990. (when-not (string/blank? page-name)
  991. (page-embed config page-name)))
  992. (and (string/starts-with? a "((")
  993. (string/ends-with? a "))"))
  994. (when-let [s (-> (string/replace a "((" "")
  995. (string/replace "))" "")
  996. string/trim)]
  997. (when-let [id (and s
  998. (let [s (string/trim s)]
  999. (and (util/uuid-string? s)
  1000. (uuid s))))]
  1001. (block-embed config id)))
  1002. :else ;TODO: maybe collections?
  1003. nil))
  1004. (get @macro/macros name)
  1005. ((get @macro/macros name) config options)
  1006. :else
  1007. (if-let [block-uuid (:block/uuid config)]
  1008. (let [format (get-in config [:block :block/format] :markdown)
  1009. macro-content (or
  1010. (-> (db/entity [:block/uuid block-uuid])
  1011. (:block/page)
  1012. (:db/id)
  1013. (db/entity)
  1014. :block/properties
  1015. :macros
  1016. (get name))
  1017. (get (state/get-macros) name)
  1018. (get (state/get-macros) (keyword name)))
  1019. macro-content (cond
  1020. (= (str name) "img")
  1021. (case (count arguments)
  1022. 1
  1023. (util/format "[:img {:src \"%s\"}]" (first arguments))
  1024. 4
  1025. (if (and (util/safe-parse-int (nth arguments 1))
  1026. (util/safe-parse-int (nth arguments 2)))
  1027. (util/format "[:img.%s {:src \"%s\" :style {:width %s :height %s}}]"
  1028. (nth arguments 3)
  1029. (first arguments)
  1030. (util/safe-parse-int (nth arguments 1))
  1031. (util/safe-parse-int (nth arguments 2))))
  1032. 3
  1033. (if (and (util/safe-parse-int (nth arguments 1))
  1034. (util/safe-parse-int (nth arguments 2)))
  1035. (util/format "[:img {:src \"%s\" :style {:width %s :height %s}}]"
  1036. (first arguments)
  1037. (util/safe-parse-int (nth arguments 1))
  1038. (util/safe-parse-int (nth arguments 2))))
  1039. 2
  1040. (cond
  1041. (and (util/safe-parse-int (nth arguments 1)))
  1042. (util/format "[:img {:src \"%s\" :style {:width %s}}]"
  1043. (first arguments)
  1044. (util/safe-parse-int (nth arguments 1)))
  1045. (contains? #{"left" "right" "center"} (string/lower-case (nth arguments 1)))
  1046. (util/format "[:img.%s {:src \"%s\"}]"
  1047. (string/lower-case (nth arguments 1))
  1048. (first arguments))
  1049. :else
  1050. macro-content)
  1051. macro-content)
  1052. (and (seq arguments) macro-content)
  1053. (block/macro-subs macro-content arguments)
  1054. :else
  1055. macro-content)
  1056. macro-content (when macro-content
  1057. (template/resolve-dynamic-template! macro-content))]
  1058. (render-macro config name arguments macro-content format))
  1059. (let [macro-content (or
  1060. (get (state/get-macros) name)
  1061. (get (state/get-macros) (keyword name)))
  1062. format (get-in config [:block :block/format] :markdown)]
  1063. (render-macro config name arguments macro-content format)))))
  1064. :else
  1065. ""))
  1066. (declare blocks-cp)
  1067. (rum/defc block-child
  1068. [block]
  1069. block)
  1070. (defn- dnd-same-block?
  1071. [uuid]
  1072. (= (:block/uuid @*dragging-block) uuid))
  1073. (defn- get-data-transfer-attr
  1074. [event attr]
  1075. (.getData (gobj/get event "dataTransfer") attr))
  1076. (defn- bullet-drag-start
  1077. [event block uuid block-id]
  1078. (editor-handler/highlight-block! uuid)
  1079. (.setData (gobj/get event "dataTransfer")
  1080. "block-uuid"
  1081. uuid)
  1082. (.setData (gobj/get event "dataTransfer")
  1083. "block-dom-id"
  1084. block-id)
  1085. (state/clear-selection!)
  1086. (reset! *dragging? true)
  1087. (reset! *dragging-block block))
  1088. (defn- bullet-on-click
  1089. [e block config uuid]
  1090. (if (gobj/get e "shiftKey")
  1091. (do
  1092. (state/sidebar-add-block!
  1093. (state/get-current-repo)
  1094. (:db/id block)
  1095. :block
  1096. block)
  1097. (util/stop e))
  1098. (route-handler/redirect! {:to :page
  1099. :path-params {:name (str uuid)}})))
  1100. (rum/defc block-children < rum/reactive
  1101. [config children collapsed? *ref-collapsed?]
  1102. (let [ref? (:ref? config)
  1103. collapsed? (if ref? (rum/react *ref-collapsed?) collapsed?)
  1104. children (and (coll? children) (filter map? children))]
  1105. (when (and (coll? children)
  1106. (seq children)
  1107. (not collapsed?))
  1108. (let [doc-mode? (:document/mode? config)]
  1109. [:div.block-children {:style {:margin-left (if doc-mode? 12 21)
  1110. :display (if collapsed? "none" "")}}
  1111. (for [child children]
  1112. (when (map? child)
  1113. (let [child (dissoc child :block/meta)
  1114. config (cond->
  1115. (-> config
  1116. (assoc :block/uuid (:block/uuid child))
  1117. (dissoc :breadcrumb-show?))
  1118. ref?
  1119. (assoc :ref-child? true))]
  1120. (rum/with-key (block-container config child)
  1121. (:block/uuid child)))))]))))
  1122. (rum/defcs block-control < rum/reactive
  1123. [state config block uuid block-id body children collapsed? *ref-collapsed? *control-show? edit-input-id]
  1124. (let [has-children-blocks? (and (coll? children) (seq children))
  1125. has-child? (and
  1126. (not (:pre-block? block))
  1127. (or has-children-blocks? (seq body)))
  1128. control-show? (and
  1129. (or (and (seq (:block/title block))
  1130. (seq body))
  1131. has-children-blocks?)
  1132. (util/react *control-show?))
  1133. ref-collapsed? (util/react *ref-collapsed?)
  1134. dark? (= "dark" (state/sub :ui/theme))
  1135. ref? (:ref? config)
  1136. collapsed? (if ref? ref-collapsed? collapsed?)
  1137. empty-content? (string/blank? (:block/content block))
  1138. edit? (state/sub [:editor/editing? edit-input-id])]
  1139. [:div.mr-2.flex.flex-row.items-center
  1140. {:style {:height 24
  1141. :margin-top 0
  1142. :float "left"}}
  1143. [:a.block-control.opacity-50.hover:opacity-100
  1144. {:id (str "control-" uuid)
  1145. :style {:width 14
  1146. :height 16
  1147. :margin-right 2}
  1148. :on-click (fn [e]
  1149. (util/stop e)
  1150. (when-not (and (not collapsed?) (not has-child?))
  1151. (if ref?
  1152. (swap! *ref-collapsed? not)
  1153. (if collapsed?
  1154. (editor-handler/expand-block! uuid)
  1155. (editor-handler/collapse-block! uuid)))))}
  1156. [:span {:class (if control-show? "control-show" "control-hide")}
  1157. (ui/rotating-arrow collapsed?)]]
  1158. (if (and empty-content? (not edit?)
  1159. (not (:block/top? block))
  1160. (not (:block/bottom? block)))
  1161. [:span.bullet-container]
  1162. [:a {:on-click (fn [e]
  1163. (bullet-on-click e block config uuid))}
  1164. [:span.bullet-container.cursor
  1165. {:id (str "dot-" uuid)
  1166. :draggable true
  1167. :on-drag-start (fn [event]
  1168. (bullet-drag-start event block uuid block-id))
  1169. :blockid (str uuid)
  1170. :class (str (when collapsed? "bullet-closed")
  1171. " "
  1172. (when (and (:document/mode? config)
  1173. (not collapsed?))
  1174. "hide-inner-bullet"))}
  1175. [:span.bullet {:blockid (str uuid)}]]])]))
  1176. (rum/defc dnd-separator
  1177. [block move-to block-content?]
  1178. [:div.relative
  1179. [:div.dnd-separator.absolute
  1180. {:style {:left (cond-> (if (= move-to :nested) 40 20)
  1181. block-content?
  1182. (- 34))
  1183. :top 0
  1184. :width "100%"
  1185. :z-index 3}}]])
  1186. (defn block-checkbox
  1187. [block class]
  1188. (let [marker (:block/marker block)
  1189. [class checked?] (cond
  1190. (nil? marker)
  1191. nil
  1192. (contains? #{"NOW" "LATER" "DOING" "IN-PROGRESS" "TODO" "WAIT" "WAITING"} marker)
  1193. [class false]
  1194. (= "DONE" marker)
  1195. [(str class " checked") true])]
  1196. (when class
  1197. (ui/checkbox {:class class
  1198. :style {:margin-top -2
  1199. :margin-right 5}
  1200. :checked checked?
  1201. :on-change (fn [_e]
  1202. ;; FIXME: Log timestamp
  1203. (if checked?
  1204. (editor-handler/uncheck block)
  1205. (editor-handler/check block)))}))))
  1206. (defn list-checkbox
  1207. [checked?]
  1208. (ui/checkbox {:style {:margin-right 6}
  1209. :checked checked?}))
  1210. (defn marker-switch
  1211. [{:block/keys [pre-block? marker] :as block}]
  1212. (when (contains? #{"NOW" "LATER" "TODO" "DOING"} marker)
  1213. (let [set-marker-fn (fn [marker]
  1214. (fn [e]
  1215. (util/stop e)
  1216. (editor-handler/set-marker block marker)))]
  1217. (case marker
  1218. "NOW"
  1219. [:a.marker-switch.block-marker
  1220. {:title "Change from NOW to LATER"
  1221. :on-click (set-marker-fn "LATER")}
  1222. "NOW"]
  1223. "LATER"
  1224. [:a.marker-switch.block-marker
  1225. {:title "Change from LATER to NOW"
  1226. :on-click (set-marker-fn "NOW")}
  1227. "LATER"]
  1228. "TODO"
  1229. [:a.marker-switch.block-marker
  1230. {:title "Change from TODO to DOING"
  1231. :on-click (set-marker-fn "DOING")}
  1232. "TODO"]
  1233. "DOING"
  1234. [:a.marker-switch.block-marker
  1235. {:title "Change from DOING to TODO"
  1236. :on-click (set-marker-fn "TODO")}
  1237. "DOING"]
  1238. nil))))
  1239. (defn marker-cp
  1240. [{:block/keys [pre-block? marker] :as block}]
  1241. (when-not pre-block?
  1242. (if (contains? #{"IN-PROGRESS" "WAIT" "WAITING"} marker)
  1243. [:span {:class (str "task-status block-marker " (string/lower-case marker))
  1244. :style {:margin-right 3.5}}
  1245. (string/upper-case marker)])))
  1246. (rum/defc set-priority
  1247. [block priority]
  1248. [:div
  1249. (let [priorities (sort (remove #(= priority %) ["A" "B" "C"]))]
  1250. (for [p priorities]
  1251. [:a.mr-2.text-base.tooltip-priority {:key (str (random-uuid))
  1252. :priority p
  1253. :on-click (fn [] (editor-handler/set-priority block p))}]))])
  1254. (rum/defc priority-text
  1255. [priority]
  1256. [:a.opacity-50.hover:opacity-100
  1257. {:class "priority"
  1258. :href (rfe/href :page {:name priority})
  1259. :style {:margin-right 3.5}}
  1260. (util/format "[#%s]" (str priority))])
  1261. (defn priority-cp
  1262. [{:block/keys [pre-block? priority] :as block}]
  1263. (when (and (not pre-block?) priority)
  1264. (ui/tippy
  1265. {:interactive true
  1266. :html (set-priority block priority)}
  1267. (priority-text priority))))
  1268. (defn block-tags-cp
  1269. [{:block/keys [pre-block? tags] :as block}]
  1270. (when (and (not pre-block?)
  1271. (seq tags))
  1272. (->elem
  1273. :span
  1274. {:class "block-tags"}
  1275. (mapv (fn [tag]
  1276. (when-let [page (db/entity (:db/id tag))]
  1277. (let [tag (:block/name page)]
  1278. [:a.tag.mx-1 {:data-ref tag
  1279. :key (str "tag-" (:db/id tag))
  1280. :href (rfe/href :page {:name tag})}
  1281. (str "#" tag)])))
  1282. tags))))
  1283. (declare block-content)
  1284. (defn build-block-title
  1285. [{:keys [slide?] :as config} {:block/keys [uuid title tags marker priority anchor meta format content pre-block? block-refs-count page properties unordered level heading-level]
  1286. :as t}]
  1287. (let [config (assoc config :block t)
  1288. slide? (boolean (:slide? config))
  1289. block-ref? (:block-ref? config)
  1290. html-export? (:html-export? config)
  1291. checkbox (when (and (not pre-block?)
  1292. (not html-export?))
  1293. (block-checkbox t (str "mr-1 cursor")))
  1294. marker-switch (when (and (not pre-block?)
  1295. (not html-export?))
  1296. (marker-switch t))
  1297. marker-cp (marker-cp t)
  1298. priority (priority-cp t)
  1299. tags (block-tags-cp t)
  1300. bg-color (:background-color properties)
  1301. heading-level (or (and heading-level
  1302. (<= heading-level 6)
  1303. heading-level)
  1304. (and (get properties :heading)
  1305. (<= level 6)
  1306. level))
  1307. elem (if heading-level
  1308. (keyword (str "h" heading-level
  1309. (when block-ref? ".inline")))
  1310. :span.inline)]
  1311. (->elem
  1312. elem
  1313. (merge
  1314. {:id anchor}
  1315. (when (and marker
  1316. (not (string/blank? marker))
  1317. (not= "nil" marker))
  1318. {:class (str (string/lower-case marker))})
  1319. (when bg-color
  1320. {:style {:background-color bg-color
  1321. :padding-left 6
  1322. :padding-right 6
  1323. :color "#FFFFFF"}
  1324. :class "with-bg-color"}))
  1325. (remove-nils
  1326. (concat
  1327. [(when-not slide? checkbox)
  1328. (when-not slide? marker-switch)
  1329. marker-cp
  1330. priority]
  1331. (if title
  1332. (map-inline config title)
  1333. [[:span.opacity-50 "Click here to start writing, type '/' to see all the commands."]])
  1334. [tags])))))
  1335. (rum/defc span-comma
  1336. []
  1337. [:span ", "])
  1338. (rum/defc property-cp
  1339. [config block k v]
  1340. (let [pre-block? (:block/pre-block? block)
  1341. date (and (= k :date) (date/get-locale-string (str v)))]
  1342. [:div
  1343. [:span.font-bold (name k)]
  1344. [:span.mr-1 ":"]
  1345. (cond
  1346. (int? v)
  1347. v
  1348. date
  1349. date
  1350. (coll? v)
  1351. (let [v (->> (remove string/blank? v)
  1352. (filter string?))
  1353. vals (for [v-item v]
  1354. (page-cp config {:block/name v-item}))
  1355. elems (interpose (span-comma) vals)]
  1356. (for [elem elems]
  1357. (rum/with-key elem (str (random-uuid)))))
  1358. :else
  1359. (inline-text (:block/format block) (str v)))]))
  1360. (rum/defc properties-cp
  1361. [config block]
  1362. (let [properties (walk/keywordize-keys (:block/properties block))
  1363. properties-order (:block/properties-order block)
  1364. properties (apply dissoc properties (property/built-in-properties))
  1365. properties-order (remove (property/built-in-properties) properties-order)
  1366. pre-block? (:block/pre-block? block)
  1367. properties (if pre-block?
  1368. (let [repo (state/get-current-repo)
  1369. properties (dissoc properties :title :filters)
  1370. aliases (db/get-page-alias-names repo
  1371. (:block/name (db/pull (:db/id (:block/page block)))))]
  1372. (if (seq aliases)
  1373. (if (:alias properties)
  1374. (update properties :alias (fn [c]
  1375. (util/distinct-by string/lower-case (concat c aliases))))
  1376. (assoc properties :alias aliases))
  1377. properties))
  1378. properties)
  1379. properties (if (seq properties-order)
  1380. (map (fn [k] [k (get properties k)]) properties-order)
  1381. properties)]
  1382. (cond
  1383. (seq properties)
  1384. [:div.block-properties
  1385. {:class (when pre-block? "page-properties")
  1386. :title (if pre-block?
  1387. "Click to edit this page's properties"
  1388. "Click to edit this block's properties")}
  1389. (for [[k v] properties]
  1390. (rum/with-key (property-cp config block k v)
  1391. (str (:block/uuid block) "-" k)))]
  1392. (and pre-block? properties)
  1393. [:span.opacity-50 "Properties"]
  1394. :else
  1395. nil)))
  1396. (rum/defcs timestamp-cp < rum/reactive
  1397. (rum/local false ::show?)
  1398. (rum/local {} ::pos)
  1399. {:will-unmount (fn [state]
  1400. (when-let [show? (::show? state)]
  1401. (reset! show? false))
  1402. state)}
  1403. [state block typ ast]
  1404. (let [show? (get state ::show?)]
  1405. [:div.flex.flex-col
  1406. [:div.text-sm.mt-1.flex.flex-row
  1407. [:div.opacity-50.font-medium {:style {:width 95}}
  1408. (str typ ": ")]
  1409. [:a.opacity-80.hover:opacity-100
  1410. {:on-click (fn []
  1411. (if @show?
  1412. (do
  1413. (reset! show? false)
  1414. (reset! commands/*current-command nil)
  1415. (state/set-editor-show-date-picker! false)
  1416. (state/set-timestamp-block! nil))
  1417. (do
  1418. (reset! show? true)
  1419. (reset! commands/*current-command typ)
  1420. (state/set-editor-show-date-picker! true)
  1421. (state/set-timestamp-block! {:block block
  1422. :typ typ
  1423. :show? show?}))))}
  1424. (repeated/timestamp->text ast)]]
  1425. (when (true? @show?)
  1426. (let [ts (repeated/timestamp->map ast)]
  1427. [:div.my-4
  1428. (datetime-comp/date-picker nil nil ts)]))]))
  1429. (defn- block-content-on-mouse-down
  1430. [e block block-id properties content format edit-input-id]
  1431. (.stopPropagation e)
  1432. (let [target (gobj/get e "target")
  1433. button (gobj/get e "buttons")]
  1434. (when (contains? #{1 0} button)
  1435. (when-not (or
  1436. (d/has-class? target "bullet")
  1437. (util/link? target)
  1438. (util/input? target)
  1439. (util/details-or-summary? target)
  1440. (and (util/sup? target)
  1441. (d/has-class? target "fn"))
  1442. (d/has-class? target "image-resize"))
  1443. (editor-handler/clear-selection!)
  1444. (editor-handler/unhighlight-blocks!)
  1445. (let [block (or (db/pull [:block/uuid (:block/uuid block)]) block)
  1446. f #(let [cursor-range (util/caret-range (gdom/getElement block-id))
  1447. content (property/remove-built-in-properties (:block/format block)
  1448. content)]
  1449. ;; save current editing block
  1450. (let [{:keys [value] :as state} (editor-handler/get-state)]
  1451. (editor-handler/save-block! state value))
  1452. (state/set-editing!
  1453. edit-input-id
  1454. content
  1455. block
  1456. cursor-range
  1457. false))]
  1458. ;; wait a while for the value of the caret range
  1459. (if (util/ios?)
  1460. (f)
  1461. (js/setTimeout f 5)))
  1462. (when block-id (state/set-selection-start-block! block-id))))))
  1463. (rum/defc dnd-separator-wrapper < rum/reactive
  1464. [block block-id slide? top? block-content?]
  1465. (let [dragging? (rum/react *dragging?)
  1466. drag-to-block (rum/react *drag-to-block)]
  1467. (when (and
  1468. (= block-id drag-to-block)
  1469. dragging?
  1470. (not slide?)
  1471. (not (:block/pre-block? block)))
  1472. (let [move-to (rum/react *move-to)]
  1473. (when-not
  1474. (or (and top? (not= move-to :top))
  1475. (and (not top?) (= move-to :top))
  1476. (and block-content? (not= move-to :nested))
  1477. (and (not block-content?)
  1478. (seq (:block/children block))
  1479. (= move-to :nested)))
  1480. (dnd-separator block move-to block-content?))))))
  1481. (rum/defc block-content < rum/reactive
  1482. [config {:block/keys [uuid title body meta content marker page format repo children pre-block? properties idx container block-refs-count scheduled deadline repeated?] :as block} edit-input-id block-id slide?]
  1483. (let [collapsed? (get properties :collapsed)
  1484. block-ref? (:block-ref? config)
  1485. block-ref-with-title? (and block-ref? (seq title))
  1486. dragging? (rum/react *dragging?)
  1487. content (if (string? content) (string/trim content) "")
  1488. mouse-down-key (if (util/ios?)
  1489. :on-click
  1490. :on-mouse-down ; TODO: it seems that Safari doesn't work well with on-mouse-down
  1491. )
  1492. attrs (cond->
  1493. {:blockid (str uuid)
  1494. :style {:width "100%"}}
  1495. (not block-ref?)
  1496. (assoc mouse-down-key (fn [e]
  1497. (block-content-on-mouse-down e block block-id properties content format edit-input-id))))]
  1498. [:div.block-content.inline
  1499. (cond-> {:id (str "block-content-" uuid)}
  1500. (not slide?)
  1501. (merge attrs))
  1502. [:span
  1503. ;; .flex.relative {:style {:width "100%"}}
  1504. [:span
  1505. ;; .flex-1.flex-col.relative.block-content
  1506. (cond
  1507. (seq title)
  1508. (build-block-title config block)
  1509. :else
  1510. nil)
  1511. (when (seq children)
  1512. (dnd-separator-wrapper block block-id slide? false true))
  1513. (when deadline
  1514. (when-let [deadline-ast (block-handler/get-deadline-ast block)]
  1515. (timestamp-cp block "DEADLINE" deadline-ast)))
  1516. (when scheduled
  1517. (when-let [scheduled-ast (block-handler/get-scheduled-ast block)]
  1518. (timestamp-cp block "SCHEDULED" scheduled-ast)))
  1519. (when (and (seq properties)
  1520. (let [hidden? (property/properties-built-in? properties)]
  1521. (not hidden?))
  1522. (not block-ref?)
  1523. (not (:slide? config)))
  1524. (properties-cp config block))
  1525. (when (and (not block-ref-with-title?) (seq body))
  1526. [:div.block-body {:style {:display (if (and collapsed? (seq title)) "none" "")}}
  1527. ;; TODO: consistent id instead of the idx (since it could be changed later)
  1528. (let [body (block/trim-break-lines! (:block/body block))]
  1529. (for [[idx child] (medley/indexed body)]
  1530. (when-let [block (markup-element-cp config child)]
  1531. (rum/with-key (block-child block)
  1532. (str uuid "-" idx)))))])]]]))
  1533. (rum/defc block-content-or-editor < rum/reactive
  1534. [config {:block/keys [uuid title body meta content page format repo children marker properties block-refs-count pre-block? idx] :as block} edit-input-id block-id slide? heading-level]
  1535. (let [editor-box (get config :editor-box)
  1536. edit? (state/sub [:editor/editing? edit-input-id])
  1537. editor-id (str "editor-" edit-input-id)
  1538. slide? (:slide? config)]
  1539. (if (and edit? editor-box)
  1540. [:div.editor-wrapper {:id editor-id}
  1541. (editor-box {:block block
  1542. :block-id uuid
  1543. :block-parent-id block-id
  1544. :format format
  1545. :heading-level heading-level
  1546. :on-hide (fn [value event]
  1547. (when (= event :esc)
  1548. (editor-handler/escape-editing)))}
  1549. edit-input-id
  1550. config)]
  1551. [:div.flex.flex-row.block-content-wrapper
  1552. [:div.flex-1.w-full {:style {:display (if (:slide? config) "block" "flex")}}
  1553. (block-content config block edit-input-id block-id slide?)]
  1554. [:div.flex.flex-row
  1555. (when (and (:embed? config)
  1556. (not (:page-embed? config))
  1557. (= (:embed-id config) uuid))
  1558. [:a.opacity-30.hover:opacity-100.svg-small.inline
  1559. {:on-mouse-down (fn [e]
  1560. (util/stop e)
  1561. (when-let [block (:block config)]
  1562. (editor-handler/edit-block! block :max (:block/format block) (:block/uuid block))))}
  1563. svg/edit])
  1564. (when (and (= (:block/marker block) "DONE")
  1565. (state/enable-timetracking?))
  1566. (let [start-time (or
  1567. (get properties :now)
  1568. (get properties :doing)
  1569. (get properties :in-progress)
  1570. (get properties :later)
  1571. (get properties :todo))
  1572. finish-time (get properties :done)]
  1573. (when (and start-time finish-time (> finish-time start-time))
  1574. [:div.text-sm.time-spent.ml-1 {:title (str (date/int->local-time start-time) " ~ " (date/int->local-time finish-time))
  1575. :style {:padding-top 3}}
  1576. [:a.opacity-30.hover:opacity-100
  1577. (utils/timeConversion (- finish-time start-time))]])))
  1578. (when (and block-refs-count (> block-refs-count 0))
  1579. [:div
  1580. [:a.open-block-ref-link.bg-base-2.text-sm.ml-2
  1581. {:title "Open block references"
  1582. :style {:margin-top -1}
  1583. :on-click (fn []
  1584. (state/sidebar-add-block!
  1585. (state/get-current-repo)
  1586. (:db/id block)
  1587. :block-ref
  1588. {:block block}))}
  1589. block-refs-count]])]])))
  1590. (defn non-dragging?
  1591. [e]
  1592. (and (= (gobj/get e "buttons") 1)
  1593. (not (d/has-class? (gobj/get e "target") "bullet-container"))
  1594. (not (d/has-class? (gobj/get e "target") "bullet"))
  1595. (not @*dragging?)))
  1596. (rum/defc breadcrumb-fragment
  1597. [href label]
  1598. [:a {:href href} label])
  1599. (rum/defc breadcrumb-separator [] [:span.mx-2.opacity-50 "➤"])
  1600. (defn block-parents
  1601. ([config repo block-id format]
  1602. (block-parents config repo block-id format true))
  1603. ([config repo block-id format show-page?]
  1604. (let [parents (db/get-block-parents repo block-id 3)
  1605. page (db/get-block-page repo block-id)
  1606. page-name (:block/name page)
  1607. page-original-name (:block/original-name page)
  1608. show? (or (seq parents) show-page? page-name)]
  1609. (when show?
  1610. (let [page-name-props (when show-page?
  1611. [(rfe/href :page {:name page-name})
  1612. (or page-original-name page-name)])
  1613. parents-props (doall
  1614. (for [{:block/keys [uuid title name]} parents]
  1615. (when-not name ; not page
  1616. [(rfe/href :page {:name uuid})
  1617. (->elem :span (map-inline config title))])))
  1618. breadcrumb (->> (into [] parents-props)
  1619. (concat [page-name-props])
  1620. (filterv identity)
  1621. (map (fn [[href label]] (breadcrumb-fragment href label)))
  1622. (interpose (breadcrumb-separator)))]
  1623. [:div.block-parents.flex-row.flex-1 breadcrumb])))))
  1624. (defn- block-drag-over
  1625. [event uuid top? block-id *move-to]
  1626. (util/stop event)
  1627. (when-not (dnd-same-block? uuid)
  1628. (let [over-block (gdom/getElement block-id)
  1629. rect (utils/getOffsetRect over-block)
  1630. element-top (gobj/get rect "top")
  1631. element-left (gobj/get rect "left")
  1632. x-offset (- (.. event -pageX) element-left)
  1633. cursor-top (gobj/get event "clientY")
  1634. move-to-value (cond
  1635. (and top? (<= (js/Math.abs (- cursor-top element-top)) 16))
  1636. :top
  1637. (> x-offset 50)
  1638. :nested
  1639. :else
  1640. :sibling)]
  1641. (reset! *drag-to-block block-id)
  1642. (reset! *move-to move-to-value))))
  1643. (defn- block-drag-leave
  1644. [*move-to]
  1645. (reset! *move-to nil))
  1646. (defn- block-drop
  1647. [event uuid block *move-to]
  1648. (when-not (dnd-same-block? uuid)
  1649. (dnd/move-block event @*dragging-block
  1650. block
  1651. @*move-to))
  1652. (reset! *dragging? false)
  1653. (reset! *dragging-block nil)
  1654. (reset! *drag-to-block nil)
  1655. (reset! *move-to nil)
  1656. (editor-handler/unhighlight-blocks!))
  1657. (defn- block-mouse-over
  1658. [e has-child? *control-show? block-id doc-mode?]
  1659. (util/stop e)
  1660. (when has-child?
  1661. (reset! *control-show? true))
  1662. (when-let [parent (gdom/getElement block-id)]
  1663. (let [node (.querySelector parent ".bullet-container")]
  1664. (when doc-mode?
  1665. (d/remove-class! node "hide-inner-bullet"))))
  1666. (when (and
  1667. (state/in-selection-mode?)
  1668. (non-dragging? e))
  1669. (util/stop e)
  1670. (editor-handler/highlight-selection-area! block-id)))
  1671. (defn- block-mouse-leave
  1672. [e has-child? *control-show? block-id doc-mode?]
  1673. (util/stop e)
  1674. (when has-child?
  1675. (reset! *control-show? false))
  1676. (when doc-mode?
  1677. (when-let [parent (gdom/getElement block-id)]
  1678. (when-let [node (.querySelector parent ".bullet-container")]
  1679. (d/add-class! node "hide-inner-bullet"))))
  1680. (when (and (non-dragging? e)
  1681. (not @*resizing-image?))
  1682. (state/into-selection-mode!)))
  1683. (defn- on-drag-and-mouse-attrs
  1684. [block uuid top? block-id *move-to has-child? *control-show? doc-mode?]
  1685. {:on-drag-over (fn [event]
  1686. (block-drag-over event uuid top? block-id *move-to))
  1687. :on-drag-leave (fn [event]
  1688. (block-drag-leave *move-to))
  1689. :on-drop (fn [event]
  1690. (block-drop event uuid block *move-to))})
  1691. (defn- build-refs-data-value
  1692. [block refs]
  1693. (let [refs (model/get-page-names-by-ids
  1694. (->> (map :db/id refs)
  1695. (remove nil?)))]
  1696. (text/build-data-value refs)))
  1697. ;; (rum/defc block-immediate-children < rum/reactive
  1698. ;; [repo config uuid ref? collapsed?]
  1699. ;; (when (and ref? (not collapsed?))
  1700. ;; (let [children (db/get-block-immediate-children repo uuid)
  1701. ;; children (block-handler/filter-blocks repo children (:filters config) false)]
  1702. ;; (when (seq children)
  1703. ;; [:div.ref-children {:style {:margin-left "1.8rem"}}
  1704. ;; (blocks-container children (assoc config
  1705. ;; :breadcrumb-show? false
  1706. ;; :ref? true
  1707. ;; :ref-child? true))]))))
  1708. (rum/defcs block-container < rum/static
  1709. {:init (fn [state]
  1710. (let [[config block] (:rum/args state)
  1711. ref-collpased? (boolean
  1712. (and
  1713. (seq (:block/children block))
  1714. (or (:custom-query? config)
  1715. (and (:ref? config)
  1716. (>= (:ref/level block)
  1717. (state/get-ref-open-blocks-level))))))]
  1718. (assoc state
  1719. ::control-show? (atom false)
  1720. ::ref-collapsed? (atom ref-collpased?))))
  1721. :should-update (fn [old-state new-state]
  1722. (not= (:block/content (second (:rum/args old-state)))
  1723. (:block/content (second (:rum/args new-state)))))}
  1724. [state config {:block/keys [uuid title body meta content page format repo children pre-block? top? properties refs path-refs heading-level level type idx] :as block}]
  1725. (let [blocks-container-id (:blocks-container-id config)
  1726. config (update config :block merge block)
  1727. ;; Each block might have multiple queries, but we store only the first query's result
  1728. config (if (nil? (:query-result config))
  1729. (assoc config :query-result (atom nil))
  1730. config)
  1731. heading? (and (= type :heading) heading-level (<= heading-level 6))
  1732. *control-show? (get state ::control-show?)
  1733. *ref-collapsed? (get state ::ref-collapsed?)
  1734. collapsed? (or @*ref-collapsed?
  1735. (get properties :collapsed))
  1736. ref? (boolean (:ref? config))
  1737. breadcrumb-show? (:breadcrumb-show? config)
  1738. sidebar? (boolean (:sidebar? config))
  1739. slide? (boolean (:slide? config))
  1740. doc-mode? (:document/mode? config)
  1741. embed? (:embed? config)
  1742. reference? (:reference? config)
  1743. block-id (str "ls-block-" blocks-container-id "-" uuid)
  1744. has-child? (boolean
  1745. (and
  1746. (not pre-block?)
  1747. (or (and (coll? children) (seq children))
  1748. (seq body))))
  1749. attrs (on-drag-and-mouse-attrs block uuid top? block-id *move-to has-child? *control-show? doc-mode?)
  1750. data-refs (build-refs-data-value block (remove (set refs) path-refs))
  1751. data-refs-self (build-refs-data-value block refs)
  1752. edit-input-id (str "edit-block-" blocks-container-id "-" uuid)]
  1753. [:div.ls-block.flex.flex-col.rounded-sm
  1754. (cond->
  1755. {:id block-id
  1756. :data-refs data-refs
  1757. :data-refs-self data-refs-self
  1758. :style {:position "relative"}
  1759. :class (str uuid
  1760. (when (and collapsed? has-child?) " collapsed")
  1761. (when pre-block? " pre-block"))
  1762. :blockid (str uuid)
  1763. :repo repo
  1764. :haschild (str has-child?)}
  1765. level
  1766. (assoc :level level)
  1767. (not slide?)
  1768. (merge attrs)
  1769. (or reference? embed?)
  1770. (assoc :data-transclude true))
  1771. (when (and ref? breadcrumb-show?)
  1772. (when-let [comp (block-parents config repo uuid format false)]
  1773. [:div.my-2.opacity-50.ml-4 comp]))
  1774. ;; only render this for the first block in each container
  1775. (when top?
  1776. (dnd-separator-wrapper block block-id slide? true false))
  1777. [:div.flex.flex-row.pr-2
  1778. {:class (if heading? "items-baseline" "")
  1779. :on-mouse-over (fn [e]
  1780. (block-mouse-over e has-child? *control-show? block-id doc-mode?))
  1781. :on-mouse-leave (fn [e]
  1782. (block-mouse-leave e has-child? *control-show? block-id doc-mode?))}
  1783. (when (not slide?)
  1784. (block-control config block uuid block-id body children collapsed? *ref-collapsed? *control-show? edit-input-id))
  1785. (block-content-or-editor config block edit-input-id block-id slide? heading-level)]
  1786. (block-children config children collapsed? *ref-collapsed?)
  1787. (dnd-separator-wrapper block block-id slide? false false)]))
  1788. (defn divide-lists
  1789. [[f & l]]
  1790. (loop [l l
  1791. ordered? (:ordered f)
  1792. result [[f]]]
  1793. (if (seq l)
  1794. (let [cur (first l)
  1795. cur-ordered? (:ordered cur)]
  1796. (if (= ordered? cur-ordered?)
  1797. (recur
  1798. (rest l)
  1799. cur-ordered?
  1800. (update result (dec (count result)) conj cur))
  1801. (recur
  1802. (rest l)
  1803. cur-ordered?
  1804. (conj result [cur]))))
  1805. result)))
  1806. (defn list-element
  1807. [l]
  1808. (match l
  1809. [l1 & tl]
  1810. (let [{:keys [ordered name]} l1]
  1811. (cond
  1812. (seq name)
  1813. :dl
  1814. ordered
  1815. :ol
  1816. :else
  1817. :ul))
  1818. :else
  1819. :ul))
  1820. (defn list-item
  1821. [config {:keys [name content checkbox items number] :as l}]
  1822. (let [content (when-not (empty? content)
  1823. (match content
  1824. [["Paragraph" i] & rest]
  1825. (vec-cat
  1826. (map-inline config i)
  1827. (markup-elements-cp config rest))
  1828. :else
  1829. (markup-elements-cp config content)))
  1830. checked? (some? checkbox)
  1831. items (if (seq items)
  1832. (->elem
  1833. (list-element items)
  1834. (for [item items]
  1835. (list-item config item))))]
  1836. (cond
  1837. (seq name)
  1838. [:dl {:checked checked?}
  1839. [:dt (map-inline config name)]
  1840. (->elem :dd
  1841. (vec-cat content [items]))]
  1842. :else
  1843. (if (nil? checkbox)
  1844. (->elem
  1845. :li
  1846. (cond->
  1847. {:checked checked?}
  1848. number
  1849. (assoc :value number))
  1850. (vec-cat
  1851. [(->elem
  1852. :p
  1853. content)]
  1854. [items]))
  1855. (->elem
  1856. :li
  1857. {:checked checked?}
  1858. (vec-cat
  1859. [(->elem
  1860. :p
  1861. (list-checkbox checkbox)
  1862. content)]
  1863. [items]))))))
  1864. (defn table
  1865. [config {:keys [header groups col_groups]}]
  1866. (let [tr (fn [elm cols]
  1867. (->elem
  1868. :tr
  1869. (mapv (fn [col]
  1870. (->elem
  1871. elm
  1872. {:scope "col"
  1873. :class "org-left"}
  1874. (map-inline config col)))
  1875. cols)))
  1876. tb-col-groups (try
  1877. (mapv (fn [number]
  1878. (let [col-elem [:col {:class "org-left"}]]
  1879. (->elem
  1880. :colgroup
  1881. (repeat number col-elem))))
  1882. col_groups)
  1883. (catch js/Error e
  1884. []))
  1885. head (if header
  1886. [:thead (tr :th header)])
  1887. groups (mapv (fn [group]
  1888. (->elem
  1889. :tbody
  1890. (mapv #(tr :td %) group)))
  1891. groups)]
  1892. [:div.table-wrapper {:style {:max-width (min 700
  1893. (gobj/get js/window "innerWidth"))}}
  1894. (->elem
  1895. :table
  1896. {:class "table-auto"
  1897. :border 2
  1898. :cell-spacing 0
  1899. :cell-padding 6
  1900. :rules "groups"
  1901. :frame "hsides"}
  1902. (vec-cat
  1903. tb-col-groups
  1904. (cons head groups)))]))
  1905. (defn map-inline
  1906. [config col]
  1907. (map #(inline config %) col))
  1908. (declare ->hiccup)
  1909. (defn built-in-custom-query?
  1910. [title]
  1911. (let [repo (state/get-current-repo)]
  1912. (let [queries (state/sub [:config repo :default-queries :journals])]
  1913. (when (seq queries)
  1914. (boolean (some #(= % title) (map :title queries)))))))
  1915. (defn- trigger-custom-query!
  1916. [state]
  1917. (let [[config query] (:rum/args state)
  1918. repo (state/get-current-repo)
  1919. result-atom (atom nil)
  1920. query-atom (if (:dsl-query? config)
  1921. (let [result (query-dsl/query (state/get-current-repo) (:query query))]
  1922. (cond
  1923. (and (util/electron?) (string? result)) ; full-text search
  1924. (if (string/blank? result)
  1925. (atom [])
  1926. (p/let [blocks (search/block-search repo result {:limit 30})]
  1927. (when (seq blocks)
  1928. (let [result (db/pull-many (state/get-current-repo) '[*] (map (fn [b] [:block/uuid (uuid (:block/uuid b))]) blocks))]
  1929. (reset! result-atom result)))))
  1930. (string? result)
  1931. (atom nil)
  1932. :else
  1933. result))
  1934. (db/custom-query query))
  1935. query-atom (if (instance? Atom query-atom)
  1936. query-atom
  1937. result-atom)]
  1938. (assoc state :query-atom query-atom)))
  1939. (rum/defcs custom-query < rum/reactive
  1940. {:will-mount trigger-custom-query!
  1941. :did-mount (fn [state]
  1942. (when-let [query (last (:rum/args state))]
  1943. (state/add-custom-query-component! query (:rum/react-component state)))
  1944. state)
  1945. :did-remount (fn [_old_state state]
  1946. (trigger-custom-query! state))
  1947. :will-unmount (fn [state]
  1948. (when-let [query (last (:rum/args state))]
  1949. (state/remove-custom-query-component! query)
  1950. (db/remove-custom-query! (state/get-current-repo) query))
  1951. state)}
  1952. [state config {:keys [title query inputs view collapsed? children?] :as q}]
  1953. (ui/catch-error
  1954. [:div.warning
  1955. [:p "Query failed: "]
  1956. [:pre (str q)]]
  1957. (let [dsl-query? (:dsl-query? config)
  1958. query-atom (:query-atom state)]
  1959. (let [current-block-uuid (or (:block/uuid (:block config))
  1960. (:block/uuid config))
  1961. current-block (db/entity [:block/uuid current-block-uuid])
  1962. ;; exclude the current one, otherwise it'll loop forever
  1963. remove-blocks (if current-block-uuid [current-block-uuid] nil)
  1964. query-result (and query-atom (rum/react query-atom))
  1965. table? (or (get-in current-block [:block/properties :query-table])
  1966. (and (string? query) (string/ends-with? (string/trim query) "table")))
  1967. transformed-query-result (when query-result
  1968. (db/custom-query-result-transform query-result remove-blocks q))
  1969. not-grouped-by-page? (or table?
  1970. (and (string? query) (string/includes? query "(by-page false)")))
  1971. result (if (and (:block/uuid (first transformed-query-result)) (not not-grouped-by-page?))
  1972. (db-utils/group-by-page transformed-query-result)
  1973. transformed-query-result)
  1974. _ (when-let [query-result (:query-result config)]
  1975. (let [result (remove (fn [b] (some? (get-in b [:block/properties :template]))) result)]
  1976. (reset! query-result result)))
  1977. view-f (and view (sci/eval-string (pr-str view)))
  1978. only-blocks? (:block/uuid (first result))
  1979. blocks-grouped-by-page? (and (seq result)
  1980. (not not-grouped-by-page?)
  1981. (coll? (first result))
  1982. (:block/name (ffirst result))
  1983. (:block/uuid (first (second (first result))))
  1984. true)
  1985. built-in? (built-in-custom-query? title)
  1986. page-list? (and (seq result)
  1987. (:block/name (first result)))]
  1988. [:div.custom-query.mt-4 (get config :attr {})
  1989. (when-not (and built-in? (empty? result))
  1990. (ui/foldable
  1991. [:div.custom-query-title
  1992. title
  1993. [:span.opacity-60.text-sm.ml-2
  1994. (str (count transformed-query-result) " results")]]
  1995. [:div
  1996. (when current-block
  1997. [:div.flex.flex-row.align-items.mt-2 {:on-mouse-down (fn [e] (util/stop e))}
  1998. (when-not page-list?
  1999. [:div.flex.flex-row
  2000. [:div.mx-2 [:span.text-sm "Table view"]]
  2001. [:div {:style {:margin-top 5}}
  2002. (ui/toggle table?
  2003. (fn []
  2004. (editor-handler/set-block-property! current-block-uuid
  2005. "query-table"
  2006. (not table?)))
  2007. true)]])
  2008. [:a.mx-2.opacity-60.hover:opacity-100.block
  2009. {:on-click (fn []
  2010. (let [all-keys (query-table/get-keys result page-list?)]
  2011. (state/pub-event! [:modal/set-query-properties current-block all-keys])))}
  2012. [:span.table-query-properties
  2013. [:span.text-sm.mr-1 "Set properties"]
  2014. svg/settings-sm]]])
  2015. (cond
  2016. (and (seq result) view-f)
  2017. (let [result (try
  2018. (sci/call-fn view-f result)
  2019. (catch js/Error error
  2020. (log/error :custom-view-failed {:error error
  2021. :result result})
  2022. [:div "Custom view failed: "
  2023. (str error)]))]
  2024. (util/hiccup-keywordize result))
  2025. page-list?
  2026. (query-table/result-table config current-block result {:page? true} map-inline page-cp ->elem inline-text)
  2027. table?
  2028. (query-table/result-table config current-block result {:page? false} map-inline page-cp ->elem inline-text)
  2029. (and (seq result) (or only-blocks? blocks-grouped-by-page?))
  2030. (->hiccup result (cond-> (assoc config
  2031. :custom-query? true
  2032. :breadcrumb-show? true
  2033. :group-by-page? blocks-grouped-by-page?
  2034. :ref? true)
  2035. children?
  2036. (assoc :ref? true))
  2037. {:style {:margin-top "0.25rem"
  2038. :margin-left "0.25rem"}})
  2039. (seq result)
  2040. (let [result (->>
  2041. (for [record result]
  2042. (if (map? record)
  2043. (str (util/pp-str record) "\n")
  2044. record))
  2045. (remove nil?))]
  2046. [:pre result])
  2047. :else
  2048. [:div.text-sm.mt-2.ml-2.font-medium.opacity-50 "Empty"])]
  2049. collapsed?))]))))
  2050. (defn admonition
  2051. [config type options result]
  2052. (when-let [icon (case (string/lower-case (name type))
  2053. "note" svg/note
  2054. "tip" svg/tip
  2055. "important" svg/important
  2056. "caution" svg/caution
  2057. "warning" svg/warning
  2058. "pinned" svg/pinned
  2059. nil)]
  2060. [:div.flex.flex-row.admonitionblock.align-items {:class type}
  2061. [:div.pr-4.admonition-icon.flex.flex-col.justify-center
  2062. {:title (string/upper-case type)} (icon)]
  2063. [:div.ml-4.text-lg
  2064. (markup-elements-cp config result)]]))
  2065. ;; TODO: move to mldoc
  2066. ;; (defn- convert-md-src-to-custom-block
  2067. ;; [item]
  2068. ;; (let [{:keys [language options lines] :as options} (second item)
  2069. ;; lang (string/lower-case (or language ""))]
  2070. ;; (cond
  2071. ;; (= lang "quote")
  2072. ;; (let [content (string/trim (string/join "\n" lines))]
  2073. ;; ["Quote" (first (mldoc/->edn content (mldoc/default-config :markdown)))])
  2074. ;; (contains? #{"query" "note" "tip" "important" "caution" "warning" "pinned"} lang)
  2075. ;; (let [content (string/trim (string/join "\n" lines))]
  2076. ;; ["Custom" lang nil (first (mldoc/->edn content (mldoc/default-config :markdown))) content])
  2077. ;; :else
  2078. ;; ["Src" options])))
  2079. (rum/defc src-cp < rum/static
  2080. [config options html-export?]
  2081. (when options
  2082. (let [{:keys [language lines pos_meta]} options
  2083. attr (if language
  2084. {:data-lang language})
  2085. code (join-lines lines)]
  2086. (cond
  2087. html-export?
  2088. (highlight/html-export attr code)
  2089. :else
  2090. (let [language (if (contains? #{"edn" "clj" "cljc" "cljs" "clojure"} language) "text/x-clojure" language)]
  2091. (if (:slide? config)
  2092. (highlight/highlight (str (medley/random-uuid)) {:data-lang language} code)
  2093. [:div
  2094. (lazy-editor/editor config (str (dc/squuid)) attr code options)
  2095. (let [options (:options options)]
  2096. (when (and (= language "text/x-clojure") (contains? (set options) ":results"))
  2097. (sci/eval-result code)))]))))))
  2098. (defn markup-element-cp
  2099. [{:keys [html-export?] :as config} item]
  2100. (let [format (or (:block/format config)
  2101. :markdown)]
  2102. (try
  2103. (match item
  2104. ["Properties" m]
  2105. [:div.properties
  2106. (for [[k v] (dissoc m :roam_alias :roam_tags)]
  2107. (when (and (not (and (= k :macros) (empty? v))) ; empty macros
  2108. (not (= k :title))
  2109. (not (= k :filters)))
  2110. [:div.property
  2111. [:span.font-medium.mr-1 (str (name k) ": ")]
  2112. (if (coll? v)
  2113. (let [vals (for [item v]
  2114. (if (coll? v)
  2115. (let [config (if (= k :alias)
  2116. (assoc config :block/alias? true))]
  2117. (page-cp config {:block/name item}))
  2118. (inline-text format item)))]
  2119. (interpose [:span ", "] vals))
  2120. (inline-text format v))]))]
  2121. ["Paragraph" l]
  2122. ;; TODO: speedup
  2123. (if (util/safe-re-find #"\"Export_Snippet\" \"embed\"" (str l))
  2124. (->elem :div (map-inline config l))
  2125. (->elem :div.is-paragraph (map-inline config l)))
  2126. ["Horizontal_Rule"]
  2127. (when-not (:slide? config)
  2128. [:hr])
  2129. ["Heading" h]
  2130. (block-container config h)
  2131. ["List" l]
  2132. (let [lists (divide-lists l)]
  2133. (if (= 1 (count lists))
  2134. (let [l (first lists)]
  2135. (->elem
  2136. (list-element l)
  2137. (map #(list-item config %) l)))
  2138. [:div.list-group
  2139. (for [l lists]
  2140. (->elem
  2141. (list-element l)
  2142. (map #(list-item config %) l)))]))
  2143. ["Table" t]
  2144. (table config t)
  2145. ["Math" s]
  2146. (if html-export?
  2147. (latex/html-export s true true)
  2148. (latex/latex (str (dc/squuid)) s true true))
  2149. ["Example" l]
  2150. [:pre.pre-wrap-white-space
  2151. (join-lines l)]
  2152. ["Quote" l]
  2153. (->elem
  2154. :blockquote
  2155. (markup-elements-cp config l))
  2156. ["Raw_Html" content]
  2157. (when (not html-export?)
  2158. [:div.raw_html {:dangerouslySetInnerHTML
  2159. {:__html content}}])
  2160. ["Export" "html" options content]
  2161. (when (not html-export?)
  2162. [:div.export_html {:dangerouslySetInnerHTML
  2163. {:__html content}}])
  2164. ["Hiccup" content]
  2165. (ui/catch-error
  2166. [:div.warning {:title "Invalid hiccup"}
  2167. content]
  2168. (-> (safe-read-string content)
  2169. (security/remove-javascript-links-in-href)))
  2170. ["Export" "latex" options content]
  2171. (if html-export?
  2172. (latex/html-export content true false)
  2173. (latex/latex (str (dc/squuid)) content true false))
  2174. ["Custom" "query" _options result content]
  2175. (try
  2176. (let [query (reader/read-string content)]
  2177. (custom-query config query))
  2178. (catch js/Error e
  2179. (println "read-string error:")
  2180. (js/console.error e)
  2181. [:div.warning {:title "Invalid query"}
  2182. content]))
  2183. ["Custom" "note" options result content]
  2184. (admonition config "note" options result)
  2185. ["Custom" "tip" options result content]
  2186. (admonition config "tip" options result)
  2187. ["Custom" "important" options result content]
  2188. (admonition config "important" options result)
  2189. ["Custom" "caution" options result content]
  2190. (admonition config "caution" options result)
  2191. ["Custom" "warning" options result content]
  2192. (admonition config "warning" options result)
  2193. ["Custom" "pinned" options result content]
  2194. (admonition config "pinned" options result)
  2195. ["Custom" name options l content]
  2196. (->elem
  2197. :div
  2198. {:class name}
  2199. (markup-elements-cp config l))
  2200. ["Latex_Fragment" l]
  2201. [:p.latex-fragment
  2202. (inline config ["Latex_Fragment" l])]
  2203. ["Latex_Environment" name option content]
  2204. (let [content (latex-environment-content name option content)]
  2205. (if html-export?
  2206. (latex/html-export content true true)
  2207. (latex/latex (str (dc/squuid)) content true true)))
  2208. ["Displayed_Math" content]
  2209. (if html-export?
  2210. (latex/html-export content true true)
  2211. (latex/latex (str (dc/squuid)) content true true))
  2212. ["Footnote_Definition" name definition]
  2213. (let [id (util/url-encode name)]
  2214. [:div.footdef
  2215. [:div.footpara
  2216. (conj
  2217. (markup-element-cp config ["Paragraph" definition])
  2218. [:a.ml-1 {:id (str "fn." id)
  2219. :style {:font-size 14}
  2220. :class "footnum"
  2221. :on-click #(route-handler/jump-to-anchor! (str "fnr." id))}
  2222. [:sup.fn (str name "↩︎")]])]])
  2223. ["Src" options]
  2224. (src-cp config options html-export?)
  2225. :else
  2226. "")
  2227. (catch js/Error e
  2228. (println "Convert to html failed, error: " e)
  2229. ""))))
  2230. (defn markup-elements-cp
  2231. [config col]
  2232. (map #(markup-element-cp config %) col))
  2233. (defn build-slide-sections
  2234. ([blocks config]
  2235. (build-slide-sections blocks config nil))
  2236. ([blocks config build-block-fn]
  2237. (when (seq blocks)
  2238. (let [blocks (map #(dissoc % :block/children) blocks)
  2239. first-block-level (:block/level (first blocks))
  2240. sections (reduce
  2241. (fn [acc block]
  2242. (let [block (dissoc block :block/meta)
  2243. level (:block/level block)
  2244. config (assoc config :block/uuid (:block/uuid block))
  2245. block-cp (if build-block-fn
  2246. (build-block-fn config block)
  2247. (rum/with-key
  2248. (block-container config block)
  2249. (str "slide-" (:block/uuid block))))]
  2250. (if (= first-block-level level)
  2251. ;; new slide
  2252. (conj acc [[block block-cp]])
  2253. (update acc (dec (count acc))
  2254. (fn [sections]
  2255. (conj sections [block block-cp]))))))
  2256. []
  2257. blocks)]
  2258. sections))))
  2259. (defn- block-list
  2260. [config blocks]
  2261. (for [[idx item] (medley/indexed blocks)]
  2262. (let [item (->
  2263. (dissoc item :block/meta)
  2264. (assoc :block/top? (zero? idx)
  2265. :block/bottom? (= (count blocks) (inc idx))))
  2266. config (assoc config :block/uuid (:block/uuid item))]
  2267. (rum/with-key
  2268. (block-container config item)
  2269. (:block/uuid item)))))
  2270. (defonce ignore-scroll? (atom false))
  2271. (rum/defcs lazy-blocks <
  2272. (rum/local 1 ::page)
  2273. [state config blocks]
  2274. (let [*page (get state ::page)
  2275. segment (->> blocks
  2276. (drop (* (dec @*page) max-blocks-per-page))
  2277. (take max-blocks-per-page))
  2278. bottom-reached (fn []
  2279. (when (and (= (count segment) max-blocks-per-page)
  2280. (> (count blocks) (* @*page max-blocks-per-page))
  2281. (not @ignore-scroll?))
  2282. (swap! *page inc)
  2283. (util/scroll-to-top))
  2284. (reset! ignore-scroll? false))
  2285. top-reached (fn []
  2286. (when (> @*page 1)
  2287. (swap! *page dec)
  2288. (reset! ignore-scroll? true)
  2289. (js/setTimeout #(util/scroll-to
  2290. (.-scrollHeight (js/document.getElementById "lazy-blocks"))) 100)))]
  2291. [:div#lazy-blocks
  2292. (when (> @*page 1)
  2293. [:div.ml-4.mb-4 [:a#prev.opacity-60.opacity-100.text-sm.font-medium {:on-click top-reached}
  2294. "Prev"]])
  2295. (ui/infinite-list
  2296. "main-container"
  2297. (block-list config segment)
  2298. {:on-load bottom-reached})
  2299. (when (> (count blocks) (* @*page max-blocks-per-page))
  2300. [:div.ml-4.mt-4 [:a#more.opacity-60.opacity-100.text-sm.font-medium {:on-click bottom-reached}
  2301. "More"]])]))
  2302. (rum/defcs blocks-container <
  2303. {:init (fn [state]
  2304. (assoc state ::init-blocks-container-id (atom nil)))}
  2305. [state blocks config]
  2306. (let [*init-blocks-container-id (::init-blocks-container-id state)
  2307. blocks-container-id (if @*init-blocks-container-id
  2308. @*init-blocks-container-id
  2309. (let [id' (swap! *blocks-container-id inc)]
  2310. (reset! *init-blocks-container-id id')
  2311. id'))
  2312. sidebar? (:sidebar? config)
  2313. ref? (:ref? config)
  2314. custom-query? (:custom-query? config)
  2315. blocks->vec-tree #(if (or custom-query? ref?) % (tree/blocks->vec-tree % (:id config)))
  2316. blocks' (blocks->vec-tree blocks)
  2317. blocks (if (seq blocks') blocks' blocks)
  2318. config (assoc config :blocks-container-id blocks-container-id)
  2319. doc-mode? (:document/mode? config)]
  2320. (when (seq blocks)
  2321. [:div.blocks-container.flex-1
  2322. {:class (when doc-mode? "document-mode")
  2323. :style {:margin-left (cond
  2324. sidebar?
  2325. 0
  2326. :else
  2327. -10)}}
  2328. (lazy-blocks config blocks)])))
  2329. ;; headers to hiccup
  2330. (defn ->hiccup
  2331. [blocks config option]
  2332. [:div.content
  2333. (cond-> option
  2334. (:document/mode? config) (assoc :class "doc-mode"))
  2335. (if (and (:group-by-page? config)
  2336. (vector? (first blocks)))
  2337. [:div.flex.flex-col
  2338. (let [blocks (sort-by (comp :block/journal-day first) > blocks)]
  2339. (for [[page blocks] blocks]
  2340. (let [alias? (:block/alias? page)
  2341. page (db/entity (:db/id page))]
  2342. [:div.my-2 (cond-> {:key (str "page-" (:db/id page))}
  2343. (:ref? config)
  2344. (assoc :class "color-level px-7 py-2 rounded"))
  2345. (ui/foldable
  2346. [:div
  2347. (page-cp config page)
  2348. (when alias? [:span.text-sm.font-medium.opacity-50 " Alias"])]
  2349. (blocks-container blocks config))])))]
  2350. (blocks-container blocks config))])