2018-08-02 06:56:17 +02:00
|
|
|
// @flow
|
2020-01-19 05:08:40 +01:00
|
|
|
import * as React from 'react';
|
2018-11-21 22:20:55 +01:00
|
|
|
import { stopContextMenu } from 'util/context-menu';
|
2018-08-02 06:56:17 +02:00
|
|
|
|
|
|
|
type Props = {
|
|
|
|
source: string,
|
|
|
|
};
|
|
|
|
|
2020-01-19 05:08:40 +01:00
|
|
|
type State = {
|
|
|
|
loading: boolean,
|
|
|
|
};
|
|
|
|
|
|
|
|
class HtmlViewer extends React.PureComponent<Props, State> {
|
|
|
|
iframe: React.ElementRef<any>;
|
|
|
|
constructor(props: Props) {
|
|
|
|
super(props);
|
|
|
|
this.state = { loading: true };
|
|
|
|
this.iframe = React.createRef();
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
|
|
|
const resize = () => {
|
|
|
|
const { scrollHeight, scrollWidth } = this.iframe.current.contentDocument.body;
|
|
|
|
this.iframe.current.style.height = `${scrollHeight}px`;
|
|
|
|
this.iframe.current.style.width = `${scrollWidth}px`;
|
|
|
|
};
|
|
|
|
this.iframe.current.onload = () => {
|
|
|
|
this.setState({ loading: false });
|
|
|
|
resize();
|
|
|
|
};
|
|
|
|
this.iframe.current.resize = () => resize();
|
|
|
|
}
|
|
|
|
|
2018-08-02 06:56:17 +02:00
|
|
|
render() {
|
|
|
|
const { source } = this.props;
|
2020-01-19 05:08:40 +01:00
|
|
|
const { loading } = this.state;
|
2018-08-02 06:56:17 +02:00
|
|
|
return (
|
2020-04-14 01:48:11 +02:00
|
|
|
<div className="file-viewer file-viewer--html file-viewer--iframe" onContextMenu={stopContextMenu}>
|
2020-01-19 05:08:40 +01:00
|
|
|
{loading && <div className="placeholder--text-document" />}
|
|
|
|
<iframe ref={this.iframe} hidden={loading} sandbox="" title={__('File preview')} src={`file://${source}`} />
|
2018-08-02 06:56:17 +02:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default HtmlViewer;
|