block.cljs 143 KB

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