Improve downloadable HighlightCode filename (#4508)

* Update highlight-code.jsx
* improve filename no more response.txt
* use new `fileName` prop for file names
* use template strings for `fileName` prop values
* fall back to old "response.txt" file name if none is provided
This commit is contained in:
Helder Sepulveda
2018-05-03 20:33:21 -04:00
committed by kyle
parent 7049de6201
commit 8055129dd2
2 changed files with 9 additions and 7 deletions

View File

@@ -7,7 +7,8 @@ export default class HighlightCode extends Component {
static propTypes = { static propTypes = {
value: PropTypes.string.isRequired, value: PropTypes.string.isRequired,
className: PropTypes.string, className: PropTypes.string,
downloadable: PropTypes.bool downloadable: PropTypes.bool,
fileName: PropTypes.string
} }
componentDidMount() { componentDidMount() {
@@ -23,7 +24,7 @@ export default class HighlightCode extends Component {
} }
downloadText = () => { downloadText = () => {
saveAs(this.props.value, "response.txt") saveAs(this.props.value, this.props.fileName || "response.txt")
} }
preventYScrollingBeyondElement = (e) => { preventYScrollingBeyondElement = (e) => {

View File

@@ -52,6 +52,7 @@ export default class ResponseBody extends React.PureComponent {
let { content, contentType, url, headers={}, getComponent } = this.props let { content, contentType, url, headers={}, getComponent } = this.props
const { parsedContent } = this.state const { parsedContent } = this.state
const HighlightCode = getComponent("highlightCode") const HighlightCode = getComponent("highlightCode")
const downloadName = "response_" + new Date().getTime()
let body, bodyEl let body, bodyEl
url = url || "" url = url || ""
@@ -100,7 +101,7 @@ export default class ResponseBody extends React.PureComponent {
body = "can't parse JSON. Raw result:\n\n" + content body = "can't parse JSON. Raw result:\n\n" + content
} }
bodyEl = <HighlightCode downloadable value={ body } /> bodyEl = <HighlightCode downloadable fileName={`${downloadName}.json`} value={ body } />
// XML // XML
} else if (/xml/i.test(contentType)) { } else if (/xml/i.test(contentType)) {
@@ -108,11 +109,11 @@ export default class ResponseBody extends React.PureComponent {
textNodesOnSameLine: true, textNodesOnSameLine: true,
indentor: " " indentor: " "
}) })
bodyEl = <HighlightCode downloadable value={ body } /> bodyEl = <HighlightCode downloadable fileName={`${downloadName}.xml`} value={ body } />
// HTML or Plain Text // HTML or Plain Text
} else if (lowerCase(contentType) === "text/html" || /text\/plain/.test(contentType)) { } else if (lowerCase(contentType) === "text/html" || /text\/plain/.test(contentType)) {
bodyEl = <HighlightCode downloadable value={ content } /> bodyEl = <HighlightCode downloadable fileName={`${downloadName}.html`} value={ content } />
// Image // Image
} else if (/^image\//i.test(contentType)) { } else if (/^image\//i.test(contentType)) {
@@ -126,7 +127,7 @@ export default class ResponseBody extends React.PureComponent {
} else if (/^audio\//i.test(contentType)) { } else if (/^audio\//i.test(contentType)) {
bodyEl = <pre><audio controls><source src={ url } type={ contentType } /></audio></pre> bodyEl = <pre><audio controls><source src={ url } type={ contentType } /></audio></pre>
} else if (typeof content === "string") { } else if (typeof content === "string") {
bodyEl = <HighlightCode downloadable value={ content } /> bodyEl = <HighlightCode downloadable fileName={`${downloadName}.txt`} value={ content } />
} else if ( content.size > 0 ) { } else if ( content.size > 0 ) {
// We don't know the contentType, but there was some content returned // We don't know the contentType, but there was some content returned
if(parsedContent) { if(parsedContent) {
@@ -136,7 +137,7 @@ export default class ResponseBody extends React.PureComponent {
<p className="i"> <p className="i">
Unrecognized response type; displaying content as text. Unrecognized response type; displaying content as text.
</p> </p>
<HighlightCode downloadable value={ parsedContent } /> <HighlightCode downloadable fileName={`${downloadName}.txt`} value={ parsedContent } />
</div> </div>
} else { } else {