-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathPrintButton.tsx
98 lines (88 loc) · 2.01 KB
/
PrintButton.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
import React, {
useState
} from 'react';
import useMap from '@terrestris/react-util/dist/Hooks/useMap/useMap';
import { PdfPrintSpec, PngPrintSpec, PrintSpec, PrintUtil } from '@terrestris/react-util/dist/Util/PrintUtil';
import SimpleButton, {
SimpleButtonProps
} from '../SimpleButton/SimpleButton';
export type PrintButtonProps = Omit<PrintSpec, 'map'> & {
format?: 'png' | 'pdf';
} & Partial<PdfPrintSpec> & Partial<PngPrintSpec> & SimpleButtonProps;
export const PrintButton: React.FC<PrintButtonProps> = ({
attributions = 'bottom-right',
dpi = 120,
extent,
extentPadding,
format = 'png',
legendTitle = 'Legend',
mapSize,
northArrow = 'top-right',
onProgressChange,
outputFileName = 'react-geo-image',
pdfPrintFunc,
scaleBar = {
position: 'bottom-left',
units: 'metric'
},
title = 'Print',
...passThroughProps
}) => {
const [loading, setLoading] = useState<boolean>(false);
const map = useMap();
const onPrintClick = async () => {
if (!map) {
return;
}
if (format === 'png') {
setLoading(true);
const pngPrintSpec: PngPrintSpec = {
attributions,
dpi,
extent,
extentPadding,
format,
map,
mapSize,
onProgressChange,
outputFileName,
scaleBar
};
await PrintUtil.printPng(pngPrintSpec);
setLoading(false);
return;
}
if (pdfPrintFunc && format === 'pdf') {
setLoading(true);
const pdfPrintSpec: PdfPrintSpec = {
dpi,
extent,
extentPadding,
format,
legendTitle,
map,
mapSize,
northArrow,
onProgressChange,
outputFileName,
pdfPrintFunc,
scaleBar,
title
};
await PrintUtil.printPdf(pdfPrintSpec);
setLoading(false);
return;
}
};
if (!map) {
return null;
}
return (
<SimpleButton
loading={loading}
onClick={onPrintClick}
{...passThroughProps}
/>
);
};
export default PrintButton;