content.cljs 20 KB

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