content.cljs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  1. (ns frontend.components.content
  2. (:require [cljs.pprint :as pprint]
  3. [clojure.string :as string]
  4. [dommy.core :as d]
  5. [frontend.commands :as commands]
  6. [frontend.components.editor :as editor]
  7. [frontend.components.page-menu :as page-menu]
  8. [frontend.components.export :as export]
  9. [frontend.config :as config]
  10. [frontend.context.i18n :refer [t]]
  11. [frontend.db :as db]
  12. [frontend.extensions.srs :as srs]
  13. [frontend.format :as format]
  14. [frontend.format.protocol :as protocol]
  15. [frontend.handler.common :as common-handler]
  16. [frontend.handler.editor :as editor-handler]
  17. [frontend.handler.image :as image-handler]
  18. [frontend.handler.notification :as notification]
  19. [frontend.handler.page :as page-handler]
  20. [frontend.handler.whiteboard :as whiteboard-handler]
  21. [frontend.mixins :as mixins]
  22. [frontend.state :as state]
  23. [frontend.ui :as ui]
  24. [frontend.util :as util]
  25. [logseq.graph-parser.util :as gp-util]
  26. [logseq.graph-parser.util.block-ref :as block-ref]
  27. [frontend.util.url :as url-util]
  28. [goog.dom :as gdom]
  29. [goog.object :as gobj]
  30. [rum.core :as rum]))
  31. ;; TODO i18n support
  32. (defn- set-format-js-loading!
  33. [format value]
  34. (when format
  35. (swap! state/state assoc-in [:format/loading format] value)))
  36. (defn- lazy-load
  37. [format]
  38. (let [format (gp-util/normalize-format format)]
  39. (when-let [record (format/get-format-record format)]
  40. (when-not (protocol/loaded? record)
  41. (set-format-js-loading! format true)
  42. (protocol/lazyLoad record
  43. (fn [_result]
  44. (set-format-js-loading! format false)))))))
  45. (defn lazy-load-js
  46. [state]
  47. (when-let [format (:format (last (:rum/args state)))]
  48. (let [loader? (contains? config/html-render-formats format)]
  49. (when loader?
  50. (when-not (format/loaded? format)
  51. (lazy-load format))))))
  52. (rum/defc custom-context-menu-content
  53. []
  54. [:.menu-links-wrapper
  55. (ui/menu-link
  56. {:key "cut"
  57. :on-click #(editor-handler/cut-selection-blocks true)}
  58. "Cut"
  59. nil)
  60. (ui/menu-link
  61. {:key "copy"
  62. :on-click editor-handler/copy-selection-blocks}
  63. "Copy"
  64. nil)
  65. (ui/menu-link
  66. {:key "copy as"
  67. :on-click (fn [_]
  68. (let [block-uuids (editor-handler/get-selected-toplevel-block-uuids)]
  69. (state/set-modal!
  70. #(export/export-blocks block-uuids))))}
  71. "Copy as..."
  72. nil)
  73. (ui/menu-link
  74. {:key "copy block refs"
  75. :on-click editor-handler/copy-block-refs}
  76. "Copy block refs"
  77. nil)
  78. (ui/menu-link
  79. {:key "copy block embeds"
  80. :on-click editor-handler/copy-block-embeds}
  81. "Copy block embeds"
  82. nil)
  83. [:hr.menu-separator]
  84. (ui/menu-link
  85. {:key "cycle todos"
  86. :on-click editor-handler/cycle-todos!}
  87. "Cycle todos"
  88. nil)])
  89. (defonce *template-including-parent? (atom nil))
  90. (rum/defc template-checkbox
  91. [template-including-parent?]
  92. [:div.flex.flex-row.w-auto.items-center
  93. [:p.text-medium.mr-2 "Including the parent block in the template?"]
  94. (ui/toggle template-including-parent?
  95. #(swap! *template-including-parent? not))])
  96. (rum/defcs block-template < rum/reactive
  97. (rum/local false ::edit?)
  98. (rum/local "" ::input)
  99. {:will-unmount (fn [state]
  100. (reset! *template-including-parent? nil)
  101. state)}
  102. [state block-id]
  103. (let [edit? (get state ::edit?)
  104. input (get state ::input)
  105. template-including-parent? (rum/react *template-including-parent?)
  106. block-id (if (string? block-id) (uuid block-id) block-id)
  107. block (db/entity [:block/uuid block-id])
  108. has-children? (seq (:block/_parent block))]
  109. (when (and (nil? template-including-parent?) has-children?)
  110. (reset! *template-including-parent? true))
  111. (if @edit?
  112. (do
  113. (state/clear-edit!)
  114. [:<>
  115. [:div.px-4.py-2.text-sm {:on-click (fn [e] (util/stop e))}
  116. [:p "What's the template's name?"]
  117. [:input#new-template.form-input.block.w-full.sm:text-sm.sm:leading-5.my-2
  118. {:auto-focus true
  119. :on-change (fn [e]
  120. (reset! input (util/evalue e)))}]
  121. (when has-children?
  122. (template-checkbox template-including-parent?))
  123. (ui/button "Submit"
  124. :on-click (fn []
  125. (let [title (string/trim @input)]
  126. (when (not (string/blank? title))
  127. (if (page-handler/template-exists? title)
  128. (notification/show!
  129. [:p "Template already exists!"]
  130. :error)
  131. (do
  132. (editor-handler/set-block-property! block-id :template title)
  133. (when (false? template-including-parent?)
  134. (editor-handler/set-block-property! block-id :template-including-parent false))
  135. (state/hide-custom-context-menu!)))))))]
  136. [:hr.menu-separator]])
  137. (ui/menu-link
  138. {:key "Make a Template"
  139. :on-click (fn [e]
  140. (util/stop e)
  141. (reset! edit? true))}
  142. "Make a Template"
  143. nil))))
  144. (rum/defc ^:large-vars/cleanup-todo block-context-menu-content
  145. [_target block-id]
  146. (when-let [block (db/entity [:block/uuid block-id])]
  147. (let [format (:block/format block)]
  148. [:.menu-links-wrapper
  149. [:div.flex.flex-row.justify-between.py-1.px-2.items-center
  150. [:div.flex.flex-row.justify-between.flex-1.mx-2.mt-2
  151. (for [color ui/block-background-colors]
  152. [:a.shadow-sm
  153. {:title (t (keyword "color" color))
  154. :on-click (fn [_e]
  155. (editor-handler/set-block-property! block-id "background-color" color))}
  156. [:div.heading-bg {:style {:background-color (str "var(--color-" color "-500)")}}]])
  157. [:a.shadow-sm
  158. {:title (t :remove-background)
  159. :on-click (fn [_e]
  160. (editor-handler/remove-block-property! block-id "background-color"))}
  161. [:div.heading-bg.remove "-"]]]]
  162. [:div.flex.flex-row.justify-between.pb-2.pt-1.px-2.items-center
  163. [:div.flex.flex-row.justify-between.flex-1.px-1
  164. (for [i (range 1 7)]
  165. (ui/button
  166. ""
  167. :icon (str "h-" i)
  168. :title (t :heading i)
  169. :class "to-heading-button"
  170. :on-click (fn [_e]
  171. (editor-handler/set-heading! block-id format i))
  172. :intent "link"
  173. :small? true))
  174. (ui/button
  175. ""
  176. :icon "h-auto"
  177. :icon-props {:extension? true}
  178. :class "to-heading-button"
  179. :title (if (= format :markdown)
  180. (str (t :auto-heading) " - " (t :not-available-in-mode format))
  181. (t :auto-heading))
  182. :disabled (= format :markdown)
  183. :on-click (fn [_e]
  184. (editor-handler/set-heading! block-id format true))
  185. :intent "link"
  186. :small? true)
  187. (ui/button
  188. ""
  189. :icon "heading-off"
  190. :icon-props {:extension? true}
  191. :class "to-heading-button"
  192. :title (t :remove-heading)
  193. :on-click (fn [_e]
  194. (editor-handler/remove-heading! block-id format))
  195. :intent "link"
  196. :small? true)]]
  197. [:hr.menu-separator]
  198. (ui/menu-link
  199. {:key "Open in sidebar"
  200. :on-click (fn [_e]
  201. (editor-handler/open-block-in-sidebar! block-id))}
  202. "Open in sidebar"
  203. ["⇧" "click"])
  204. [:hr.menu-separator]
  205. (ui/menu-link
  206. {:key "Copy block ref"
  207. :on-click (fn [_e]
  208. (editor-handler/copy-block-ref! block-id block-ref/->block-ref))}
  209. "Copy block ref"
  210. nil)
  211. (ui/menu-link
  212. {:key "Copy block embed"
  213. :on-click (fn [_e]
  214. (editor-handler/copy-block-ref! block-id #(util/format "{{embed ((%s))}}" %)))}
  215. "Copy block embed"
  216. nil)
  217. ;; TODO Logseq protocol mobile support
  218. (when (util/electron?)
  219. (ui/menu-link
  220. {:key "Copy block URL"
  221. :on-click (fn [_e]
  222. (let [current-repo (state/get-current-repo)
  223. tap-f (fn [block-id]
  224. (url-util/get-logseq-graph-uuid-url nil current-repo block-id))]
  225. (editor-handler/copy-block-ref! block-id tap-f)))}
  226. "Copy block URL"
  227. nil))
  228. (ui/menu-link
  229. {:key "Copy as"
  230. :on-click (fn [_]
  231. (state/set-modal! #(export/export-blocks [block-id])))}
  232. "Copy as..."
  233. nil)
  234. (ui/menu-link
  235. {:key "Cut"
  236. :on-click (fn [_e]
  237. (editor-handler/cut-block! block-id))}
  238. "Cut"
  239. nil)
  240. [:hr.menu-separator]
  241. (block-template block-id)
  242. (cond
  243. (srs/card-block? block)
  244. (ui/menu-link
  245. {:key "Preview Card"
  246. :on-click #(srs/preview (:db/id block))}
  247. "Preview Card"
  248. nil)
  249. (state/enable-flashcards?)
  250. (ui/menu-link
  251. {:key "Make a Card"
  252. :on-click #(srs/make-block-a-card! block-id)}
  253. "Make a Flashcard"
  254. nil)
  255. :else
  256. nil)
  257. [:hr.menu-separator]
  258. (ui/menu-link
  259. {:key "Expand all"
  260. :on-click (fn [_e]
  261. (editor-handler/expand-all! block-id))}
  262. "Expand all"
  263. nil)
  264. (ui/menu-link
  265. {:key "Collapse all"
  266. :on-click (fn [_e]
  267. (editor-handler/collapse-all! block-id {}))}
  268. "Collapse all"
  269. nil)
  270. (when (state/sub [:plugin/simple-commands])
  271. (when-let [cmds (state/get-plugins-commands-with-type :block-context-menu-item)]
  272. (for [[_ {:keys [key label] :as cmd} action pid] cmds]
  273. (ui/menu-link
  274. {:key key
  275. :on-click #(commands/exec-plugin-simple-command!
  276. pid (assoc cmd :uuid block-id) action)}
  277. label
  278. nil))))
  279. (when (state/sub [:ui/developer-mode?])
  280. (ui/menu-link
  281. {:key "(Dev) Show block data"
  282. :on-click (fn []
  283. (let [block-data (with-out-str (pprint/pprint (db/pull [:block/uuid block-id])))]
  284. (println block-data)
  285. (notification/show!
  286. [:div
  287. [:pre.code block-data]
  288. [:br]
  289. (ui/button "Copy to clipboard"
  290. :on-click #(.writeText js/navigator.clipboard block-data))]
  291. :success
  292. false)))}
  293. "(Dev) Show block data"
  294. nil))])))
  295. (rum/defc block-ref-custom-context-menu-content
  296. [block block-ref-id]
  297. (when (and block block-ref-id)
  298. [:.menu-links-wrapper
  299. (ui/menu-link
  300. {:key "open-in-sidebar"
  301. :on-click (fn []
  302. (state/sidebar-add-block!
  303. (state/get-current-repo)
  304. block-ref-id
  305. :block-ref))}
  306. "Open in sidebar"
  307. ["⇧" "click"])
  308. (ui/menu-link
  309. {:key "copy"
  310. :on-click (fn [] (editor-handler/copy-current-ref block-ref-id))}
  311. "Copy this reference"
  312. nil)
  313. (ui/menu-link
  314. {:key "delete"
  315. :on-click (fn [] (editor-handler/delete-current-ref! block block-ref-id))}
  316. "Delete this reference"
  317. nil)
  318. (ui/menu-link
  319. {:key "replace-with-text"
  320. :on-click (fn [] (editor-handler/replace-ref-with-text! block block-ref-id))}
  321. "Replace with text"
  322. nil)
  323. (ui/menu-link
  324. {:key "replace-with-embed"
  325. :on-click (fn [] (editor-handler/replace-ref-with-embed! block block-ref-id))}
  326. "Replace with embed"
  327. nil)]))
  328. (rum/defc page-title-custom-context-menu-content
  329. [page]
  330. (when-not (string/blank? page)
  331. (let [page-menu-options (page-menu/page-menu page)]
  332. [:.menu-links-wrapper
  333. (for [{:keys [title options]} page-menu-options]
  334. (ui/menu-link
  335. (merge
  336. {:key title}
  337. options)
  338. title
  339. nil))])))
  340. ;; TODO: content could be changed
  341. ;; Also, keyboard bindings should only be activated after
  342. ;; blocks were already selected.
  343. (rum/defc hiccup-content < rum/static
  344. (mixins/event-mixin
  345. (fn [state]
  346. (mixins/listen state js/window "contextmenu"
  347. (fn [e]
  348. (let [target (gobj/get e "target")
  349. block-id (d/attr target "blockid")
  350. {:keys [block block-ref]} (state/sub :block-ref/context)
  351. {:keys [page]} (state/sub :page-title/context)]
  352. ;; TODO: Find a better way to handle this on whiteboards
  353. (when-not (whiteboard-handler/inside-portal? target)
  354. (cond
  355. page
  356. (do
  357. (common-handler/show-custom-context-menu!
  358. e
  359. (page-title-custom-context-menu-content page))
  360. (state/set-state! :page-title/context nil))
  361. block-ref
  362. (do
  363. (common-handler/show-custom-context-menu!
  364. e
  365. (block-ref-custom-context-menu-content block block-ref))
  366. (state/set-state! :block-ref/context nil))
  367. (and (state/selection?) (not (d/has-class? target "bullet")))
  368. (common-handler/show-custom-context-menu!
  369. e
  370. (custom-context-menu-content))
  371. (and block-id (parse-uuid block-id))
  372. (let [block (.closest target ".ls-block")]
  373. (when block
  374. (util/select-highlight! [block]))
  375. (common-handler/show-custom-context-menu!
  376. e
  377. (block-context-menu-content target (uuid block-id))))
  378. :else
  379. nil)))))))
  380. [id {:keys [hiccup]}]
  381. [:div {:id id}
  382. (if hiccup
  383. hiccup
  384. [:div.cursor "Click to edit"])])
  385. (rum/defc non-hiccup-content < rum/reactive
  386. [id content on-click on-hide config format]
  387. (let [edit? (state/sub [:editor/editing? id])
  388. loading (state/sub :format/loading)]
  389. (if edit?
  390. (editor/box {:on-hide on-hide
  391. :format format}
  392. id
  393. config)
  394. (let [format (gp-util/normalize-format format)
  395. loading? (get loading format)
  396. markup? (contains? config/html-render-formats format)
  397. on-click (fn [e]
  398. (when-not (util/link? (gobj/get e "target"))
  399. (util/stop e)
  400. (editor-handler/reset-cursor-range! (gdom/getElement (str id)))
  401. (state/set-edit-content! id content)
  402. (state/set-edit-input-id! id)
  403. (when on-click
  404. (on-click e))))]
  405. (cond
  406. (and markup? loading?)
  407. [:div "loading ..."]
  408. :else ; other text formats
  409. [:pre.cursor.content.pre-white-space
  410. {:id id
  411. :on-click on-click}
  412. (if (string/blank? content)
  413. [:div.cursor "Click to edit"]
  414. content)])))))
  415. (defn- set-draw-iframe-style!
  416. []
  417. (let [width (gobj/get js/window "innerWidth")]
  418. (when (>= width 1024)
  419. (let [draws (d/by-class "draw-iframe")
  420. width (- width 200)]
  421. (doseq [draw draws]
  422. (d/set-style! draw :width (str width "px"))
  423. (let [height (max 700 (/ width 2))]
  424. (d/set-style! draw :height (str height "px")))
  425. (d/set-style! draw :margin-left (str (- (/ (- width 570) 2)) "px")))))))
  426. (rum/defcs content < rum/reactive
  427. {:will-mount (fn [state]
  428. (lazy-load-js state)
  429. state)
  430. :did-mount (fn [state]
  431. (set-draw-iframe-style!)
  432. (image-handler/render-local-images!)
  433. state)
  434. :did-update (fn [state]
  435. (set-draw-iframe-style!)
  436. (lazy-load-js state)
  437. (image-handler/render-local-images!)
  438. state)}
  439. [state id {:keys [format
  440. config
  441. hiccup
  442. content
  443. on-click
  444. on-hide]
  445. :as option}]
  446. (if hiccup
  447. [:div
  448. (hiccup-content id option)]
  449. (let [format (gp-util/normalize-format format)]
  450. (non-hiccup-content id content on-click on-hide config format))))