Response.vue 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <template>
  2. <AppSection ref="response" label="response">
  3. <div
  4. v-if="responseString === 'loading'"
  5. class="flex flex-col p-4 items-center justify-center"
  6. >
  7. <SmartSpinner class="my-4" />
  8. <span class="text-secondaryLight">{{ t("state.loading") }}</span>
  9. </div>
  10. <div v-else-if="responseString">
  11. <div
  12. class="
  13. bg-primary
  14. border-b border-dividerLight
  15. flex flex-1
  16. pl-4
  17. top-0
  18. z-10
  19. sticky
  20. items-center
  21. justify-between
  22. "
  23. >
  24. <label class="font-semibold text-secondaryLight">
  25. {{ t("response.title") }}
  26. </label>
  27. <div class="flex">
  28. <ButtonSecondary
  29. v-tippy="{ theme: 'tooltip' }"
  30. :title="t('state.linewrap')"
  31. :class="{ '!text-accent': linewrapEnabled }"
  32. svg="corner-down-left"
  33. @click.native.prevent="linewrapEnabled = !linewrapEnabled"
  34. />
  35. <ButtonSecondary
  36. ref="downloadResponse"
  37. v-tippy="{ theme: 'tooltip' }"
  38. :title="t('action.download_file')"
  39. :svg="downloadResponseIcon"
  40. @click.native="downloadResponse"
  41. />
  42. <ButtonSecondary
  43. ref="copyResponseButton"
  44. v-tippy="{ theme: 'tooltip' }"
  45. :title="t('action.copy')"
  46. :svg="copyResponseIcon"
  47. @click.native="copyResponse"
  48. />
  49. </div>
  50. </div>
  51. <div ref="schemaEditor"></div>
  52. </div>
  53. <div
  54. v-else
  55. class="
  56. flex flex-col flex-1
  57. text-secondaryLight
  58. p-4
  59. items-center
  60. justify-center
  61. "
  62. >
  63. <div class="flex space-x-2 pb-4 my-4">
  64. <div class="flex flex-col space-y-4 text-right items-end">
  65. <span class="flex flex-1 items-center">
  66. {{ t("shortcut.general.command_menu") }}
  67. </span>
  68. <span class="flex flex-1 items-center">
  69. {{ t("shortcut.general.help_menu") }}
  70. </span>
  71. </div>
  72. <div class="flex flex-col space-y-4">
  73. <div class="flex">
  74. <span class="shortcut-key">/</span>
  75. </div>
  76. <div class="flex">
  77. <span class="shortcut-key">?</span>
  78. </div>
  79. </div>
  80. </div>
  81. <ButtonSecondary
  82. :label="`${t('app.documentation')}`"
  83. to="https://docs.hoppscotch.io/features/response"
  84. svg="external-link"
  85. blank
  86. outline
  87. reverse
  88. />
  89. </div>
  90. </AppSection>
  91. </template>
  92. <script setup lang="ts">
  93. import { reactive, ref } from "@nuxtjs/composition-api"
  94. import { useCodemirror } from "~/helpers/editor/codemirror"
  95. import { copyToClipboard } from "~/helpers/utils/clipboard"
  96. import {
  97. useReadonlyStream,
  98. useI18n,
  99. useToast,
  100. } from "~/helpers/utils/composables"
  101. import { gqlResponse$ } from "~/newstore/GQLSession"
  102. const t = useI18n()
  103. const toast = useToast()
  104. const responseString = useReadonlyStream(gqlResponse$, "")
  105. const schemaEditor = ref<any | null>(null)
  106. const linewrapEnabled = ref(true)
  107. useCodemirror(
  108. schemaEditor,
  109. responseString,
  110. reactive({
  111. extendedEditorConfig: {
  112. mode: "application/ld+json",
  113. readOnly: true,
  114. lineWrapping: linewrapEnabled,
  115. },
  116. linter: null,
  117. completer: null,
  118. })
  119. )
  120. const downloadResponseIcon = ref("download")
  121. const copyResponseIcon = ref("copy")
  122. const copyResponse = () => {
  123. copyToClipboard(responseString.value!)
  124. copyResponseIcon.value = "check"
  125. toast.success(`${t("state.copied_to_clipboard")}`)
  126. setTimeout(() => (copyResponseIcon.value = "copy"), 1000)
  127. }
  128. const downloadResponse = () => {
  129. const dataToWrite = responseString.value
  130. const file = new Blob([dataToWrite!], { type: "application/json" })
  131. const a = document.createElement("a")
  132. const url = URL.createObjectURL(file)
  133. a.href = url
  134. a.download = `${url.split("/").pop()!.split("#")[0].split("?")[0]}`
  135. document.body.appendChild(a)
  136. a.click()
  137. downloadResponseIcon.value = "check"
  138. toast.success(`${t("state.download_started")}`)
  139. setTimeout(() => {
  140. document.body.removeChild(a)
  141. URL.revokeObjectURL(url)
  142. downloadResponseIcon.value = "download"
  143. }, 1000)
  144. }
  145. </script>
  146. <style lang="scss" scoped>
  147. .shortcut-key {
  148. @apply bg-dividerLight;
  149. @apply rounded;
  150. @apply ml-2;
  151. @apply py-1;
  152. @apply px-2;
  153. @apply inline-flex;
  154. }
  155. </style>