block.cljs 146 KB

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