page_menu.cljs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. (ns frontend.components.page-menu
  2. (:require [cljs.pprint :as pprint]
  3. [frontend.commands :as commands]
  4. [frontend.components.export :as export]
  5. [frontend.context.i18n :refer [t]]
  6. [frontend.db :as db]
  7. [frontend.handler.notification :as notification]
  8. [frontend.handler.page :as page-handler]
  9. [frontend.handler.route :as route-handler]
  10. [frontend.state :as state]
  11. [frontend.ui :as ui]
  12. [frontend.util :as util]
  13. [frontend.handler.shell :as shell]
  14. [frontend.handler.plugin :as plugin-handler]
  15. [frontend.mobile.util :as mobile-util]))
  16. (defn- delete-page!
  17. [page-name]
  18. (page-handler/delete! page-name
  19. (fn []
  20. (notification/show! (str "Page " page-name " was deleted successfully!")
  21. :success)))
  22. (state/close-modal!)
  23. (route-handler/redirect-to-home!))
  24. (defn delete-page-dialog
  25. [page-name]
  26. (fn [close-fn]
  27. [:div
  28. [:div.sm:flex.items-center
  29. [:div.mx-auto.flex-shrink-0.flex.items-center.justify-center.h-12.w-12.rounded-full.bg-red-100.sm:mx-0.sm:h-10.sm:w-10
  30. [:span.text-red-600.text-xl
  31. (ui/icon "alert-triangle")]]
  32. [:div.mt-3.text-center.sm:mt-0.sm:ml-4.sm:text-left
  33. [:h3#modal-headline.text-lg.leading-6.font-medium
  34. (t :page/delete-confirmation)]]]
  35. [:div.mt-5.sm:mt-4.sm:flex.sm:flex-row-reverse
  36. [:span.flex.w-full.rounded-md.shadow-sm.sm:ml-3.sm:w-auto
  37. [:button.inline-flex.justify-center.w-full.rounded-md.border.border-transparent.px-4.py-2.bg-indigo-600.text-base.leading-6.font-medium.text-white.shadow-sm.hover:bg-indigo-500.focus:outline-none.focus:border-indigo-700.focus:shadow-outline-indigo.transition.ease-in-out.duration-150.sm:text-sm.sm:leading-5
  38. {:type "button"
  39. :class "ui__modal-enter"
  40. :on-click (fn []
  41. (delete-page! page-name))}
  42. (t :yes)]]
  43. [:span.mt-3.flex.w-full.rounded-md.shadow-sm.sm:mt-0.sm:w-auto
  44. [:button.inline-flex.justify-center.w-full.rounded-md.border.border-gray-300.px-4.py-2.bg-white.text-base.leading-6.font-medium.text-gray-700.shadow-sm.hover:text-gray-500.focus:outline-none.focus:border-blue-300.focus:shadow-outline-blue.transition.ease-in-out.duration-150.sm:text-sm.sm:leading-5
  45. {:type "button"
  46. :on-click close-fn}
  47. (t :cancel)]]]]))
  48. (defn page-menu
  49. [page-name]
  50. (when-let [page-name (or
  51. page-name
  52. (state/get-current-page))]
  53. (let [page-name (util/page-name-sanity-lc page-name)
  54. repo (state/sub :git/current-repo)
  55. page (db/entity repo [:block/name page-name])
  56. page-original-name (:block/original-name page)
  57. block? (and page (util/uuid-string? page-name))
  58. contents? (= page-name "contents")
  59. properties (:block/properties page)
  60. public? (true? (:public properties))
  61. favorites (:favorites (state/sub-graph-config))
  62. favorited? (contains? (set (map util/page-name-sanity-lc favorites))
  63. page-name)
  64. developer-mode? (state/sub [:ui/developer-mode?])]
  65. (when (and page (not block?))
  66. (->>
  67. [{:title (if favorited?
  68. (t :page/unfavorite)
  69. (t :page/add-to-favorites))
  70. :options {:on-click
  71. (fn []
  72. (if favorited?
  73. (page-handler/unfavorite-page! page-original-name)
  74. (page-handler/favorite-page! page-original-name)))}}
  75. (when-not (mobile-util/is-native-platform?)
  76. {:title (t :page/presentation-mode)
  77. :options {:on-click (fn []
  78. (state/sidebar-add-block!
  79. repo
  80. (:db/id page)
  81. :page-presentation
  82. {:page page}))}})
  83. ;; TODO: In the future, we'd like to extract file-related actions
  84. ;; (such as open-in-finder & open-with-default-app) into a sub-menu of
  85. ;; this one. However this component doesn't yet exist. PRs are welcome!
  86. ;; Details: https://github.com/logseq/logseq/pull/3003#issuecomment-952820676
  87. (when-let [file-path (and (util/electron?) (page-handler/get-page-file-path))]
  88. [{:title (t :page/open-in-finder)
  89. :options {:on-click #(js/window.apis.showItemInFolder file-path)}}
  90. {:title (t :page/open-with-default-app)
  91. :options {:on-click #(js/window.apis.openPath file-path)}}])
  92. (when-not contents?
  93. {:title (t :page/delete)
  94. :options {:on-click #(state/set-modal! (delete-page-dialog page-name))}})
  95. (when (state/get-current-page)
  96. {:title (t :export-page)
  97. :options {:on-click #(state/set-modal!
  98. (fn []
  99. (export/export-blocks [(:block/uuid page)])))}})
  100. (when (util/electron?)
  101. {:title (t (if public? :page/make-private :page/make-public))
  102. :options {:on-click
  103. (fn []
  104. (page-handler/update-public-attribute!
  105. page-name
  106. (if public? false true))
  107. (state/close-modal!))}})
  108. (when (util/electron?)
  109. {:title (t :page/version-history)
  110. :options {:on-click
  111. (fn []
  112. (shell/get-file-latest-git-log page 100))}})
  113. (when plugin-handler/lsp-enabled?
  114. (for [[_ {:keys [label] :as cmd} action pid] (state/get-plugins-commands-with-type :page-menu-item)]
  115. {:title label
  116. :options {:on-click #(commands/exec-plugin-simple-command!
  117. pid (assoc cmd :page (state/get-current-page)) action)}}))
  118. (when developer-mode?
  119. {:title "(Dev) Show page data"
  120. :options {:on-click (fn []
  121. (let [page-data (with-out-str (pprint/pprint (db/pull (:db/id page))))]
  122. (println page-data)
  123. (notification/show!
  124. [:div
  125. [:pre.code page-data]
  126. [:br]
  127. (ui/button
  128. "Copy to clipboard"
  129. :on-click #(.writeText js/navigator.clipboard page-data))]
  130. :success
  131. false)))}})]
  132. (flatten)
  133. (remove nil?))))))