documentation.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. <template>
  2. <Splitpanes
  3. class="smart-splitter"
  4. :rtl="SIDEBAR_ON_LEFT && windowInnerWidth.x.value >= 768"
  5. :class="{
  6. '!flex-row-reverse': SIDEBAR_ON_LEFT && windowInnerWidth.x.value >= 768,
  7. }"
  8. :horizontal="!(windowInnerWidth.x.value >= 768)"
  9. >
  10. <Pane size="75" min-size="65" class="hide-scrollbar !overflow-auto">
  11. <Splitpanes class="smart-splitter" :horizontal="COLUMN_LAYOUT">
  12. <Pane class="hide-scrollbar !overflow-auto">
  13. <AppSection label="import">
  14. <div class="flex p-4 items-start justify-between">
  15. <label>
  16. {{ $t("documentation.generate_message") }}
  17. </label>
  18. <span
  19. class="
  20. bg-accentDark
  21. rounded
  22. text-accentContrast
  23. py-1
  24. px-2
  25. inline-flex
  26. "
  27. >
  28. BETA
  29. </span>
  30. </div>
  31. <div
  32. class="
  33. bg-primary
  34. border-b border-dividerLight
  35. flex
  36. top-0
  37. z-10
  38. items-start
  39. justify-between
  40. sticky
  41. "
  42. >
  43. <label for="collectionUpload">
  44. <ButtonSecondary
  45. v-tippy="{ theme: 'tooltip' }"
  46. title="JSON"
  47. svg="folder"
  48. class="!rounded-none"
  49. :label="$t('import.collections')"
  50. @click.native="$refs.collectionUpload.click()"
  51. />
  52. </label>
  53. <input
  54. ref="collectionUpload"
  55. class="input"
  56. name="collectionUpload"
  57. type="file"
  58. @change="uploadCollection"
  59. />
  60. <ButtonSecondary
  61. v-tippy="{ theme: 'tooltip' }"
  62. :title="$t('action.clear')"
  63. svg="trash-2"
  64. @click.native="collectionJSON = '[]'"
  65. />
  66. </div>
  67. <textarea-autosize
  68. id="import-curl"
  69. v-model="collectionJSON"
  70. class="font-mono p-4 bg-primary"
  71. autofocus
  72. rows="8"
  73. />
  74. <div
  75. class="
  76. bg-primary
  77. border-t border-b border-dividerLight
  78. flex
  79. p-4
  80. bottom-0
  81. z-10
  82. justify-between
  83. items-start
  84. sticky
  85. "
  86. >
  87. <ButtonPrimary
  88. :label="$t('documentation.generate')"
  89. @click.native="getDoc"
  90. />
  91. </div>
  92. </AppSection>
  93. </Pane>
  94. <Pane class="hide-scrollbar !overflow-auto">
  95. <AppSection label="documentation">
  96. <div class="flex flex-col">
  97. <div
  98. v-if="items.length === 0"
  99. class="
  100. flex flex-col
  101. text-secondaryLight
  102. p-4
  103. items-center
  104. justify-center
  105. "
  106. >
  107. <i class="opacity-75 pb-2 material-icons">topic</i>
  108. <span class="text-center">
  109. {{ $t("helpers.generate_documentation_first") }}
  110. </span>
  111. </div>
  112. <div
  113. v-else
  114. class="
  115. bg-primary
  116. border-b border-dividerLight
  117. flex flex-1
  118. p-4
  119. top-0
  120. z-10
  121. sticky
  122. "
  123. >
  124. <span
  125. v-tippy="{ theme: 'tooltip' }"
  126. :title="
  127. !currentUser
  128. ? $t('export.require_github')
  129. : currentUser.provider !== 'github.com'
  130. ? $t('export.require_github')
  131. : 'Beta'
  132. "
  133. >
  134. <ButtonPrimary
  135. :disabled="
  136. !currentUser
  137. ? true
  138. : currentUser.provider !== 'github.com'
  139. ? true
  140. : false
  141. "
  142. :label="$t('export.create_secret_gist')"
  143. @click.native="createDocsGist"
  144. />
  145. </span>
  146. </div>
  147. <div
  148. v-for="(collection, index) in items"
  149. :key="`collection-${index}`"
  150. >
  151. <DocsCollection :collection="collection" />
  152. </div>
  153. </div>
  154. </AppSection>
  155. </Pane>
  156. </Splitpanes>
  157. </Pane>
  158. <Pane
  159. v-if="SIDEBAR"
  160. size="25"
  161. min-size="20"
  162. class="hide-scrollbar !overflow-auto"
  163. >
  164. <aside>
  165. <Collections
  166. :selected="selected"
  167. :doc="true"
  168. @use-collection="useSelectedCollection($event)"
  169. @remove-collection="removeSelectedCollection($event)"
  170. />
  171. </aside>
  172. </Pane>
  173. </Splitpanes>
  174. </template>
  175. <script>
  176. import { defineComponent } from "@nuxtjs/composition-api"
  177. import { Splitpanes, Pane } from "splitpanes"
  178. import "splitpanes/dist/splitpanes.css"
  179. import Mustache from "mustache"
  180. import { currentUser$ } from "~/helpers/fb/auth"
  181. import DocsTemplate from "~/assets/md/docs.md"
  182. import folderContents from "~/assets/md/folderContents.md"
  183. import folderBody from "~/assets/md/folderBody.md"
  184. import { useSetting } from "~/newstore/settings"
  185. import { useReadonlyStream } from "~/helpers/utils/composables"
  186. import useWindowSize from "~/helpers/utils/useWindowSize"
  187. export default defineComponent({
  188. components: { Splitpanes, Pane },
  189. setup() {
  190. return {
  191. windowInnerWidth: useWindowSize(),
  192. SIDEBAR: useSetting("SIDEBAR"),
  193. COLUMN_LAYOUT: useSetting("COLUMN_LAYOUT"),
  194. currentUser: useReadonlyStream(currentUser$, null),
  195. SIDEBAR_ON_LEFT: useSetting("SIDEBAR_ON_LEFT"),
  196. }
  197. },
  198. data() {
  199. return {
  200. collectionJSON: "[]",
  201. items: [],
  202. docsMarkdown: "",
  203. selected: [],
  204. }
  205. },
  206. head() {
  207. return {
  208. title: `${this.$t("navigation.doc")} • Hoppscotch`,
  209. }
  210. },
  211. methods: {
  212. async createDocsGist() {
  213. await this.$axios
  214. .$post(
  215. "https://api.github.com/gists",
  216. {
  217. files: {
  218. "api-docs.md": {
  219. content: this.docsMarkdown,
  220. },
  221. },
  222. },
  223. {
  224. headers: {
  225. Authorization: `token ${this.currentUser.accessToken}`,
  226. Accept: "application/vnd.github.v3+json",
  227. },
  228. }
  229. )
  230. .then((res) => {
  231. this.$toast.success(this.$t("export.gist_created"), {
  232. icon: "done",
  233. })
  234. window.open(res.html_url)
  235. })
  236. .catch((e) => {
  237. this.$toast.error(this.$t("error.something_went_wrong"), {
  238. icon: "error_outline",
  239. })
  240. console.error(e)
  241. })
  242. },
  243. uploadCollection() {
  244. const file = this.$refs.collectionUpload.files[0]
  245. if (file !== undefined && file !== null) {
  246. const reader = new FileReader()
  247. reader.onload = ({ target }) => {
  248. this.collectionJSON = target.result
  249. }
  250. reader.readAsText(file)
  251. this.$toast.success(this.$t("state.file_imported"), {
  252. icon: "attach_file",
  253. })
  254. } else {
  255. this.$toast.error(this.$t("action.choose_file"), {
  256. icon: "attach_file",
  257. })
  258. }
  259. this.$refs.collectionUpload.value = ""
  260. },
  261. assignIDs(items, pref, nestingLevel) {
  262. for (let i = 0; i < items.length; ++i) {
  263. items[i].id = `&emsp;${pref}${i + 1}.`
  264. items[i].ref = `${items[i].name.split(" ").join("-")}`
  265. items[i].nestingLevel = nestingLevel
  266. items[i].folders = this.assignIDs(
  267. items[i].folders,
  268. items[i].id,
  269. nestingLevel + "#"
  270. )
  271. for (let j = 0; j < items[i].requests.length; ++j) {
  272. items[i].requests[j].id = `&emsp;${items[i].id}${i + 1}`
  273. items[i].requests[j].ref = `${items[i].requests[j].name
  274. .split(" ")
  275. .join("-")}`
  276. items[i].requests[j].nestingLevel = nestingLevel + "#"
  277. }
  278. }
  279. return items
  280. },
  281. getDoc() {
  282. try {
  283. this.items = JSON.parse(this.collectionJSON)
  284. this.assignIDs(this.items, "", "#")
  285. this.$toast.clear()
  286. this.$toast.success(this.$t("state.docs_generated"), {
  287. icon: "book",
  288. })
  289. const docsMarkdown = Mustache.render(
  290. DocsTemplate,
  291. {
  292. collections: this.items,
  293. isHeaders() {
  294. return this.headers.length
  295. },
  296. isParams() {
  297. return this.params.length
  298. },
  299. isAuth() {
  300. return this.auth !== "None"
  301. },
  302. isAuthBasic() {
  303. return this.httpUser && this.httpPassword
  304. },
  305. isRawParams() {
  306. return this.rawParams && this.rawParams !== ""
  307. },
  308. isPreRequestScript() {
  309. return this.preRequestScript && this.preRequestScript !== ""
  310. },
  311. isTestScript() {
  312. return this.testScript && this.testScript !== ""
  313. },
  314. },
  315. {
  316. folderContents,
  317. folderBody,
  318. }
  319. )
  320. this.docsMarkdown = docsMarkdown.replace(/^\s*[\r\n]/gm, "\n\n")
  321. } catch (e) {
  322. console.error(e)
  323. this.$toast.error(this.$t("error.something_went_wrong"), {
  324. icon: "error_outline",
  325. })
  326. }
  327. },
  328. useSelectedCollection(collection) {
  329. if (this.selected.find((coll) => coll === collection)) {
  330. return
  331. }
  332. this.selected.push(collection)
  333. const importCollection = JSON.stringify(this.selected, null, 2)
  334. this.collectionJSON = JSON.stringify(
  335. JSON.parse(importCollection),
  336. null,
  337. 2
  338. )
  339. },
  340. removeSelectedCollection(collection) {
  341. this.selected = this.selected.filter((coll) => coll !== collection)
  342. const importCollection = JSON.stringify(this.selected, null, 2)
  343. this.collectionJSON = JSON.stringify(
  344. JSON.parse(importCollection),
  345. null,
  346. 2
  347. )
  348. },
  349. },
  350. })
  351. </script>