From 1c50b71f14a2098c46859224917096bfe456ac87 Mon Sep 17 00:00:00 2001 From: btzr-io Date: Wed, 25 Jul 2018 23:18:35 -0600 Subject: [PATCH 01/15] add document viewer - fix markdown viewer - fix txt viewer --- src/renderer/component/fileRender/view.jsx | 2 + .../component/viewers/documentViewer.jsx | 80 +++++++++++++++++++ 2 files changed, 82 insertions(+) create mode 100644 src/renderer/component/viewers/documentViewer.jsx diff --git a/src/renderer/component/fileRender/view.jsx b/src/renderer/component/fileRender/view.jsx index 79aca834a..49717bec7 100644 --- a/src/renderer/component/fileRender/view.jsx +++ b/src/renderer/component/fileRender/view.jsx @@ -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 { // Supported mediaTypes const mediaTypes = { '3D-file': , + document: , // Add routes to viewer... }; diff --git a/src/renderer/component/viewers/documentViewer.jsx b/src/renderer/component/viewers/documentViewer.jsx new file mode 100644 index 000000000..6d19383fd --- /dev/null +++ b/src/renderer/component/viewers/documentViewer.jsx @@ -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 { + 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 => , + }; + + if (docs[fileType]) { + // Use custom parser + return docs[fileType](data); + } + // Render plain text + this.setState({ loading: false }); + return