-
-
Notifications
You must be signed in to change notification settings - Fork 893
/
Test.jsx
198 lines (182 loc) · 5.92 KB
/
Test.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
import React, { Component } from 'react';
import { Document, Outline, Page } from 'react-pdf/src/entry.webpack';
import './Test.less';
import LoadingOptions from './LoadingOptions';
import ViewOptions from './ViewOptions';
import { dataURItoBlob } from './shared/util';
export default class Test extends Component {
state = {
displayAll: false,
file: null,
numPages: null,
pageNumber: null,
pageWidth: null,
passMethod: 'normal',
render: true,
renderAnnotations: true,
renderTextLayer: true,
rotate: null,
}
onDocumentLoadSuccess = ({ numPages }) =>
this.setState({
numPages,
pageNumber: 1,
})
onItemClick = ({ pageNumber }) =>
this.setState({ pageNumber })
setFile = file => this.setState({ file })
previousPage = () => this.changePage(-1)
nextPage = () => this.changePage(1)
changePage = by =>
this.setState(prevState => ({
pageNumber: (prevState.pageNumber || 1) + by,
}))
get file() {
const { file } = this.state;
if (!file) {
return null;
}
switch (this.state.passMethod) {
case 'object': {
const result = {};
if (typeof file === 'string') {
result.url = file;
return result;
}
return file;
}
case 'blob':
if (file instanceof File || file instanceof Blob) {
return file;
}
return dataURItoBlob(file);
case 'normal':
default:
return file;
}
}
render() {
const {
displayAll,
numPages,
pageNumber,
pageWidth,
passMethod,
render,
renderAnnotations,
renderTextLayer,
rotate,
} = this.state;
const { file } = this;
const setState = state => this.setState(state);
return (
<div className="Test">
<header>
<h1>react-pdf test page</h1>
</header>
<div className="Test__container">
<aside className="Test__container__options">
<LoadingOptions
file={this.state.file}
passMethod={passMethod}
setFile={this.setFile}
setState={setState}
/>
<ViewOptions
displayAll={displayAll}
pageWidth={pageWidth}
renderAnnotations={renderAnnotations}
renderTextLayer={renderTextLayer}
rotate={rotate}
setState={setState}
/>
</aside>
<main className="Test__container__content">
<div className="Test__container__content__toc">
{
render &&
<Document
className="custom-classname-document"
file={file}
>
<Outline
className="custom-classname-outline"
onItemClick={this.onItemClick}
/>
</Document>
}
</div>
<div className="Test__container__content__document">
{
render &&
<Document
className="custom-classname-document"
file={file}
onClick={(event, pdf) => console.log('Clicked a document', { event, pdf })}
onLoadSuccess={this.onDocumentLoadSuccess}
onLoadError={this.onDocumentLoadError}
onSourceError={this.onDocumentLoadError}
rotate={rotate}
>
{
displayAll ?
Array.from(
new Array(numPages),
(el, index) => (
<Page
className="custom-classname-page"
inputRef={(ref) => {
if (!ref) {
return;
}
if (pageNumber === index + 1) {
ref.scrollIntoView();
}
}}
key={`page_${index + 1}`}
onClick={(event, page) => console.log('Clicked a page', { event, page })}
pageNumber={index + 1}
renderAnnotations={renderAnnotations}
renderTextLayer={renderTextLayer}
width={pageWidth}
onRenderSuccess={this.onPageRenderSuccess}
/>
),
) :
<Page
className="custom-classname-page"
onClick={(event, page) => console.log('Clicked a page', { event, page })}
pageNumber={pageNumber || 1}
renderAnnotations={renderAnnotations}
renderTextLayer={renderTextLayer}
width={pageWidth}
onRenderSuccess={this.onPageRenderSuccess}
/>
}
</Document>
}
</div>
{
displayAll ||
<div className="Test__container__content__controls">
<button
disabled={pageNumber <= 1}
onClick={this.previousPage}
>
Previous
</button>
<span>Page {pageNumber || (numPages ? 1 : '--')} of {numPages || '--'}</span>
<button
disabled={pageNumber >= numPages}
onClick={this.nextPage}
>
Next
</button>
</div>
}
</main>
</div>
</div>
);
}
}