2018-07-26 07:18:35 +02:00
|
|
|
// @flow
|
|
|
|
|
2019-04-03 07:56:58 +02:00
|
|
|
import React, { Suspense } from 'react';
|
2018-07-26 07:18:35 +02:00
|
|
|
import LoadingScreen from 'component/common/loading-screen';
|
|
|
|
import MarkdownPreview from 'component/common/markdown-preview';
|
2019-11-07 20:39:22 +01:00
|
|
|
import CodeViewer from 'component/viewers/codeViewer';
|
2019-03-27 05:40:02 +01:00
|
|
|
|
2018-07-26 07:18:35 +02:00
|
|
|
type Props = {
|
2018-07-28 03:42:35 +02:00
|
|
|
theme: string,
|
2018-07-26 07:18:35 +02:00
|
|
|
source: {
|
2018-10-15 02:27:09 +02:00
|
|
|
stream: string => any,
|
2018-07-26 07:18:35 +02:00
|
|
|
fileType: string,
|
2018-08-02 02:53:38 +02:00
|
|
|
contentType: string,
|
2018-07-26 07:18:35 +02:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2018-10-15 02:27:09 +02:00
|
|
|
type State = {
|
|
|
|
error: boolean,
|
|
|
|
loading: boolean,
|
|
|
|
content: ?string,
|
|
|
|
};
|
|
|
|
|
|
|
|
class DocumentViewer extends React.PureComponent<Props, State> {
|
|
|
|
constructor(props: Props) {
|
2018-07-26 07:18:35 +02:00
|
|
|
super(props);
|
|
|
|
this.state = {
|
2018-10-15 02:27:09 +02:00
|
|
|
error: false,
|
2018-07-26 07:18:35 +02:00
|
|
|
loading: true,
|
2018-10-15 02:27:09 +02:00
|
|
|
content: null,
|
2018-07-26 07:18:35 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
|
|
|
const { source } = this.props;
|
|
|
|
|
2019-03-21 14:59:31 +01:00
|
|
|
if (source && source.stream) {
|
|
|
|
const stream = source.stream('utf8');
|
2018-07-26 07:18:35 +02:00
|
|
|
|
2019-03-21 14:59:31 +01:00
|
|
|
let data = '';
|
2018-07-26 07:18:35 +02:00
|
|
|
|
2019-03-21 14:59:31 +01:00
|
|
|
stream.on('data', chunk => {
|
|
|
|
data += chunk;
|
|
|
|
});
|
2018-07-26 07:18:35 +02:00
|
|
|
|
2019-03-21 14:59:31 +01:00
|
|
|
stream.on('end', () => {
|
|
|
|
this.setState({ content: data, loading: false });
|
|
|
|
});
|
|
|
|
|
|
|
|
stream.on('error', () => {
|
|
|
|
this.setState({ error: true, loading: false });
|
|
|
|
});
|
|
|
|
}
|
2018-07-26 07:18:35 +02:00
|
|
|
}
|
|
|
|
|
2018-10-15 02:27:09 +02:00
|
|
|
renderDocument() {
|
2018-07-27 02:24:00 +02:00
|
|
|
let viewer = null;
|
2018-10-15 02:27:09 +02:00
|
|
|
const { content } = this.state;
|
2018-07-28 03:42:35 +02:00
|
|
|
const { source, theme } = this.props;
|
|
|
|
const { fileType, contentType } = source;
|
2018-07-27 02:24:00 +02:00
|
|
|
const markdownType = ['md', 'markdown'];
|
2018-07-26 07:18:35 +02:00
|
|
|
|
2018-08-02 02:53:38 +02:00
|
|
|
if (markdownType.includes(fileType)) {
|
2018-07-27 02:24:00 +02:00
|
|
|
// Render markdown
|
|
|
|
viewer = <MarkdownPreview content={content} promptLinks />;
|
2018-08-02 02:53:38 +02:00
|
|
|
} else {
|
2018-07-27 02:24:00 +02:00
|
|
|
// Render plain text
|
2019-11-07 20:39:22 +01:00
|
|
|
viewer = <CodeViewer value={content} contentType={contentType} theme={theme} />;
|
2018-07-26 07:18:35 +02:00
|
|
|
}
|
2018-07-27 02:24:00 +02:00
|
|
|
|
|
|
|
return viewer;
|
2018-07-26 07:18:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
2018-08-02 02:53:38 +02:00
|
|
|
const { error, loading, content } = this.state;
|
|
|
|
const isReady = content && !error;
|
2018-07-26 07:18:35 +02:00
|
|
|
const loadingMessage = __('Rendering document.');
|
2018-07-28 03:54:06 +02:00
|
|
|
const errorMessage = __("Sorry, looks like we can't load the document.");
|
2018-07-26 07:18:35 +02:00
|
|
|
|
|
|
|
return (
|
2019-08-13 07:35:13 +02:00
|
|
|
<div className="file-render__viewer--document">
|
2018-07-26 07:18:35 +02:00
|
|
|
{loading && !error && <LoadingScreen status={loadingMessage} spinner />}
|
2018-08-02 02:53:38 +02:00
|
|
|
{error && <LoadingScreen status={errorMessage} spinner={!error} />}
|
2019-11-07 20:39:22 +01:00
|
|
|
{isReady && this.renderDocument()}
|
2018-07-26 07:18:35 +02:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default DocumentViewer;
|