export.cljs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. (ns frontend.components.export
  2. (:require [rum.core :as rum]
  3. [frontend.ui :as ui]
  4. [frontend.util :as util]
  5. [frontend.handler.export :as export]
  6. [frontend.state :as state]
  7. [frontend.context.i18n :as i18n]))
  8. (rum/defc export
  9. []
  10. (when-let [current-repo (state/get-current-repo)]
  11. (rum/with-context [[t] i18n/*tongue-context*]
  12. [:div.export.w-96
  13. [:h1.title "Export"]
  14. [:ul.mr-1
  15. (when (util/electron?)
  16. [:li.mb-4
  17. [:a.font-medium {:on-click #(export/export-repo-as-html! current-repo)}
  18. (t :export-public-pages)]])
  19. [:li.mb-4
  20. [:a.font-medium {:on-click #(export/export-repo-as-markdown! current-repo)}
  21. (t :export-markdown)]]
  22. [:li.mb-4
  23. [:a.font-medium {:on-click #(export/export-repo-as-opml! current-repo)}
  24. (t :export-opml)]]
  25. [:li.mb-4
  26. [:a.font-medium {:on-click #(export/export-repo-as-edn-v2! current-repo)}
  27. (t :export-edn)]]
  28. [:li.mb-4
  29. [:a.font-medium {:on-click #(export/export-repo-as-json-v2! current-repo)}
  30. (t :export-json)]]
  31. [:li.mb-4
  32. [:a.font-medium {:on-click #(export/export-repo-as-roam-json! current-repo)}
  33. (t :export-roam-json)]]]
  34. [:a#download-as-edn-v2.hidden]
  35. [:a#download-as-json-v2.hidden]
  36. [:a#download-as-roam-json.hidden]
  37. [:a#download-as-html.hidden]
  38. [:a#download-as-zip.hidden]
  39. [:a#export-as-markdown.hidden]
  40. [:a#export-as-opml.hidden]
  41. [:a#convert-markdown-to-unordered-list-or-heading.hidden]])))
  42. (rum/defc export-page
  43. []
  44. (when-let [current-repo (state/get-current-repo)]
  45. (when-let [page (state/get-current-page)]
  46. (rum/with-context [[t] i18n/*tongue-context*]
  47. [:div.export.w-96
  48. [:h1.title "Export"]
  49. [:ul.mr-1
  50. [:li.mb-4
  51. [:a.font-medium {:on-click #(export/export-page-as-markdown! page)}
  52. (t :export-markdown)]]
  53. [:li.mb-4
  54. [:a.font-medium {:on-click #(export/export-page-as-opml! page)}
  55. (t :export-opml)]]]
  56. [:a#export-page-as-markdown.hidden]
  57. [:a#export-page-as-opml.hidden]
  58. [:a#convert-markdown-to-unordered-list-or-heading.hidden]]))))
  59. (def *export-block-type (atom :text))
  60. (def text-indent-style-options [{:label "dashes"
  61. :selected false}
  62. {:label "spaces"
  63. :selected false}
  64. {:label "no-indent"
  65. :selected false}])
  66. (rum/defcs export-blocks
  67. < rum/reactive
  68. (rum/local false ::copied?)
  69. [state root-block-id]
  70. (let [current-repo (state/get-current-repo)
  71. type (rum/react *export-block-type)
  72. text-indent-style (rum/react (state/get-export-block-text-indent-style))
  73. text-remove-options (rum/react (state/get-export-block-text-remove-options))
  74. copied? (::copied? state)
  75. content
  76. (case type
  77. :text (export/export-blocks-as-markdown current-repo root-block-id text-indent-style (into [] text-remove-options))
  78. :opml (export/export-blocks-as-opml current-repo root-block-id)
  79. :html (export/export-blocks-as-html current-repo root-block-id)
  80. (export/export-blocks-as-markdown current-repo root-block-id text-indent-style (into [] text-remove-options)))]
  81. [:div.export.w-96.resize
  82. [:div
  83. {:class "mb-2"}
  84. (ui/button "Text"
  85. :class "mr-2 w-20"
  86. :on-click #(reset! *export-block-type :text))
  87. (ui/button "OPML"
  88. :class "mr-2 w-20"
  89. :on-click #(reset! *export-block-type :opml))
  90. (ui/button "HTML"
  91. :class "w-20"
  92. :on-click #(reset! *export-block-type :html))]
  93. [:textarea.overflow-y-auto.h-96 {:value content}]
  94. (let [options (->> text-indent-style-options
  95. (mapv (fn [opt]
  96. (if (= text-indent-style (:label opt))
  97. (assoc opt :selected true)
  98. opt))))]
  99. [:div [:div.flex.items-center
  100. [:label.mr-8
  101. {:style {:visibility (if (= :text type) "visible" "hidden")}}
  102. "Indentation style:"]
  103. [:select.block.my-2.text-lg.rounded.border
  104. {:style {:padding "0 0 0 12px"
  105. :visibility (if (= :text type) "visible" "hidden")}
  106. :on-change (fn [e]
  107. (let [value (util/evalue e)]
  108. (reset! (state/get-export-block-text-indent-style) value)))}
  109. (for [{:keys [label value selected]} options]
  110. [:option (cond->
  111. {:key label
  112. :value (or value label)}
  113. selected
  114. (assoc :selected selected))
  115. label])]]
  116. [:div.flex.items-center
  117. (ui/checkbox {:style {:margin-right 6
  118. :visibility (if (= :text type) "visible" "hidden")}
  119. :checked (contains? text-remove-options :page-ref)
  120. :on-change (fn [e] (if (util/echecked? e)
  121. (swap! (state/get-export-block-text-remove-options)
  122. #(conj % :page-ref))
  123. (swap! (state/get-export-block-text-remove-options)
  124. #(disj % :page-ref))))})
  125. [:div
  126. {:style {:visibility (if (= :text type) "visible" "hidden")}}
  127. "[[text]] -> text"]
  128. (ui/checkbox {:style {:margin-right 6
  129. :margin-left 10
  130. :visibility (if (= :text type) "visible" "hidden")}
  131. :checked (contains? text-remove-options :emphasis)
  132. :on-change (fn [e] (if (util/echecked? e)
  133. (swap! (state/get-export-block-text-remove-options)
  134. #(conj % :emphasis))
  135. (swap! (state/get-export-block-text-remove-options)
  136. #(disj % :emphasis))))})
  137. [:div
  138. {:style {:visibility (if (= :text type) "visible" "hidden")}}
  139. "remove emphasis"]]])
  140. (ui/button (if @copied? "Copied to clipboard!" "Copy to clipboard")
  141. :on-click (fn []
  142. (util/copy-to-clipboard! content)
  143. (reset! copied? true)))]))