block.cljs 104 KB

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