HTMLLensRenderer.vue 4.8 KB

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