-
Notifications
You must be signed in to change notification settings - Fork 346
/
Copy pathfile-viewer.jsx
98 lines (90 loc) · 2.25 KB
/
file-viewer.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// Copyright (c) 2017 PlanGrid, Inc.
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import 'styles/main.scss';
import withFetching from './fetch-wrapper';
import {
CsvViewer,
DocxViewer,
VideoViewer,
XlsxViewer,
XBimViewer,
PDFViewer,
UnsupportedViewer,
PhotoViewerWrapper,
AudioViewer,
} from './drivers';
class FileViewer extends Component {
constructor(props) {
super(props);
this.uniqIdentifier = Math.floor(Math.random() * 100000);
this.state = {
loading: true,
};
}
getDriver(commonProps) {
switch (this.props.fileType) {
case 'csv': {
return withFetching(CsvViewer, commonProps);
}
case 'xlsx': {
const newProps = Object.assign({}, commonProps, { responseType: 'arraybuffer' });
return withFetching(XlsxViewer, newProps);
}
case 'jpg':
case 'jpeg':
case 'gif':
case 'bmp':
case 'png': {
return PhotoViewerWrapper;
}
case 'pdf': {
return PDFViewer;
}
case 'docx': {
return DocxViewer;
}
case 'mp3': {
return AudioViewer;
}
case 'webm':
case 'mp4': {
return VideoViewer;
}
case 'wexbim': {
return XBimViewer;
}
default: {
return UnsupportedViewer;
}
}
}
render() {
const commonProps = Object.assign({}, this.props, { uniqIdentifier: this.uniqIdentifier });
const Driver = this.getDriver(commonProps);
const container = document.getElementById('pg-viewer');
const height = container ? container.clientHeight : 0;
const width = container ? container.clientWidth : 0;
return (
<div className="pg-viewer-wrapper">
<div className="pg-viewer" id="pg-viewer">
<Driver {...commonProps} width={width} height={height} />
</div>
</div>
);
}
}
FileViewer.propTypes = {
fileType: PropTypes.string.isRequired,
filePath: PropTypes.string.isRequired,
onError: PropTypes.func,
errorComponent: PropTypes.element,
unsupportedComponent: PropTypes.element,
};
FileViewer.defaultProps = {
onError: () => null,
errorComponent: null,
unsupportedComponent: null,
};
export default FileViewer;
module.exports = FileViewer;