Skip to content

Commit 8055129

Browse files
heldersepushockey
authored andcommitted
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
1 parent 7049de6 commit 8055129

File tree

2 files changed

+9
-7
lines changed

2 files changed

+9
-7
lines changed

src/core/components/highlight-code.jsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ export default class HighlightCode extends Component {
77
static propTypes = {
88
value: PropTypes.string.isRequired,
99
className: PropTypes.string,
10-
downloadable: PropTypes.bool
10+
downloadable: PropTypes.bool,
11+
fileName: PropTypes.string
1112
}
1213

1314
componentDidMount() {
@@ -23,7 +24,7 @@ export default class HighlightCode extends Component {
2324
}
2425

2526
downloadText = () => {
26-
saveAs(this.props.value, "response.txt")
27+
saveAs(this.props.value, this.props.fileName || "response.txt")
2728
}
2829

2930
preventYScrollingBeyondElement = (e) => {

src/core/components/response-body.jsx

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ export default class ResponseBody extends React.PureComponent {
5252
let { content, contentType, url, headers={}, getComponent } = this.props
5353
const { parsedContent } = this.state
5454
const HighlightCode = getComponent("highlightCode")
55+
const downloadName = "response_" + new Date().getTime()
5556
let body, bodyEl
5657
url = url || ""
5758

@@ -100,19 +101,19 @@ export default class ResponseBody extends React.PureComponent {
100101
body = "can't parse JSON. Raw result:\n\n" + content
101102
}
102103

103-
bodyEl = <HighlightCode downloadable value={ body } />
104+
bodyEl = <HighlightCode downloadable fileName={`${downloadName}.json`} value={ body } />
104105

105106
// XML
106107
} else if (/xml/i.test(contentType)) {
107108
body = formatXml(content, {
108109
textNodesOnSameLine: true,
109110
indentor: " "
110111
})
111-
bodyEl = <HighlightCode downloadable value={ body } />
112+
bodyEl = <HighlightCode downloadable fileName={`${downloadName}.xml`} value={ body } />
112113

113114
// HTML or Plain Text
114115
} else if (lowerCase(contentType) === "text/html" || /text\/plain/.test(contentType)) {
115-
bodyEl = <HighlightCode downloadable value={ content } />
116+
bodyEl = <HighlightCode downloadable fileName={`${downloadName}.html`} value={ content } />
116117

117118
// Image
118119
} else if (/^image\//i.test(contentType)) {
@@ -126,7 +127,7 @@ export default class ResponseBody extends React.PureComponent {
126127
} else if (/^audio\//i.test(contentType)) {
127128
bodyEl = <pre><audio controls><source src={ url } type={ contentType } /></audio></pre>
128129
} else if (typeof content === "string") {
129-
bodyEl = <HighlightCode downloadable value={ content } />
130+
bodyEl = <HighlightCode downloadable fileName={`${downloadName}.txt`} value={ content } />
130131
} else if ( content.size > 0 ) {
131132
// We don't know the contentType, but there was some content returned
132133
if(parsedContent) {
@@ -136,7 +137,7 @@ export default class ResponseBody extends React.PureComponent {
136137
<p className="i">
137138
Unrecognized response type; displaying content as text.
138139
</p>
139-
<HighlightCode downloadable value={ parsedContent } />
140+
<HighlightCode downloadable fileName={`${downloadName}.txt`} value={ parsedContent } />
140141
</div>
141142

142143
} else {

0 commit comments

Comments
 (0)