Added XML lens

This commit is contained in:
Andrew Bastin
2020-06-23 03:20:51 -04:00
parent 0fc901bd24
commit 4da44131eb
4 changed files with 136 additions and 1 deletions

View File

@@ -26,6 +26,7 @@ export default {
json: () => import("../lenses/renderers/JSONLensRenderer"),
imageres: () => import("../lenses/renderers/ImageLensRenderer"),
htmlres: () => import("../lenses/renderers/HTMLLensRenderer"),
xmlres: () => import("../lenses/renderers/XMLLensRenderer"),
},
props: {
response: {},

View File

@@ -0,0 +1,126 @@
<template>
<ul>
<li>
<div class="flex-wrap">
<label for="body">{{ $t("response") }}</label>
<div>
<button
class="icon"
@click="ToggleExpandResponse"
ref="ToggleExpandResponse"
v-if="response.body"
v-tooltip="{
content: !expandResponse ? $t('expand_response') : $t('collapse_response'),
}"
>
<i class="material-icons">
{{ !expandResponse ? "unfold_more" : "unfold_less" }}
</i>
</button>
<button
class="icon"
@click="downloadResponse"
ref="downloadResponse"
v-if="response.body"
v-tooltip="$t('download_file')"
>
<i class="material-icons">save_alt</i>
</button>
<button
class="icon"
@click="copyResponse"
ref="copyResponse"
v-if="response.body"
v-tooltip="$t('copy_response')"
>
<i class="material-icons">content_copy</i>
</button>
</div>
</div>
<div id="response-details-wrapper">
<Editor
:value="responseBodyText"
:lang="'xml'"
:options="{
maxLines: responseBodyMaxLines,
minLines: '16',
fontSize: '16px',
autoScrollEditorIntoView: true,
readOnly: true,
showPrintMargin: false,
useWorker: false,
}"
/>
</div>
</li>
</ul>
</template>
<script>
import AceEditor from "../../ui/ace-editor"
export default {
components: {
Editor: AceEditor,
},
props: {
response: {},
},
data() {
return {
expandResponse: false,
responseBodyMaxLines: 16,
doneButton: '<i class="material-icons">done</i>',
downloadButton: '<i class="material-icons">save_alt</i>',
copyButton: '<i class="material-icons">content_copy</i>',
}
},
computed: {
responseBodyText() {
return new TextDecoder("utf-8").decode(this.response.body)
},
responseType() {
return (this.response.headers["content-type"] || "").split(";")[0].toLowerCase()
},
},
methods: {
ToggleExpandResponse() {
this.expandResponse = !this.expandResponse
this.responseBodyMaxLines = this.responseBodyMaxLines == Infinity ? 16 : Infinity
},
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 = `response on ${Date()}`.replace(/\./g, "[dot]")
document.body.appendChild(a)
a.click()
this.$refs.downloadResponse.innerHTML = this.doneButton
this.$toast.success(this.$t("download_started"), {
icon: "done",
})
setTimeout(() => {
document.body.removeChild(a)
window.URL.revokeObjectURL(url)
this.$refs.downloadResponse.innerHTML = this.downloadButton
}, 1000)
},
copyResponse() {
this.$refs.copyResponse.innerHTML = this.doneButton
this.$toast.success(this.$t("copied_to_clipboard"), {
icon: "done",
})
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)
setTimeout(() => (this.$refs.copyResponse.innerHTML = this.copyButton), 1000)
},
},
}
</script>

View File

@@ -2,8 +2,9 @@ import jsonLens from "./jsonLens"
import rawLens from "./rawLens"
import imageLens from "./imageLens"
import htmlLens from "./htmlLens"
import xmlLens from "./xmlLens"
const lenses = [jsonLens, imageLens, htmlLens, rawLens]
const lenses = [jsonLens, imageLens, htmlLens, xmlLens, rawLens]
function getSuitableLenses(response) {
const result = []

View File

@@ -0,0 +1,7 @@
const htmlLens = {
lensName: "XML",
supportedContentTypes: ["application/xml", "image/svg+xml", "text/xml", "application/rss+xml"],
renderer: "xmlres",
}
export default htmlLens