block.cljs 135 KB

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