|
1 |
| -import React, {Component, PropTypes} from 'react'; |
| 1 | +import React from 'react'; |
2 | 2 |
|
3 | 3 | if (typeof window !== 'undefined') {
|
4 | 4 | require('pdfjs-dist/build/pdf.combined');
|
5 | 5 | }
|
6 | 6 |
|
7 |
| -class Pdf extends Component { |
| 7 | +class Pdf extends React.Component { |
8 | 8 | constructor(props) {
|
9 | 9 | super(props);
|
10 | 10 | this.onDocumentComplete = this.onDocumentComplete.bind(this);
|
11 | 11 | this.onPageComplete = this.onPageComplete.bind(this);
|
12 | 12 | }
|
13 | 13 |
|
14 |
| - state = {}; |
15 |
| - |
16 | 14 | componentDidMount() {
|
17 | 15 | this.loadPDFDocument(this.props);
|
18 | 16 | this.renderPdf();
|
19 | 17 | }
|
20 | 18 |
|
21 | 19 | componentWillReceiveProps(newProps) {
|
22 |
| - const {pdf} = this.state; |
| 20 | + const { pdf } = this.state; |
23 | 21 | if ((newProps.file && newProps.file !== this.props.file) ||
|
24 | 22 | (newProps.content && newProps.content !== this.props.content)) {
|
25 | 23 | this.loadPDFDocument(newProps);
|
26 | 24 | }
|
27 | 25 |
|
28 | 26 | if (pdf && ((newProps.page && newProps.page !== this.props.page) ||
|
29 | 27 | (newProps.scale && newProps.scale !== this.props.scale))) {
|
30 |
| - this.setState({page: null}); |
| 28 | + this.setState({ page: null }); |
31 | 29 | pdf.getPage(newProps.page).then(this.onPageComplete);
|
32 | 30 | }
|
33 | 31 | }
|
34 | 32 |
|
35 | 33 | onDocumentComplete(pdf) {
|
36 |
| - this.setState({pdf: pdf}); |
37 |
| - const {onDocumentComplete} = this.props; |
| 34 | + this.setState({ pdf }); |
| 35 | + const { onDocumentComplete } = this.props; |
38 | 36 | if (typeof onDocumentComplete === 'function') {
|
39 | 37 | onDocumentComplete(pdf.numPages);
|
40 | 38 | }
|
41 | 39 | pdf.getPage(this.props.page).then(this.onPageComplete);
|
42 | 40 | }
|
43 | 41 |
|
44 | 42 | onPageComplete(page) {
|
45 |
| - this.setState({page: page}); |
| 43 | + this.setState({ page }); |
46 | 44 | this.renderPdf();
|
47 |
| - const {onPageComplete} = this.props; |
| 45 | + const { onPageComplete } = this.props; |
48 | 46 | if (typeof onPageComplete === 'function') {
|
49 | 47 | onPageComplete(page.pageIndex + 1);
|
50 | 48 | }
|
@@ -79,37 +77,37 @@ class Pdf extends Component {
|
79 | 77 | }
|
80 | 78 |
|
81 | 79 | renderPdf() {
|
82 |
| - const {page} = this.state; |
| 80 | + const { page } = this.state; |
83 | 81 | if (page) {
|
84 |
| - let {canvas} = this.refs; |
| 82 | + let { canvas } = this.refs; |
85 | 83 | if (canvas.getDOMNode) { // compatible with react 0.13
|
86 | 84 | canvas = canvas.getDOMNode();
|
87 | 85 | }
|
88 | 86 | const canvasContext = canvas.getContext('2d');
|
89 |
| - const {scale} = this.props; |
| 87 | + const { scale } = this.props; |
90 | 88 | const viewport = page.getViewport(scale);
|
91 | 89 | canvas.height = viewport.height;
|
92 | 90 | canvas.width = viewport.width;
|
93 |
| - page.render({canvasContext, viewport}); |
| 91 | + page.render({ canvasContext, viewport }); |
94 | 92 | }
|
95 | 93 | }
|
96 | 94 |
|
97 | 95 | render() {
|
98 |
| - const {loading} = this.props; |
99 |
| - const {page} = this.state; |
100 |
| - return page ? <canvas ref="canvas"/> : loading || <div>Loading PDF...</div>; |
| 96 | + const { loading } = this.props; |
| 97 | + const { page } = this.state; |
| 98 | + return page ? <canvas ref="canvas" /> : loading || <div>Loading PDF...</div>; |
101 | 99 | }
|
102 | 100 | }
|
103 | 101 | Pdf.displayName = 'React-PDFjs';
|
104 | 102 | Pdf.propTypes = {
|
105 |
| - content: PropTypes.string, |
106 |
| - file: PropTypes.string, |
107 |
| - loading: PropTypes.any, |
108 |
| - page: PropTypes.number, |
109 |
| - scale: PropTypes.number, |
110 |
| - onDocumentComplete: PropTypes.func, |
111 |
| - onPageComplete: PropTypes.func |
| 103 | + content: React.PropTypes.string, |
| 104 | + file: React.PropTypes.string, |
| 105 | + loading: React.PropTypes.any, |
| 106 | + page: React.PropTypes.number, |
| 107 | + scale: React.PropTypes.number, |
| 108 | + onDocumentComplete: React.PropTypes.func, |
| 109 | + onPageComplete: React.PropTypes.func, |
112 | 110 | };
|
113 |
| -Pdf.defaultProps = {page: 1, scale: 1.0}; |
| 111 | +Pdf.defaultProps = { page: 1, scale: 1.0 }; |
114 | 112 |
|
115 | 113 | export default Pdf;
|
0 commit comments