block.cljs 178 KB

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