Repository navigation
Expand file tree
/
Copy pathdocument-file-preview.tsx
More file actions
101 lines (95 loc) · 3.1 KB
/
Copy pathdocument-file-preview.tsx
File metadata and controls
101 lines (95 loc) · 3.1 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
98
99
100
101
import { useI18n } from "@web/shared/i18n.js";
import { createPortal } from "react-dom";
import { lazy } from "react";
import { LazyContent } from "@web/shared/components/lazy-content.js";
import { Download, X } from "lucide-react";
import type { PreviewSource } from "@web/features/files/open-file-viewer-preview.js";
const OpenFileViewerPreview = lazy(() => import("@web/features/files/open-file-viewer-preview.js").then((module) => ({ default: module.OpenFileViewerPreview })));
import "@web/features/documents/document-file-preview.css";
export type { PreviewSource };
export function isVisualMedia(mime: string, name = "") {
const type = (mime || guessMime(name)).toLowerCase();
return type.startsWith("image/") || type.startsWith("video/");
}
export function guessMime(name: string, mime?: string) {
if (mime) return mime;
const ext = name.split(".").pop()?.toLowerCase() ?? "";
return (
(
{
pdf: "application/pdf",
doc: "application/msword",
docx: "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
xls: "application/vnd.ms-excel",
xlsx: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
ppt: "application/vnd.ms-powerpoint",
pptx: "application/vnd.openxmlformats-officedocument.presentationml.presentation",
txt: "text/plain",
md: "text/markdown",
csv: "text/csv",
zip: "application/zip",
png: "image/png",
jpg: "image/jpeg",
jpeg: "image/jpeg",
gif: "image/gif",
webp: "image/webp",
mp4: "video/mp4",
webm: "video/webm",
mov: "video/quicktime",
mp3: "audio/mpeg",
} as Record<string, string>
)[ext] ?? "application/octet-stream"
);
}
export function downloadUrl(url: string) {
return url + (url.includes("?") ? "&" : "?") + "download=1";
}
export function DocumentFilePreview({
file,
close,
}: {
file: PreviewSource;
close: () => void;
}) {
const { t } = useI18n();
return createPortal(
<div
className="document-file-preview-backdrop"
role="presentation"
onClick={close}
onKeyDown={(event) => {
if (event.key === "Escape") close();
}}
>
<section
className="document-file-preview"
role="dialog"
aria-modal="true"
aria-label={file.name}
onClick={(event) => event.stopPropagation()}
>
<header>
<strong title={file.name}>{file.name}</strong>
<div>
<a
className="icon"
href={downloadUrl(file.url)}
download={file.name}
aria-label={t("doc.download")}
title={t("doc.download")}
>
<Download size={17} />
</a>
<button className="icon" onClick={close} aria-label={t("dialog.close")}>
<X size={18} />
</button>
</div>
</header>
<div className="document-file-preview-body">
<LazyContent><OpenFileViewerPreview file={file} /></LazyContent>
</div>
</section>
</div>,
document.body,
);
}