XMLLensRenderer.vue 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 "codemirror/mode/xml/xml"
  54. import { HoppRESTResponse } from "~/helpers/types/HoppRESTResponse"
  55. const props = defineProps<{
  56. response: HoppRESTResponse
  57. }>()
  58. const {
  59. $toast,
  60. app: { i18n },
  61. } = useContext()
  62. const t = i18n.t.bind(i18n)
  63. const responseBodyText = computed(() => {
  64. if (
  65. props.response.type === "loading" ||
  66. props.response.type === "network_fail"
  67. )
  68. return ""
  69. if (typeof props.response.body === "string") return props.response.body
  70. else {
  71. const res = new TextDecoder("utf-8").decode(props.response.body)
  72. // HACK: Temporary trailing null character issue from the extension fix
  73. return res.replace(/\0+$/, "")
  74. }
  75. })
  76. const downloadIcon = ref("download")
  77. const copyIcon = ref("copy")
  78. const responseType = computed(() => {
  79. return (
  80. props.response.headers.find((h) => h.key.toLowerCase() === "content-type")
  81. .value || ""
  82. )
  83. .split(";")[0]
  84. .toLowerCase()
  85. })
  86. const xmlResponse = ref<any | null>(null)
  87. const linewrapEnabled = ref(true)
  88. useCodemirror(
  89. xmlResponse,
  90. responseBodyText,
  91. reactive({
  92. extendedEditorConfig: {
  93. mode: "application/xml",
  94. readOnly: true,
  95. lineWrapping: linewrapEnabled,
  96. },
  97. linter: null,
  98. completer: null,
  99. })
  100. )
  101. const downloadResponse = () => {
  102. const dataToWrite = responseBodyText.value
  103. const file = new Blob([dataToWrite], { type: responseType.value })
  104. const a = document.createElement("a")
  105. const url = URL.createObjectURL(file)
  106. a.href = url
  107. // TODO get uri from meta
  108. a.download = `${url.split("/").pop().split("#")[0].split("?")[0]}`
  109. document.body.appendChild(a)
  110. a.click()
  111. downloadIcon.value = "check"
  112. $toast.success(`${t("state.download_started")}`, {
  113. icon: "downloading",
  114. })
  115. setTimeout(() => {
  116. document.body.removeChild(a)
  117. URL.revokeObjectURL(url)
  118. downloadIcon.value = "download"
  119. }, 1000)
  120. }
  121. const copyResponse = () => {
  122. copyToClipboard(responseBodyText.value)
  123. copyIcon.value = "check"
  124. $toast.success(`${t("state.copied_to_clipboard")}`, {
  125. icon: "content_paste",
  126. })
  127. setTimeout(() => (copyIcon.value = "copy"), 1000)
  128. }
  129. </script>