block.cljs 200 KB

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