block.cljs 143 KB

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