block.cljs 197 KB

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