block.cljs 99 KB

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