page_menu.cljs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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.util.url :as url-util]
  14. [frontend.handler.shell :as shell]
  15. [frontend.mobile.util :as mobile-util]
  16. [electron.ipc :as ipc]
  17. [frontend.config :as config]
  18. [frontend.handler.user :as user-handler]
  19. [frontend.handler.file-sync :as file-sync-handler]))
  20. (defn- delete-page!
  21. [page-name]
  22. (page-handler/delete! page-name
  23. (fn []
  24. (notification/show! (str "Page " page-name " was deleted successfully!")
  25. :success)))
  26. (state/close-modal!)
  27. (route-handler/redirect-to-home!))
  28. (defn delete-page-dialog
  29. [page-name]
  30. (fn [close-fn]
  31. [:div
  32. [:div.sm:flex.items-center
  33. [:div.mx-auto.flex-shrink-0.flex.items-center.justify-center.h-12.w-12.rounded-full.bg-error.sm:mx-0.sm:h-10.sm:w-10
  34. [:span.text-error.text-xl
  35. (ui/icon "alert-triangle")]]
  36. [:div.mt-3.text-center.sm:mt-0.sm:ml-4.sm:text-left
  37. [:h3#modal-headline.text-lg.leading-6.font-medium
  38. (t :page/delete-confirmation)]]]
  39. [:div.mt-5.sm:mt-4.sm:flex.sm:flex-row-reverse
  40. [:span.flex.w-full.rounded-md.shadow-sm.sm:ml-3.sm:w-auto
  41. [: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
  42. {:type "button"
  43. :class "ui__modal-enter"
  44. :on-click (fn []
  45. (delete-page! page-name))}
  46. (t :yes)]]
  47. [:span.mt-3.flex.w-full.rounded-md.shadow-sm.sm:mt-0.sm:w-auto
  48. [: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
  49. {:type "button"
  50. :on-click close-fn}
  51. (t :cancel)]]]]))
  52. (defn ^:large-vars/cleanup-todo page-menu
  53. [page-name]
  54. (when-let [page-name (or
  55. page-name
  56. (state/get-current-page)
  57. (state/get-current-whiteboard))]
  58. (let [page-name (util/page-name-sanity-lc page-name)
  59. repo (state/sub :git/current-repo)
  60. page (db/entity repo [:block/name page-name])
  61. page-original-name (:block/original-name page)
  62. whiteboard? (= "whiteboard" (:block/type page))
  63. block? (and page (util/uuid-string? page-name) (not whiteboard?))
  64. contents? (= page-name "contents")
  65. properties (:block/properties page)
  66. public? (true? (:public properties))
  67. favorites (:favorites (state/sub-config))
  68. favorited? (contains? (set (map util/page-name-sanity-lc favorites))
  69. page-name)
  70. developer-mode? (state/sub [:ui/developer-mode?])
  71. file-path (when (util/electron?) (page-handler/get-page-file-path))
  72. _ (state/sub :auth/id-token)
  73. file-sync-graph-uuid (and (user-handler/logged-in?)
  74. (file-sync-handler/enable-sync?)
  75. (file-sync-handler/get-current-graph-uuid))]
  76. (when (and page (not block?))
  77. (->>
  78. [{:title (if favorited?
  79. (t :page/unfavorite)
  80. (t :page/add-to-favorites))
  81. :options {:on-click
  82. (fn []
  83. (if favorited?
  84. (page-handler/unfavorite-page! page-original-name)
  85. (page-handler/favorite-page! page-original-name)))}}
  86. (when (or (util/electron?) file-sync-graph-uuid)
  87. {:title (t :page/version-history)
  88. :options {:on-click
  89. (fn []
  90. (cond
  91. file-sync-graph-uuid
  92. (state/pub-event! [:graph/pick-page-histories file-sync-graph-uuid page-name])
  93. (util/electron?)
  94. (shell/get-file-latest-git-log page 100)
  95. :else
  96. nil))
  97. :class "cp__btn_history_version"}})
  98. (when (or (util/electron?)
  99. (mobile-util/native-platform?))
  100. {:title (t :page/copy-page-url)
  101. :options {:on-click #(util/copy-to-clipboard!
  102. (url-util/get-logseq-graph-page-url nil repo page-original-name))}})
  103. (when-not contents?
  104. {:title (t :page/delete)
  105. :options {:on-click #(state/set-modal! (delete-page-dialog page-name))}})
  106. (when-not (mobile-util/native-platform?)
  107. {:title (t :page/presentation-mode)
  108. :options {:on-click (fn []
  109. (state/sidebar-add-block!
  110. repo
  111. (:db/id page)
  112. :page-presentation))}})
  113. ;; TODO: In the future, we'd like to extract file-related actions
  114. ;; (such as open-in-finder & open-with-default-app) into a sub-menu of
  115. ;; this one. However this component doesn't yet exist. PRs are welcome!
  116. ;; Details: https://github.com/logseq/logseq/pull/3003#issuecomment-952820676
  117. (when file-path
  118. [{:title (t :page/open-in-finder)
  119. :options {:on-click #(js/window.apis.showItemInFolder file-path)}}
  120. {:title (t :page/open-with-default-app)
  121. :options {:on-click #(js/window.apis.openPath file-path)}}])
  122. (when (state/get-current-page)
  123. {:title (t :export-page)
  124. :options {:on-click #(state/set-modal!
  125. (fn []
  126. (export/export-blocks [(:block/uuid page)])))}})
  127. (when (util/electron?)
  128. {:title (t (if public? :page/make-private :page/make-public))
  129. :options {:on-click
  130. (fn []
  131. (page-handler/update-public-attribute!
  132. page-name
  133. (if public? false true))
  134. (state/close-modal!))}})
  135. (when (and (util/electron?) file-path
  136. (not (file-sync-handler/synced-file-graph? repo)))
  137. {:title (t :page/open-backup-directory)
  138. :options {:on-click
  139. (fn []
  140. (ipc/ipc "openFileBackupDir" (config/get-local-dir repo) file-path))}})
  141. (when config/lsp-enabled?
  142. (for [[_ {:keys [label] :as cmd} action pid] (state/get-plugins-commands-with-type :page-menu-item)]
  143. {:title label
  144. :options {:on-click #(commands/exec-plugin-simple-command!
  145. pid (assoc cmd :page page-name) action)}}))
  146. (when developer-mode?
  147. {:title "(Dev) Show page data"
  148. :options {:on-click (fn []
  149. (let [page-data (with-out-str (pprint/pprint (db/pull (:db/id page))))]
  150. (println page-data)
  151. (notification/show!
  152. [:div
  153. [:pre.code page-data]
  154. [:br]
  155. (ui/button
  156. "Copy to clipboard"
  157. :on-click #(.writeText js/navigator.clipboard page-data))]
  158. :success
  159. false)))}})]
  160. (flatten)
  161. (remove nil?))))))