block.cljs 126 KB

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