2017-08-10 01:56:27 +00:00
|
|
|
import React, {PureComponent} from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
2017-03-03 01:44:07 +00:00
|
|
|
import autobind from 'autobind-decorator';
|
2017-02-27 21:00:13 +00:00
|
|
|
import contextMenu from 'electron-context-menu';
|
2016-11-29 20:55:31 +00:00
|
|
|
|
2017-03-03 01:44:07 +00:00
|
|
|
@autobind
|
2016-11-29 20:55:31 +00:00
|
|
|
class ResponseWebview extends PureComponent {
|
2017-03-03 01:44:07 +00:00
|
|
|
_handleSetWebviewRef (n) {
|
2017-02-27 21:00:13 +00:00
|
|
|
this._webview = n;
|
2017-05-25 16:31:33 +00:00
|
|
|
if (n) {
|
|
|
|
contextMenu({window: this._webview});
|
|
|
|
}
|
2017-03-03 01:44:07 +00:00
|
|
|
}
|
2017-02-27 21:00:13 +00:00
|
|
|
|
2017-03-03 01:44:07 +00:00
|
|
|
_handleDOMReady () {
|
2016-12-01 01:50:59 +00:00
|
|
|
this._webview.removeEventListener('dom-ready', this._handleDOMReady);
|
|
|
|
this._setBody();
|
2017-03-03 01:44:07 +00:00
|
|
|
}
|
2016-04-29 01:00:12 +00:00
|
|
|
|
|
|
|
_setBody () {
|
2016-06-19 07:21:43 +00:00
|
|
|
const {body, contentType, url} = this.props;
|
|
|
|
const newBody = body.replace('<head>', `<head><base href="${url}">`);
|
2016-08-15 17:04:36 +00:00
|
|
|
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 01:00:12 +00:00
|
|
|
componentDidMount () {
|
2016-12-01 01:50:59 +00:00
|
|
|
this._webview.addEventListener('dom-ready', this._handleDOMReady);
|
2016-04-29 01:00:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
render () {
|
|
|
|
return (
|
2016-11-29 20:55:31 +00:00
|
|
|
<webview ref={this._handleSetWebviewRef} src="about:blank"></webview>
|
2016-04-29 01:00:12 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-15 17:04:36 +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
|
|
|
};
|
|
|
|
|
2016-08-15 17:04:36 +00:00
|
|
|
export default ResponseWebview;
|