-
Notifications
You must be signed in to change notification settings - Fork 110
Expand file tree
/
Copy pathiFrameComponent.tsx
More file actions
91 lines (83 loc) · 3.49 KB
/
iFrameComponent.tsx
File metadata and controls
91 lines (83 loc) · 3.49 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
import React from "react";
import { useTranslation } from "react-i18next";
import { Document } from "../../api/apiTypes/embedded";
import { TIFFViewer } from "react-tiff";
interface IIFrameComponentProps {
className?: string;
metadata: Document | null;
urlWithSasToken: string | undefined;
iframeKey: number;
}
export function IFrameComponent({ className, metadata, urlWithSasToken, iframeKey }: IIFrameComponentProps) {
const { t } = useTranslation();
const getContentComponent = () => {
if (!metadata || !urlWithSasToken) {
return <div>{t("components.iframe.error")}</div>;
}
switch (metadata.mimeType) {
case "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet":
case "application/vnd.ms-excel.sheet.macroEnabled.12":
case "application/vnd.openxmlformats-officedocument.wordprocessingml.document":
case "application/vnd.openxmlformats-officedocument.presentationml.presentation": {
return (
<iframe
key={iframeKey}
src={`https://view.officeapps.live.com/op/embed.aspx?src=${encodeURIComponent(
urlWithSasToken
)}`}
width="100%"
height="100%"
title={getTitle(metadata.mimeType)}
/>
);
}
case "application/pdf": {
const url = new URL(urlWithSasToken, window.location.origin);
url.searchParams.append("embed", "True");
return <iframe title="PDF Viewer" key={iframeKey} src={url.toString()} width="100%" height="100%" />;
}
case "image/jpeg":
case "image/png":
case "image/gif":
case "image/svg+xml": {
return <img src={urlWithSasToken} alt={"Document"} className="document-image" />;
}
case "image/tiff": {
return (
<div
style={{
width: "100%",
height: "100%",
objectFit: "contain",
overflowX: "scroll",
overflowY: "auto",
}}
>
<TIFFViewer tiff={urlWithSasToken} style={{ width: 100, height: 100, objectFit: "contain" }} />
</div>
);
}
default: {
return (
<iframe key={iframeKey} src={urlWithSasToken} width="100%" height="100%" title="Doc visualizer" />
);
}
}
};
const getTitle = (mimeType: string) => {
switch (mimeType) {
case "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet":
case "application/vnd.ms-excel.sheet.macroEnabled.12":
return "Excel viewer";
case "application/vnd.openxmlformats-officedocument.presentationml.presentation":
return "PowerPoint viewer";
case "application/vnd.openxmlformats-officedocument.wordprocessingml.document":
return "Word viewer";
case "application/pdf":
return "PDF Viewer";
default:
return "Doc visualizer";
}
};
return <div className={`${className}`}>{getContentComponent()}</div>;
}