block.cljs 189 KB

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