block.cljs 187 KB

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