block.cljs 130 KB

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