/
CustomThumbnailItemsExample.tsx
124 lines (114 loc) · 4.25 KB
/
CustomThumbnailItemsExample.tsx
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
import * as React from 'react';
import { Button, DocumentLoadEvent, Viewer } from '@react-pdf-viewer/core';
import { thumbnailPlugin } from '@react-pdf-viewer/thumbnail';
import type { RenderThumbnailItemProps } from '@react-pdf-viewer/thumbnail';
interface CustomThumbnailItemsExampleProps {
fileUrl: string;
}
const CustomThumbnailItemsExample: React.FC<CustomThumbnailItemsExampleProps> = ({ fileUrl }) => {
const [selectedPages, setSelectedPages] = React.useState<boolean[]>([]);
const handleDocumentLoad = (e: DocumentLoadEvent) => {
setSelectedPages(Array(e.doc.numPages).fill(false));
};
const selectAllPages = () => {
setSelectedPages((selectedPages) => Array(selectedPages.length).fill(true));
};
const deselectAllPages = () => {
setSelectedPages((selectedPages) => Array(selectedPages.length).fill(false));
};
const handleChoosePage = (e: React.ChangeEvent<HTMLInputElement>, pageIndex: number) => {
const isSelected = e.target.checked;
selectedPages[pageIndex] = isSelected;
setSelectedPages([...selectedPages]);
};
const renderThumbnailItem = (props: RenderThumbnailItemProps) => (
<div
key={props.pageIndex}
className="custom-thumbnail-item"
data-testid={`thumbnail-${props.pageIndex}`}
style={{
backgroundColor: props.pageIndex === props.currentPage ? 'rgba(0, 0, 0, 0.3)' : '#fff',
cursor: 'pointer',
padding: '0.5rem',
width: '100%',
}}
>
<div style={{ marginBottom: '0.5rem' }} onClick={props.onJumpToPage}>
{props.renderPageThumbnail}
</div>
<div
style={{
alignItems: 'center',
display: 'flex',
justifyContent: 'center',
margin: '0 auto',
width: '100px',
}}
>
<div style={{ marginRight: 'auto' }}>Page {props.renderPageLabel}</div>
<input
type="checkbox"
checked={selectedPages[props.pageIndex] || false}
onChange={(e) => handleChoosePage(e, props.pageIndex)}
/>
</div>
</div>
);
const thumbnailPluginInstance = thumbnailPlugin();
const { Thumbnails } = thumbnailPluginInstance;
return (
<div
style={{
margin: '1rem auto',
width: '64rem',
}}
>
<div
style={{
alignItems: 'center',
display: 'flex',
marginBottom: '1rem',
}}
>
<div style={{ marginRight: '0.5rem' }}>
<Button onClick={selectAllPages}>Select all pages</Button>
</div>
<div style={{ marginRight: '0.5rem' }}>
<Button onClick={deselectAllPages}>Deselect all pages</Button>
</div>
<div style={{ marginRight: '0.5rem' }}>
Selected pages:{' '}
{selectedPages
.map((v, idx) => (v ? idx + 1 : false))
.filter(Number)
.join(', ')}
</div>
</div>
<div
style={{
border: '1px solid rgba(0, 0, 0, 0.1)',
display: 'flex',
height: '50rem',
}}
>
<div
style={{
borderRight: '1px solid rgba(0, 0, 0, 0.1)',
width: '20%',
}}
>
<Thumbnails renderThumbnailItem={renderThumbnailItem} />
</div>
<div
style={{
flex: 1,
overflow: 'hidden',
}}
>
<Viewer onDocumentLoad={handleDocumentLoad} fileUrl={fileUrl} plugins={[thumbnailPluginInstance]} />
</div>
</div>
</div>
);
};
export default CustomThumbnailItemsExample;