HTMLLensRenderer.vue 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <template>
  2. <div class="flex flex-col flex-1">
  3. <div
  4. class="bg-primary border-b border-dividerLight flex flex-1 top-lowerSecondaryStickyFold pl-4 z-10 sticky items-center justify-between"
  5. >
  6. <label class="font-semibold text-secondaryLight">
  7. {{ t("response.body") }}
  8. </label>
  9. <div class="flex">
  10. <ButtonSecondary
  11. v-if="response.body"
  12. v-tippy="{ theme: 'tooltip' }"
  13. :title="t('state.linewrap')"
  14. :class="{ '!text-accent': linewrapEnabled }"
  15. svg="wrap-text"
  16. @click.native.prevent="linewrapEnabled = !linewrapEnabled"
  17. />
  18. <ButtonSecondary
  19. v-if="response.body"
  20. v-tippy="{ theme: 'tooltip' }"
  21. :title="
  22. previewEnabled ? t('hide.preview') : t('response.preview_html')
  23. "
  24. :svg="!previewEnabled ? 'eye' : 'eye-off'"
  25. @click.native.prevent="togglePreview"
  26. />
  27. <ButtonSecondary
  28. v-if="response.body"
  29. ref="downloadResponse"
  30. v-tippy="{ theme: 'tooltip' }"
  31. :title="t('action.download_file')"
  32. :svg="downloadIcon"
  33. @click.native="downloadResponse"
  34. />
  35. <ButtonSecondary
  36. v-if="response.body"
  37. ref="copyResponse"
  38. v-tippy="{ theme: 'tooltip' }"
  39. :title="t('action.copy')"
  40. :svg="copyIcon"
  41. @click.native="copyResponse"
  42. />
  43. </div>
  44. </div>
  45. <div v-show="!previewEnabled" ref="htmlResponse"></div>
  46. <iframe
  47. v-show="previewEnabled"
  48. ref="previewFrame"
  49. class="covers-response"
  50. src="about:blank"
  51. loading="lazy"
  52. ></iframe>
  53. </div>
  54. </template>
  55. <script setup lang="ts">
  56. import { computed, ref, reactive } from "@nuxtjs/composition-api"
  57. import { useCodemirror } from "~/helpers/editor/codemirror"
  58. import { copyToClipboard } from "~/helpers/utils/clipboard"
  59. import { HoppRESTResponse } from "~/helpers/types/HoppRESTResponse"
  60. import { useI18n, useToast } from "~/helpers/utils/composables"
  61. const t = useI18n()
  62. const props = defineProps<{
  63. response: HoppRESTResponse
  64. }>()
  65. const toast = useToast()
  66. const responseBodyText = computed(() => {
  67. if (
  68. props.response.type === "loading" ||
  69. props.response.type === "network_fail"
  70. )
  71. return ""
  72. if (typeof props.response.body === "string") return props.response.body
  73. else {
  74. const res = new TextDecoder("utf-8").decode(props.response.body)
  75. // HACK: Temporary trailing null character issue from the extension fix
  76. return res.replace(/\0+$/, "")
  77. }
  78. })
  79. const downloadIcon = ref("download")
  80. const copyIcon = ref("copy")
  81. const previewEnabled = ref(false)
  82. const previewFrame = ref<any | null>(null)
  83. const url = ref("")
  84. const htmlResponse = ref<any | null>(null)
  85. const linewrapEnabled = ref(true)
  86. useCodemirror(
  87. htmlResponse,
  88. responseBodyText,
  89. reactive({
  90. extendedEditorConfig: {
  91. mode: "htmlmixed",
  92. readOnly: true,
  93. lineWrapping: linewrapEnabled,
  94. },
  95. linter: null,
  96. completer: null,
  97. })
  98. )
  99. const downloadResponse = () => {
  100. const dataToWrite = responseBodyText.value
  101. const file = new Blob([dataToWrite], { type: "text/html" })
  102. const a = document.createElement("a")
  103. const url = URL.createObjectURL(file)
  104. a.href = url
  105. // TODO get uri from meta
  106. a.download = `${url.split("/").pop().split("#")[0].split("?")[0]}`
  107. document.body.appendChild(a)
  108. a.click()
  109. downloadIcon.value = "check"
  110. toast.success(`${t("state.download_started")}`)
  111. setTimeout(() => {
  112. document.body.removeChild(a)
  113. URL.revokeObjectURL(url)
  114. downloadIcon.value = "download"
  115. }, 1000)
  116. }
  117. const copyResponse = () => {
  118. copyToClipboard(responseBodyText.value)
  119. copyIcon.value = "check"
  120. toast.success(`${t("state.copied_to_clipboard")}`)
  121. setTimeout(() => (copyIcon.value = "copy"), 1000)
  122. }
  123. const togglePreview = () => {
  124. previewEnabled.value = !previewEnabled.value
  125. if (previewEnabled.value) {
  126. if (previewFrame.value.getAttribute("data-previewing-url") === url.value)
  127. return
  128. // Use DOMParser to parse document HTML.
  129. const previewDocument = new DOMParser().parseFromString(
  130. responseBodyText.value,
  131. "text/html"
  132. )
  133. // Inject <base href="..."> tag to head, to fix relative CSS/HTML paths.
  134. previewDocument.head.innerHTML =
  135. `<base href="${url.value}">` + previewDocument.head.innerHTML
  136. // Finally, set the iframe source to the resulting HTML.
  137. previewFrame.value.srcdoc = previewDocument.documentElement.outerHTML
  138. previewFrame.value.setAttribute("data-previewing-url", url.value)
  139. }
  140. }
  141. </script>
  142. <style lang="scss" scoped>
  143. .covers-response {
  144. @apply bg-white;
  145. @apply h-full;
  146. @apply w-full;
  147. @apply border;
  148. @apply border-dividerLight;
  149. @apply z-5;
  150. }
  151. </style>