From 15cbdd42a86e83cccf64df9f9f57a2f5e7cd3cae Mon Sep 17 00:00:00 2001 From: btzr-io Date: Sun, 14 Oct 2018 18:27:09 -0600 Subject: [PATCH 1/4] fix more flow errors for document viewers --- .../component/common/markdown-preview.jsx | 33 ++++++++++++------- src/renderer/component/fileRender/view.jsx | 4 +-- src/renderer/component/viewers/codeViewer.jsx | 6 ++-- .../component/viewers/documentViewer.jsx | 23 ++++++++----- src/renderer/component/viewers/docxViewer.jsx | 14 +++++--- src/renderer/component/viewers/pdfViewer.jsx | 4 ++- 6 files changed, 55 insertions(+), 29 deletions(-) diff --git a/src/renderer/component/common/markdown-preview.jsx b/src/renderer/component/common/markdown-preview.jsx index b00ae7025..78af2273d 100644 --- a/src/renderer/component/common/markdown-preview.jsx +++ b/src/renderer/component/common/markdown-preview.jsx @@ -6,25 +6,34 @@ import remarkEmoji from 'remark-emoji'; import ExternalLink from 'component/externalLink'; import defaultSchema from 'hast-util-sanitize/lib/github.json'; +type MarkdownProps = { + content: ?string, + promptLinks?: boolean, +}; + +type SimpleLinkProps = { + href?: string, + title?: string, + children?: React.Node, +}; + +const SimpleLink = (props: SimpleLinkProps) => { + const { href, title, children } = props; + return ( + + {children} + + ); +}; + // Use github sanitation schema const schema = { ...defaultSchema }; // Extend sanitation schema to support lbry protocol schema.protocols.href[3] = 'lbry'; -type MarkdownProps = { - content: string, - promptLinks?: boolean, -}; - -const SimpleLink = ({ href, title, children }) => ( - - {children} - -); - const MarkdownPreview = (props: MarkdownProps) => { - const { content, externalLinks, promptLinks } = props; + const { content, promptLinks } = props; const remarkOptions = { sanitize: schema, remarkReactComponents: { diff --git a/src/renderer/component/fileRender/view.jsx b/src/renderer/component/fileRender/view.jsx index b9901564e..80a3236a5 100644 --- a/src/renderer/component/fileRender/view.jsx +++ b/src/renderer/component/fileRender/view.jsx @@ -10,11 +10,11 @@ import HtmlViewer from 'component/viewers/htmlViewer'; type Props = { mediaType: string, source: { + stream: string => void, fileName: string, fileType: string, + contentType: string, downloadPath: string, - stream: opts => void, - blob: callback => void, }, currentTheme: string, }; diff --git a/src/renderer/component/viewers/codeViewer.jsx b/src/renderer/component/viewers/codeViewer.jsx index 1d5164468..af6ea8fc0 100644 --- a/src/renderer/component/viewers/codeViewer.jsx +++ b/src/renderer/component/viewers/codeViewer.jsx @@ -22,12 +22,12 @@ import 'codemirror/mode/javascript/javascript'; type Props = { theme: string, - value: string, + value: ?string, contentType: string, }; class CodeViewer extends React.PureComponent { - constructor(props) { + constructor(props: Props) { super(props); this.codeMirror = null; this.textarea = React.createRef(); @@ -54,6 +54,8 @@ class CodeViewer extends React.PureComponent { this.codeMirror.on('contextmenu', openSnippetMenu); } + codeMirror: any; + render() { const { value } = this.props; return ( diff --git a/src/renderer/component/viewers/documentViewer.jsx b/src/renderer/component/viewers/documentViewer.jsx index 608d34873..ed309236d 100644 --- a/src/renderer/component/viewers/documentViewer.jsx +++ b/src/renderer/component/viewers/documentViewer.jsx @@ -8,19 +8,25 @@ import MarkdownPreview from 'component/common/markdown-preview'; type Props = { theme: string, source: { - stream: opts => void, + stream: string => any, fileType: string, contentType: string, }, }; -class DocumentViewer extends React.PureComponent { - constructor(props) { +type State = { + error: boolean, + loading: boolean, + content: ?string, +}; + +class DocumentViewer extends React.PureComponent { + constructor(props: Props) { super(props); this.state = { - error: null, - content: null, + error: false, loading: true, + content: null, }; } @@ -38,13 +44,14 @@ class DocumentViewer extends React.PureComponent { this.setState({ content: data, loading: false }); }); - stream.on('error', error => { + stream.on('error', () => { this.setState({ error: true, loading: false }); }); } - renderDocument(content = null) { + renderDocument() { let viewer = null; + const { content } = this.state; const { source, theme } = this.props; const { fileType, contentType } = source; const markdownType = ['md', 'markdown']; @@ -70,7 +77,7 @@ class DocumentViewer extends React.PureComponent {
{loading && !error && } {error && } - {isReady && this.renderDocument(content)} + {isReady && this.renderDocument()}
); } diff --git a/src/renderer/component/viewers/docxViewer.jsx b/src/renderer/component/viewers/docxViewer.jsx index 9959678c5..790269fe6 100644 --- a/src/renderer/component/viewers/docxViewer.jsx +++ b/src/renderer/component/viewers/docxViewer.jsx @@ -10,11 +10,17 @@ type Props = { source: string, }; -class DocxViewer extends React.PureComponent { - constructor(props) { +type State = { + error: boolean, + loading: boolean, + content: ?string, +}; + +class DocxViewer extends React.PureComponent { + constructor(props: Props) { super(props); this.state = { - error: null, + error: false, content: null, loading: true, }; @@ -46,7 +52,7 @@ class DocxViewer extends React.PureComponent { const markdown = breakdance.render(result.value); this.setState({ content: markdown, loading: false }); }) - .catch(error => { + .catch(() => { this.setState({ error: true, loading: false }); }) .done(); diff --git a/src/renderer/component/viewers/pdfViewer.jsx b/src/renderer/component/viewers/pdfViewer.jsx index ce0a2095a..4f15ec315 100644 --- a/src/renderer/component/viewers/pdfViewer.jsx +++ b/src/renderer/component/viewers/pdfViewer.jsx @@ -7,11 +7,13 @@ type Props = { }; class PdfViewer extends React.PureComponent { - constructor(props) { + constructor(props: Props) { super(props); this.viewer = React.createRef(); } + viewer: { current: any }; + render() { const { source } = this.props; return ( -- 2.45.2 From 46dc577d10f0c9c29baa6e16b5a271f6a21d1387 Mon Sep 17 00:00:00 2001 From: btzr-io Date: Sun, 14 Oct 2018 19:42:01 -0600 Subject: [PATCH 2/4] fix some flow errors of the three-viewer component --- .../component/viewers/threeViewer/index.jsx | 108 ++++++++++++------ 1 file changed, 73 insertions(+), 35 deletions(-) diff --git a/src/renderer/component/viewers/threeViewer/index.jsx b/src/renderer/component/viewers/threeViewer/index.jsx index 0f4afb433..18e13d523 100644 --- a/src/renderer/component/viewers/threeViewer/index.jsx +++ b/src/renderer/component/viewers/threeViewer/index.jsx @@ -12,6 +12,13 @@ import ThreeScene from './internal/scene'; import ThreeLoader from './internal/loader'; import ThreeRenderer from './internal/renderer'; +type viewerTheme = { + gridColor: string, + groundColor: string, + backgroundColor: string, + centerLineColor: string, +}; + type Props = { theme: string, source: { @@ -32,7 +39,7 @@ class ThreeViewer extends React.PureComponent { else reject(); }); - static createOrbitControls(camera, canvas) { + static createOrbitControls(camera: any, canvas: any) { const controls = new THREE.OrbitControls(camera, canvas); // Controls configuration controls.enableDamping = true; @@ -46,7 +53,7 @@ class ThreeViewer extends React.PureComponent { return controls; } - static fitMeshToCamera(group) { + static fitMeshToCamera(group: any) { const max = { x: 0, y: 0, z: 0 }; const min = { x: 0, y: 0, z: 0 }; @@ -82,18 +89,6 @@ class ThreeViewer extends React.PureComponent { See: https://github.com/mrdoob/three.js/blob/dev/docs/scenes/js/material.js#L195 */ - static updateMaterial(material, geometry) { - material.vertexColors = +material.vertexColors; // Ensure number - material.side = +material.side; // Ensure number - material.needsUpdate = true; - // If Geometry needs update - if (geometry) { - geometry.verticesNeedUpdate = true; - geometry.normalsNeedUpdate = true; - geometry.colorsNeedUpdate = true; - } - } - constructor(props: Props) { super(props); const { theme } = this.props; @@ -124,6 +119,17 @@ class ThreeViewer extends React.PureComponent { isReady: false, isLoading: false, }; + // Internal objects + this.gui = null; + this.grid = null; + this.mesh = null; + this.camera = null; + this.frameID = null; + this.renderer = null; + this.material = null; + this.geometry = null; + this.targetCenter = null; + this.bufferGeometry = null; } componentDidMount() { @@ -187,7 +193,37 @@ class ThreeViewer extends React.PureComponent { } } - transformGroup(group) { + // Define component types + theme: viewerTheme; + themes: { dark: viewerTheme, light: viewerTheme }; + materialColor: string; + // Too complex to add a custom type + // Posible solution: https://www.npmjs.com/package/@types/three + gui: any; + grid: any; + mesh: any; + scene: any; + camera: any; + frameID: any; + controls: any; + material: any; + geometry: any; + targetCenter: any; + bufferGeometry: any; + + updateMaterial(material: any, geometry: any) { + this.material.vertexColors = +material.vertexColors; // Ensure number + this.material.side = +material.side; // Ensure number + this.material.needsUpdate = true; + // If Geometry needs update + if (geometry) { + this.geometry.verticesNeedUpdate = true; + this.geometry.normalsNeedUpdate = true; + this.geometry.colorsNeedUpdate = true; + } + } + + transformGroup(group: any) { ThreeViewer.fitMeshToCamera(group); if (!this.targetCenter) { @@ -203,20 +239,19 @@ class ThreeViewer extends React.PureComponent { const config = { color: this.materialColor, - }; - - config.reset = () => { - // Reset material color - config.color = this.materialColor; - // Reset material - this.material.color.set(config.color); - this.material.flatShading = true; - this.material.shininess = 30; - this.material.wireframe = false; - // Reset autoRotate - this.controls.autoRotate = false; - // Reset camera - this.restoreCamera(); + reset: () => { + // Reset material color + config.color = this.materialColor; + // Reset material + this.material.color.set(config.color); + this.material.flatShading = true; + this.material.shininess = 30; + this.material.wireframe = false; + // Reset autoRotate + this.controls.autoRotate = false; + // Reset camera + this.restoreCamera(); + }, }; const materialFolder = this.gui.addFolder('Material'); @@ -240,7 +275,7 @@ class ThreeViewer extends React.PureComponent { .add(this.material, 'flatShading') .name('FlatShading') .onChange(() => { - ThreeViewer.updateMaterial(this.material); + this.updateMaterial(this.material); }) .listen(); @@ -262,7 +297,7 @@ class ThreeViewer extends React.PureComponent { } } - createGeometry(data) { + createGeometry(data: any) { this.bufferGeometry = data; this.bufferGeometry.computeBoundingBox(); this.bufferGeometry.center(); @@ -308,7 +343,7 @@ class ThreeViewer extends React.PureComponent { this.renderer.setSize(width, height); }; - updateControlsTarget(point) { + updateControlsTarget(point: { x: number, y: number, z: number }) { this.controls.target.fromArray([point.x, point.y, point.z]); this.controls.update(); } @@ -319,7 +354,10 @@ class ThreeViewer extends React.PureComponent { this.updateControlsTarget(this.targetCenter); } - renderStl(data) { + // Flow requested to add it here + renderer: any; + + renderStl(data: any) { this.createGeometry(data); this.mesh = new THREE.Mesh(this.geometry, this.material); this.mesh.name = 'model'; @@ -327,7 +365,7 @@ class ThreeViewer extends React.PureComponent { this.transformGroup(this.mesh); } - renderObj(event) { + renderObj(event: any) { const mesh = event.detail.loaderRootNode; this.mesh = new THREE.Group(); this.mesh.name = 'model'; @@ -352,7 +390,7 @@ class ThreeViewer extends React.PureComponent { this.transformGroup(this.mesh); } - renderModel(fileType, parsedData) { + renderModel(fileType: string, parsedData: any) { const renderTypes = { stl: data => this.renderStl(data), obj: data => this.renderObj(data), -- 2.45.2 From 2dbb7717dadfc99be190d7720322127ef49d91b9 Mon Sep 17 00:00:00 2001 From: btzr-io Date: Mon, 15 Oct 2018 00:26:46 -0600 Subject: [PATCH 3/4] fix flow type errors of react refs --- package.json | 1 + src/renderer/component/viewers/codeViewer.jsx | 8 ++--- src/renderer/component/viewers/pdfViewer.jsx | 11 ++----- .../component/viewers/threeViewer/index.jsx | 32 +++++++++++-------- yarn.lock | 4 +++ 5 files changed, 29 insertions(+), 27 deletions(-) diff --git a/package.json b/package.json index b10cec32d..0fe9622c2 100644 --- a/package.json +++ b/package.json @@ -32,6 +32,7 @@ "postinstall": "electron-builder install-app-deps && node build/downloadDaemon.js" }, "dependencies": { + "@types/three": "^0.93.1", "bluebird": "^3.5.1", "breakdance": "^3.0.1", "classnames": "^2.2.5", diff --git a/src/renderer/component/viewers/codeViewer.jsx b/src/renderer/component/viewers/codeViewer.jsx index af6ea8fc0..d10f3135c 100644 --- a/src/renderer/component/viewers/codeViewer.jsx +++ b/src/renderer/component/viewers/codeViewer.jsx @@ -1,6 +1,6 @@ // @flow -import React from 'react'; +import * as React from 'react'; import CodeMirror from 'codemirror/lib/codemirror'; import { openSnippetMenu, stopContextMenu } from 'util/contextMenu'; @@ -30,13 +30,12 @@ class CodeViewer extends React.PureComponent { constructor(props: Props) { super(props); this.codeMirror = null; - this.textarea = React.createRef(); } componentDidMount() { const { theme, contentType } = this.props; // Init CodeMirror - this.codeMirror = CodeMirror.fromTextArea(this.textarea.current, { + this.codeMirror = CodeMirror.fromTextArea(this.textarea, { // Auto detect syntax with file contentType mode: contentType, // Adaptive theme @@ -54,13 +53,14 @@ class CodeViewer extends React.PureComponent { this.codeMirror.on('contextmenu', openSnippetMenu); } + textarea: ?HTMLTextAreaElement; codeMirror: any; render() { const { value } = this.props; return (
-