-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathdom.js
187 lines (150 loc) Β· 3.91 KB
/
dom.js
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
/* eslint-disable no-console */
/* eslint-disable react/jsx-props-no-spreading */
import * as primitives from '@react-pdf/primitives';
import React, { useEffect, useRef, useState } from 'react';
import queue from 'queue';
import { pdf, version, Font, StyleSheet } from './index';
export const usePDF = ({ document }) => {
const pdfInstance = useRef(null);
const previousUrl = useRef(null);
const [state, setState] = useState({
url: null,
blob: null,
error: null,
loading: false,
});
// Setup rendering queue
useEffect(() => {
const renderQueue = queue({ autostart: true, concurrency: 1 });
const queueDocumentRender = () => {
setState(prev => ({ ...prev, loading: true }));
renderQueue.splice(0, renderQueue.length, () =>
state.error ? Promise.resolve() : pdfInstance.current.toBlob(),
);
};
const onRenderFailed = error => {
console.error(error);
setState(prev => ({ ...prev, error }));
};
const onRenderSuccessful = blob => {
previousUrl.current = state.url;
setState({
blob,
error: null,
loading: false,
url: URL.createObjectURL(blob),
});
};
pdfInstance.current = pdf();
pdfInstance.current.on('change', queueDocumentRender);
pdfInstance.current.updateContainer(document);
renderQueue.on('error', onRenderFailed);
renderQueue.on('success', onRenderSuccessful);
return () => {
renderQueue.end();
};
}, []);
// Revoke old unused url instances
useEffect(() => {
if (previousUrl.current) URL.revokeObjectURL(previousUrl.current);
}, [state.blob]);
const update = () => {
pdfInstance.current.updateContainer(document);
};
return [state, update];
};
export const BlobProvider = ({ document: doc, children }) => {
const [instance, updateInstance] = usePDF({ document: doc });
useEffect(updateInstance, [doc]);
if (!doc) {
console.warn('You should pass a valid document to BlobProvider');
return null;
}
return children(instance);
};
export const PDFViewer = ({
title,
style,
className,
children,
innerRef,
...props
}) => {
const [instance, updateInstance] = usePDF({ document: children });
useEffect(updateInstance, [children]);
return (
<iframe
title={title}
ref={innerRef}
style={style}
src={instance.url}
className={className}
{...props}
/>
);
};
export const PDFDownloadLink = ({
style,
children,
className,
document: doc,
fileName = 'document.pdf',
}) => {
const [instance, updateInstance] = usePDF({ document: doc });
useEffect(updateInstance, [children]);
if (!doc) {
console.warn('You should pass a valid document to PDFDownloadLink');
return null;
}
const handleDownloadIE = () => {
if (window.navigator.msSaveBlob) {
// IE
window.navigator.msSaveBlob(instance.blob, fileName);
}
};
return (
<a
style={style}
href={instance.url}
download={fileName}
className={className}
onClick={handleDownloadIE}
>
{typeof children === 'function' ? children(instance) : children}
</a>
);
};
const throwEnvironmentError = name => {
throw new Error(
`${name} is a Node specific API. You're either using this method in a browser, or your bundler is not loading react-pdf from the appropriate web build.`,
);
};
export const renderToStream = () => {
throwEnvironmentError('renderToStream');
};
export const renderToString = () => {
throwEnvironmentError('renderToString');
};
export const renderToFile = () => {
throwEnvironmentError('renderToFile');
};
export const render = () => {
throwEnvironmentError('render');
};
export * from './index';
export * from '@react-pdf/primitives';
export default {
pdf,
usePDF,
Font,
version,
StyleSheet,
PDFViewer,
BlobProvider,
PDFDownloadLink,
renderToStream,
renderToString,
renderToFile,
render,
...primitives,
};