-
Notifications
You must be signed in to change notification settings - Fork 73
Expand file tree
/
Copy pathBaseViewer.tsx
More file actions
97 lines (90 loc) · 3.5 KB
/
BaseViewer.tsx
File metadata and controls
97 lines (90 loc) · 3.5 KB
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
import { CSSProperties, Fragment, PropsWithChildren, ReactElement, ReactNode, useCallback } from "react";
import { useZoomScale } from "../utils/useZoomScale";
import { DocumentViewerContainerProps } from "../../typings/DocumentViewerProps";
import { downloadFile } from "../utils/helpers";
type FileFormat = {
status: "available";
value: {
uri: string;
name: string;
};
};
interface BaseControlViewerProps extends PropsWithChildren {
file: DocumentViewerContainerProps["file"] | FileFormat;
CustomControl?: ReactNode;
}
interface BaseViewerProps extends PropsWithChildren {
fileName: string;
CustomControl?: ReactNode;
SecondaryControl?: ReactNode;
}
const BaseViewer = (props: BaseViewerProps): ReactElement => {
const { fileName, CustomControl, SecondaryControl, children } = props;
return (
<Fragment>
<div className="widget-document-viewer-controls">
<div className="widget-document-viewer-controls-left">
<div className="document-title">{fileName}</div>
</div>
<div className="widget-document-viewer-controls-icons">{CustomControl}</div>
</div>
{SecondaryControl}
<div className="widget-document-viewer-content">{children}</div>
</Fragment>
);
};
const BaseControlViewer = (props: BaseControlViewerProps): ReactElement => {
const { CustomControl, children, file } = props;
const { zoomLevel, zoomIn, zoomOut, reset } = useZoomScale();
const onDownloadClick = useCallback(() => {
downloadFile(file.value?.uri);
}, [file]);
return (
<BaseViewer
fileName={file.value?.name || ""}
CustomControl={
<Fragment>
{CustomControl}
<button
onClick={onDownloadClick}
className="icons icon-Download btn btn-icon-only"
aria-label={"Download"}
title={"Download"}
></button>
<div className="widget-document-viewer-zoom">
<button
onClick={zoomOut}
disabled={zoomLevel <= 0.3}
className="icons icon-ZoomOut btn btn-icon-only"
aria-label={"Zoom out"}
title={"Zoom out"}
></button>
<button
onClick={zoomIn}
disabled={zoomLevel >= 10}
className="icons icon-ZoomIn btn btn-icon-only"
aria-label={"Zoom in"}
title={"Zoom in"}
></button>
<button
onClick={reset}
disabled={zoomLevel >= 10}
className="icons icon-FitToWidth btn btn-icon-only"
aria-label={"Fit to width"}
title={"Fit to width"}
></button>
</div>
</Fragment>
}
>
<div
className="widget-document-viewer-zoom-container"
style={{ "--current-zoom-scale": zoomLevel } as CSSProperties}
>
{children}
</div>
</BaseViewer>
);
};
export default BaseViewer;
export { BaseControlViewer };