Files
hoppscotch/components/lenses/renderers/RawLensRenderer.vue
2021-07-17 23:10:28 +05:30

123 lines
3.2 KiB
Vue

<template>
<div>
<div
class="
bg-primary
border-b border-dividerLight
flex flex-1
top-23
z-10
sticky
items-center
justify-between
"
>
<label for="body" class="font-semibold text-xs px-4">
{{ $t("response_body") }}
</label>
<div>
<ButtonSecondary
v-if="response.body && canDownloadResponse"
ref="downloadResponse"
v-tippy="{ theme: 'tooltip' }"
:title="$t('download_file')"
:icon="downloadIcon"
@click.native="downloadResponse"
/>
<ButtonSecondary
v-if="response.body"
ref="copyResponse"
v-tippy="{ theme: 'tooltip' }"
:title="$t('copy_response')"
:icon="copyIcon"
@click.native="copyResponse"
/>
</div>
</div>
<div id="response-details-wrapper">
<SmartAceEditor
:value="responseBodyText"
:lang="'plain_text'"
:options="{
maxLines: Infinity,
minLines: '16',
fontSize: '14px',
autoScrollEditorIntoView: true,
readOnly: true,
showPrintMargin: false,
useWorker: false,
}"
/>
</div>
</div>
</template>
<script>
import TextContentRendererMixin from "./mixins/TextContentRendererMixin"
import { isJSONContentType } from "~/helpers/utils/contenttypes"
export default {
mixins: [TextContentRendererMixin],
props: {
response: { type: Object, default: () => {} },
},
data() {
return {
downloadIcon: "save_alt",
copyIcon: "content_copy",
}
},
computed: {
responseType() {
return (this.response.headers["content-type"] || "")
.split(";")[0]
.toLowerCase()
},
canDownloadResponse() {
return (
this.response &&
this.response.headers &&
this.response.headers["content-type"] &&
isJSONContentType(this.response.headers["content-type"])
)
},
},
methods: {
downloadResponse() {
const dataToWrite = this.responseBodyText
const file = new Blob([dataToWrite], { type: this.responseType })
const a = document.createElement("a")
const url = URL.createObjectURL(file)
a.href = url
// TODO get uri from meta
a.download = `${url.split("/").pop().split("#")[0].split("?")[0]}`
document.body.appendChild(a)
a.click()
this.downloadIcon = "done"
this.$toast.success(this.$t("download_started"), {
icon: "done",
})
setTimeout(() => {
document.body.removeChild(a)
window.URL.revokeObjectURL(url)
this.downloadIcon = "save_alt"
}, 1000)
},
copyResponse() {
const aux = document.createElement("textarea")
const copy = this.responseBodyText
aux.innerText = copy
document.body.appendChild(aux)
aux.select()
document.execCommand("copy")
document.body.removeChild(aux)
this.copyIcon = "done"
this.$toast.success(this.$t("copied_to_clipboard"), {
icon: "done",
})
setTimeout(() => (this.copyIcon = "content_copy"), 1000)
},
},
}
</script>