add document viewer

- fix markdown viewer

- fix txt viewer
This commit is contained in:
btzr-io 2018-07-25 23:18:35 -06:00
parent 71bc347224
commit 1c50b71f14
2 changed files with 82 additions and 0 deletions

View file

@ -3,6 +3,7 @@ import React from 'react';
import LoadingScreen from 'component/common/loading-screen';
import PdfViewer from 'component/viewers/pdfViewer';
import ThreeViewer from 'component/viewers/threeViewer';
import DocumentViewer from 'component/viewers/documentViewer';
type Props = {
mediaType: string,
@ -22,6 +23,7 @@ class FileRender extends React.PureComponent<Props> {
// Supported mediaTypes
const mediaTypes = {
'3D-file': <ThreeViewer {...viewerProps} />,
document: <DocumentViewer {...viewerProps} />,
// Add routes to viewer...
};

View file

@ -0,0 +1,80 @@
// @flow
import React from 'react';
import fs from 'fs';
import LoadingScreen from 'component/common/loading-screen';
import MarkdownPreview from 'component/common/markdown-preview';
type Props = {
source: {
fileType: string,
filePath: string,
downloadPath: string,
},
};
class DocumentViewer extends React.PureComponent<Props> {
constructor(props) {
super(props);
this.state = {
error: null,
content: null,
loading: true,
};
}
componentDidMount() {
const { source } = this.props;
const stream = fs.createReadStream(source.downloadPath, 'utf8');
let data = '';
stream.on('data', chunk => {
data += chunk;
});
stream.on('end', () => {
this.setState({ content: data });
});
stream.on('error', error => {
this.setState({ error });
});
}
renderDocument(data) {
const { source } = this.props;
const { fileType } = source;
// Match supported documents
const docs = {
md: content => <MarkdownPreview content={content} promptLinks />,
};
if (docs[fileType]) {
// Use custom parser
return docs[fileType](data);
}
// Render plain text
this.setState({ loading: false });
return <textarea disabled="true" value={data} />;
}
render() {
const { content, error, loading } = this.state;
const loadingMessage = __('Rendering document.');
const errorMessage = __("Sorry looks like we can't load the document.");
return (
<div className="document-viewer file-render__viewer">
{loading && !error && <LoadingScreen status={loadingMessage} spinner />}
{error && <LoadingScreen status={errorMessage} spinner={false} />}
{content && (
<div className="document-viewer__content">{content && this.renderDocument(content)}</div>
)}
</div>
);
}
}
export default DocumentViewer;