insomnia/app/components/viewers/ResponseWebview.js

50 lines
1.1 KiB
JavaScript
Raw Normal View History

import React, {Component, PropTypes} from 'react';
2016-04-29 01:00:12 +00:00
class ResponseWebview extends Component {
2016-04-29 01:00:12 +00:00
_setBody () {
2016-06-19 07:21:43 +00:00
const {body, contentType, url} = this.props;
2016-04-29 01:00:12 +00:00
2016-06-19 07:21:43 +00:00
const newBody = body.replace('<head>', `<head><base href="${url}">`);
this._webview.loadURL(`data:${contentType},${encodeURIComponent(newBody)}`);
2016-04-29 01:00:12 +00:00
}
componentDidUpdate () {
this._setBody();
}
2016-05-01 19:56:30 +00:00
2016-04-29 05:58:37 +00:00
shouldComponentUpdate (nextProps) {
2016-04-30 05:01:57 +00:00
for (let key in nextProps) {
if (nextProps.hasOwnProperty(key)) {
if (nextProps[key] !== this.props[key]) {
return true;
}
}
}
return false;
2016-04-29 05:58:37 +00:00
}
2016-04-29 01:00:12 +00:00
componentDidMount () {
const cb = () => {
this._webview.removeEventListener('dom-ready', cb);
2016-04-29 01:00:12 +00:00
this._setBody();
};
2016-05-01 19:56:30 +00:00
this._webview.addEventListener('dom-ready', cb);
2016-04-29 01:00:12 +00:00
}
render () {
return (
<webview ref={node => this._webview = node} src="about:blank"></webview>
2016-04-29 01:00:12 +00:00
);
}
}
ResponseWebview.propTypes = {
2016-04-30 05:01:57 +00:00
body: PropTypes.string.isRequired,
2016-06-19 07:21:43 +00:00
contentType: PropTypes.string.isRequired,
url: PropTypes.string.isRequired
2016-04-29 01:00:12 +00:00
};
export default ResponseWebview;