block.cljs 142 KB

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