Response.vue 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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"
  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, useContext } from "@nuxtjs/composition-api"
  94. import { useCodemirror } from "~/helpers/editor/codemirror"
  95. import { copyToClipboard } from "~/helpers/utils/clipboard"
  96. import { useReadonlyStream } from "~/helpers/utils/composables"
  97. import { gqlResponse$ } from "~/newstore/GQLSession"
  98. const {
  99. $toast,
  100. app: { i18n },
  101. } = useContext()
  102. const t = i18n.t.bind(i18n)
  103. const responseString = useReadonlyStream(gqlResponse$, "")
  104. const schemaEditor = ref<any | null>(null)
  105. const linewrapEnabled = ref(true)
  106. useCodemirror(
  107. schemaEditor,
  108. responseString,
  109. reactive({
  110. extendedEditorConfig: {
  111. mode: "application/ld+json",
  112. readOnly: true,
  113. lineWrapping: linewrapEnabled,
  114. },
  115. linter: null,
  116. completer: null,
  117. })
  118. )
  119. const downloadResponseIcon = ref("download")
  120. const copyResponseIcon = ref("copy")
  121. const copyResponse = () => {
  122. copyToClipboard(responseString.value!)
  123. copyResponseIcon.value = "check"
  124. setTimeout(() => (copyResponseIcon.value = "copy"), 1000)
  125. }
  126. const downloadResponse = () => {
  127. const dataToWrite = responseString.value
  128. const file = new Blob([dataToWrite!], { type: "application/json" })
  129. const a = document.createElement("a")
  130. const url = URL.createObjectURL(file)
  131. a.href = url
  132. a.download = `${url.split("/").pop()!.split("#")[0].split("?")[0]}`
  133. document.body.appendChild(a)
  134. a.click()
  135. downloadResponseIcon.value = "check"
  136. $toast.success(`${t("state.download_started")}`, {
  137. icon: "downloading",
  138. })
  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>