XMLLensRenderer.vue 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <template>
  2. <div>
  3. <div
  4. class="
  5. bg-primary
  6. border-b border-dividerLight
  7. flex flex-1
  8. top-lowerSecondaryStickyFold
  9. pl-4
  10. z-10
  11. sticky
  12. items-center
  13. justify-between
  14. "
  15. >
  16. <label class="font-semibold text-secondaryLight">
  17. {{ $t("response.body") }}
  18. </label>
  19. <div class="flex">
  20. <ButtonSecondary
  21. v-if="response.body"
  22. v-tippy="{ theme: 'tooltip' }"
  23. :title="$t('state.linewrap')"
  24. :class="{ '!text-accent': linewrapEnabled }"
  25. svg="corner-down-left"
  26. @click.native.prevent="linewrapEnabled = !linewrapEnabled"
  27. />
  28. <ButtonSecondary
  29. v-if="response.body"
  30. ref="downloadResponse"
  31. v-tippy="{ theme: 'tooltip' }"
  32. :title="$t('action.download_file')"
  33. :svg="downloadIcon"
  34. @click.native="downloadResponse"
  35. />
  36. <ButtonSecondary
  37. v-if="response.body"
  38. ref="copyResponse"
  39. v-tippy="{ theme: 'tooltip' }"
  40. :title="$t('action.copy')"
  41. :svg="copyIcon"
  42. @click.native="copyResponse"
  43. />
  44. </div>
  45. </div>
  46. <div ref="xmlResponse"></div>
  47. </div>
  48. </template>
  49. <script setup lang="ts">
  50. import { computed, ref, useContext, reactive } from "@nuxtjs/composition-api"
  51. import { useCodemirror } from "~/helpers/editor/codemirror"
  52. import { copyToClipboard } from "~/helpers/utils/clipboard"
  53. import { HoppRESTResponse } from "~/helpers/types/HoppRESTResponse"
  54. const props = defineProps<{
  55. response: HoppRESTResponse
  56. }>()
  57. const {
  58. $toast,
  59. app: { i18n },
  60. } = useContext()
  61. const t = i18n.t.bind(i18n)
  62. const responseBodyText = computed(() => {
  63. if (
  64. props.response.type === "loading" ||
  65. props.response.type === "network_fail"
  66. )
  67. return ""
  68. if (typeof props.response.body === "string") return props.response.body
  69. else {
  70. const res = new TextDecoder("utf-8").decode(props.response.body)
  71. // HACK: Temporary trailing null character issue from the extension fix
  72. return res.replace(/\0+$/, "")
  73. }
  74. })
  75. const downloadIcon = ref("download")
  76. const copyIcon = ref("copy")
  77. const responseType = computed(() => {
  78. return (
  79. props.response.headers.find((h) => h.key.toLowerCase() === "content-type")
  80. .value || ""
  81. )
  82. .split(";")[0]
  83. .toLowerCase()
  84. })
  85. const xmlResponse = ref<any | null>(null)
  86. const linewrapEnabled = ref(true)
  87. useCodemirror(
  88. xmlResponse,
  89. responseBodyText,
  90. reactive({
  91. extendedEditorConfig: {
  92. mode: "application/xml",
  93. readOnly: true,
  94. lineWrapping: linewrapEnabled,
  95. },
  96. linter: null,
  97. completer: null,
  98. })
  99. )
  100. const downloadResponse = () => {
  101. const dataToWrite = responseBodyText.value
  102. const file = new Blob([dataToWrite], { type: responseType.value })
  103. const a = document.createElement("a")
  104. const url = URL.createObjectURL(file)
  105. a.href = url
  106. // TODO get uri from meta
  107. a.download = `${url.split("/").pop().split("#")[0].split("?")[0]}`
  108. document.body.appendChild(a)
  109. a.click()
  110. downloadIcon.value = "check"
  111. $toast.success(`${t("state.download_started")}`, {
  112. icon: "downloading",
  113. })
  114. setTimeout(() => {
  115. document.body.removeChild(a)
  116. URL.revokeObjectURL(url)
  117. downloadIcon.value = "download"
  118. }, 1000)
  119. }
  120. const copyResponse = () => {
  121. copyToClipboard(responseBodyText.value)
  122. copyIcon.value = "check"
  123. $toast.success(`${t("state.copied_to_clipboard")}`, {
  124. icon: "content_paste",
  125. })
  126. setTimeout(() => (copyIcon.value = "copy"), 1000)
  127. }
  128. </script>