block.cljs 124 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222
  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.svg :as svg]
  16. [frontend.components.macro :as macro]
  17. [frontend.config :as config]
  18. [frontend.context.i18n :refer [t]]
  19. [frontend.date :as date]
  20. [frontend.db :as db]
  21. [frontend.db.utils :as db-utils]
  22. [frontend.db-mixins :as db-mixins]
  23. [frontend.db.model :as model]
  24. [frontend.db.query-dsl :as query-dsl]
  25. [frontend.extensions.highlight :as highlight]
  26. [frontend.extensions.latex :as latex]
  27. [frontend.extensions.sci :as sci]
  28. [frontend.extensions.pdf.assets :as pdf-assets]
  29. [frontend.extensions.zotero :as zotero]
  30. [frontend.extensions.lightbox :as lightbox]
  31. [frontend.extensions.video.youtube :as youtube]
  32. [frontend.format.block :as block]
  33. [frontend.format.mldoc :as mldoc]
  34. [frontend.components.plugins :as plugins]
  35. [frontend.handler.plugin :as plugin-handler]
  36. [frontend.handler.block :as block-handler]
  37. [frontend.handler.dnd :as dnd]
  38. [frontend.handler.editor :as editor-handler]
  39. [frontend.handler.repeated :as repeated]
  40. [frontend.handler.route :as route-handler]
  41. [frontend.handler.ui :as ui-handler]
  42. [frontend.handler.query :as query-handler]
  43. [frontend.handler.common :as common-handler]
  44. [frontend.modules.outliner.tree :as tree]
  45. [frontend.search :as search]
  46. [frontend.security :as security]
  47. [frontend.state :as state]
  48. [frontend.template :as template]
  49. [logseq.graph-parser.text :as text]
  50. [frontend.ui :as ui]
  51. [frontend.util :as util]
  52. [frontend.util.clock :as clock]
  53. [frontend.util.property :as property]
  54. [frontend.util.drawer :as drawer]
  55. [frontend.util.text :as text-util]
  56. [logseq.graph-parser.config :as gp-config]
  57. [logseq.graph-parser.util :as gp-util]
  58. [logseq.graph-parser.mldoc :as gp-mldoc]
  59. [logseq.graph-parser.block :as gp-block]
  60. [goog.dom :as gdom]
  61. [goog.object :as gobj]
  62. [lambdaisland.glogi :as log]
  63. [medley.core :as medley]
  64. [promesa.core :as p]
  65. [reitit.frontend.easy :as rfe]
  66. [rum.core :as rum]
  67. [shadow.loader :as loader]
  68. [frontend.components.query-table :as query-table]
  69. [frontend.mobile.util :as mobile-util]))
  70. (defn safe-read-string
  71. ([s]
  72. (safe-read-string s true))
  73. ([s warn?]
  74. (try
  75. (reader/read-string s)
  76. (catch :default e
  77. (log/error :read-string-error e :string s)
  78. (when warn?
  79. [:div.warning {:title "read-string failed"}
  80. s])))))
  81. ;; local state
  82. (defonce *dragging?
  83. (atom false))
  84. (defonce *dragging-block
  85. (atom nil))
  86. (defonce *drag-to-block
  87. (atom nil))
  88. (def *move-to (atom nil))
  89. ;; TODO: dynamic
  90. (defonce max-depth-of-links 5)
  91. (defonce *blocks-container-id (atom 0))
  92. ;; TODO:
  93. ;; add `key`
  94. (defn- remove-nils
  95. [col]
  96. (remove nil? col))
  97. (defn vec-cat
  98. [& args]
  99. (->> (apply concat args)
  100. remove-nils
  101. vec))
  102. (defn ->elem
  103. ([elem items]
  104. (->elem elem nil items))
  105. ([elem attrs items]
  106. (let [elem (keyword elem)]
  107. (if attrs
  108. (vec
  109. (cons elem
  110. (cons attrs
  111. (seq items))))
  112. (vec
  113. (cons elem
  114. (seq items)))))))
  115. (defn- join-lines
  116. [l]
  117. (string/trim (apply str l)))
  118. (defn- string-of-url
  119. [url]
  120. (match url
  121. ["File" s]
  122. (-> (string/replace s "file://" "")
  123. ;; "file:/Users/ll/Downloads/test.pdf" is a normal org file link
  124. (string/replace "file:" ""))
  125. ["Complex" m]
  126. (let [{:keys [link protocol]} m]
  127. (if (= protocol "file")
  128. link
  129. (str protocol "://" link)))))
  130. (defn- get-file-absolute-path
  131. [config path]
  132. (let [path (string/replace path "file:" "")
  133. block-id (:block/uuid config)
  134. current-file (and block-id
  135. (:file/path (:block/file (:block/page (db/entity [:block/uuid block-id])))))]
  136. (when current-file
  137. (let [parts (string/split current-file #"/")
  138. parts-2 (string/split path #"/")
  139. current-dir (string/join "/" (drop-last 1 parts))]
  140. (cond
  141. (if util/win32? (utils/win32 path) (util/starts-with? path "/"))
  142. path
  143. (and (not (util/starts-with? path ".."))
  144. (not (util/starts-with? path ".")))
  145. (str current-dir "/" path)
  146. :else
  147. (let [parts (loop [acc []
  148. parts (reverse parts)
  149. col (reverse parts-2)]
  150. (if (empty? col)
  151. acc
  152. (let [[part parts] (case (first col)
  153. ".."
  154. [(first parts) (rest parts)]
  155. "."
  156. ["" parts]
  157. [(first col) (rest parts)])]
  158. (recur (conj acc part)
  159. parts
  160. (rest col)))))
  161. parts (remove #(string/blank? %) parts)]
  162. (string/join "/" (reverse parts))))))))
  163. (defonce *resizing-image? (atom false))
  164. (rum/defcs resizable-image <
  165. (rum/local nil ::size)
  166. {:will-unmount (fn [state]
  167. (reset! *resizing-image? false)
  168. state)}
  169. [state config title src metadata full_text local?]
  170. (let [size (get state ::size)]
  171. (ui/resize-provider
  172. (ui/resize-consumer
  173. (if-not (mobile-util/native-ios?)
  174. (cond->
  175. {:className "resize image-resize"
  176. :onSizeChanged (fn [value]
  177. (when (and (not @*resizing-image?)
  178. (some? @size)
  179. (not= value @size))
  180. (reset! *resizing-image? true))
  181. (reset! size value))
  182. :onMouseUp (fn []
  183. (when (and @size @*resizing-image?)
  184. (when-let [block-id (:block/uuid config)]
  185. (let [size (bean/->clj @size)]
  186. (editor-handler/resize-image! block-id metadata full_text size))))
  187. (when @*resizing-image?
  188. ;; TODO: need a better way to prevent the clicking to edit current block
  189. (js/setTimeout #(reset! *resizing-image? false) 200)))
  190. :onClick (fn [e]
  191. (when @*resizing-image? (util/stop e)))}
  192. (and (:width metadata) (not (util/mobile?)))
  193. (assoc :style {:width (:width metadata)}))
  194. {})
  195. [:div.asset-container {:key "resize-asset-container"}
  196. [:img.rounded-sm.shadow-xl.relative
  197. (merge
  198. {:loading "lazy"
  199. :src src
  200. :title title}
  201. metadata)]
  202. [:span.ctl
  203. [:a.delete
  204. {:title "Delete this image"
  205. :on-click
  206. (fn [e]
  207. (when-let [block-id (:block/uuid config)]
  208. (let [confirm-fn (ui/make-confirm-modal
  209. {:title (t :asset/confirm-delete (.toLocaleLowerCase (t :text/image)))
  210. :sub-title (if local? :asset/physical-delete "")
  211. :sub-checkbox? local?
  212. :on-confirm (fn [_e {:keys [close-fn sub-selected]}]
  213. (close-fn)
  214. (editor-handler/delete-asset-of-block!
  215. {:block-id block-id
  216. :local? local?
  217. :delete-local? (first sub-selected)
  218. :repo (state/get-current-repo)
  219. :href src
  220. :title title
  221. :full-text full_text}))})]
  222. (state/set-modal! confirm-fn)
  223. (util/stop e))))}
  224. svg/trash-sm]
  225. [:a.delete.ml-1
  226. {:title "maximize image"
  227. :on-click (fn [^js e] (let [images (js/document.querySelectorAll ".asset-container img")
  228. images (to-array images)
  229. images (if-not (= (count images) 1)
  230. (let [^js _image (.closest (.-target e) ".asset-container")
  231. image (. _image querySelector "img")]
  232. (cons image (remove #(= image %) images)))
  233. images)
  234. images (for [^js it images] {:src (.-src it)
  235. :w (.-naturalWidth it)
  236. :h (.-naturalHeight it)})]
  237. (when (seq images)
  238. (lightbox/preview-images! images))))}
  239. (svg/maximize)]]]))))
  240. (rum/defc audio-cp [src]
  241. [:audio {:src src
  242. :controls true}])
  243. (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? (gp-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-util/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-util/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-util/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-util/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-util/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-util/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. :checked checked?
  1437. :on-mouse-down (fn [e]
  1438. (util/stop-propagation e))
  1439. :on-change (fn [_e]
  1440. (if checked?
  1441. (editor-handler/uncheck block)
  1442. (editor-handler/check block)))}))))
  1443. (defn list-checkbox
  1444. [config checked?]
  1445. (ui/checkbox
  1446. {:style {:margin-right 6}
  1447. :checked checked?
  1448. :on-change (fn [event]
  1449. (let [target (.-target event)
  1450. block (:block config)
  1451. item-content (.. target -nextSibling -data)
  1452. item-full-content (str (if checked? "[X]" "[ ]") " " item-content)
  1453. new-item-full-content (str (if checked? "[ ]" "[X]") " " item-content)]
  1454. (editor-handler/toggle-list-checkbox block item-full-content new-item-full-content)))}))
  1455. (defn marker-switch
  1456. [{:block/keys [marker] :as block}]
  1457. (when (contains? #{"NOW" "LATER" "TODO" "DOING"} marker)
  1458. (let [set-marker-fn (fn [new-marker]
  1459. (fn [e]
  1460. (util/stop e)
  1461. (editor-handler/set-marker block new-marker)))
  1462. next-marker (case marker
  1463. "NOW" "LATER"
  1464. "LATER" "NOW"
  1465. "TODO" "DOING"
  1466. "DOING" "TODO")]
  1467. [:a
  1468. {:class (str "marker-switch block-marker " marker)
  1469. :title (util/format "Change from %s to %s" marker next-marker)
  1470. :on-mouse-down (set-marker-fn next-marker)}
  1471. marker])))
  1472. (defn marker-cp
  1473. [{:block/keys [pre-block? marker] :as _block}]
  1474. (when-not pre-block?
  1475. (when (contains? #{"IN-PROGRESS" "WAIT" "WAITING"} marker)
  1476. [:span {:class (str "task-status block-marker " (string/lower-case marker))
  1477. :style {:margin-right 3.5}}
  1478. (string/upper-case marker)])))
  1479. (rum/defc set-priority
  1480. [block priority]
  1481. [:div
  1482. (let [priorities (sort (remove #(= priority %) ["A" "B" "C"]))]
  1483. (for [p priorities]
  1484. [:a.mr-2.text-base.tooltip-priority {:key (str (random-uuid))
  1485. :priority p
  1486. :on-click (fn [] (editor-handler/set-priority block p))}]))])
  1487. (rum/defc priority-text
  1488. [priority]
  1489. [:a.opacity-50.hover:opacity-100
  1490. {:class "priority"
  1491. :href (rfe/href :page {:name priority})
  1492. :style {:margin-right 3.5}}
  1493. (util/format "[#%s]" (str priority))])
  1494. (defn priority-cp
  1495. [{:block/keys [pre-block? priority] :as block}]
  1496. (when (and (not pre-block?) priority)
  1497. (ui/tippy
  1498. {:interactive true
  1499. :html (set-priority block priority)}
  1500. (priority-text priority))))
  1501. (defn block-tags-cp
  1502. [{:block/keys [pre-block? tags] :as _block}]
  1503. (when (and (not pre-block?)
  1504. (seq tags))
  1505. (->elem
  1506. :span
  1507. {:class "block-tags"}
  1508. (mapv (fn [tag]
  1509. (when-let [page (db/entity (:db/id tag))]
  1510. (let [tag (:block/name page)]
  1511. [:a.tag.mx-1 {:data-ref tag
  1512. :key (str "tag-" (:db/id tag))
  1513. :href (rfe/href :page {:name tag})}
  1514. (str "#" tag)])))
  1515. tags))))
  1516. (declare block-content)
  1517. (defn build-block-title
  1518. [config {:block/keys [title marker pre-block? properties level heading-level]
  1519. :as t}]
  1520. (let [config (assoc config :block t)
  1521. slide? (boolean (:slide? config))
  1522. block-ref? (:block-ref? config)
  1523. block-type (or (keyword (:ls-type properties)) :default)
  1524. html-export? (:html-export? config)
  1525. checkbox (when (and (not pre-block?)
  1526. (not html-export?))
  1527. (block-checkbox t "mr-1 cursor"))
  1528. marker-switch (when (and (not pre-block?)
  1529. (not html-export?))
  1530. (marker-switch t))
  1531. marker-cp (marker-cp t)
  1532. priority (priority-cp t)
  1533. tags (block-tags-cp t)
  1534. bg-color (:background-color properties)
  1535. heading-level (or (and heading-level
  1536. (<= heading-level 6)
  1537. heading-level)
  1538. (and (get properties :heading)
  1539. (<= level 6)
  1540. level))
  1541. elem (if heading-level
  1542. (keyword (str "h" heading-level
  1543. (when block-ref? ".inline")))
  1544. :span.inline-flex.items-center)]
  1545. (->elem
  1546. elem
  1547. (merge
  1548. {:data-hl-type (:hl-type properties)}
  1549. (when (and marker
  1550. (not (string/blank? marker))
  1551. (not= "nil" marker))
  1552. {:class (str (string/lower-case marker))})
  1553. (when bg-color
  1554. {:style {:background-color bg-color
  1555. :padding-left 6
  1556. :padding-right 6
  1557. :color "#FFFFFF"}
  1558. :class "with-bg-color"}))
  1559. (remove-nils
  1560. (concat
  1561. [(when-not slide? checkbox)
  1562. (when-not slide? marker-switch)
  1563. marker-cp
  1564. priority]
  1565. (if title
  1566. (conj
  1567. (map-inline config title)
  1568. (when (and (util/electron?) (not= block-type :default))
  1569. [:a.prefix-link
  1570. {:on-click #(case block-type
  1571. ;; pdf annotation
  1572. :annotation (pdf-assets/open-block-ref! t)
  1573. (.preventDefault %))}
  1574. [:span.hl-page
  1575. [:strong.forbid-edit (str "P" (or (:hl-page properties) "?"))]
  1576. [:label.blank " "]]
  1577. (when-let [st (and (= :area (keyword (:hl-type properties)))
  1578. (:hl-stamp properties))]
  1579. (pdf-assets/area-display t st))]))
  1580. [[:span.opacity-50 "Click here to start writing, type '/' to see all the commands."]])
  1581. [tags])))))
  1582. (rum/defc span-comma
  1583. []
  1584. [:span ", "])
  1585. (rum/defc property-cp
  1586. [config block k v]
  1587. (let [date (and (= k :date) (date/get-locale-string (str v)))]
  1588. [:div
  1589. [:span.page-property-key.font-medium (name k)]
  1590. [:span.mr-1 ":"]
  1591. (cond
  1592. (int? v)
  1593. v
  1594. date
  1595. date
  1596. (coll? v)
  1597. (let [v (->> (remove string/blank? v)
  1598. (filter string?))
  1599. vals (for [v-item v]
  1600. (page-cp config {:block/name v-item}))
  1601. elems (interpose (span-comma) vals)]
  1602. (for [elem elems]
  1603. (rum/with-key elem (str (random-uuid)))))
  1604. (and (string? v) (gp-util/wrapped-by-quotes? v))
  1605. (gp-util/unquote-string v)
  1606. :else
  1607. (inline-text config (:block/format block) (str v)))]))
  1608. (rum/defc properties-cp
  1609. [config block]
  1610. (let [properties (walk/keywordize-keys (:block/properties block))
  1611. properties-order (:block/properties-order block)
  1612. properties (apply dissoc properties (property/built-in-properties))
  1613. properties-order (remove (property/built-in-properties) properties-order)
  1614. pre-block? (:block/pre-block? block)
  1615. properties (if pre-block?
  1616. (let [repo (state/get-current-repo)
  1617. properties (dissoc properties :title :filters)
  1618. aliases (db/get-page-alias-names repo
  1619. (:block/name (db/pull (:db/id (:block/page block)))))]
  1620. (if (seq aliases)
  1621. (if (:alias properties)
  1622. (update properties :alias (fn [c]
  1623. (util/distinct-by string/lower-case (concat c aliases))))
  1624. (assoc properties :alias aliases))
  1625. properties))
  1626. properties)
  1627. properties-order (if pre-block?
  1628. (remove #{:title :filters} properties-order)
  1629. properties-order)
  1630. properties (if (seq properties-order)
  1631. (map (fn [k] [k (get properties k)]) properties-order)
  1632. properties)]
  1633. (cond
  1634. (seq properties)
  1635. [:div.block-properties
  1636. {:class (when pre-block? "page-properties")
  1637. :title (if pre-block?
  1638. "Click to edit this page's properties"
  1639. "Click to edit this block's properties")}
  1640. (for [[k v] properties]
  1641. (rum/with-key (property-cp config block k v)
  1642. (str (:block/uuid block) "-" k)))]
  1643. (and pre-block? properties)
  1644. [:span.opacity-50 "Properties"]
  1645. :else
  1646. nil)))
  1647. (rum/defcs timestamp-cp < rum/reactive
  1648. (rum/local false ::show?)
  1649. (rum/local {} ::pos)
  1650. {:will-unmount (fn [state]
  1651. (when-let [show? (::show? state)]
  1652. (reset! show? false))
  1653. state)}
  1654. [state block typ ast]
  1655. (let [show? (get state ::show?)]
  1656. [:div.flex.flex-col.timestamp
  1657. [:div.text-sm.flex.flex-row
  1658. [:div.opacity-50.font-medium.timestamp-label
  1659. (str typ ": ")]
  1660. [:a.opacity-80.hover:opacity-100
  1661. {:on-mouse-down (fn [e]
  1662. (util/stop e)
  1663. (if @show?
  1664. (do
  1665. (reset! show? false)
  1666. (reset! commands/*current-command nil)
  1667. (state/set-editor-show-date-picker! false)
  1668. (state/set-timestamp-block! nil))
  1669. (do
  1670. (reset! show? true)
  1671. (reset! commands/*current-command typ)
  1672. (state/set-editor-show-date-picker! true)
  1673. (state/set-timestamp-block! {:block block
  1674. :typ typ
  1675. :show? show?}))))}
  1676. [:span.time-start "<"] [:time (repeated/timestamp->text ast)] [:span.time-stop ">"]]]
  1677. (when (true? @show?)
  1678. (let [ts (repeated/timestamp->map ast)]
  1679. [:div.my-4
  1680. (datetime-comp/date-picker nil nil ts)]))]))
  1681. (defn- target-forbidden-edit?
  1682. [target]
  1683. (or
  1684. (dom/has-class? target "forbid-edit")
  1685. (dom/has-class? target "bullet")
  1686. (dom/has-class? target "logbook")
  1687. (util/link? target)
  1688. (util/time? target)
  1689. (util/input? target)
  1690. (util/audio? target)
  1691. (util/video? target)
  1692. (util/details-or-summary? target)
  1693. (and (util/sup? target)
  1694. (dom/has-class? target "fn"))
  1695. (dom/has-class? target "image-resize")))
  1696. (defn- block-content-on-mouse-down
  1697. [e block block-id _content edit-input-id]
  1698. (.stopPropagation e)
  1699. (let [target (gobj/get e "target")
  1700. button (gobj/get e "buttons")
  1701. shift? (gobj/get e "shiftKey")
  1702. meta? (util/meta-key? e)]
  1703. (if (and meta? (not (state/get-edit-input-id)))
  1704. (do
  1705. (util/stop e)
  1706. (state/conj-selection-block! (gdom/getElement block-id) :down))
  1707. (when (contains? #{1 0} button)
  1708. (when-not (target-forbidden-edit? target)
  1709. (if (and shift? (state/get-selection-start-block))
  1710. (editor-handler/highlight-selection-area! block-id)
  1711. (do
  1712. (editor-handler/clear-selection!)
  1713. (editor-handler/unhighlight-blocks!)
  1714. (let [f #(let [block (or (db/pull [:block/uuid (:block/uuid block)]) block)
  1715. cursor-range (util/caret-range (gdom/getElement block-id))
  1716. {:block/keys [content format]} block
  1717. content (->> content
  1718. (property/remove-built-in-properties format)
  1719. (drawer/remove-logbook))]
  1720. ;; save current editing block
  1721. (let [{:keys [value] :as state} (editor-handler/get-state)]
  1722. (editor-handler/save-block! state value))
  1723. (state/set-editing!
  1724. edit-input-id
  1725. content
  1726. block
  1727. cursor-range
  1728. false))]
  1729. ;; wait a while for the value of the caret range
  1730. (if (util/ios?)
  1731. (f)
  1732. (js/setTimeout f 5))
  1733. (when block-id (state/set-selection-start-block! block-id))))))))))
  1734. (rum/defc dnd-separator-wrapper < rum/reactive
  1735. [block block-id slide? top? block-content?]
  1736. (let [dragging? (rum/react *dragging?)
  1737. drag-to-block (rum/react *drag-to-block)]
  1738. (when (and
  1739. (= block-id drag-to-block)
  1740. dragging?
  1741. (not slide?)
  1742. (not (:block/pre-block? block)))
  1743. (let [move-to (rum/react *move-to)]
  1744. (when-not
  1745. (or (and top? (not= move-to :top))
  1746. (and (not top?) (= move-to :top))
  1747. (and block-content? (not= move-to :nested))
  1748. (and (not block-content?)
  1749. (seq (:block/children block))
  1750. (= move-to :nested)))
  1751. (dnd-separator move-to block-content?))))))
  1752. (defn clock-summary-cp
  1753. [block body]
  1754. (when (and (state/enable-timetracking?)
  1755. (or (= (:block/marker block) "DONE")
  1756. (contains? #{"TODO" "LATER"} (:block/marker block))))
  1757. (let [summary (clock/clock-summary body true)]
  1758. (when (and summary
  1759. (not= summary "0m")
  1760. (not (string/blank? summary)))
  1761. [:div {:style {:max-width 100}}
  1762. (ui/tippy {:html (fn []
  1763. (when-let [logbook (drawer/get-logbook body)]
  1764. (let [clocks (->> (last logbook)
  1765. (filter #(string/starts-with? % "CLOCK:"))
  1766. (remove string/blank?))]
  1767. [:div.p-4
  1768. [:div.font-bold.mb-2 "LOGBOOK:"]
  1769. [:ul
  1770. (for [clock (take 10 (reverse clocks))]
  1771. [:li clock])]])))
  1772. :interactive true
  1773. :in-editor? true
  1774. :delay [1000, 100]}
  1775. [:div.text-sm.time-spent.ml-1 {:style {:padding-top 3}}
  1776. [:a.fade-link
  1777. summary]])]))))
  1778. (rum/defc block-content < rum/reactive
  1779. [config {:block/keys [uuid content children properties scheduled deadline format pre-block?] :as block} edit-input-id block-id slide?]
  1780. (let [{:block/keys [title body] :as block} (if (:block/title block) block
  1781. (merge block (block/parse-title-and-body uuid format pre-block? content)))
  1782. collapsed? (util/collapsed? block)
  1783. block-ref? (:block-ref? config)
  1784. block-ref-with-title? (and block-ref? (seq title))
  1785. block-type (or (:ls-type properties) :default)
  1786. content (if (string? content) (string/trim content) "")
  1787. mouse-down-key (if (util/ios?)
  1788. :on-click
  1789. :on-mouse-down ; TODO: it seems that Safari doesn't work well with on-mouse-down
  1790. )
  1791. attrs (cond->
  1792. {:blockid (str uuid)
  1793. :data-type (name block-type)
  1794. :style {:width "100%"}}
  1795. (not block-ref?)
  1796. (assoc mouse-down-key (fn [e]
  1797. (block-content-on-mouse-down e block block-id content edit-input-id))))]
  1798. [:div.block-content.inline
  1799. (cond-> {:id (str "block-content-" uuid)
  1800. :on-mouse-up (fn [e]
  1801. (when (and
  1802. (state/in-selection-mode?)
  1803. (not (string/includes? content "```"))
  1804. (not (gobj/get e "shiftKey"))
  1805. (not (util/meta-key? e)))
  1806. ;; clear highlighted text
  1807. (util/clear-selection!)))}
  1808. (not slide?)
  1809. (merge attrs))
  1810. [:<>
  1811. [:div.flex.flex-row.justify-between
  1812. [:div.flex-1
  1813. (cond
  1814. (seq title)
  1815. (build-block-title config block)
  1816. :else
  1817. nil)]
  1818. (clock-summary-cp block body)]
  1819. (when (seq children)
  1820. (dnd-separator-wrapper block block-id slide? false true))
  1821. (when deadline
  1822. (when-let [deadline-ast (block-handler/get-deadline-ast block)]
  1823. (timestamp-cp block "DEADLINE" deadline-ast)))
  1824. (when scheduled
  1825. (when-let [scheduled-ast (block-handler/get-scheduled-ast block)]
  1826. (timestamp-cp block "SCHEDULED" scheduled-ast)))
  1827. (when (and (seq properties)
  1828. (let [hidden? (property/properties-built-in? properties)]
  1829. (not hidden?))
  1830. (not (and block-ref? (or (seq title) (seq body))))
  1831. (not (:slide? config)))
  1832. (properties-cp config block))
  1833. (when (and (not block-ref-with-title?) (seq body))
  1834. [:div.block-body {:style {:display (if (and collapsed? (seq title)) "none" "")}}
  1835. ;; TODO: consistent id instead of the idx (since it could be changed later)
  1836. (let [body (block/trim-break-lines! (:block/body block))]
  1837. (for [[idx child] (medley/indexed body)]
  1838. (when-let [block (markup-element-cp config child)]
  1839. (rum/with-key (block-child block)
  1840. (str uuid "-" idx)))))])
  1841. (case (:block/warning block)
  1842. :multiple-blocks
  1843. [:p.warning.text-sm "Full content is not displayed, Logseq doesn't support multiple unordered lists or headings in a block."]
  1844. nil)]]))
  1845. (rum/defc block-refs-count < rum/static
  1846. [block *hide-block-refs?]
  1847. (let [block-refs-count (count (:block/_refs block))]
  1848. (when (> block-refs-count 0)
  1849. [:div
  1850. [:a.open-block-ref-link.bg-base-2.text-sm.ml-2.fade-link
  1851. {:title "Open block references"
  1852. :style {:margin-top -1}
  1853. :on-click (fn [e]
  1854. (if (gobj/get e "shiftKey")
  1855. (state/sidebar-add-block!
  1856. (state/get-current-repo)
  1857. (:db/id block)
  1858. :block-ref)
  1859. (swap! *hide-block-refs? not)))}
  1860. block-refs-count]])))
  1861. (rum/defcs block-content-or-editor < rum/reactive
  1862. (rum/local true :hide-block-refs?)
  1863. [state config {:block/keys [uuid format] :as block} edit-input-id block-id heading-level edit?]
  1864. (let [*hide-block-refs? (get state :hide-block-refs?)
  1865. editor-box (get config :editor-box)
  1866. editor-id (str "editor-" edit-input-id)
  1867. slide? (:slide? config)]
  1868. (if (and edit? editor-box)
  1869. [:div.editor-wrapper {:id editor-id}
  1870. (ui/catch-error
  1871. (ui/block-error "Something wrong in the editor" {})
  1872. (editor-box {:block block
  1873. :block-id uuid
  1874. :block-parent-id block-id
  1875. :format format
  1876. :heading-level heading-level
  1877. :on-hide (fn [value event]
  1878. (when (= event :esc)
  1879. (editor-handler/save-block! (editor-handler/get-state) value)
  1880. (let [select? (not (string/includes? value "```"))]
  1881. (editor-handler/escape-editing select?))))}
  1882. edit-input-id
  1883. config))]
  1884. (let [refs-count (count (:block/_refs block))]
  1885. [:div.flex.flex-col.block-content-wrapper
  1886. [:div.flex.flex-row
  1887. [:div.flex-1.w-full {:style {:display (if (:slide? config) "block" "flex")}}
  1888. (ui/catch-error
  1889. (ui/block-error "Block Render Error:"
  1890. {:content (:block/content block)
  1891. :section-attrs
  1892. {:on-click #(state/set-editing! edit-input-id (:block/content block) block "")}})
  1893. (block-content config block edit-input-id block-id slide?))]
  1894. [:div.flex.flex-row.items-center
  1895. (when (and (:embed? config)
  1896. (:embed-parent config))
  1897. [:a.opacity-30.hover:opacity-100.svg-small.inline
  1898. {:on-mouse-down (fn [e]
  1899. (util/stop e)
  1900. (when-let [block (:embed-parent config)]
  1901. (editor-handler/edit-block! block :max (:block/uuid block))))}
  1902. svg/edit])
  1903. (block-refs-count block *hide-block-refs?)]]
  1904. (when (and (not @*hide-block-refs?) (> refs-count 0))
  1905. (let [refs-cp (state/get-component :block/linked-references)]
  1906. (refs-cp uuid)))]))))
  1907. (defn non-dragging?
  1908. [e]
  1909. (and (= (gobj/get e "buttons") 1)
  1910. (not (dom/has-class? (gobj/get e "target") "bullet-container"))
  1911. (not (dom/has-class? (gobj/get e "target") "bullet"))
  1912. (not @*dragging?)))
  1913. (rum/defc breadcrumb-fragment
  1914. [config block label opts]
  1915. [:a {:on-mouse-down
  1916. (fn [e]
  1917. (cond
  1918. (gobj/get e "shiftKey")
  1919. (do
  1920. (util/stop e)
  1921. (state/sidebar-add-block!
  1922. (state/get-current-repo)
  1923. (:db/id block)
  1924. :block-ref))
  1925. (util/atom? (:navigating-block opts))
  1926. (do
  1927. (util/stop e)
  1928. (reset! (:navigating-block opts) (:block/uuid block)))
  1929. (some? (:sidebar-key config))
  1930. (do
  1931. (util/stop e)
  1932. (state/sidebar-replace-block!
  1933. (:sidebar-key config)
  1934. [(state/get-current-repo)
  1935. (:db/id block)
  1936. (if (:block/name block) :page :block)]))
  1937. :else
  1938. (route-handler/redirect-to-page! (:block/uuid block))))}
  1939. label])
  1940. (rum/defc breadcrumb-separator [] [:span.mx-2.opacity-50 "➤"])
  1941. (defn breadcrumb
  1942. [config repo block-id {:keys [show-page? indent? level-limit _navigating-block]
  1943. :or {show-page? true
  1944. level-limit 3}
  1945. :as opts}]
  1946. (let [parents (db/get-block-parents repo block-id (inc level-limit))
  1947. page (db/get-block-page repo block-id)
  1948. page-name (:block/name page)
  1949. page-original-name (:block/original-name page)
  1950. show? (or (seq parents) show-page? page-name)
  1951. parents (if (= page-name (:block/name (first parents)))
  1952. (rest parents)
  1953. parents)
  1954. more? (> (count parents) level-limit)
  1955. parents (if more? (take-last level-limit parents) parents)
  1956. config (assoc config :breadcrumb? true)]
  1957. (when show?
  1958. (let [page-name-props (when show-page?
  1959. [page
  1960. (page-cp (dissoc config :breadcrumb? true) page)
  1961. {:block/name (or page-original-name page-name)}])
  1962. parents-props (doall
  1963. (for [{:block/keys [uuid name content] :as block} parents]
  1964. (when-not name ; not page
  1965. (let [{:block/keys [title body]} (block/parse-title-and-body
  1966. uuid
  1967. (:block/format block)
  1968. (:block/pre-block? block)
  1969. content)
  1970. config (assoc config :block/uuid uuid)]
  1971. [block
  1972. (if (seq title)
  1973. (->elem :span (map-inline config title))
  1974. (->elem :div (markup-elements-cp config body)))]))))
  1975. breadcrumb (->> (into [] parents-props)
  1976. (concat [page-name-props] (when more? [:more]))
  1977. (filterv identity)
  1978. (map (fn [x] (if (vector? x)
  1979. (let [[block label] x]
  1980. (breadcrumb-fragment config block label opts))
  1981. [:span.opacity-70 "⋯"])))
  1982. (interpose (breadcrumb-separator)))]
  1983. [:div.breadcrumb.block-parents.flex-row.flex-1
  1984. {:class (when (seq breadcrumb)
  1985. (str (when-not (:search? config)
  1986. " my-2")
  1987. (when indent?
  1988. " ml-4")))}
  1989. breadcrumb]))))
  1990. (defn- block-drag-over
  1991. [event uuid top? block-id *move-to]
  1992. (util/stop event)
  1993. (when-not (dnd-same-block? uuid)
  1994. (let [over-block (gdom/getElement block-id)
  1995. rect (utils/getOffsetRect over-block)
  1996. element-top (gobj/get rect "top")
  1997. element-left (gobj/get rect "left")
  1998. x-offset (- (.. event -pageX) element-left)
  1999. cursor-top (gobj/get event "clientY")
  2000. move-to-value (cond
  2001. (and top? (<= (js/Math.abs (- cursor-top element-top)) 16))
  2002. :top
  2003. (> x-offset 50)
  2004. :nested
  2005. :else
  2006. :sibling)]
  2007. (reset! *drag-to-block block-id)
  2008. (reset! *move-to move-to-value))))
  2009. (defn- block-drag-leave
  2010. [*move-to]
  2011. (reset! *move-to nil))
  2012. (defn- block-drop
  2013. [event uuid target-block *move-to]
  2014. (util/stop event)
  2015. (when-not (dnd-same-block? uuid)
  2016. (let [block-uuids (state/get-selection-block-ids)
  2017. lookup-refs (map (fn [id] [:block/uuid id]) block-uuids)
  2018. selected (db/pull-many (state/get-current-repo) '[*] lookup-refs)
  2019. blocks (if (seq selected) selected [@*dragging-block])]
  2020. (dnd/move-blocks event blocks target-block @*move-to)))
  2021. (reset! *dragging? false)
  2022. (reset! *dragging-block nil)
  2023. (reset! *drag-to-block nil)
  2024. (reset! *move-to nil)
  2025. (editor-handler/unhighlight-blocks!))
  2026. (defn- block-drag-end
  2027. [_event *move-to]
  2028. (reset! *dragging? false)
  2029. (reset! *dragging-block nil)
  2030. (reset! *drag-to-block nil)
  2031. (reset! *move-to nil)
  2032. (editor-handler/unhighlight-blocks!))
  2033. (defn- block-mouse-over
  2034. [uuid e *control-show? block-id doc-mode?]
  2035. (when-not @*dragging?
  2036. (util/stop e)
  2037. (when (or
  2038. (model/block-collapsed? uuid)
  2039. (editor-handler/collapsable? uuid {:semantic? true}))
  2040. (reset! *control-show? true))
  2041. (when-let [parent (gdom/getElement block-id)]
  2042. (let [node (.querySelector parent ".bullet-container")]
  2043. (when doc-mode?
  2044. (dom/remove-class! node "hide-inner-bullet"))))
  2045. (when (and
  2046. (state/in-selection-mode?)
  2047. (non-dragging? e))
  2048. (util/stop e)
  2049. (editor-handler/highlight-selection-area! block-id))))
  2050. (defn- block-mouse-leave
  2051. [e *control-show? block-id doc-mode?]
  2052. (util/stop e)
  2053. (reset! *control-show? false)
  2054. (when doc-mode?
  2055. (when-let [parent (gdom/getElement block-id)]
  2056. (when-let [node (.querySelector parent ".bullet-container")]
  2057. (dom/add-class! node "hide-inner-bullet"))))
  2058. (when (and (non-dragging? e)
  2059. (not @*resizing-image?))
  2060. (state/into-selection-mode!)))
  2061. (defn- on-drag-and-mouse-attrs
  2062. [block uuid top? block-id *move-to]
  2063. {:on-drag-over (fn [event]
  2064. (block-drag-over event uuid top? block-id *move-to))
  2065. :on-drag-leave (fn [_event]
  2066. (block-drag-leave *move-to))
  2067. :on-drop (fn [event]
  2068. (block-drop event uuid block *move-to))
  2069. :on-drag-end (fn [event]
  2070. (block-drag-end event *move-to))})
  2071. (defn- build-refs-data-value
  2072. [refs]
  2073. (let [refs (model/get-page-names-by-ids
  2074. (->> (map :db/id refs)
  2075. (remove nil?)))]
  2076. (text-util/build-data-value refs)))
  2077. (defn- get-children-refs
  2078. [children]
  2079. (let [refs (atom [])]
  2080. (walk/postwalk
  2081. (fn [m]
  2082. (when (and (map? m) (:block/refs m))
  2083. (swap! refs concat (:block/refs m)))
  2084. m)
  2085. children)
  2086. (distinct @refs)))
  2087. (defn- root-block?
  2088. [config block]
  2089. (and (:block? config)
  2090. (util/collapsed? block)
  2091. (= (:id config)
  2092. (str (:block/uuid block)))))
  2093. (rum/defc ^:large-vars/cleanup-todo block-container-inner < rum/reactive db-mixins/query
  2094. [state repo config block]
  2095. (let [ref? (:ref? config)
  2096. custom-query? (boolean (:custom-query? config))
  2097. ref-or-custom-query? (or ref? custom-query?)
  2098. *navigating-block (get state ::navigating-block)
  2099. navigating-block (rum/react *navigating-block)
  2100. navigated? (and (not= (:block/uuid block) navigating-block) navigating-block)
  2101. block (if navigated?
  2102. (let [block (db/pull [:block/uuid navigating-block])
  2103. blocks (db/get-paginated-blocks repo (:db/id block)
  2104. {:scoped-block-id (:db/id block)})
  2105. tree (tree/blocks->vec-tree blocks (:block/uuid (first blocks)))]
  2106. (first tree))
  2107. block)
  2108. {:block/keys [uuid children pre-block? top? refs heading-level level type format content]} block
  2109. config (if navigated? (assoc config :id (str navigating-block)) config)
  2110. block (merge block (block/parse-title-and-body uuid format pre-block? content))
  2111. blocks-container-id (:blocks-container-id config)
  2112. config (update config :block merge block)
  2113. ;; Each block might have multiple queries, but we store only the first query's result
  2114. config (if (nil? (:query-result config))
  2115. (assoc config :query-result (atom nil))
  2116. config)
  2117. heading? (or (= type :heading) (and heading-level (<= heading-level 6)))
  2118. *control-show? (get state ::control-show?)
  2119. db-collapsed? (util/collapsed? block)
  2120. collapsed? (cond
  2121. (or ref-or-custom-query? (root-block? config block))
  2122. (state/sub-collapsed uuid)
  2123. :else
  2124. db-collapsed?)
  2125. children (if (and ref-or-custom-query?
  2126. (not collapsed?))
  2127. (map
  2128. (fn [b] (assoc b
  2129. :block/level (inc (:block/level block))))
  2130. (model/sub-block-direct-children repo uuid))
  2131. children)
  2132. breadcrumb-show? (:breadcrumb-show? config)
  2133. slide? (boolean (:slide? config))
  2134. doc-mode? (:document/mode? config)
  2135. embed? (:embed? config)
  2136. reference? (:reference? config)
  2137. block-id (str "ls-block-" blocks-container-id "-" uuid)
  2138. has-child? (first (:block/_parent (db/entity (:db/id block))))
  2139. attrs (on-drag-and-mouse-attrs block uuid top? block-id *move-to)
  2140. children-refs (get-children-refs children)
  2141. data-refs (build-refs-data-value children-refs)
  2142. data-refs-self (build-refs-data-value refs)
  2143. edit-input-id (str "edit-block-" blocks-container-id "-" uuid)
  2144. edit? (state/sub [:editor/editing? edit-input-id])
  2145. card? (string/includes? data-refs-self "\"card\"")
  2146. review-cards? (:review-cards? config)]
  2147. [:div.ls-block
  2148. (cond->
  2149. {:id block-id
  2150. :data-refs data-refs
  2151. :data-refs-self data-refs-self
  2152. :data-collapsed (and collapsed? has-child?)
  2153. :class (str uuid
  2154. (when pre-block? " pre-block")
  2155. (when (and card? (not review-cards?)) " shadow-xl")
  2156. (when (:ui/selected? block) " selected noselect"))
  2157. :blockid (str uuid)
  2158. :haschild (str has-child?)}
  2159. level
  2160. (assoc :level level)
  2161. (not slide?)
  2162. (merge attrs)
  2163. (or reference? embed?)
  2164. (assoc :data-transclude true)
  2165. embed?
  2166. (assoc :data-embed true)
  2167. custom-query?
  2168. (assoc :data-query true))
  2169. (when (and ref? breadcrumb-show?)
  2170. (breadcrumb config repo uuid {:show-page? false
  2171. :indent? true
  2172. :navigating-block *navigating-block}))
  2173. ;; only render this for the first block in each container
  2174. (when top?
  2175. (dnd-separator-wrapper block block-id slide? true false))
  2176. [:div.flex.flex-row.pr-2
  2177. {:class (if (and heading? (seq (:block/title block))) "items-baseline" "")
  2178. :on-mouse-over (fn [e]
  2179. (block-mouse-over uuid e *control-show? block-id doc-mode?))
  2180. :on-mouse-leave (fn [e]
  2181. (block-mouse-leave e *control-show? block-id doc-mode?))}
  2182. (when (not slide?)
  2183. (block-control config block uuid block-id collapsed? *control-show? edit?))
  2184. (block-content-or-editor config block edit-input-id block-id heading-level edit?)]
  2185. (block-children config children collapsed?)
  2186. (dnd-separator-wrapper block block-id slide? false false)]))
  2187. (rum/defcs block-container < rum/reactive
  2188. {:init (fn [state]
  2189. (let [[config block] (:rum/args state)
  2190. block-id (:block/uuid block)]
  2191. (cond
  2192. (root-block? config block)
  2193. (state/set-collapsed-block! block-id false)
  2194. (or (:ref? config) (:custom-query? config))
  2195. (state/set-collapsed-block! block-id
  2196. (editor-handler/block-default-collapsed? block config))
  2197. :else
  2198. nil)
  2199. (assoc state
  2200. ::control-show? (atom false)
  2201. ::navigating-block (atom (:block/uuid block)))))
  2202. :should-update (fn [old-state new-state]
  2203. (let [compare-keys [:block/uuid :block/content :block/parent :block/collapsed?
  2204. :block/properties :block/left :block/children :block/_refs :ui/selected?]
  2205. config-compare-keys [:show-cloze?]
  2206. b1 (second (:rum/args old-state))
  2207. b2 (second (:rum/args new-state))
  2208. result (or
  2209. (not= (select-keys b1 compare-keys)
  2210. (select-keys b2 compare-keys))
  2211. (not= (select-keys (first (:rum/args old-state)) config-compare-keys)
  2212. (select-keys (first (:rum/args new-state)) config-compare-keys)))]
  2213. (boolean result)))}
  2214. [state config block]
  2215. (let [repo (state/get-current-repo)
  2216. ref? (:ref? config)
  2217. custom-query? (boolean (:custom-query? config))]
  2218. (if (and ref? (not custom-query?) (not (:ref-query-child? config)))
  2219. (ui/lazy-visible
  2220. (fn []
  2221. (block-container-inner state repo config block))
  2222. nil
  2223. {})
  2224. (block-container-inner state repo config block))))
  2225. (defn divide-lists
  2226. [[f & l]]
  2227. (loop [l l
  2228. ordered? (:ordered f)
  2229. result [[f]]]
  2230. (if (seq l)
  2231. (let [cur (first l)
  2232. cur-ordered? (:ordered cur)]
  2233. (if (= ordered? cur-ordered?)
  2234. (recur
  2235. (rest l)
  2236. cur-ordered?
  2237. (update result (dec (count result)) conj cur))
  2238. (recur
  2239. (rest l)
  2240. cur-ordered?
  2241. (conj result [cur]))))
  2242. result)))
  2243. (defn list-element
  2244. [l]
  2245. (match l
  2246. [l1 & _tl]
  2247. (let [{:keys [ordered name]} l1]
  2248. (cond
  2249. (seq name)
  2250. :dl
  2251. ordered
  2252. :ol
  2253. :else
  2254. :ul))
  2255. :else
  2256. :ul))
  2257. (defn list-item
  2258. [config {:keys [name content checkbox items number] :as _list}]
  2259. (let [content (when-not (empty? content)
  2260. (match content
  2261. [["Paragraph" i] & rest]
  2262. (vec-cat
  2263. (map-inline config i)
  2264. (markup-elements-cp config rest))
  2265. :else
  2266. (markup-elements-cp config content)))
  2267. checked? (some? checkbox)
  2268. items (when (seq items)
  2269. (->elem
  2270. (list-element items)
  2271. (for [item items]
  2272. (list-item config item))))]
  2273. (cond
  2274. (seq name)
  2275. [:dl {:checked checked?}
  2276. [:dt (map-inline config name)]
  2277. (->elem :dd
  2278. (vec-cat content [items]))]
  2279. :else
  2280. (if (nil? checkbox)
  2281. (->elem
  2282. :li
  2283. (cond->
  2284. {:checked checked?}
  2285. number
  2286. (assoc :value number))
  2287. (vec-cat
  2288. [(->elem
  2289. :p
  2290. content)]
  2291. [items]))
  2292. (->elem
  2293. :li
  2294. {:checked checked?}
  2295. (vec-cat
  2296. [(->elem
  2297. :p
  2298. (list-checkbox config checkbox)
  2299. content)]
  2300. [items]))))))
  2301. (defn table
  2302. [config {:keys [header groups col_groups]}]
  2303. (let [tr (fn [elm cols]
  2304. (->elem
  2305. :tr
  2306. (mapv (fn [col]
  2307. (->elem
  2308. elm
  2309. {:scope "col"
  2310. :class "org-left"}
  2311. (map-inline config col)))
  2312. cols)))
  2313. tb-col-groups (try
  2314. (mapv (fn [number]
  2315. (let [col-elem [:col {:class "org-left"}]]
  2316. (->elem
  2317. :colgroup
  2318. (repeat number col-elem))))
  2319. col_groups)
  2320. (catch js/Error _e
  2321. []))
  2322. head (when header
  2323. [:thead (tr :th header)])
  2324. groups (mapv (fn [group]
  2325. (->elem
  2326. :tbody
  2327. (mapv #(tr :td %) group)))
  2328. groups)]
  2329. [:div.table-wrapper {:style {:max-width (min 700
  2330. (gobj/get js/window "innerWidth"))}}
  2331. (->elem
  2332. :table
  2333. {:class "table-auto"
  2334. :border 2
  2335. :cell-spacing 0
  2336. :cell-padding 6
  2337. :rules "groups"
  2338. :frame "hsides"}
  2339. (vec-cat
  2340. tb-col-groups
  2341. (cons head groups)))]))
  2342. (defn logbook-cp
  2343. [log]
  2344. (let [clocks (filter #(string/starts-with? % "CLOCK:") log)
  2345. clocks (reverse (sort-by str clocks))
  2346. ;; TODO: diplay states change log
  2347. ; states (filter #(not (string/starts-with? % "CLOCK:")) log)
  2348. ]
  2349. (when (seq clocks)
  2350. (let [tr (fn [elm cols] (->elem :tr
  2351. (mapv (fn [col] (->elem elm col)) cols)))
  2352. head [:thead.overflow-x-scroll (tr :th.py-0 ["Type" "Start" "End" "Span"])]
  2353. clock-tbody (->elem
  2354. :tbody.overflow-scroll.sm:overflow-auto
  2355. (mapv (fn [clock]
  2356. (let [cols (->> (string/split clock #": |--|=>")
  2357. (map string/trim))]
  2358. (mapv #(tr :td.py-0 %) [cols])))
  2359. clocks))]
  2360. [:div.overflow-x-scroll.sm:overflow-auto
  2361. (->elem
  2362. :table.m-0
  2363. {:class "logbook-table"
  2364. :border 0
  2365. :style {:width "max-content"}
  2366. :cell-spacing 15}
  2367. (cons head [clock-tbody]))]))))
  2368. (defn map-inline
  2369. [config col]
  2370. (map #(inline config %) col))
  2371. (declare ->hiccup)
  2372. (defn built-in-custom-query?
  2373. [title]
  2374. (let [repo (state/get-current-repo)
  2375. queries (state/sub [:config repo :default-queries :journals])]
  2376. (when (seq queries)
  2377. (boolean (some #(= % title) (map :title queries))))))
  2378. (defn- trigger-custom-query!
  2379. [state]
  2380. (let [[config query] (:rum/args state)
  2381. repo (state/get-current-repo)
  2382. result-atom (atom nil)
  2383. query-atom (if (:dsl-query? config)
  2384. (let [q (:query query)
  2385. form (safe-read-string q false)]
  2386. (cond
  2387. ;; Searches like 'foo' or 'foo bar' come back as symbols
  2388. ;; and are meant to go directly to full text search
  2389. (and (util/electron?) (symbol? form)) ; full-text search
  2390. (p/let [blocks (search/block-search repo (string/trim (str form)) {:limit 30})]
  2391. (when (seq blocks)
  2392. (let [result (db/pull-many (state/get-current-repo) '[*] (map (fn [b] [:block/uuid (uuid (:block/uuid b))]) blocks))]
  2393. (reset! result-atom result))))
  2394. (symbol? form)
  2395. (atom nil)
  2396. :else
  2397. (query-dsl/query (state/get-current-repo) q)))
  2398. (db/custom-query query))
  2399. query-atom (if (instance? Atom query-atom)
  2400. query-atom
  2401. result-atom)]
  2402. (assoc state :query-atom query-atom)))
  2403. (rum/defcs ^:large-vars/cleanup-todo custom-query* < rum/reactive
  2404. {:will-mount trigger-custom-query!
  2405. :did-mount (fn [state]
  2406. (when-let [query (last (:rum/args state))]
  2407. (state/add-custom-query-component! query (:rum/react-component state)))
  2408. state)
  2409. :did-remount (fn [_old_state state]
  2410. (trigger-custom-query! state))
  2411. :will-unmount (fn [state]
  2412. (when-let [query (last (:rum/args state))]
  2413. (state/remove-custom-query-component! query)
  2414. (db/remove-custom-query! (state/get-current-repo) query))
  2415. state)}
  2416. [state config {:keys [title query view collapsed? children? breadcrumb-show? table-view?] :as q}]
  2417. (let [dsl-query? (:dsl-query? config)
  2418. query-atom (:query-atom state)
  2419. current-block-uuid (or (:block/uuid (:block config))
  2420. (:block/uuid config))
  2421. current-block (db/entity [:block/uuid current-block-uuid])
  2422. ;; exclude the current one, otherwise it'll loop forever
  2423. remove-blocks (if current-block-uuid [current-block-uuid] nil)
  2424. query-result (and query-atom (rum/react query-atom))
  2425. table? (or table-view?
  2426. (get-in current-block [:block/properties :query-table])
  2427. (and (string? query) (string/ends-with? (string/trim query) "table")))
  2428. transformed-query-result (when query-result
  2429. (db/custom-query-result-transform query-result remove-blocks q))
  2430. not-grouped-by-page? (or table?
  2431. (boolean (:result-transform q))
  2432. (and (string? query) (string/includes? query "(by-page false)")))
  2433. result (if (and (:block/uuid (first transformed-query-result)) (not not-grouped-by-page?))
  2434. (db-utils/group-by-page transformed-query-result)
  2435. transformed-query-result)
  2436. _ (when-let [query-result (:query-result config)]
  2437. (let [result (remove (fn [b] (some? (get-in b [:block/properties :template]))) result)]
  2438. (reset! query-result result)))
  2439. view-f (and view (sci/eval-string (pr-str view)))
  2440. only-blocks? (:block/uuid (first result))
  2441. blocks-grouped-by-page? (and (seq result)
  2442. (not not-grouped-by-page?)
  2443. (coll? (first result))
  2444. (:block/name (ffirst result))
  2445. (:block/uuid (first (second (first result))))
  2446. true)
  2447. built-in? (built-in-custom-query? title)
  2448. page-list? (and (seq result)
  2449. (:block/name (first result)))
  2450. nested-query? (:custom-query? config)]
  2451. (if nested-query?
  2452. [:code (if dsl-query?
  2453. (util/format "{{query %s}}" query)
  2454. "{{query hidden}}")]
  2455. (when-not (and built-in? (empty? result))
  2456. [:div.custom-query.mt-4 (get config :attr {})
  2457. (ui/foldable
  2458. [:div.custom-query-title
  2459. [:span.title-text (cond
  2460. (vector? title) title
  2461. (string? title) (inline-text config
  2462. (get-in config [:block :block/format] :markdown)
  2463. title)
  2464. :else title)]
  2465. [:span.opacity-60.text-sm.ml-2.results-count
  2466. (str (count transformed-query-result) " results")]]
  2467. (fn []
  2468. [:div
  2469. (when (and current-block (not view-f) (nil? table-view?))
  2470. [:div.flex.flex-row.align-items.mt-2 {:on-mouse-down (fn [e] (util/stop e))}
  2471. (when-not page-list?
  2472. [:div.flex.flex-row
  2473. [:div.mx-2 [:span.text-sm "Table view"]]
  2474. [:div {:style {:margin-top 5}}
  2475. (ui/toggle table?
  2476. (fn []
  2477. (editor-handler/set-block-property! current-block-uuid
  2478. "query-table"
  2479. (not table?)))
  2480. true)]])
  2481. [:a.mx-2.block.fade-link
  2482. {:on-click (fn []
  2483. (let [all-keys (query-table/get-keys result page-list?)]
  2484. (state/pub-event! [:modal/set-query-properties current-block all-keys])))}
  2485. [:span.table-query-properties
  2486. [:span.text-sm.mr-1 "Set properties"]
  2487. svg/settings-sm]]])
  2488. (cond
  2489. (and (seq result) view-f)
  2490. (let [result (try
  2491. (sci/call-fn view-f result)
  2492. (catch js/Error error
  2493. (log/error :custom-view-failed {:error error
  2494. :result result})
  2495. [:div "Custom view failed: "
  2496. (str error)]))]
  2497. (util/hiccup-keywordize result))
  2498. page-list?
  2499. (query-table/result-table config current-block result {:page? true} map-inline page-cp ->elem inline-text)
  2500. table?
  2501. (query-table/result-table config current-block result {:page? false} map-inline page-cp ->elem inline-text)
  2502. (and (seq result) (or only-blocks? blocks-grouped-by-page?))
  2503. (->hiccup result (cond-> (assoc config
  2504. :custom-query? true
  2505. :breadcrumb-show? (if (some? breadcrumb-show?)
  2506. breadcrumb-show?
  2507. true)
  2508. :group-by-page? blocks-grouped-by-page?
  2509. :ref? true)
  2510. children?
  2511. (assoc :ref? true))
  2512. {:style {:margin-top "0.25rem"
  2513. :margin-left "0.25rem"}})
  2514. (seq result)
  2515. (let [result (->>
  2516. (for [record result]
  2517. (if (map? record)
  2518. (str (util/pp-str record) "\n")
  2519. record))
  2520. (remove nil?))]
  2521. [:pre result])
  2522. :else
  2523. [:div.text-sm.mt-2.ml-2.font-medium.opacity-50 "Empty"])])
  2524. {:default-collapsed? collapsed?
  2525. :title-trigger? true})]))))
  2526. (rum/defc custom-query
  2527. [config q]
  2528. (ui/catch-error
  2529. (ui/block-error "Query Error:" {:content (:query q)})
  2530. (ui/lazy-visible
  2531. (fn [] (custom-query* config q))
  2532. nil
  2533. {})))
  2534. (defn admonition
  2535. [config type result]
  2536. (when-let [icon (case (string/lower-case (name type))
  2537. "note" svg/note
  2538. "tip" svg/tip
  2539. "important" svg/important
  2540. "caution" svg/caution
  2541. "warning" svg/warning
  2542. "pinned" svg/pinned
  2543. nil)]
  2544. [:div.flex.flex-row.admonitionblock.align-items {:class type}
  2545. [:div.pr-4.admonition-icon.flex.flex-col.justify-center
  2546. {:title (string/upper-case type)} (icon)]
  2547. [:div.ml-4.text-lg
  2548. (markup-elements-cp config result)]]))
  2549. ;; TODO: move to mldoc
  2550. ;; (defn- convert-md-src-to-custom-block
  2551. ;; [item]
  2552. ;; (let [{:keys [language options lines] :as options} (second item)
  2553. ;; lang (string/lower-case (or language ""))]
  2554. ;; (cond
  2555. ;; (= lang "quote")
  2556. ;; (let [content (string/trim (string/join "\n" lines))]
  2557. ;; ["Quote" (first (mldoc/->edn content (gp-mldoc/default-config :markdown)))])
  2558. ;; (contains? #{"query" "note" "tip" "important" "caution" "warning" "pinned"} lang)
  2559. ;; (let [content (string/trim (string/join "\n" lines))]
  2560. ;; ["Custom" lang nil (first (mldoc/->edn content (gp-mldoc/default-config :markdown))) content])
  2561. ;; :else
  2562. ;; ["Src" options])))
  2563. (rum/defc src-cp < rum/static
  2564. [config options html-export?]
  2565. (when options
  2566. (let [{:keys [lines language]} options
  2567. attr (when language
  2568. {:data-lang language})
  2569. code (apply str lines)]
  2570. (cond
  2571. html-export?
  2572. (highlight/html-export attr code)
  2573. :else
  2574. (let [language (if (contains? #{"edn" "clj" "cljc" "cljs"} language) "clojure" language)]
  2575. (if (:slide? config)
  2576. (highlight/highlight (str (random-uuid))
  2577. {:class (str "language-" language)
  2578. :data-lang language}
  2579. code)
  2580. [:div
  2581. (lazy-editor/editor config (str (d/squuid)) attr code options)
  2582. (let [options (:options options)]
  2583. (when (and (= language "clojure") (contains? (set options) ":results"))
  2584. (sci/eval-result code)))]))))))
  2585. (defn ^:large-vars/cleanup-todo markup-element-cp
  2586. [{:keys [html-export?] :as config} item]
  2587. (let [format (or (:block/format config)
  2588. :markdown)]
  2589. (try
  2590. (match item
  2591. ["Drawer" name lines]
  2592. (when (or (not= name "logbook")
  2593. (and
  2594. (= name "logbook")
  2595. (state/enable-timetracking?)
  2596. (or (get-in (state/get-config) [:logbook/settings :enabled-in-all-blocks])
  2597. (when (get-in (state/get-config)
  2598. [:logbook/settings :enabled-in-timestamped-blocks] true)
  2599. (or (:block/scheduled (:block config))
  2600. (:block/deadline (:block config)))))))
  2601. [:div
  2602. [:div.text-sm
  2603. [:div.drawer {:data-drawer-name name}
  2604. (ui/foldable
  2605. [:div.opacity-50.font-medium.logbook
  2606. (util/format ":%s:" (string/upper-case name))]
  2607. [:div.opacity-50.font-medium
  2608. (if (= name "logbook")
  2609. (logbook-cp lines)
  2610. (apply str lines))
  2611. [:div ":END:"]]
  2612. {:default-collapsed? true
  2613. :title-trigger? true})]]])
  2614. ["Properties" m]
  2615. [:div.properties
  2616. (for [[k v] (dissoc m :roam_alias :roam_tags)]
  2617. (when (and (not (and (= k :macros) (empty? v))) ; empty macros
  2618. (not (= k :title))
  2619. (not (= k :filters)))
  2620. [:div.property
  2621. [:span.font-medium.mr-1 (str (name k) ": ")]
  2622. (if (coll? v)
  2623. (let [vals (for [item v]
  2624. (if (coll? v)
  2625. (let [config (when (= k :alias)
  2626. (assoc config :block/alias? true))]
  2627. (page-cp config {:block/name item}))
  2628. (inline-text format item)))]
  2629. (interpose [:span ", "] vals))
  2630. (inline-text format v))]))]
  2631. ;; for file-level property in orgmode: #+key: value
  2632. ;; only display caption. https://orgmode.org/manual/Captions.html.
  2633. ["Directive" key value]
  2634. [:div.file-level-property
  2635. (when (contains? #{"caption"} (string/lower-case key))
  2636. [:span.font-medium
  2637. [:span.font-bold (string/upper-case key)]
  2638. (str ": " value)])]
  2639. ["Paragraph" l]
  2640. ;; TODO: speedup
  2641. (if (util/safe-re-find #"\"Export_Snippet\" \"embed\"" (str l))
  2642. (->elem :div (map-inline config l))
  2643. (->elem :div.is-paragraph (map-inline config l)))
  2644. ["Horizontal_Rule"]
  2645. (when-not (:slide? config)
  2646. [:hr])
  2647. ["Heading" h]
  2648. (block-container config h)
  2649. ["List" l]
  2650. (let [lists (divide-lists l)]
  2651. (if (= 1 (count lists))
  2652. (let [l (first lists)]
  2653. (->elem
  2654. (list-element l)
  2655. (map #(list-item config %) l)))
  2656. [:div.list-group
  2657. (for [l lists]
  2658. (->elem
  2659. (list-element l)
  2660. (map #(list-item config %) l)))]))
  2661. ["Table" t]
  2662. (table config t)
  2663. ["Math" s]
  2664. (if html-export?
  2665. (latex/html-export s true true)
  2666. (latex/latex (str (d/squuid)) s true true))
  2667. ["Example" l]
  2668. [:pre.pre-wrap-white-space
  2669. (join-lines l)]
  2670. ["Quote" l]
  2671. (->elem
  2672. :blockquote
  2673. (markup-elements-cp config l))
  2674. ["Raw_Html" content]
  2675. (when (not html-export?)
  2676. [:div.raw_html {:dangerouslySetInnerHTML
  2677. {:__html content}}])
  2678. ["Export" "html" _options content]
  2679. (when (not html-export?)
  2680. [:div.export_html {:dangerouslySetInnerHTML
  2681. {:__html content}}])
  2682. ["Hiccup" content]
  2683. (ui/catch-error
  2684. [:div.warning {:title "Invalid hiccup"}
  2685. content]
  2686. (-> (safe-read-string content)
  2687. (security/remove-javascript-links-in-href)))
  2688. ["Export" "latex" _options content]
  2689. (if html-export?
  2690. (latex/html-export content true false)
  2691. (latex/latex (str (d/squuid)) content true false))
  2692. ["Custom" "query" _options _result content]
  2693. (try
  2694. (let [query (reader/read-string content)]
  2695. (custom-query config query))
  2696. (catch :default e
  2697. (log/error :read-string-error e)
  2698. (ui/block-error "Invalid query:" {:content content})))
  2699. ["Custom" "note" _options result _content]
  2700. (admonition config "note" result)
  2701. ["Custom" "tip" _options result _content]
  2702. (admonition config "tip" result)
  2703. ["Custom" "important" _options result _content]
  2704. (admonition config "important" result)
  2705. ["Custom" "caution" _options result _content]
  2706. (admonition config "caution" result)
  2707. ["Custom" "warning" _options result _content]
  2708. (admonition config "warning" result)
  2709. ["Custom" "pinned" _options result _content]
  2710. (admonition config "pinned" result)
  2711. ["Custom" "center" _options l _content]
  2712. (->elem
  2713. :div.text-center
  2714. (markup-elements-cp config l))
  2715. ["Custom" name _options l _content]
  2716. (->elem
  2717. :div
  2718. {:class name}
  2719. (markup-elements-cp config l))
  2720. ["Latex_Fragment" l]
  2721. [:p.latex-fragment
  2722. (inline config ["Latex_Fragment" l])]
  2723. ["Latex_Environment" name option content]
  2724. (let [content (latex-environment-content name option content)]
  2725. (if html-export?
  2726. (latex/html-export content true true)
  2727. (latex/latex (str (d/squuid)) content true true)))
  2728. ["Displayed_Math" content]
  2729. (if html-export?
  2730. (latex/html-export content true true)
  2731. (latex/latex (str (d/squuid)) content true true))
  2732. ["Footnote_Definition" name definition]
  2733. (let [id (util/url-encode name)]
  2734. [:div.footdef
  2735. [:div.footpara
  2736. (conj
  2737. (markup-element-cp config ["Paragraph" definition])
  2738. [:a.ml-1 {:id (str "fn." id)
  2739. :style {:font-size 14}
  2740. :class "footnum"
  2741. :on-click #(route-handler/jump-to-anchor! (str "fnr." id))}
  2742. [:sup.fn (str name "↩︎")]])]])
  2743. ["Src" options]
  2744. [:div.cp__fenced-code-block
  2745. (if-let [opts (plugin-handler/hook-fenced-code-by-type (util/safe-lower-case (:language options)))]
  2746. (plugins/hook-ui-fenced-code (string/join "" (:lines options)) opts)
  2747. (src-cp config options html-export?))]
  2748. :else
  2749. "")
  2750. (catch js/Error e
  2751. (println "Convert to html failed, error: " e)
  2752. ""))))
  2753. (defn markup-elements-cp
  2754. [config col]
  2755. (map #(markup-element-cp config %) col))
  2756. (defn- block-item
  2757. [config blocks idx item]
  2758. (let [item (->
  2759. (dissoc item :block/meta)
  2760. (assoc :block/top? (zero? idx)
  2761. :block/bottom? (= (count blocks) (inc idx))))
  2762. config (assoc config :block/uuid (:block/uuid item))]
  2763. (rum/with-key (block-container config item)
  2764. (str (:block/uuid item)))))
  2765. (defn- block-list
  2766. [config blocks]
  2767. (for [[idx item] (medley/indexed blocks)]
  2768. (block-item config blocks idx item)))
  2769. (defn- custom-query-or-ref?
  2770. [config]
  2771. (let [ref? (:ref? config)
  2772. custom-query? (:custom-query? config)]
  2773. (or custom-query? ref?)))
  2774. (defn- load-more-blocks!
  2775. [config flat-blocks]
  2776. (when-let [db-id (:db/id config)]
  2777. (let [last-block-id (:db/id (last flat-blocks))]
  2778. (block-handler/load-more! db-id last-block-id))))
  2779. (rum/defcs lazy-blocks < rum/reactive
  2780. {:init (fn [state]
  2781. (assoc state ::id (str (random-uuid))))}
  2782. [state config flat-blocks blocks->vec-tree]
  2783. (let [db-id (:db/id config)
  2784. selected-blocks (set (state/get-selection-block-ids))
  2785. flat-blocks (if (seq selected-blocks)
  2786. (map (fn [b]
  2787. (assoc b :ui/selected? (contains? selected-blocks (:block/uuid b)))) flat-blocks)
  2788. flat-blocks)
  2789. blocks (blocks->vec-tree flat-blocks)]
  2790. (if-not db-id
  2791. (block-list config blocks)
  2792. (let [bottom-reached (fn []
  2793. ;; To prevent scrolling after inserting new blocks
  2794. (when (> (- (util/time-ms) (:start-time config)) 100)
  2795. (load-more-blocks! config flat-blocks)))
  2796. has-more? (and
  2797. (>= (count flat-blocks) model/initial-blocks-length)
  2798. (some? (model/get-next-open-block (db/get-db) (last flat-blocks) db-id)))
  2799. dom-id (str "lazy-blocks-" (::id state))]
  2800. [:div {:id dom-id}
  2801. (ui/infinite-list
  2802. "main-content-container"
  2803. (block-list config blocks)
  2804. {:on-load bottom-reached
  2805. :bottom-reached (fn []
  2806. (when-let [node (gdom/getElement dom-id)]
  2807. (ui/bottom-reached? node 1000)))
  2808. :has-more has-more?
  2809. :more (if (or (:preview? config) (:sidebar? config))
  2810. "More"
  2811. (ui/loading "Loading"))})]))))
  2812. (rum/defcs blocks-container <
  2813. {:init (fn [state]
  2814. (assoc state
  2815. ::init-blocks-container-id (atom nil)))}
  2816. [state blocks config]
  2817. (let [*init-blocks-container-id (::init-blocks-container-id state)
  2818. blocks-container-id (if @*init-blocks-container-id
  2819. @*init-blocks-container-id
  2820. (let [id' (swap! *blocks-container-id inc)]
  2821. (reset! *init-blocks-container-id id')
  2822. id'))
  2823. config (assoc config :blocks-container-id blocks-container-id)
  2824. doc-mode? (:document/mode? config)]
  2825. (when (seq blocks)
  2826. (let [blocks->vec-tree #(if (custom-query-or-ref? config) % (tree/blocks->vec-tree % (:id config)))
  2827. flat-blocks (vec blocks)
  2828. config (assoc config :start-time (util/time-ms))]
  2829. [:div.blocks-container.flex-1
  2830. {:class (when doc-mode? "document-mode")}
  2831. (lazy-blocks config flat-blocks blocks->vec-tree)]))))
  2832. (rum/defcs breadcrumb-with-container < rum/reactive
  2833. {:init (fn [state]
  2834. (let [first-block (ffirst (:rum/args state))]
  2835. (assoc state
  2836. ::initial-block first-block
  2837. ::navigating-block (atom (:block/uuid first-block)))))}
  2838. [state blocks config]
  2839. (let [repo (state/get-current-repo)
  2840. *navigating-block (::navigating-block state)
  2841. navigating-block (rum/react *navigating-block)
  2842. navigating-block-entity (db/entity [:block/uuid navigating-block])
  2843. navigated? (and
  2844. navigating-block
  2845. (not= (:db/id (:block/parent (::initial-block state)))
  2846. (:db/id (:block/parent navigating-block-entity))))
  2847. blocks (if navigated?
  2848. (let [block navigating-block-entity]
  2849. (db/get-paginated-blocks repo (:db/id block)
  2850. {:scoped-block-id (:db/id block)}))
  2851. blocks)]
  2852. [:div
  2853. (when (:breadcrumb-show? config)
  2854. (breadcrumb config (state/get-current-repo) navigating-block
  2855. {:show-page? false
  2856. :navigating-block *navigating-block}))
  2857. (blocks-container blocks (assoc config
  2858. :breadcrumb-show? false
  2859. :navigating-block *navigating-block))]))
  2860. ;; headers to hiccup
  2861. (defn ->hiccup
  2862. [blocks config option]
  2863. [:div.content
  2864. (cond-> option
  2865. (:document/mode? config) (assoc :class "doc-mode"))
  2866. (cond
  2867. (and (:custom-query? config)
  2868. (:group-by-page? config))
  2869. [:div.flex.flex-col
  2870. (let [blocks (sort-by (comp :block/journal-day first) > blocks)]
  2871. (for [[page blocks] blocks]
  2872. (let [alias? (:block/alias? page)
  2873. page (db/entity (:db/id page))
  2874. parent-blocks (group-by :block/parent blocks)]
  2875. [:div.my-2 (cond-> {:key (str "page-" (:db/id page))}
  2876. (:ref? config)
  2877. (assoc :class "color-level px-2 sm:px-7 py-2 rounded"))
  2878. (ui/foldable
  2879. [:div
  2880. (page-cp config page)
  2881. (when alias? [:span.text-sm.font-medium.opacity-50 " Alias"])]
  2882. (for [[_parent blocks] parent-blocks]
  2883. (breadcrumb-with-container blocks config))
  2884. {})])))]
  2885. (and (:group-by-page? config)
  2886. (vector? (first blocks)))
  2887. [:div.flex.flex-col
  2888. (let [blocks (sort-by (comp :block/journal-day first) > blocks)]
  2889. (for [[page blocks] blocks]
  2890. (let [alias? (:block/alias? page)
  2891. page (db/entity (:db/id page))]
  2892. [:div.my-2 (cond-> {:key (str "page-" (:db/id page))}
  2893. (:ref? config)
  2894. (assoc :class "color-level px-2 sm:px-7 py-2 rounded"))
  2895. (ui/foldable
  2896. [:div
  2897. (page-cp config page)
  2898. (when alias? [:span.text-sm.font-medium.opacity-50 " Alias"])]
  2899. (blocks-container blocks config)
  2900. {})])))]
  2901. :else
  2902. (blocks-container blocks config))])