widgets.cljs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. (ns frontend.components.widgets
  2. (:require [clojure.string :as string]
  3. [frontend.context.i18n :as i18n]
  4. [frontend.handler.notification :as notification]
  5. [frontend.handler.page :as page-handler]
  6. [frontend.handler.repo :as repo-handler]
  7. [frontend.handler.user :as user-handler]
  8. [frontend.handler.web.nfs :as nfs]
  9. [frontend.modules.shortcut.core :as shortcut]
  10. [frontend.state :as state]
  11. [frontend.ui :as ui]
  12. [frontend.util :as util]
  13. [rum.core :as rum]))
  14. (rum/defc choose-preferred-format
  15. []
  16. (rum/with-context [[t] i18n/*tongue-context*]
  17. [:div
  18. [:h1.title {:style {:margin-bottom "0.25rem"}}
  19. (t :format/preferred-mode)]
  20. [:div.mt-4.ml-1
  21. (ui/button
  22. "Markdown"
  23. :on-click
  24. #(user-handler/set-preferred-format! :markdown))
  25. [:span.ml-2.mr-2 "-OR-"]
  26. (ui/button
  27. "Org Mode"
  28. :on-click
  29. #(user-handler/set-preferred-format! :org))]]))
  30. (rum/defcs add-github-repo <
  31. (rum/local "" ::repo)
  32. (rum/local "" ::branch)
  33. [state]
  34. (let [repo (get state ::repo)
  35. branch (get state ::branch)]
  36. (rum/with-context [[t] i18n/*tongue-context*]
  37. [:div.flex.flex-col
  38. [:div.w-full.mx-auto
  39. [:div
  40. [:div
  41. [:h1.title
  42. (t :git/add-repo-prompt)]
  43. [:div.mt-4.mb-4.relative.rounded-md.shadow-sm.max-w-xs
  44. [:input#repo.form-input.block.w-full.sm:text-sm.sm:leading-5
  45. {:autoFocus true
  46. :placeholder "https://github.com/username/repo"
  47. :on-change (fn [e]
  48. (reset! repo (util/evalue e)))}]]
  49. [:label.font-medium "Default Branch (make sure it's matched with your setting on Github): "]
  50. [:div.mt-2.mb-4.relative.rounded-md.shadow-sm.max-w-xs
  51. [:input#branch.form-input.block.w-full.sm:text-sm.sm:leading-5
  52. {:value @branch
  53. :placeholder "e.g. master"
  54. :on-change (fn [e]
  55. (reset! branch (util/evalue e)))}]]]]
  56. (ui/button
  57. (t :git/add-repo-prompt-confirm)
  58. :on-click
  59. (fn []
  60. (let [branch (string/trim @branch)]
  61. (if (string/blank? branch)
  62. (notification/show!
  63. [:p.text-gray-700.dark:text-gray-300 "Please input a branch, make sure it's matched with your setting on Github."]
  64. :error
  65. false)
  66. (let [repo (util/lowercase-first @repo)]
  67. (if (util/starts-with? repo "https://github.com/")
  68. (let [repo (string/replace repo ".git" "")]
  69. (repo-handler/create-repo! repo branch))
  70. (notification/show!
  71. [:p.text-gray-700.dark:text-gray-300 "Please input a valid repo url, e.g. https://github.com/username/repo"]
  72. :error
  73. false)))))))]])))
  74. (rum/defcs add-local-directory
  75. []
  76. (rum/with-context [[t] i18n/*tongue-context*]
  77. [:div.flex.flex-col
  78. [:h1.title "Add a graph"]
  79. (let [nfs-supported? (nfs/supported?)]
  80. [:div.cp__widgets-open-local-directory
  81. [:div.select-file-wrap.cursor
  82. (when nfs-supported?
  83. {:on-click #(page-handler/ls-dir-files! shortcut/refresh!)})
  84. [:div
  85. [:h1.title "Open a local directory"]
  86. [:p "Logseq supports both Markdown and Org-mode. You can open an existing directory or create a new one on your device, a directory is also known simply as a folder. Your data will be stored only on this device."]
  87. [:p "After you have opened your directory, it will create three folders in that directory:"]
  88. [:ul
  89. [:li "/journals - store your journal pages"]
  90. [:li "/pages - store the other pages"]
  91. [:li "/logseq - store configuration, custom.css, and some metadata."]]
  92. (when-not nfs-supported?
  93. (ui/admonition :warning
  94. [:p "It seems that your browser doesn't support the "
  95. [:a {:href "https://web.dev/file-system-access/"
  96. :target "_blank"}
  97. "new native filesystem API"]
  98. [:span ", please use any Chromium 86+ based browser like Chrome, Vivaldi, Edge, etc. Notice that the API doesn't support mobile browsers at the moment."]]))]]])]))
  99. (rum/defcs add-graph <
  100. [state & {:keys [graph-types]
  101. :or {graph-types [:local :github]}
  102. :as opts}]
  103. (let [github-authed? (state/github-authed?)
  104. generate-f (fn [x]
  105. (case x
  106. :github
  107. (when (and github-authed? (not (util/electron?)))
  108. (rum/with-key (add-github-repo)
  109. "add-github-repo"))
  110. :local
  111. (rum/with-key (add-local-directory)
  112. "add-local-directory")
  113. nil))
  114. available-graph (->> (set graph-types)
  115. (keep generate-f)
  116. (vec)
  117. (interpose [:b.mt-10.mb-5.opacity-50 "OR"]))]
  118. (rum/with-context [[t] i18n/*tongue-context*]
  119. [:div.p-8.flex.flex-col available-graph])))