block.cljs 199 KB

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