block.cljs 195 KB

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