block.cljs 198 KB

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