block.cljs 142 KB

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