block.cljs 184 KB

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