block.cljs 77 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050
  1. (ns frontend.components.block
  2. (:refer-clojure :exclude [range])
  3. (:require [frontend.config :as config]
  4. [cljs.core.match :refer-macros [match]]
  5. [promesa.core :as p]
  6. [frontend.fs :as fs]
  7. [clojure.string :as string]
  8. [frontend.util :as util]
  9. [rum.core :as rum]
  10. [frontend.state :as state]
  11. [frontend.db :as db]
  12. [frontend.db.model :as model]
  13. [frontend.db.query-dsl :as query-dsl]
  14. [dommy.core :as d]
  15. [datascript.core :as dc]
  16. [goog.dom :as gdom]
  17. [frontend.handler.expand :as expand]
  18. [frontend.components.svg :as svg]
  19. [frontend.components.draw :as draw]
  20. [frontend.components.datetime :as datetime-comp]
  21. [frontend.ui :as ui]
  22. [frontend.handler.editor :as editor-handler]
  23. [frontend.handler.block :as block-handler]
  24. [frontend.handler.route :as route-handler]
  25. [frontend.handler.dnd :as dnd]
  26. [frontend.handler.ui :as ui-handler]
  27. [frontend.handler.repeated :as repeated]
  28. [goog.object :as gobj]
  29. [medley.core :as medley]
  30. [cljs.reader :as reader]
  31. [frontend.util :as util :refer-macros [profile]]
  32. [frontend.db-mixins :as db-mixins]
  33. [frontend.extensions.latex :as latex]
  34. [frontend.components.lazy-editor :as lazy-editor]
  35. [frontend.extensions.highlight :as highlight]
  36. [frontend.extensions.sci :as sci]
  37. ["/frontend/utils" :as utils]
  38. [frontend.format.block :as block]
  39. [clojure.walk :as walk]
  40. [cljs-bean.core :as bean]
  41. [frontend.handler.image :as image-handler]
  42. [frontend.format.mldoc :as mldoc]
  43. [frontend.text :as text]
  44. [frontend.utf8 :as utf8]
  45. [frontend.date :as date]
  46. [frontend.security :as security]
  47. [reitit.frontend.easy :as rfe]
  48. [frontend.commands :as commands]
  49. [lambdaisland.glogi :as log]
  50. [frontend.context.i18n :as i18n]))
  51. (defn safe-read-string
  52. ([s]
  53. (safe-read-string s true))
  54. ([s warn?]
  55. (try
  56. (reader/read-string s)
  57. (catch js/Error e
  58. (println "read-string error:")
  59. (js/console.error e)
  60. (when warn?
  61. [:div.warning {:title "read-string failed"}
  62. s])))))
  63. ;; local state
  64. (defonce *block-children
  65. (atom {}))
  66. (defonce *dragging?
  67. (atom false))
  68. (defonce *dragging-block
  69. (atom nil))
  70. (defonce *move-to-top?
  71. (atom false))
  72. ;; TODO: Improve blocks grouped by pages
  73. (defonce max-blocks-per-page 500)
  74. (defonce virtual-list-scroll-step 450)
  75. (defonce virtual-list-previous 50)
  76. (defonce container-ids (atom {}))
  77. (defonce container-idx (atom 0))
  78. ;; TODO:
  79. ;; add `key`
  80. (defn- remove-nils
  81. [col]
  82. (remove nil? col))
  83. (defn vec-cat
  84. [& args]
  85. (->> (apply concat args)
  86. remove-nils
  87. vec))
  88. (defn ->elem
  89. ([elem items]
  90. (->elem elem nil items))
  91. ([elem attrs items]
  92. (let [elem (keyword elem)]
  93. (if attrs
  94. (vec
  95. (cons elem
  96. (cons attrs
  97. (seq items))))
  98. (vec
  99. (cons elem
  100. (seq items)))))))
  101. (defn- join-lines
  102. [l]
  103. (string/trim (apply str l)))
  104. (defn- string-of-url
  105. [url]
  106. (match url
  107. (:or ["File" s] ["Search" s])
  108. s
  109. ["Complex" m]
  110. (let [{:keys [link protocol]} m]
  111. (if (= protocol "file")
  112. link
  113. (str protocol ":" link)))))
  114. (defn- get-file-absolute-path
  115. [config path]
  116. (let [path (string/replace path "file:" "")
  117. block-id (:block/uuid config)
  118. current-file (and block-id
  119. (:file/path (:page/file (:block/page (db/entity [:block/uuid block-id])))))]
  120. (when current-file
  121. (let [parts (string/split current-file #"/")
  122. parts-2 (string/split path #"/")
  123. current-dir (string/join "/" (drop-last 1 parts))]
  124. (cond
  125. (util/starts-with? path "/")
  126. path
  127. (and (not (util/starts-with? path ".."))
  128. (not (util/starts-with? path ".")))
  129. (str current-dir "/" path)
  130. :else
  131. (let [parts (loop [acc []
  132. parts (reverse parts)
  133. col (reverse parts-2)]
  134. (if (empty? col)
  135. acc
  136. (let [[part parts] (case (first col)
  137. ".."
  138. [(first parts) (rest parts)]
  139. "."
  140. ["" parts]
  141. [(first col) (rest parts)])]
  142. (recur (conj acc part)
  143. parts
  144. (rest col)))))
  145. parts (remove #(string/blank? %) parts)]
  146. (string/join "/" (reverse parts))))))))
  147. (defonce *resizing-image? (atom false))
  148. (rum/defcs resizable-image <
  149. (rum/local nil ::size)
  150. {:will-unmount (fn [state]
  151. (reset! *resizing-image? false)
  152. state)}
  153. [state config title src metadata full_text local?]
  154. (rum/with-context [[t] i18n/*tongue-context*]
  155. (let [size (get state ::size)]
  156. (ui/resize-provider
  157. (ui/resize-consumer
  158. (cond->
  159. {:className "resize"
  160. :onSizeChanged (fn [value]
  161. (when (and (not @*resizing-image?)
  162. (some? @size)
  163. (not= value @size))
  164. (reset! *resizing-image? true))
  165. (reset! size value))
  166. :onMouseUp (fn []
  167. (when @size
  168. (when-let [block-id (:block/uuid config)]
  169. (let [size (bean/->clj @size)]
  170. (editor-handler/resize-image! block-id metadata full_text size))))
  171. (when @*resizing-image?
  172. ;; TODO: need a better way to prevent the clicking to edit current block
  173. (js/setTimeout #(reset! *resizing-image? false) 200)))
  174. :onClick (fn [e]
  175. (when @*resizing-image? (util/stop e)))}
  176. (:width metadata)
  177. (assoc :style {:width (:width metadata)}))
  178. [:div.asset-container
  179. [:img.rounded-sm.shadow-xl.relative
  180. (merge
  181. {:loading "lazy"
  182. :src src
  183. :title title}
  184. metadata)]
  185. [:span.ctl
  186. [:a.delete
  187. {:title "Delete this image"
  188. :on-click
  189. (fn [e]
  190. (when-let [block-id (:block/uuid config)]
  191. (let [confirm-fn (ui/make-confirm-modal
  192. {:title (t :asset/confirm-delete (.toLocaleLowerCase (t :text/image)))
  193. :sub-title (if local? :asset/physical-delete "")
  194. :sub-checkbox? local?
  195. :on-confirm (fn [e {:keys [close-fn sub-selected]}]
  196. (close-fn)
  197. (editor-handler/delete-asset-of-block!
  198. {:block-id block-id
  199. :local? local?
  200. :repo (state/get-current-repo)
  201. :href src
  202. :title title
  203. :full-text full_text}))})]
  204. (state/set-modal! confirm-fn)
  205. (util/stop e))))}
  206. svg/trash-sm]]])))))
  207. (rum/defcs asset-link < rum/reactive
  208. (rum/local nil ::src)
  209. [state config title href label metadata full_text]
  210. (let [src (::src state)
  211. granted? (state/sub [:nfs/user-granted? (state/get-current-repo)])]
  212. (when granted?
  213. (p/then (editor-handler/make-asset-url href) #(reset! src %)))
  214. (when @src
  215. (resizable-image config title @src metadata full_text true))))
  216. ;; TODO: safe encoding asciis
  217. ;; TODO: image link to another link
  218. (defn image-link [config url href label metadata full_text]
  219. (let [metadata (if (string/blank? metadata)
  220. nil
  221. (safe-read-string metadata false))
  222. title (second (first label))]
  223. (if (config/local-asset? href)
  224. (asset-link config title href label metadata full_text)
  225. (let [href (if (util/starts-with? href "http")
  226. href
  227. (get-file-absolute-path config href))]
  228. (resizable-image config title href metadata full_text false)))))
  229. (defn repetition-to-string
  230. [[[kind] [duration] n]]
  231. (let [kind (case kind
  232. "Dotted" "."
  233. "Plus" "+"
  234. "DoublePlus" "++")]
  235. (str kind n (string/lower-case (str (first duration))))))
  236. (defn timestamp-to-string
  237. [{:keys [active date time repetition wday active]}]
  238. (let [{:keys [year month day]} date
  239. {:keys [hour min]} time
  240. [open close] (if active ["<" ">"] ["[" "]"])
  241. repetition (if repetition
  242. (str " " (repetition-to-string repetition))
  243. "")
  244. hour (if hour (util/zero-pad hour))
  245. min (if min (util/zero-pad min))
  246. time (cond
  247. (and hour min)
  248. (util/format " %s:%s" hour min)
  249. hour
  250. (util/format " %s" hour)
  251. :else
  252. "")]
  253. (util/format "%s%s-%s-%s %s%s%s%s"
  254. open
  255. (str year)
  256. (util/zero-pad month)
  257. (util/zero-pad day)
  258. wday
  259. time
  260. repetition
  261. close)))
  262. (defn timestamp [{:keys [active date time repetition wday] :as t} kind]
  263. (let [prefix (case kind
  264. "Scheduled"
  265. [:i {:class "fa fa-calendar"
  266. :style {:margin-right 3.5}}]
  267. "Deadline"
  268. [:i {:class "fa fa-calendar-times-o"
  269. :style {:margin-right 3.5}}]
  270. "Date"
  271. nil
  272. "Closed"
  273. nil
  274. "Started"
  275. [:i {:class "fa fa-clock-o"
  276. :style {:margin-right 3.5}}]
  277. "Start"
  278. "From: "
  279. "Stop"
  280. "To: "
  281. nil)]
  282. (let [class (if (= kind "Closed")
  283. "line-through")]
  284. [:span.timestamp (cond-> {:active (str active)}
  285. class
  286. (assoc :class class))
  287. prefix
  288. (timestamp-to-string t)])))
  289. (defn range [{:keys [start stop]} stopped?]
  290. [:div {:class "timestamp-range"
  291. :stopped stopped?}
  292. (timestamp start "Start")
  293. (timestamp stop "Stop")])
  294. (declare map-inline)
  295. (declare markup-element-cp)
  296. (declare markup-elements-cp)
  297. (declare page-reference)
  298. (defn page-cp
  299. [{:keys [html-export? label children contents-page?] :as config} page]
  300. (when-let [page-name (:page/name page)]
  301. (let [source-page (model/get-alias-source-page (state/get-current-repo)
  302. (string/lower-case page-name))
  303. original-page-name (get page :page/original-name page-name)
  304. original-page-name (if (date/valid-journal-title? original-page-name)
  305. (string/capitalize original-page-name)
  306. original-page-name)
  307. page (string/lower-case page-name)
  308. redirect-page-name (cond
  309. (:page/alias? config)
  310. page
  311. (db/page-empty? (state/get-current-repo) page-name)
  312. (or (when source-page (:page/name source-page))
  313. page)
  314. :else
  315. page)
  316. href (if html-export?
  317. (util/encode-str page)
  318. (rfe/href :page {:name redirect-page-name}))]
  319. [:a.page-ref
  320. {:href href
  321. :on-click (fn [e]
  322. (util/stop e)
  323. (if (gobj/get e "shiftKey")
  324. (when-let [page-entity (db/entity [:page/name redirect-page-name])]
  325. (state/sidebar-add-block!
  326. (state/get-current-repo)
  327. (:db/id page-entity)
  328. :page
  329. {:page page-entity}))
  330. (route-handler/redirect! {:to :page
  331. :path-params {:name redirect-page-name}}))
  332. (when (and contents-page?
  333. (state/get-left-sidebar-open?))
  334. (ui-handler/close-left-sidebar!)))}
  335. (if (seq children)
  336. (for [child children]
  337. (if (= (first child) "Label")
  338. [:span (last child)]
  339. (let [{:keys [content children]} (last child)
  340. page-name (subs content 2 (- (count content) 2))]
  341. (page-reference html-export? page-name (assoc config :children children) nil))))
  342. (if (and label
  343. (string? label)
  344. (not (string/blank? label))) ; alias
  345. label
  346. original-page-name))])))
  347. (rum/defc page-reference < rum/reactive
  348. [html-export? s config label]
  349. (let [show-brackets? (state/show-brackets?)
  350. nested-link? (:nested-link? config)
  351. contents-page? (= "contents" (string/lower-case (str (:id config))))]
  352. [:span.page-reference
  353. (when (and (or show-brackets? nested-link?)
  354. (not html-export?)
  355. (not contents-page?))
  356. [:span.text-gray-500 "[["])
  357. (if (string/ends-with? s ".excalidraw")
  358. [:a.page-ref
  359. {:on-click (fn [e]
  360. (util/stop e)
  361. (set! (.-href js/window.location)
  362. (rfe/href :draw nil {:file (string/replace s (str config/default-draw-directory "/") "")})))}
  363. [:span
  364. (svg/excalidraw-logo)
  365. (string/capitalize (draw/get-file-title s))]]
  366. (page-cp (assoc config
  367. :label (mldoc/plain->text label)
  368. :contents-page? contents-page?) {:page/name s}))
  369. (when (and (or show-brackets? nested-link?)
  370. (not html-export?)
  371. (not contents-page?))
  372. [:span.text-gray-500 "]]"])]))
  373. (defn- latex-environment-content
  374. [name option content]
  375. (if (= (string/lower-case name) "equation")
  376. content
  377. (util/format "\\begin%s\n%s\\end{%s}"
  378. (str "{" name "}" option)
  379. content
  380. name)))
  381. (declare blocks-container)
  382. (rum/defc block-embed < rum/reactive db-mixins/query
  383. [config id]
  384. (let [blocks (db/get-block-and-children (state/get-current-repo) id)]
  385. [:div.color-level.embed-block.bg-base-2 {:style {:z-index 2}}
  386. [:div.px-3.pt-1.pb-2
  387. (blocks-container blocks (assoc config
  388. :embed? true
  389. :ref? false))]]))
  390. (rum/defc page-embed < rum/reactive db-mixins/query
  391. [config page-name]
  392. (let [page-name (string/lower-case page-name)
  393. current-page (state/get-current-page)]
  394. [:div.color-level.embed.embed-page.bg-base-2
  395. {:class (if (:sidebar? config) "in-sidebar")}
  396. [:section.flex.items-center.p-1.embed-header
  397. [:div.mr-3 svg/page]
  398. (page-cp config {:page/name page-name})]
  399. (when (and
  400. (not= (string/lower-case (or current-page ""))
  401. page-name)
  402. (not= (string/lower-case (get config :id ""))
  403. page-name))
  404. (let [blocks (db/get-page-blocks (state/get-current-repo) page-name)]
  405. (blocks-container blocks (assoc config
  406. :embed? true
  407. :ref? false))))]))
  408. (defn- get-label-text
  409. [label]
  410. (and (= 1 (count label))
  411. (let [label (first label)]
  412. (string? (last label))
  413. (last label))))
  414. (defn- get-page
  415. [label]
  416. (when-let [label-text (get-label-text label)]
  417. (db/entity [:page/name (string/lower-case label-text)])))
  418. (defn- macro->text
  419. [name arguments]
  420. (if (and (seq arguments)
  421. (not= arguments ["null"]))
  422. (util/format "{{{%s %s}}}" name (string/join ", " arguments))
  423. (util/format "{{{%s}}}" name)))
  424. (declare block-content)
  425. (defn block-reference
  426. [config id]
  427. (rum/with-context [[t] i18n/*tongue-context*]
  428. (when-not (string/blank? id)
  429. (let [block (and (util/uuid-string? id)
  430. (db/pull-block (uuid id)))]
  431. (if block
  432. [:span
  433. [:div.block-ref-wrap
  434. {:on-click (fn [e]
  435. (util/stop e)
  436. (if (gobj/get e "shiftKey")
  437. (state/sidebar-add-block!
  438. (state/get-current-repo)
  439. (:db/id block)
  440. :block-ref
  441. {:block block})
  442. (route-handler/redirect! {:to :page
  443. :path-params {:name id}})))}
  444. (let [title (:block/title block)]
  445. (if (empty? title)
  446. ;; display the content
  447. [:div.block-ref
  448. (block-content config block nil (:block/uuid block) (:slide? config))]
  449. (->elem
  450. :span.block-ref
  451. (map-inline config title))))]]
  452. [:span.warning.mr-1 {:title "Block ref invalid"}
  453. (util/format "((%s))" id)])))))
  454. (defn inline-text
  455. [format v]
  456. (when (string? v)
  457. (let [inline-list (mldoc/inline->edn v (mldoc/default-config format))]
  458. [:div.inline.mr-1 (map-inline {} inline-list)])))
  459. (defn selection-range-in-block? []
  460. (and (= "Range" (. (js/window.getSelection) -type))
  461. (-> (js/window.getSelection)
  462. (.-anchorNode)
  463. (.-parentNode)
  464. (.closest ".block-content"))))
  465. (defn- render-macro
  466. [config name arguments macro-content format]
  467. (if macro-content
  468. (let [ast (->> (mldoc/->edn macro-content (mldoc/default-config format))
  469. (map first))
  470. block? (contains? #{"Paragraph"
  471. "Raw_Html"
  472. "Hiccup"}
  473. (ffirst ast))]
  474. (if block?
  475. [:div
  476. (markup-elements-cp (assoc config :block/format format) ast)]
  477. (inline-text format macro-content)))
  478. [:span.warning {:title (str "Unsupported macro name: " name)}
  479. (macro->text name arguments)]))
  480. (rum/defc nested-link < rum/reactive
  481. [config html-export? link]
  482. (let [show-brackets? (state/show-brackets?)
  483. {:keys [content children]} link]
  484. [:span.page-reference.nested
  485. (when (and show-brackets?
  486. (not html-export?)
  487. (not (= (:id config) "contents")))
  488. [:span.text-gray-500 "[["])
  489. (let [page-name (subs content 2 (- (count content) 2))]
  490. (page-cp (assoc config
  491. :children children
  492. :nested-link? true) {:page/name page-name}))
  493. (when (and show-brackets?
  494. (not html-export?)
  495. (not (= (:id config) "contents")))
  496. [:span.text-gray-500 "]]"])]))
  497. (declare custom-query)
  498. (defn inline
  499. [{:keys [html-export?] :as config} item]
  500. (match item
  501. ["Plain" s]
  502. s
  503. ["Spaces" s]
  504. s
  505. ["Superscript" l]
  506. (->elem :sup (map-inline config l))
  507. ["Subscript" l]
  508. (->elem :sub (map-inline config l))
  509. ["Tag" s]
  510. (if (and s (util/tag-valid? s))
  511. [:a.tag {:href (rfe/href :page {:name s})
  512. :on-click (fn [e]
  513. (.preventDefault e)
  514. (let [repo (state/get-current-repo)
  515. page (db/pull repo '[*] [:page/name (string/lower-case (util/url-decode s))])]
  516. (when (gobj/get e "shiftKey")
  517. (state/sidebar-add-block!
  518. repo
  519. (:db/id page)
  520. :page
  521. {:page page}))))}
  522. (str "#" s)]
  523. [:span.warning.mr-1 {:title "Invalid tag, tags only accept alphanumeric characters, \"-\", \"_\", \"@\" and \"%\"."}
  524. (str "#" s)])
  525. ["Emphasis" [[kind] data]]
  526. (let [elem (case kind
  527. "Bold" :b
  528. "Italic" :i
  529. "Underline" :ins
  530. "Strike_through" :del
  531. "Highlight" :mark)]
  532. (->elem elem (map-inline config data)))
  533. ["Entity" e]
  534. [:span {:dangerouslySetInnerHTML
  535. {:__html (:html e)}}]
  536. ["Latex_Fragment" ["Displayed" s]]
  537. (if html-export?
  538. (latex/html-export s false true)
  539. (latex/latex (str (dc/squuid)) s false true))
  540. ["Latex_Fragment" ["Inline" s]]
  541. (if html-export?
  542. (latex/html-export s false true)
  543. (latex/latex (str (dc/squuid)) s false false))
  544. ["Target" s]
  545. [:a {:id s} s]
  546. ["Radio_Target" s]
  547. [:a {:id s} s]
  548. ["Email" address]
  549. (let [{:keys [local_part domain]} address
  550. address (str local_part "@" domain)]
  551. [:a {:href (str "mainto:" address)}
  552. address])
  553. ["Block_reference" id]
  554. ;; FIXME: alert when self block reference
  555. (block-reference config id)
  556. ["Nested_link" link]
  557. (nested-link config html-export? link)
  558. ["Link" link]
  559. (let [{:keys [url label title metadata full_text]} link
  560. img-formats (set (map name (config/img-formats)))]
  561. (match url
  562. ["Search" s]
  563. (cond
  564. ;; image
  565. (some (fn [fmt] (re-find (re-pattern (str "(?i)\\." fmt)) s)) img-formats)
  566. (image-link config url s label metadata full_text)
  567. (= \# (first s))
  568. (->elem :a {:href (str "#" (mldoc/anchorLink (subs s 1)))} (map-inline config label))
  569. ;; FIXME: same headline, see more https://orgmode.org/manual/Internal-Links.html
  570. (and (= \* (first s))
  571. (not= \* (last s)))
  572. (->elem :a {:href (str "#" (mldoc/anchorLink (subs s 1)))} (map-inline config label))
  573. (re-find #"(?i)^http[s]?://" s)
  574. (->elem :a {:href s}
  575. (map-inline config label))
  576. :else
  577. (page-reference html-export? s config label))
  578. :else
  579. (let [href (string-of-url url)
  580. protocol (or
  581. (and (= "Complex" (first url))
  582. (:protocol (second url)))
  583. (and (= "File" (first url))
  584. "file"))]
  585. (cond
  586. (and (= "Complex" (first url))
  587. (= protocol "id")
  588. (string? (:link (second url)))
  589. (util/uuid-string? (:link (second url)))) ; org mode id
  590. (block-reference config (:link (second url)))
  591. (= protocol "file")
  592. (if (some (fn [fmt] (re-find (re-pattern (str "(?i)\\." fmt)) href)) img-formats)
  593. (image-link config url href label metadata full_text)
  594. (let [label-text (get-label-text label)
  595. page (if (string/blank? label-text)
  596. {:page/name (db/get-file-page (string/replace href "file:" ""))}
  597. (get-page label))]
  598. (if (and page
  599. (when-let [ext (util/get-file-ext href)]
  600. (config/mldoc-support? ext)))
  601. [:span.page-reference
  602. [:span.text-gray-500 "[["]
  603. (page-cp config page)
  604. [:span.text-gray-500 "]]"]]
  605. (->elem
  606. :a
  607. (cond->
  608. {:href href}
  609. title
  610. (assoc :title title))
  611. (map-inline config label)))))
  612. ;; image
  613. (some (fn [fmt] (re-find (re-pattern (str "(?i)\\." fmt)) href)) img-formats)
  614. (image-link config url href label metadata full_text)
  615. :else
  616. (->elem
  617. :a.external-link
  618. (cond->
  619. {:href href
  620. :target "_blank"}
  621. title
  622. (assoc :title title))
  623. (map-inline config label))))))
  624. ["Verbatim" s]
  625. [:code s]
  626. ["Code" s]
  627. [:code s]
  628. ["Inline_Source_Block" x]
  629. [:code (:code x)]
  630. ["Export_Snippet" "html" s]
  631. (when (not html-export?)
  632. [:span {:dangerouslySetInnerHTML
  633. {:__html s}}])
  634. ;; String to hiccup
  635. ["Inline_Hiccup" s]
  636. (ui/catch-error
  637. [:div.warning {:title "Invalid hiccup"} s]
  638. (-> (safe-read-string s)
  639. (security/remove-javascript-links-in-href)))
  640. ["Break_Line"]
  641. [:br]
  642. ["Hard_Break_Line"]
  643. [:br]
  644. ["Timestamp" ["Scheduled" t]]
  645. (timestamp t "Scheduled")
  646. ["Timestamp" ["Deadline" t]]
  647. (timestamp t "Deadline")
  648. ["Timestamp" ["Date" t]]
  649. (timestamp t "Date")
  650. ["Timestamp" ["Closed" t]]
  651. (timestamp t "Closed")
  652. ["Timestamp" ["Range" t]]
  653. (range t false)
  654. ["Timestamp" ["Clock" ["Stopped" t]]]
  655. (range t true)
  656. ["Timestamp" ["Clock" ["Started" t]]]
  657. (timestamp t "Started")
  658. ["Cookie" ["Percent" n]]
  659. [:span {:class "cookie-percent"}
  660. (util/format "[d%%]" n)]
  661. ["Cookie" ["Absolute" current total]]
  662. [:span {:class "cookie-absolute"}
  663. (util/format "[%d/%d]" current total)]
  664. ["Footnote_Reference" options]
  665. (let [{:keys [id name]} options
  666. encode-name (util/url-encode name)]
  667. [:sup.fn
  668. [:a {:id (str "fnr." encode-name)
  669. :class "footref"
  670. :href (str "#fn." encode-name)}
  671. name]])
  672. ["Macro" options]
  673. (let [{:keys [name arguments]} options
  674. arguments (if (and
  675. (>= (count arguments) 2)
  676. (and (string/starts-with? (first arguments) "[[")
  677. (string/ends-with? (last arguments) "]]"))) ; page reference
  678. (let [title (string/join ", " arguments)]
  679. [title])
  680. arguments)]
  681. (cond
  682. (= name "query")
  683. [:div.dsl-query
  684. (let [query (string/join ", " arguments)]
  685. (custom-query (assoc config :dsl-query? true)
  686. {:title [:code.p-1 (str "Query: " query)]
  687. :query query}))]
  688. (= name "youtube")
  689. (let [url (first arguments)]
  690. (let [YouTube-regex #"^((?:https?:)?//)?((?:www|m).)?((?:youtube.com|youtu.be))(/(?:[\w-]+\?v=|embed/|v/)?)([\w-]+)(\S+)?$"]
  691. (when-let [youtube-id (cond
  692. (== 11 (count url)) url
  693. :else
  694. (nth (re-find YouTube-regex url) 5))]
  695. (when-not (string/blank? youtube-id)
  696. (let [width (min (- (util/get-width) 96)
  697. 560)
  698. height (int (* width (/ 315 560)))]
  699. [:iframe
  700. {:allow-full-screen "allowfullscreen"
  701. :allow
  702. "accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture"
  703. :frame-border "0"
  704. :src (str "https://www.youtube.com/embed/" youtube-id)
  705. :height height
  706. :width width}])))))
  707. (= name "embed")
  708. (let [a (first arguments)]
  709. (cond
  710. (nil? a) ; empty embed
  711. nil
  712. (and (string/starts-with? a "[[")
  713. (string/ends-with? a "]]"))
  714. (let [page-name (-> (string/replace a "[[" "")
  715. (string/replace "]]" "")
  716. string/trim)]
  717. (when-not (string/blank? page-name)
  718. (page-embed config page-name)))
  719. (and (string/starts-with? a "((")
  720. (string/ends-with? a "))"))
  721. (when-let [s (-> (string/replace a "((" "")
  722. (string/replace "))" "")
  723. string/trim)]
  724. (when-let [id (and s
  725. (let [s (string/trim s)]
  726. (and (util/uuid-string? s)
  727. (uuid s))))]
  728. (block-embed config id)))
  729. :else ;TODO: maybe collections?
  730. nil))
  731. :else
  732. (if-let [block-uuid (:block/uuid config)]
  733. (let [format (get-in config [:block :block/format] :markdown)
  734. macro-content (or
  735. (-> (db/entity [:block/uuid block-uuid])
  736. (:block/page)
  737. (:db/id)
  738. (db/entity)
  739. :page/properties
  740. :macros
  741. (get name))
  742. (get (state/get-macros) name)
  743. (get (state/get-macros) (keyword name)))
  744. macro-content (if (and (seq arguments) macro-content)
  745. (block/macro-subs macro-content arguments)
  746. macro-content)]
  747. (render-macro config name arguments macro-content format))
  748. (when-let [macro-txt (macro->text name arguments)]
  749. (let [format (get-in config [:block :block/format] :markdown)]
  750. (render-macro config name arguments macro-txt format))))))
  751. :else
  752. ""))
  753. (declare blocks-cp)
  754. (rum/defc block-child
  755. [block]
  756. block)
  757. (defonce *control-show? (atom {}))
  758. (rum/defcs block-control < rum/reactive
  759. {:will-mount (fn [state]
  760. (let [block (nth (:rum/args state) 1)
  761. collapsed? (:block/collapsed? block)]
  762. (state/set-collapsed-state! (:block/uuid block)
  763. collapsed?))
  764. state)}
  765. [state config block uuid block-id level start-level body children dummy?]
  766. (let [has-child? (and
  767. (not (:pre-block? block))
  768. (or (seq children)
  769. (seq body)))
  770. collapsed? (state/sub [:ui/collapsed-blocks uuid])
  771. collapsed? (and has-child? collapsed?)
  772. control-show (util/react (rum/cursor *control-show? block-id))
  773. dark? (= "dark" (state/sub :ui/theme))
  774. heading? (= (get (:block/properties block) "heading") "true")]
  775. [:div.mr-2.flex.flex-row.items-center
  776. {:style {:height 24
  777. :margin-top (if (and heading? (<= level 6))
  778. (case level
  779. 1
  780. 32
  781. 2
  782. 22
  783. 18)
  784. 0)
  785. :float "left"}}
  786. [:a.block-control.opacity-50.hover:opacity-100
  787. {:id (str "control-" uuid)
  788. :style {:width 14
  789. :height 16
  790. :margin-right 2}
  791. :on-click (fn [e]
  792. (util/stop e)
  793. (if collapsed?
  794. (expand/expand! block)
  795. (expand/collapse! block))
  796. (state/set-collapsed-state! uuid (not collapsed?)))}
  797. (cond
  798. (and control-show collapsed?)
  799. (svg/caret-right)
  800. (and control-show has-child?)
  801. (svg/caret-down)
  802. :else
  803. [:span ""])]
  804. [:a (if (not dummy?)
  805. {:href (rfe/href :page {:name uuid})
  806. :on-click (fn [e]
  807. (.preventDefault e)
  808. (when (gobj/get e "shiftKey")
  809. (state/sidebar-add-block!
  810. (state/get-current-repo)
  811. (:db/id block)
  812. :block
  813. block)))})
  814. [:span.bullet-container.cursor
  815. {:id (str "dot-" uuid)
  816. :draggable true
  817. :on-drag-start (fn [event]
  818. (editor-handler/highlight-block! uuid)
  819. (.setData (gobj/get event "dataTransfer")
  820. "block-uuid"
  821. uuid)
  822. (.setData (gobj/get event "dataTransfer")
  823. "block-dom-id"
  824. block-id)
  825. (state/clear-selection!)
  826. (reset! *dragging? true)
  827. (reset! *dragging-block block))
  828. :blockid (str uuid)
  829. :class (str (when collapsed? "bullet-closed")
  830. " "
  831. (when (and (:document/mode? config)
  832. (not collapsed?))
  833. "hide-inner-bullet"))}
  834. [:span.bullet {:blockid (str uuid)
  835. :class (if heading? "bullet-heading" "")}]]]]))
  836. (defn- build-id
  837. [config ref? sidebar? embed?]
  838. (let [k (pr-str config)
  839. n (or
  840. (get @container-ids k)
  841. (let [n' (swap! container-idx inc)]
  842. (swap! container-ids assoc k n')
  843. n'))]
  844. (str n "-")))
  845. (rum/defc dnd-separator
  846. [block margin-left bottom top? nested?]
  847. (let [id (str (:block/uuid block)
  848. (cond nested?
  849. "-nested"
  850. top?
  851. "-top"
  852. :else
  853. nil))]
  854. [:div.dnd-separator
  855. {:id id
  856. :style (merge
  857. {:position "absolute"
  858. :left margin-left
  859. :width "100%"
  860. :z-index 3}
  861. (if top?
  862. {:top 0}
  863. {:bottom 0}))}]))
  864. (declare block-container)
  865. (defn block-checkbox
  866. [block class]
  867. (let [marker (:block/marker block)
  868. [class checked?] (cond
  869. (nil? marker)
  870. nil
  871. (contains? #{"NOW" "LATER" "DOING" "IN-PROGRESS" "TODO" "WAIT" "WAITING"} marker)
  872. [class false]
  873. (= "DONE" marker)
  874. [(str class " checked") true])]
  875. (when class
  876. (ui/checkbox {:class class
  877. :style {:margin-top -2
  878. :margin-right 5}
  879. :checked checked?
  880. :on-change (fn [_e]
  881. ;; FIXME: Log timestamp
  882. (if checked?
  883. (editor-handler/uncheck block)
  884. (editor-handler/check block)))}))))
  885. (defn list-checkbox
  886. [checked?]
  887. (ui/checkbox {:style {:margin-right 6}
  888. :checked checked?}))
  889. (defn marker-switch
  890. [{:block/keys [pre-block? marker] :as block}]
  891. (when (contains? #{"NOW" "LATER" "TODO" "DOING"} marker)
  892. (let [set-marker-fn (fn [marker]
  893. (fn [e]
  894. (util/stop e)
  895. (editor-handler/set-marker block marker)))]
  896. (case marker
  897. "NOW"
  898. [:a.marker-switch
  899. {:title "Change from NOW to LATER"
  900. :on-click (set-marker-fn "LATER")}
  901. [:span "N"]]
  902. "LATER"
  903. [:a.marker-switch
  904. {:title "Change from LATER to NOW"
  905. :on-click (set-marker-fn "NOW")}
  906. "L"]
  907. "TODO"
  908. [:a.marker-switch
  909. {:title "Change from TODO to DOING"
  910. :on-click (set-marker-fn "DOING")}
  911. "T"]
  912. "DOING"
  913. [:a.marker-switch
  914. {:title "Change from DOING to TODO"
  915. :on-click (set-marker-fn "TODO")}
  916. "D"]
  917. nil))))
  918. (defn marker-cp
  919. [{:block/keys [pre-block? marker] :as block}]
  920. (when-not pre-block?
  921. (if (contains? #{"IN-PROGRESS" "WAIT" "WAITING"} marker)
  922. [:span {:class (str "task-status " (string/lower-case marker))
  923. :style {:margin-right 3.5}}
  924. (string/upper-case marker)])))
  925. (defn priority-cp
  926. [{:block/keys [pre-block? priority] :as block}]
  927. (when (and (not pre-block?) priority)
  928. (ui/tooltip
  929. [:ul
  930. (for [p (remove #(= priority %) ["A" "B" "C"])]
  931. [:a.mr-2.text-base.tooltip-priority {:priority p
  932. :on-click (fn [] (editor-handler/set-priority block p))}])]
  933. [:a.opacity-50.hover:opacity-100
  934. {:class "priority"
  935. :href (rfe/href :page {:name priority})
  936. :style {:margin-right 3.5}}
  937. (util/format "[#%s]" (str priority))])))
  938. (defn block-tags-cp
  939. [{:block/keys [pre-block? tags] :as block}]
  940. (when (and (not pre-block?)
  941. (seq tags))
  942. (->elem
  943. :span
  944. {:class "block-tags"}
  945. (mapv (fn [tag]
  946. (when-let [page (db/entity (:db/id tag))]
  947. (let [tag (:page/name page)]
  948. [:a.tag.mx-1 {:key (str "tag-" (:db/id tag))
  949. :href (rfe/href :page {:name tag})}
  950. (str "#" tag)])))
  951. tags))))
  952. (defn build-block-part
  953. [{:keys [slide?] :as config} {:block/keys [uuid title tags marker level priority anchor meta format content pre-block? dummy? block-refs-count page properties]
  954. :as t}]
  955. (let [config (assoc config :block t)
  956. slide? (boolean (:slide? config))
  957. html-export? (:html-export? config)
  958. checkbox (when (and (not pre-block?)
  959. (not html-export?))
  960. (block-checkbox t (str "mr-1 cursor")))
  961. marker-switch (when (and (not pre-block?)
  962. (not html-export?))
  963. (marker-switch t))
  964. marker-cp (marker-cp t)
  965. priority (priority-cp t)
  966. tags (block-tags-cp t)
  967. contents? (= (:id config) "contents")
  968. heading? (= (get properties "heading") "true")
  969. bg-color (get properties "background_color")]
  970. (when level
  971. (let [element (if (and (<= level 6) heading?)
  972. (keyword (str "h" level))
  973. :div)]
  974. (->elem
  975. element
  976. (merge
  977. {:id anchor}
  978. (when (and marker
  979. (not (string/blank? marker))
  980. (not= "nil" marker))
  981. {:class (str (string/lower-case marker))})
  982. (when bg-color
  983. {:style {:background-color bg-color
  984. :padding-left 6
  985. :padding-right 6
  986. :color "#FFFFFF"}}))
  987. (remove-nils
  988. (concat
  989. [(when-not slide? checkbox)
  990. (when-not slide? marker-switch)
  991. marker-cp
  992. priority]
  993. (cond
  994. dummy?
  995. [[:span.opacity-50 "Click here to start writing"]]
  996. ;; empty item
  997. (and contents? (or
  998. (empty? title)
  999. (= title [["Plain" "[[]]"]])))
  1000. [[:span.opacity-50 "Click here to add a page, e.g. [[favorite-page]]"]]
  1001. :else
  1002. (map-inline config title))
  1003. [tags])))))))
  1004. (defn dnd-same-block?
  1005. [uuid]
  1006. (= (:block/uuid @*dragging-block) uuid))
  1007. (defn show-dnd-separator
  1008. [element-id]
  1009. (when-let [element (gdom/getElement element-id)]
  1010. (when (d/has-class? element "dnd-separator")
  1011. (d/remove-class! element "dnd-separator")
  1012. (d/add-class! element "dnd-separator-cur"))))
  1013. (defn hide-dnd-separator
  1014. [element-id]
  1015. (when-let [element (gdom/getElement element-id)]
  1016. (when (d/has-class? element "dnd-separator-cur")
  1017. (d/remove-class! element "dnd-separator-cur")
  1018. (d/add-class! element "dnd-separator"))))
  1019. (defn- get-data-transfer-attr
  1020. [event attr]
  1021. (.getData (gobj/get event "dataTransfer") attr))
  1022. (defn- pre-block-cp
  1023. [config content format]
  1024. (rum/with-context [[t] i18n/*tongue-context*]
  1025. (let [ast (mldoc/->edn content (mldoc/default-config format))
  1026. ast (map first ast)
  1027. slide? (:slide? config)
  1028. only-title? (and (= 1 (count ast))
  1029. (= "Properties" (ffirst ast))
  1030. (let [m (second (first ast))]
  1031. (= (keys m) [:title])))
  1032. block-cp [:div {:class (if only-title?
  1033. (util/hiccup->class "pre-block.opacity-50")
  1034. (util/hiccup->class "pre-block.bg-base-2.p-2.rounded"))}
  1035. (if only-title?
  1036. [:span (t :page/edit-properties-placeholder)]
  1037. (markup-elements-cp (assoc config :block/format format) ast))]]
  1038. (if slide?
  1039. [:div [:h1 (:page-name config)]
  1040. block-cp]
  1041. block-cp))))
  1042. (rum/defc properties-cp
  1043. [config block]
  1044. (let [properties (apply dissoc (:block/properties block) text/hidden-properties)]
  1045. (when (seq properties)
  1046. [:div.blocks-properties.text-sm.opacity-80.my-1.p-2
  1047. (for [[k v] properties]
  1048. ^{:key (str (:block/uuid block) "-" k)}
  1049. [:div.my-1
  1050. [:b k]
  1051. [:span.mr-1 ":"]
  1052. (if (coll? v)
  1053. (let [v (->> (remove string/blank? v)
  1054. (filter string?))
  1055. vals (for [v-item v]
  1056. (page-cp config {:page/name v-item}))]
  1057. (interpose [:span ", "] vals))
  1058. (let [page-name (string/lower-case (str v))]
  1059. (if (db/entity [:page/name page-name])
  1060. (page-cp config {:page/name page-name})
  1061. (inline-text (:block/format block) (str v)))))])])))
  1062. (rum/defcs timestamp-cp < rum/reactive
  1063. (rum/local false ::show?)
  1064. (rum/local {} ::pos)
  1065. {:will-unmount (fn [state]
  1066. (when-let [show? (::show? state)]
  1067. (reset! show? false))
  1068. state)}
  1069. [state block typ ast]
  1070. (let [show? (get state ::show?)]
  1071. [:div.flex.flex-col
  1072. [:div.text-sm.mt-1.flex.flex-row
  1073. [:div.opacity-50.font-medium {:style {:width 95}}
  1074. (str typ ": ")]
  1075. [:a.opacity-80.hover:opacity-100
  1076. {:on-click (fn []
  1077. (if @show?
  1078. (do
  1079. (reset! show? false)
  1080. (reset! commands/*current-command nil)
  1081. (state/set-editor-show-date-picker! false)
  1082. (state/set-timestamp-block! nil))
  1083. (do
  1084. (reset! show? true)
  1085. (reset! commands/*current-command typ)
  1086. (state/set-editor-show-date-picker! true)
  1087. (state/set-timestamp-block! {:block block
  1088. :typ typ
  1089. :show? show?}))))}
  1090. (repeated/timestamp->text ast)]]
  1091. (when (true? @show?)
  1092. (let [ts (repeated/timestamp->map ast)]
  1093. [:div.my-4
  1094. (datetime-comp/date-picker nil nil ts)]))]))
  1095. (rum/defc block-content < rum/reactive
  1096. [config {:block/keys [uuid title level body meta content marker dummy? page format repo children pre-block? properties collapsed? idx container block-refs-count scheduled scheduled-ast deadline deadline-ast repeated?] :as block} edit-input-id block-id slide?]
  1097. (let [dragging? (rum/react *dragging?)
  1098. attrs {:blockid (str uuid)
  1099. ;; FIXME: Click to copy a selection instead of click first and then copy
  1100. ;; It seems that `util/caret-range` can't get the correct range
  1101. :on-click (fn [e]
  1102. (when-not (selection-range-in-block?)
  1103. (let [target (gobj/get e "target")]
  1104. (when-not (or (util/link? target)
  1105. (util/input? target)
  1106. (util/details-or-summary? target)
  1107. (and (util/sup? target)
  1108. (d/has-class? target "fn")))
  1109. (editor-handler/clear-selection! nil)
  1110. (editor-handler/unhighlight-block!)
  1111. (let [cursor-range (util/caret-range (gdom/getElement block-id))
  1112. properties-hidden? (text/properties-hidden? properties)
  1113. content (text/remove-level-spaces content format)
  1114. content (if properties-hidden? (text/remove-properties! content) content)
  1115. block (db/pull [:block/uuid (:block/uuid block)])]
  1116. (state/set-editing!
  1117. edit-input-id
  1118. content
  1119. block
  1120. cursor-range))
  1121. (util/stop e)))))
  1122. :on-drag-over (fn [event]
  1123. (util/stop event)
  1124. (when-not (dnd-same-block? uuid)
  1125. (show-dnd-separator (str uuid "-nested"))))
  1126. :on-drag-leave (fn [event]
  1127. (hide-dnd-separator (str uuid))
  1128. (hide-dnd-separator (str uuid "-nested"))
  1129. (hide-dnd-separator (str uuid "-top")))
  1130. :on-drop (fn [event]
  1131. (util/stop event)
  1132. (when-not (dnd-same-block? uuid)
  1133. (let [from-dom-id (get-data-transfer-attr event "block-dom-id")]
  1134. (dnd/move-block @*dragging-block
  1135. block
  1136. from-dom-id
  1137. false
  1138. true)))
  1139. (reset! *dragging? false)
  1140. (reset! *dragging-block nil)
  1141. (editor-handler/unhighlight-block!))}]
  1142. [:div.flex.relative
  1143. [:div.flex-1.flex-col.relative.block-content
  1144. (cond-> {:id (str "block-content-" uuid)}
  1145. (not slide?)
  1146. (merge attrs))
  1147. (if pre-block?
  1148. (pre-block-cp config (string/trim content) format)
  1149. (build-block-part config block))
  1150. (when (and dragging? (not slide?))
  1151. (dnd-separator block 0 -4 false true))
  1152. (when (and deadline deadline-ast)
  1153. (timestamp-cp block "DEADLINE" deadline-ast))
  1154. (when (and scheduled scheduled-ast)
  1155. (timestamp-cp block "SCHEDULED" scheduled-ast))
  1156. (when (and (seq properties)
  1157. (let [hidden? (text/properties-hidden? properties)]
  1158. (not hidden?))
  1159. (not (:slide? config)))
  1160. (properties-cp config block))
  1161. (when (and (not pre-block?) (seq body))
  1162. [:div.block-body {:style {:display (if collapsed? "none" "")}}
  1163. ;; TODO: consistent id instead of the idx (since it could be changed later)
  1164. (let [body (block/trim-break-lines! (:block/body block))]
  1165. (for [[idx child] (medley/indexed body)]
  1166. (when-let [block (markup-element-cp config child)]
  1167. (rum/with-key (block-child block)
  1168. (str uuid "-" idx)))))])]
  1169. (when (and block-refs-count (> block-refs-count 0))
  1170. [:div
  1171. [:a.open-block-ref-link.bg-base-2
  1172. {:title "Open block references"
  1173. :style {:margin-top -1}
  1174. :on-click (fn []
  1175. (state/sidebar-add-block!
  1176. (state/get-current-repo)
  1177. (:db/id block)
  1178. :block-ref
  1179. {:block block}))}
  1180. block-refs-count]])
  1181. (when (and (= marker "DONE")
  1182. (state/enable-timetracking?))
  1183. (let [start-time (or
  1184. (get properties "now")
  1185. (get properties "doing")
  1186. (get properties "in-progress")
  1187. (get properties "later")
  1188. (get properties "todo"))
  1189. finish-time (get properties "done")]
  1190. (when (and start-time finish-time (> finish-time start-time))
  1191. [:div.text-sm.absolute.time-spent {:style {:top 0
  1192. :right 0
  1193. :padding-left 2}
  1194. :title (str (date/int->local-time start-time) " ~ " (date/int->local-time finish-time))}
  1195. [:span.opacity-70
  1196. (utils/timeConversion (- finish-time start-time))]])))]))
  1197. (rum/defc block-content-or-editor < rum/reactive
  1198. [config {:block/keys [uuid title level body meta content dummy? page format repo children pre-block? collapsed? idx] :as block} edit-input-id block-id slide?]
  1199. (let [edit? (state/sub [:editor/editing? edit-input-id])
  1200. editor-box (get config :editor-box)]
  1201. (if (and edit? editor-box)
  1202. [:div.editor-wrapper {:id (str "editor-" edit-input-id)}
  1203. (editor-box {:block block
  1204. :block-id uuid
  1205. :block-parent-id block-id
  1206. :format format
  1207. :dummy? dummy?
  1208. :on-hide (fn [value event]
  1209. (when (= event :esc)
  1210. (editor-handler/highlight-block! uuid)))}
  1211. edit-input-id
  1212. config)]
  1213. (block-content config block edit-input-id block-id slide?))))
  1214. (rum/defc dnd-separator-wrapper < rum/reactive
  1215. [block slide? top?]
  1216. (let [dragging? (rum/react *dragging?)]
  1217. (cond
  1218. (and dragging? (not slide?))
  1219. (dnd-separator block 20 0 top? false)
  1220. :else
  1221. nil)))
  1222. (defn non-dragging?
  1223. [e]
  1224. (and (= (gobj/get e "buttons") 1)
  1225. (not (d/has-class? (gobj/get e "target") "bullet-container"))
  1226. (not (d/has-class? (gobj/get e "target") "bullet"))
  1227. (not @*dragging?)))
  1228. (defn block-parents
  1229. ([repo block-id format]
  1230. (block-parents repo block-id format true))
  1231. ([repo block-id format show-page?]
  1232. (let [parents (db/get-block-parents repo block-id 3)
  1233. page (db/get-block-page repo block-id)
  1234. page-name (:page/name page)]
  1235. (when (or (seq parents)
  1236. show-page?
  1237. page-name)
  1238. (let [parents-atom (atom parents)
  1239. component [:div.block-parents.flex-row.flex-1
  1240. (when show-page?
  1241. [:a {:href (rfe/href :page {:name page-name})}
  1242. (or (:page/original-name page)
  1243. (:page/name page))])
  1244. (when (and show-page? (seq parents))
  1245. [:span.mx-2.opacity-50 "➤"])
  1246. (when (seq parents)
  1247. (let [parents (for [{:block/keys [uuid content]} parents]
  1248. (let [title (string/trim (text/remove-level-spaces content format))]
  1249. (when (and (not (string/blank? title))
  1250. (not= (string/lower-case page-name) (string/lower-case title)))
  1251. [:a {:href (rfe/href :page {:name uuid})}
  1252. title])))
  1253. parents (remove nil? parents)]
  1254. (reset! parents-atom parents)
  1255. (when (seq parents)
  1256. (interpose [:span.mx-2.opacity-50 "➤"]
  1257. parents))))]
  1258. component (filterv identity component)]
  1259. (when (or (seq @parents-atom) show-page?)
  1260. component))))))
  1261. (rum/defc block-container < rum/static
  1262. {:did-mount (fn [state]
  1263. (let [block (nth (:rum/args state) 1)
  1264. collapsed? (:block/collapsed? block)]
  1265. (when collapsed?
  1266. (expand/collapse! block))
  1267. state))
  1268. :should-update (fn [old-state new-state]
  1269. (not= (:block/content (second (:rum/args old-state)))
  1270. (:block/content (second (:rum/args new-state)))))}
  1271. [config {:block/keys [uuid title level body meta content dummy? page format repo children collapsed? pre-block? idx properties] :as block}]
  1272. (let [ref? (boolean (:ref? config))
  1273. breadcrumb-show? (:breadcrumb-show? config)
  1274. sidebar? (boolean (:sidebar? config))
  1275. slide? (boolean (:slide? config))
  1276. doc-mode? (:document/mode? config)
  1277. embed? (:embed? config)
  1278. unique-dom-id (build-id (dissoc config :block/uuid) ref? sidebar? embed?)
  1279. edit-input-id (str "edit-block-" unique-dom-id uuid)
  1280. block-id (str "ls-block-" unique-dom-id uuid)
  1281. has-child? (boolean
  1282. (and
  1283. (not pre-block?)
  1284. (or (seq children)
  1285. (seq body))))
  1286. start-level (or (:start-level config) 1)
  1287. attrs {:on-drag-over (fn [event]
  1288. (util/stop event)
  1289. (when-not (dnd-same-block? uuid)
  1290. (if (zero? idx)
  1291. (let [element-top (gobj/get (utils/getOffsetRect (gdom/getElement block-id)) "top")
  1292. cursor-top (gobj/get event "clientY")]
  1293. (if (<= (js/Math.abs (- cursor-top element-top)) 16)
  1294. ;; top
  1295. (do
  1296. (hide-dnd-separator (str uuid))
  1297. (show-dnd-separator (str uuid "-top"))
  1298. (reset! *move-to-top? true))
  1299. (do
  1300. (hide-dnd-separator (str uuid "-top"))
  1301. (show-dnd-separator (str uuid)))))
  1302. (show-dnd-separator (str uuid)))))
  1303. :on-drag-leave (fn [event]
  1304. (hide-dnd-separator (str uuid))
  1305. (hide-dnd-separator (str uuid "-nested"))
  1306. (hide-dnd-separator (str uuid "-top"))
  1307. (reset! *move-to-top? false))
  1308. :on-drop (fn [event]
  1309. (when-not (dnd-same-block? uuid)
  1310. (let [from-dom-id (get-data-transfer-attr event "block-dom-id")]
  1311. (dnd/move-block @*dragging-block
  1312. block
  1313. from-dom-id
  1314. @*move-to-top?
  1315. false)))
  1316. (reset! *dragging? false)
  1317. (reset! *dragging-block nil)
  1318. (editor-handler/unhighlight-block!))
  1319. :on-mouse-move (fn [e]
  1320. (when (and (non-dragging? e)
  1321. (not @*resizing-image?))
  1322. (state/into-selection-mode!)))
  1323. :on-mouse-down (fn [e]
  1324. (when (and
  1325. (not (state/get-selection-start-block))
  1326. (= (gobj/get e "buttons") 1))
  1327. (when block-id (state/set-selection-start-block! block-id))))
  1328. :on-mouse-over (fn [e]
  1329. (util/stop e)
  1330. (when has-child?
  1331. (swap! *control-show? assoc block-id true))
  1332. (when-let [parent (gdom/getElement block-id)]
  1333. (let [node (.querySelector parent ".bullet-container")
  1334. closed? (d/has-class? node "bullet-closed")]
  1335. (if closed?
  1336. (state/collapse-block! uuid)
  1337. (state/expand-block! uuid))
  1338. (when doc-mode?
  1339. (d/remove-class! node "hide-inner-bullet"))))
  1340. (when (and
  1341. (state/in-selection-mode?)
  1342. (non-dragging? e))
  1343. (util/stop e)
  1344. (editor-handler/highlight-selection-area! block-id)))
  1345. :on-mouse-out (fn [e]
  1346. (util/stop e)
  1347. (when has-child?
  1348. (swap! *control-show?
  1349. assoc block-id false))
  1350. (when doc-mode?
  1351. (when-let [parent (gdom/getElement block-id)]
  1352. (when-let [node (.querySelector parent ".bullet-container")]
  1353. (d/add-class! node "hide-inner-bullet")))))}]
  1354. [:div.ls-block.flex.flex-col.rounded-sm
  1355. (cond->
  1356. {:id block-id
  1357. :style {:position "relative"}
  1358. :class (str uuid
  1359. (when dummy? " dummy")
  1360. (when (and collapsed? has-child?) " collapsed")
  1361. (when pre-block? " pre-block"))
  1362. :blockid (str uuid)
  1363. :repo repo
  1364. :level level
  1365. :haschild (str has-child?)}
  1366. (not slide?)
  1367. (merge attrs))
  1368. (when (and ref? breadcrumb-show?)
  1369. (when-let [comp (block-parents repo uuid format false)]
  1370. [:div.my-2.opacity-50.ml-4 comp]))
  1371. (dnd-separator-wrapper block slide? (zero? idx))
  1372. [:div.flex-1.flex-row
  1373. (when (not slide?)
  1374. (block-control config block uuid block-id level start-level body children dummy?))
  1375. (block-content-or-editor config block edit-input-id block-id slide?)]
  1376. (when (seq children)
  1377. [:div.block-children {:style {:margin-left (if doc-mode? 12 22)
  1378. :display (if collapsed? "none" "")}}
  1379. (for [child children]
  1380. (when (map? child)
  1381. (let [child (dissoc child :block/meta)]
  1382. (rum/with-key (block-container (assoc config :block/uuid (:block/uuid child)) child)
  1383. (:block/uuid child)))))])
  1384. (when ref?
  1385. (let [children (-> (db/get-block-immediate-children repo uuid)
  1386. db/sort-by-pos)]
  1387. (when (seq children)
  1388. [:div.ref-children.ml-12
  1389. (blocks-container children (assoc config
  1390. :breadcrumb-show? false
  1391. :ref? true))])))
  1392. (dnd-separator-wrapper block slide? false)]))
  1393. (defn divide-lists
  1394. [[f & l]]
  1395. (loop [l l
  1396. ordered? (:ordered f)
  1397. result [[f]]]
  1398. (if (seq l)
  1399. (let [cur (first l)
  1400. cur-ordered? (:ordered cur)]
  1401. (if (= ordered? cur-ordered?)
  1402. (recur
  1403. (rest l)
  1404. cur-ordered?
  1405. (update result (dec (count result)) conj cur))
  1406. (recur
  1407. (rest l)
  1408. cur-ordered?
  1409. (conj result [cur]))))
  1410. result)))
  1411. (defn list-element
  1412. [l]
  1413. (match l
  1414. [l1 & tl]
  1415. (let [{:keys [ordered name]} l1]
  1416. (cond
  1417. (seq name)
  1418. :dl
  1419. ordered
  1420. :ol
  1421. :else
  1422. :ul))
  1423. :else
  1424. :ul))
  1425. (defn list-item
  1426. [config {:keys [name content checkbox items number] :as l}]
  1427. (let [content (when-not (empty? content)
  1428. (match content
  1429. [["Paragraph" i] & rest]
  1430. (vec-cat
  1431. (map-inline config i)
  1432. (markup-elements-cp config rest))
  1433. :else
  1434. (markup-elements-cp config content)))
  1435. checked? (some? checkbox)
  1436. items (if (seq items)
  1437. (->elem
  1438. (list-element items)
  1439. (for [item items]
  1440. (list-item config item))))]
  1441. (cond
  1442. (seq name)
  1443. [:dl {:checked checked?}
  1444. [:dt (map-inline config name)]
  1445. (->elem :dd
  1446. (vec-cat content [items]))]
  1447. :else
  1448. (if (nil? checkbox)
  1449. (->elem
  1450. :li
  1451. {:checked checked?}
  1452. (vec-cat
  1453. [(->elem
  1454. :p
  1455. content)]
  1456. [items]))
  1457. (->elem
  1458. :li
  1459. {:checked checked?}
  1460. (vec-cat
  1461. [(->elem
  1462. :p
  1463. (list-checkbox checkbox)
  1464. content)]
  1465. [items]))))))
  1466. (defn table
  1467. [config {:keys [header groups col_groups]}]
  1468. (let [tr (fn [elm cols]
  1469. (->elem
  1470. :tr
  1471. (mapv (fn [col]
  1472. (->elem
  1473. elm
  1474. {:scope "col"
  1475. :class "org-left"}
  1476. (map-inline config col)))
  1477. cols)))
  1478. tb-col-groups (try
  1479. (mapv (fn [number]
  1480. (let [col-elem [:col {:class "org-left"}]]
  1481. (->elem
  1482. :colgroup
  1483. (repeat number col-elem))))
  1484. col_groups)
  1485. (catch js/Error e
  1486. []))
  1487. head (if header
  1488. [:thead (tr :th header)])
  1489. groups (mapv (fn [group]
  1490. (->elem
  1491. :tbody
  1492. (mapv #(tr :td %) group)))
  1493. groups)]
  1494. [:div.table-wrapper {:style {:max-width (min 700
  1495. (gobj/get js/window "innerWidth"))}}
  1496. (->elem
  1497. :table
  1498. {:class "table-auto"
  1499. :border 2
  1500. :cell-spacing 0
  1501. :cell-padding 6
  1502. :rules "groups"
  1503. :frame "hsides"}
  1504. (vec-cat
  1505. tb-col-groups
  1506. (cons head groups)))]))
  1507. (defn map-inline
  1508. [config col]
  1509. (map #(inline config %) col))
  1510. (declare ->hiccup)
  1511. (defn built-in-custom-query?
  1512. [title]
  1513. (let [repo (state/get-current-repo)]
  1514. (let [queries (state/sub [:config repo :default-queries :journals])]
  1515. (when (seq queries)
  1516. (boolean (some #(= % title) (map :title queries)))))))
  1517. (defn- trigger-custom-query!
  1518. [state]
  1519. (let [[config query] (:rum/args state)
  1520. query-atom (if (:dsl-query? config)
  1521. (query-dsl/query (state/get-current-repo) (:query query))
  1522. (db/custom-query query))]
  1523. (assoc state :query-atom query-atom)))
  1524. (rum/defcs custom-query < rum/reactive
  1525. {:will-mount trigger-custom-query!
  1526. :did-mount (fn [state]
  1527. (when-let [query (last (:rum/args state))]
  1528. (state/add-custom-query-component! query (:rum/react-component state)))
  1529. state)
  1530. :did-remount (fn [_old_state state]
  1531. (trigger-custom-query! state))
  1532. :will-unmount (fn [state]
  1533. (when-let [query (last (:rum/args state))]
  1534. (state/remove-custom-query-component! query)
  1535. (db/remove-custom-query! (state/get-current-repo) query))
  1536. state)}
  1537. [state config {:keys [title query inputs view collapsed? children?] :as q}]
  1538. (let [dsl-query? (:dsl-query? config)
  1539. query-atom (:query-atom state)]
  1540. (let [current-block-uuid (or (:block/uuid (:block config))
  1541. (:block/uuid config))
  1542. ;; exclude the current one, otherwise it'll loop forever
  1543. remove-blocks (if current-block-uuid [current-block-uuid] nil)
  1544. query-result (and query-atom (rum/react query-atom))
  1545. result (if (and query-result dsl-query?)
  1546. query-result
  1547. (db/custom-query-result-transform query-result remove-blocks q))
  1548. result (if query-result
  1549. (db/custom-query-result-transform query-result remove-blocks q))
  1550. view-f (and view (sci/eval-string (pr-str view)))
  1551. only-blocks? (:block/uuid (first result))
  1552. blocks-grouped-by-page? (and (seq result)
  1553. (coll? (first result))
  1554. (:page/name (ffirst result))
  1555. (:block/uuid (first (second (first result))))
  1556. true)
  1557. built-in? (built-in-custom-query? title)]
  1558. [:div.custom-query.mt-2 (get config :attr {})
  1559. (when-not (and built-in? (empty? result))
  1560. (ui/foldable
  1561. [:div.opacity-70
  1562. title]
  1563. (cond
  1564. (and (seq result) view-f)
  1565. (let [result (try
  1566. (sci/call-fn view-f result)
  1567. (catch js/Error error
  1568. (log/error :custom-view-failed {:error error
  1569. :result result})
  1570. [:div "Custom view failed: "
  1571. (str error)]))]
  1572. (util/hiccup-keywordize result))
  1573. (and (seq result)
  1574. (or only-blocks? blocks-grouped-by-page?))
  1575. (->hiccup result (cond-> (assoc config
  1576. ;; :editor-box editor/box
  1577. :custom-query? true
  1578. :group-by-page? blocks-grouped-by-page?)
  1579. children?
  1580. (assoc :ref? true))
  1581. {:style {:margin-top "0.25rem"
  1582. :margin-left "0.25rem"}})
  1583. ;; page list
  1584. (and (seq result)
  1585. (:page/name (first result)))
  1586. [:ul#query-pages.mt-1
  1587. (for [{:page/keys [name original-name] :as page-entity} result]
  1588. [:li.mt-1
  1589. [:a {:href (rfe/href :page {:name name})
  1590. :on-click (fn [e]
  1591. (util/stop e)
  1592. (if (gobj/get e "shiftKey")
  1593. (state/sidebar-add-block!
  1594. (state/get-current-repo)
  1595. (:db/id page-entity)
  1596. :page
  1597. {:page page-entity})
  1598. (route-handler/redirect! {:to :page
  1599. :path-params {:name name}})))}
  1600. (or original-name name)]])]
  1601. (seq result) ;TODO: table
  1602. (let [result (->>
  1603. (for [record result]
  1604. (if (map? record)
  1605. (str (util/pp-str record) "\n")
  1606. record))
  1607. (remove nil?))]
  1608. [:pre result])
  1609. :else
  1610. [:div.text-sm.mt-2.ml-2.font-medium.opacity-50 "Empty"])
  1611. collapsed?))])))
  1612. (defn admonition
  1613. [config type options result]
  1614. (when-let [icon (case (string/lower-case (name type))
  1615. "note" svg/note
  1616. "tip" svg/tip
  1617. "important" svg/important
  1618. "caution" svg/caution
  1619. "warning" svg/warning
  1620. nil)]
  1621. [:div.flex.flex-row.admonitionblock.align-items {:class type}
  1622. [:div.pr-4.admonition-icon.flex.flex-col.justify-center
  1623. {:title (string/upper-case type)} (icon)]
  1624. [:div.ml-4.text-lg
  1625. (markup-elements-cp config result)]]))
  1626. (defn markup-element-cp
  1627. [{:keys [html-export?] :as config} item]
  1628. (try
  1629. (match item
  1630. ["Properties" m]
  1631. [:div.properties
  1632. (let [format (:block/format config)]
  1633. (for [[k v] (dissoc m :roam_alias :roam_tags)]
  1634. (when (and (not (and (= k :macros) (empty? v))) ; empty macros
  1635. )
  1636. [:div.property
  1637. [:span.font-medium.mr-1 (str (name k) ": ")]
  1638. (if (coll? v)
  1639. (let [vals (for [item v]
  1640. (if (coll? v)
  1641. (let [config (if (= k :alias)
  1642. (assoc config :page/alias? true))]
  1643. (page-cp config {:page/name item}))
  1644. (inline-text format item)))]
  1645. (interpose [:span ", "] vals))
  1646. (inline-text format v))])))]
  1647. ["Paragraph" l]
  1648. ;; TODO: speedup
  1649. (if (re-find #"\"Export_Snippet\" \"embed\"" (str l))
  1650. (->elem :div (map-inline config l))
  1651. (->elem :div.is-paragraph (map-inline config l)))
  1652. ["Horizontal_Rule"]
  1653. (when-not (:slide? config)
  1654. [:hr])
  1655. ["Heading" h]
  1656. (block-container config h)
  1657. ["List" l]
  1658. (let [lists (divide-lists l)]
  1659. (if (= 1 (count lists))
  1660. (let [l (first lists)]
  1661. (->elem
  1662. (list-element l)
  1663. (map #(list-item config %) l)))
  1664. [:div.list-group
  1665. (for [l lists]
  1666. (->elem
  1667. (list-element l)
  1668. (map #(list-item config %) l)))]))
  1669. ["Table" t]
  1670. (table config t)
  1671. ["Math" s]
  1672. (if html-export?
  1673. (latex/html-export s true true)
  1674. (latex/latex (str (dc/squuid)) s true true))
  1675. ["Example" l]
  1676. [:pre.pre-wrap-white-space
  1677. (join-lines l)]
  1678. ["Src" options]
  1679. (let [{:keys [language options lines pos_meta]} options
  1680. attr (if language
  1681. {:data-lang language})
  1682. code (join-lines lines)]
  1683. (cond
  1684. html-export?
  1685. (highlight/html-export attr code)
  1686. :else
  1687. (let [language (if (contains? #{"edn" "clj" "cljc" "cljs" "clojure"} language) "text/x-clojure" language)]
  1688. (if (:slide? config)
  1689. (highlight/highlight (str (medley/random-uuid)) {:data-lang language} code)
  1690. [:div
  1691. (lazy-editor/editor config (str (dc/squuid)) attr code pos_meta)
  1692. (when (and (= language "text/x-clojure") (contains? (set options) ":results"))
  1693. (sci/eval-result code))]))))
  1694. ["Quote" l]
  1695. (->elem
  1696. :blockquote
  1697. (markup-elements-cp config l))
  1698. ["Raw_Html" content]
  1699. (when (not html-export?)
  1700. [:div.raw_html {:dangerouslySetInnerHTML
  1701. {:__html content}}])
  1702. ["Export" "html" options content]
  1703. (when (not html-export?)
  1704. [:div.export_html {:dangerouslySetInnerHTML
  1705. {:__html content}}])
  1706. ["Hiccup" content]
  1707. (ui/catch-error
  1708. [:div.warning {:title "Invalid hiccup"}
  1709. content]
  1710. (-> (safe-read-string content)
  1711. (security/remove-javascript-links-in-href)))
  1712. ["Export" "latex" options content]
  1713. (if html-export?
  1714. (latex/html-export content true false)
  1715. (latex/latex (str (dc/squuid)) content true false))
  1716. ["Custom" "query" _options result content]
  1717. (try
  1718. (let [query (reader/read-string content)]
  1719. (custom-query config query))
  1720. (catch js/Error e
  1721. (println "read-string error:")
  1722. (js/console.error e)
  1723. [:div.warning {:title "Invalid query"}
  1724. content]))
  1725. ["Custom" "note" options result content]
  1726. (admonition config "note" options result)
  1727. ["Custom" "tip" options result content]
  1728. (admonition config "tip" options result)
  1729. ["Custom" "important" options result content]
  1730. (admonition config "important" options result)
  1731. ["Custom" "caution" options result content]
  1732. (admonition config "caution" options result)
  1733. ["Custom" "warning" options result content]
  1734. (admonition config "warning" options result)
  1735. ["Custom" name options l content]
  1736. (->elem
  1737. :div
  1738. {:class name}
  1739. (markup-elements-cp config l))
  1740. ["Latex_Fragment" l]
  1741. [:p.latex-fragment
  1742. (inline config ["Latex_Fragment" l])]
  1743. ["Latex_Environment" name option content]
  1744. (let [content (latex-environment-content name option content)]
  1745. (if html-export?
  1746. (latex/html-export content true true)
  1747. (latex/latex (str (dc/squuid)) content true true)))
  1748. ["Displayed_Math" content]
  1749. (if html-export?
  1750. (latex/html-export content true true)
  1751. (latex/latex (str (dc/squuid)) content true true))
  1752. ["Footnote_Definition" name definition]
  1753. (let [id (util/url-encode name)]
  1754. [:div.footdef
  1755. [:div.footpara
  1756. (conj
  1757. (markup-element-cp config ["Paragraph" definition])
  1758. [:a.ml-1 {:id (str "fn." id)
  1759. :style {:font-size 14}
  1760. :class "footnum"
  1761. :href (str "#fnr." id)}
  1762. [:sup.fn (str name "↩︎")]])]])
  1763. :else
  1764. "")
  1765. (catch js/Error e
  1766. (println "Convert to html failed, error: " e)
  1767. "")))
  1768. (defn markup-elements-cp
  1769. [config col]
  1770. (map #(markup-element-cp config %) col))
  1771. (defn build-slide-sections
  1772. ([blocks config]
  1773. (build-slide-sections blocks config nil))
  1774. ([blocks config build-block-fn]
  1775. (when (seq blocks)
  1776. (let [blocks (map #(dissoc % :block/children) blocks)
  1777. first-block-level (:block/level (first blocks))
  1778. sections (reduce
  1779. (fn [acc block]
  1780. (let [block (dissoc block :block/meta)
  1781. level (:block/level block)
  1782. block-cp (if build-block-fn
  1783. (build-block-fn config block)
  1784. (rum/with-key
  1785. (block-container config block)
  1786. (str "slide-" (:block/uuid block))))]
  1787. (if (= first-block-level level)
  1788. ;; new slide
  1789. (conj acc [[block block-cp]])
  1790. (update acc (dec (count acc))
  1791. (fn [sections]
  1792. (conj sections [block block-cp]))))))
  1793. []
  1794. blocks)]
  1795. sections))))
  1796. (rum/defc add-button < rum/reactive
  1797. [config ref? custom-query? blocks]
  1798. (let [editing (state/sub [:editor/editing?])]
  1799. (when-not (or ref? custom-query?
  1800. (:block/dummy? (last blocks))
  1801. (second (first editing)))
  1802. (when-let [last-block (last blocks)]
  1803. [:a.add-button-link {:on-click (fn []
  1804. (editor-handler/insert-new-block-without-save-previous! config last-block))}
  1805. svg/plus-circle]))))
  1806. (defn blocks-container
  1807. [blocks config]
  1808. (let [blocks (map #(dissoc % :block/children) blocks)
  1809. sidebar? (:sidebar? config)
  1810. ref? (:ref? config)
  1811. custom-query? (:custom-query? config)
  1812. blocks->vec-tree #(if (or custom-query? ref?) % (block-handler/blocks->vec-tree %))
  1813. blocks (blocks->vec-tree blocks)]
  1814. (when (seq blocks)
  1815. [:div.blocks-container.flex-1
  1816. {:style {:margin-left (cond
  1817. sidebar?
  1818. 0
  1819. :else
  1820. -10)}}
  1821. (let [first-block (first blocks)
  1822. blocks (if (and (:block/pre-block? first-block)
  1823. (block-handler/pre-block-with-only-title? (:block/repo first-block) (:block/uuid first-block)))
  1824. (rest blocks)
  1825. blocks)
  1826. first-id (:block/uuid (first blocks))]
  1827. (for [[idx item] (medley/indexed blocks)]
  1828. (let [item (-> (if (:block/dummy? item)
  1829. item
  1830. (dissoc item :block/meta)))
  1831. config (assoc config :block/uuid (:block/uuid item))]
  1832. (rum/with-key
  1833. (block-container config item)
  1834. (:block/uuid item)))))
  1835. ;; (add-button config ref? custom-query? blocks)
  1836. ])))
  1837. ;; headers to hiccup
  1838. (defn ->hiccup
  1839. [blocks config option]
  1840. [:div.content
  1841. (cond-> option
  1842. (:document/mode? config)
  1843. (assoc :class "doc-mode"))
  1844. (if (:group-by-page? config)
  1845. [:div.flex.flex-col
  1846. (for [[page blocks] blocks]
  1847. (let [alias? (:page/alias? page)
  1848. page (db/entity (:db/id page))]
  1849. [:div.my-2 (cond-> {:key (str "page-" (:db/id page))}
  1850. (:ref? config)
  1851. (assoc :class "color-level px-7 py-2 rounded"))
  1852. (ui/foldable
  1853. [:div
  1854. (page-cp config page)
  1855. (when alias? [:span.text-sm.font-medium.opacity-50 " Alias"])]
  1856. (blocks-container blocks config))]))]
  1857. (blocks-container blocks config))])
  1858. (comment
  1859. ;; timestamps
  1860. ;; [2020-02-10 Mon 13:22]
  1861. ;; repetition
  1862. (def r1 "<2005-10-01 Sat +1m>")
  1863. ;; TODO: mldoc add supports
  1864. (def r2 "<2005-10-01 Sat +1m -3d>")
  1865. (def l
  1866. "1. First item
  1867. hello world
  1868. 2. Second item
  1869. nice
  1870. 3. Third item")
  1871. (def t
  1872. "| Name | Phone | Age |
  1873. |-------+-------+-----|
  1874. | Peter | 1234 | 17 |
  1875. | Anna | 4321 | 25 |"))