block.cljs 146 KB

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