block.cljs 119 KB

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