Zeige PDF-Anlagen in der Mail wieder direkt an, statt nur einen Download anzubieten.
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
e2475555c0
commit
4327f5a434
1 file changed
+9
-90
@@ -5,7 +5,7 @@ import { deviceClass, readImageChoice } from "../mail-images.js";
|
||||
import { MarkdownView } from "../markdown.jsx";
|
||||
import { NoteEditor } from "../notes.jsx";
|
||||
import { DirectionColumn, directionWord } from "../pages/content.jsx";
|
||||
import workerSrc from "pdfjs-dist/legacy/build/pdf.worker.min.mjs?url";
|
||||
import { DocumentView } from "../pages/DocumentView.jsx";
|
||||
import { editors, registerStage } from "./registry.jsx";
|
||||
|
||||
function previewKind(type, filename) {
|
||||
@@ -21,13 +21,6 @@ function isPdfBytes(buffer) {
|
||||
return sample.startsWith("%PDF");
|
||||
}
|
||||
|
||||
function downloadName(file, pdf) {
|
||||
const name = file?.filename || "anlage";
|
||||
if (!pdf || name.toLowerCase().endsWith(".pdf")) return name;
|
||||
const stem = name.toLowerCase().endsWith(".bin") ? name.slice(0, -4).trim() : name;
|
||||
return `${stem || "anlage"}.pdf`;
|
||||
}
|
||||
|
||||
export function NoteStage({ element, context, binding, onReload }) {
|
||||
const editor = editors.note;
|
||||
const [text, setText] = useState(() => documentToText(element.document));
|
||||
@@ -143,10 +136,6 @@ export function MailStage({ element, context, binding, shownDirection }) {
|
||||
const [phase, setPhase] = useState("idle");
|
||||
const [media, setMedia] = useState("");
|
||||
const [blobUrl, setBlobUrl] = useState("");
|
||||
const [pdf, setPdf] = useState(null);
|
||||
const [page, setPage] = useState(1);
|
||||
const [pages, setPages] = useState(0);
|
||||
const canvasRef = useRef(null);
|
||||
const objectUrl = useRef("");
|
||||
const shownIndex = shownFor?.elementId === element?.id ? shownFor.index : null;
|
||||
const shownFile = (letter?.attachments || []).find((item) => item.index === shownIndex) || null;
|
||||
@@ -175,8 +164,6 @@ export function MailStage({ element, context, binding, shownDirection }) {
|
||||
useEffect(() => {
|
||||
if (shownIndex === null) {
|
||||
setMedia("");
|
||||
setPdf(null);
|
||||
setPages(0);
|
||||
setPhase("idle");
|
||||
if (objectUrl.current) {
|
||||
URL.revokeObjectURL(objectUrl.current);
|
||||
@@ -189,9 +176,6 @@ export function MailStage({ element, context, binding, shownDirection }) {
|
||||
if (!file) return undefined;
|
||||
let alive = true;
|
||||
setPhase("loading");
|
||||
setPdf(null);
|
||||
setPages(0);
|
||||
setPage(1);
|
||||
setMedia("");
|
||||
fetchMailPart(element.id, context, `attachments/${file.index}`).then(async (blob) => {
|
||||
if (!alive) return;
|
||||
@@ -203,36 +187,11 @@ export function MailStage({ element, context, binding, shownDirection }) {
|
||||
URL.revokeObjectURL(objectUrl.current);
|
||||
objectUrl.current = "";
|
||||
}
|
||||
setBlobUrl("");
|
||||
if (kind === "image") {
|
||||
const url = URL.createObjectURL(new Blob([buffer], { type: blob.type || file.content_type || "application/octet-stream" }));
|
||||
objectUrl.current = url;
|
||||
setBlobUrl(url);
|
||||
setMedia("image");
|
||||
setPhase("ready");
|
||||
return;
|
||||
}
|
||||
if (kind === "pdf") {
|
||||
setMedia("pdf");
|
||||
try {
|
||||
const pdfjs = await import("pdfjs-dist/legacy/build/pdf.mjs");
|
||||
pdfjs.GlobalWorkerOptions.workerSrc = workerSrc;
|
||||
const doc = await pdfjs.getDocument({ data: new Uint8Array(buffer) }).promise;
|
||||
if (!alive) {
|
||||
doc.destroy();
|
||||
return;
|
||||
}
|
||||
setPdf(doc);
|
||||
setPages(doc.numPages);
|
||||
setPhase("ready");
|
||||
} catch (err) {
|
||||
if (!alive) return;
|
||||
setPdf(null);
|
||||
setPhase("ready");
|
||||
}
|
||||
return;
|
||||
}
|
||||
setMedia("file");
|
||||
const type = kind === "pdf" ? "application/pdf" : (blob.type || file.content_type || "application/octet-stream");
|
||||
const url = URL.createObjectURL(new Blob([buffer], { type }));
|
||||
objectUrl.current = url;
|
||||
setBlobUrl(url);
|
||||
setMedia(kind === "pdf" ? "pdf" : kind);
|
||||
setPhase("ready");
|
||||
}).catch((err) => {
|
||||
if (!alive) return;
|
||||
@@ -246,26 +205,8 @@ export function MailStage({ element, context, binding, shownDirection }) {
|
||||
}, [element.id, shownIndex, letter, context?.kind, context?.id]);
|
||||
|
||||
useEffect(() => () => {
|
||||
pdf?.destroy?.();
|
||||
if (objectUrl.current) URL.revokeObjectURL(objectUrl.current);
|
||||
}, [pdf]);
|
||||
|
||||
useEffect(() => {
|
||||
if (!pdf || !canvasRef.current) return undefined;
|
||||
let alive = true;
|
||||
pdf.getPage(page).then((pdfPage) => {
|
||||
if (!alive || !canvasRef.current) return;
|
||||
const canvas = canvasRef.current;
|
||||
const context2d = canvas.getContext("2d");
|
||||
const viewport = pdfPage.getViewport({ scale: 1 });
|
||||
canvas.width = viewport.width;
|
||||
canvas.height = viewport.height;
|
||||
return pdfPage.render({ canvasContext: context2d, viewport }).promise;
|
||||
}).catch(() => {});
|
||||
return () => {
|
||||
alive = false;
|
||||
};
|
||||
}, [pdf, page]);
|
||||
}, []);
|
||||
|
||||
async function openOriginal() {
|
||||
setError("");
|
||||
@@ -276,17 +217,6 @@ export function MailStage({ element, context, binding, shownDirection }) {
|
||||
}
|
||||
}
|
||||
|
||||
async function saveAttachment() {
|
||||
if (!shownFile) return;
|
||||
setError("");
|
||||
const namedPdf = media === "pdf" || (shownFile.content_type || "").toLowerCase().includes("pdf") || (shownFile.filename || "").toLowerCase().endsWith(".pdf");
|
||||
try {
|
||||
await downloadMailPart(element.id, context, `attachments/${shownFile.index}`, downloadName(shownFile, namedPdf));
|
||||
} catch (err) {
|
||||
setError(err.message);
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<article className="mail-letter">
|
||||
<p className="mail-letter__way"><DirectionColumn direction={shownDirection} />{directionWord(shownDirection) ? <span>{directionWord(shownDirection)}</span> : null}</p>
|
||||
@@ -305,19 +235,8 @@ export function MailStage({ element, context, binding, shownDirection }) {
|
||||
{shownFile ? <p className="mail-attachment__caption">Anlage: {shownFile.filename}</p> : null}
|
||||
{shownFile && phase === "loading" ? <p className="muted">Die Anlage wird geladen …</p> : null}
|
||||
{media === "image" && blobUrl ? <img className="reader-image" alt={shownFile?.filename || element.title} src={blobUrl} /> : null}
|
||||
{media === "pdf" && pdf ? <canvas ref={canvasRef} className="reader-canvas" /> : null}
|
||||
{shownFile && phase === "ready" && binding?.rights?.download && (media === "file" || (media === "pdf" && !pdf)) ? (
|
||||
<p className="muted">
|
||||
<button type="button" onClick={saveAttachment}>{downloadName(shownFile, media === "pdf")} herunterladen</button>
|
||||
</p>
|
||||
) : null}
|
||||
{pages > 1 ? (
|
||||
<div className="reader-pages">
|
||||
<button type="button" disabled={page <= 1} onClick={() => setPage((current) => Math.max(1, current - 1))}>Vorherige Seite</button>
|
||||
<span>{page} / {pages}</span>
|
||||
<button type="button" disabled={page >= pages} onClick={() => setPage((current) => Math.min(pages, current + 1))}>Nächste Seite</button>
|
||||
</div>
|
||||
) : null}
|
||||
{media === "pdf" && blobUrl ? <DocumentView title={shownFile?.filename || element.title} url={blobUrl} /> : null}
|
||||
{media === "other" && blobUrl && binding?.rights?.download ? <a href={blobUrl} target="_blank" rel="noreferrer">Anlage öffnen</a> : null}
|
||||
<MailBody
|
||||
html={letter.html}
|
||||
text={letter.text}
|
||||
|
||||
Reference in new issue
Block a user