-
Notifications
You must be signed in to change notification settings - Fork 340
/
pdf-viewer.jsx
169 lines (144 loc) · 4.46 KB
/
pdf-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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
// Copyright (c) 2017 PlanGrid, Inc.
import React from 'react';
import VisibilitySensor from 'react-visibility-sensor';
import { PDFJS } from 'pdfjs-dist/build/pdf.combined';
import 'pdfjs-dist/web/compatibility';
PDFJS.disableWorker = true;
const INCREASE_PERCENTAGE = 0.2;
const DEFAULT_SCALE = 1.1;
export class PDFPage extends React.Component {
constructor(props) {
super(props);
this.state = {};
this.onChange = this.onChange.bind(this);
}
componentDidMount() {
if (this.props.disableVisibilityCheck) this.fetchAndRenderPage();
}
componentDidUpdate(prevProps, prevState) {
if (this.props.disableVisibilityCheck) {
if (prevProps.zoom !== this.props.zoom) this.fetchAndRenderPage();
return;
}
// we want to render/re-render in two scenarias
// user scrolls to the pdf
// user zooms in
if (prevState.isVisible === this.state.isVisible && prevProps.zoom === this.props.zoom) return;
if (this.state.isVisible) this.fetchAndRenderPage();
}
onChange(isVisible) {
if (isVisible) this.setState({ isVisible });
}
fetchAndRenderPage() {
const { pdf, index } = this.props;
pdf.getPage(index).then(this.renderPage.bind(this));
}
renderPage(page) {
const { containerWidth, zoom } = this.props;
const calculatedScale = (containerWidth / page.getViewport(DEFAULT_SCALE).width);
const scale = calculatedScale > DEFAULT_SCALE ? DEFAULT_SCALE : calculatedScale;
const viewport = page.getViewport(scale + zoom);
const { width, height } = viewport;
const context = this.canvas.getContext('2d');
this.canvas.width = width;
this.canvas.height = height;
page.render({
canvasContext: context,
viewport,
});
}
render() {
const { index } = this.props;
return (
<div key={`page-${index}`} className="pdf-canvas">
{this.props.disableVisibilityCheck ? <canvas ref={node => this.canvas = node} width="670" height="870" /> : (
<VisibilitySensor onChange={this.onChange} partialVisibility >
<canvas ref={node => this.canvas = node} width="670" height="870" />
</VisibilitySensor>
)
}
</div>
);
}
}
export default class PDFDriver extends React.Component {
constructor(props) {
super(props);
this.state = {
pdf: null,
zoom: 0,
percent: 0,
};
this.increaseZoom = this.increaseZoom.bind(this);
this.reduceZoom = this.reduceZoom.bind(this);
this.resetZoom = this.resetZoom.bind(this);
}
componentDidMount() {
const { filePath } = this.props;
const containerWidth = this.container.offsetWidth;
PDFJS.getDocument(filePath, null, null, this.progressCallback.bind(this)).then((pdf) => {
this.setState({ pdf, containerWidth });
});
}
setZoom(zoom) {
this.setState({
zoom,
});
}
progressCallback(progress) {
const percent = ((progress.loaded / progress.total) * 100).toFixed();
this.setState({ percent });
}
reduceZoom() {
if (this.state.zoom === 0) return;
this.setZoom(this.state.zoom - 1);
}
increaseZoom() {
this.setZoom(this.state.zoom + 1);
}
resetZoom() {
this.setZoom(0);
}
renderPages() {
const { pdf, containerWidth, zoom } = this.state;
if (!pdf) return null;
const pages = Array.apply(null, { length: pdf.numPages });
return pages.map((v, i) => (
(<PDFPage
index={i + 1}
pdf={pdf}
containerWidth={containerWidth}
zoom={zoom * INCREASE_PERCENTAGE}
disableVisibilityCheck={this.props.disableVisibilityCheck}
/>)
));
}
renderLoading() {
if (this.state.pdf) return null;
return (<div className="pdf-loading">LOADING ({this.state.percent}%)</div>);
}
render() {
return (
<div className="pdf-viewer-container">
<div className="pdf-viewer" ref={node => this.container = node} >
<div className="pdf-controlls-container">
<div className="view-control" onClick={this.increaseZoom} >
<i className="zoom-in" />
</div>
<div className="view-control" onClick={this.resetZoom}>
<i className="zoom-reset" />
</div>
<div className="view-control" onClick={this.reduceZoom}>
<i className="zoom-out" />
</div>
</div>
{this.renderLoading()}
{this.renderPages()}
</div>
</div>
);
}
}
PDFDriver.defaultProps = {
disableVisibilityCheck: false,
};