block.cljs 127 KB

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