block.cljs 116 KB

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