Stelle PDFs auf iPhone und iPad seitenweise dar und nutze das Legacy-Build von pdf.js.
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
7b2d0c298e
commit
07e46c77f1
6 files changed
+100
-10
No files matched your search
@@ -134,3 +134,6 @@ body.is-resizing .library__resizer { background: color-mix(in srgb, var(--accent
|
|||||||
@media (max-width: 900px) { .library__resizer { display: none; } }
|
@media (max-width: 900px) { .library__resizer { display: none; } }
|
||||||
|
|
||||||
.inline-actions { display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap; }
|
.inline-actions { display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap; }
|
||||||
|
|
||||||
|
.document-pages { width: 100%; }
|
||||||
|
.document-pages canvas { display: block; width: 100%; margin: 0 0 8px; background: #fff; }
|
||||||
@@ -0,0 +1,82 @@
|
|||||||
|
import { useEffect, useRef, useState } from "react";
|
||||||
|
import workerSrc from "pdfjs-dist/legacy/build/pdf.worker.min.mjs?url";
|
||||||
|
|
||||||
|
// Safari on iPhone and iPad shows a PDF in an iframe as a white area, or only its first page.
|
||||||
|
export function isAppleTouch() {
|
||||||
|
if (typeof navigator === "undefined") return false;
|
||||||
|
return /iP(hone|ad|od)/.test(navigator.userAgent) || (navigator.platform === "MacIntel" && navigator.maxTouchPoints > 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Safari limits a canvas to about 16.7 million pixels. Stay below that.
|
||||||
|
const MAX_PIXELS = 12_000_000;
|
||||||
|
|
||||||
|
// Shows a document from a blob URL. Desktop browsers use their own viewer in a frame;
|
||||||
|
// on Apple touch devices a PDF is drawn page by page, which works there.
|
||||||
|
export function DocumentView({ url, title, className = "reader-frame", frameRef = null }) {
|
||||||
|
const holder = useRef(null);
|
||||||
|
const [mode, setMode] = useState(isAppleTouch() ? "loading" : "frame");
|
||||||
|
const [failed, setFailed] = useState(false);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (!url || !isAppleTouch()) {
|
||||||
|
setMode("frame");
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
let alive = true;
|
||||||
|
let doc = null;
|
||||||
|
setMode("loading");
|
||||||
|
setFailed(false);
|
||||||
|
(async () => {
|
||||||
|
try {
|
||||||
|
const blob = await (await fetch(url)).blob();
|
||||||
|
if (!alive) return;
|
||||||
|
if (!/pdf/i.test(blob.type)) {
|
||||||
|
setMode("frame");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const pdfjs = await import("pdfjs-dist/legacy/build/pdf.mjs");
|
||||||
|
pdfjs.GlobalWorkerOptions.workerSrc = workerSrc;
|
||||||
|
doc = await pdfjs.getDocument({ data: await blob.arrayBuffer() }).promise;
|
||||||
|
if (!alive) return;
|
||||||
|
setMode("pages");
|
||||||
|
await new Promise((resolve) => requestAnimationFrame(resolve));
|
||||||
|
const target = holder.current;
|
||||||
|
if (!target) return;
|
||||||
|
target.replaceChildren();
|
||||||
|
const width = target.clientWidth || 320;
|
||||||
|
const ratio = Math.min(window.devicePixelRatio || 1, 2);
|
||||||
|
for (let number = 1; number <= doc.numPages; number += 1) {
|
||||||
|
const page = await doc.getPage(number);
|
||||||
|
if (!alive) return;
|
||||||
|
const base = page.getViewport({ scale: 1 });
|
||||||
|
let scale = (width / base.width) * ratio;
|
||||||
|
scale = Math.min(scale, Math.sqrt(MAX_PIXELS / (base.width * base.height)));
|
||||||
|
const viewport = page.getViewport({ scale });
|
||||||
|
const canvas = document.createElement("canvas");
|
||||||
|
canvas.width = Math.floor(viewport.width);
|
||||||
|
canvas.height = Math.floor(viewport.height);
|
||||||
|
canvas.style.width = "100%";
|
||||||
|
canvas.style.height = "auto";
|
||||||
|
canvas.className = "reader-canvas";
|
||||||
|
target.appendChild(canvas);
|
||||||
|
await page.render({ canvasContext: canvas.getContext("2d"), viewport }).promise;
|
||||||
|
}
|
||||||
|
} catch {
|
||||||
|
if (alive) setFailed(true);
|
||||||
|
}
|
||||||
|
})();
|
||||||
|
return () => {
|
||||||
|
alive = false;
|
||||||
|
doc?.destroy?.();
|
||||||
|
};
|
||||||
|
}, [url]);
|
||||||
|
|
||||||
|
if (mode === "frame") return <iframe ref={frameRef} className={className} title={title} src={url} />;
|
||||||
|
return (
|
||||||
|
<div className="document-pages">
|
||||||
|
{mode === "loading" && !failed ? <p className="muted">Dokument wird dargestellt …</p> : null}
|
||||||
|
{failed ? <p className="error">Die Vorschau ist hier nicht möglich. <a href={url} target="_blank" rel="noreferrer">Dokument öffnen</a></p> : null}
|
||||||
|
<div ref={holder} />
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -1,3 +1,4 @@
|
|||||||
|
import { DocumentView } from "./DocumentView.jsx";
|
||||||
import { useContext, useEffect, useRef, useState } from "react";
|
import { useContext, useEffect, useRef, useState } from "react";
|
||||||
import { SessionContext } from "../session.js";
|
import { SessionContext } from "../session.js";
|
||||||
import { documentToText, downloadMailPart, fetchExternal, fetchFile, fetchMailPart, isDirty, markDirty, textToDocument, api } from "../api.js";
|
import { documentToText, downloadMailPart, fetchExternal, fetchFile, fetchMailPart, isDirty, markDirty, textToDocument, api } from "../api.js";
|
||||||
@@ -7,7 +8,7 @@ import { ErrorNote } from "./widgets.jsx";
|
|||||||
import { UnitDetail } from "./correspondence.jsx";
|
import { UnitDetail } from "./correspondence.jsx";
|
||||||
import { dateCaption, DateField, DirectionChoice, DirectionColumn, directionValue, directionWord, formatDate, mailFacts, sourceLabel } from "./content.jsx";
|
import { dateCaption, DateField, DirectionChoice, DirectionColumn, directionValue, directionWord, formatDate, mailFacts, sourceLabel } from "./content.jsx";
|
||||||
import { NavIcon } from "./icons.jsx";
|
import { NavIcon } from "./icons.jsx";
|
||||||
import workerSrc from "pdfjs-dist/build/pdf.worker.min.mjs?url";
|
import workerSrc from "pdfjs-dist/legacy/build/pdf.worker.min.mjs?url";
|
||||||
|
|
||||||
const SCALES = [0.75, 1, 1.25, 1.5, 2];
|
const SCALES = [0.75, 1, 1.25, 1.5, 2];
|
||||||
|
|
||||||
@@ -173,7 +174,7 @@ export function ReadingPane({ binding, context, backLabel, onClose, onReload, ca
|
|||||||
setMedia("pdf");
|
setMedia("pdf");
|
||||||
try {
|
try {
|
||||||
const bytes = await blob.arrayBuffer();
|
const bytes = await blob.arrayBuffer();
|
||||||
const pdfjs = await import("pdfjs-dist");
|
const pdfjs = await import("pdfjs-dist/legacy/build/pdf.mjs");
|
||||||
pdfjs.GlobalWorkerOptions.workerSrc = workerSrc;
|
pdfjs.GlobalWorkerOptions.workerSrc = workerSrc;
|
||||||
const doc = await pdfjs.getDocument({ data: bytes }).promise;
|
const doc = await pdfjs.getDocument({ data: bytes }).promise;
|
||||||
if (!alive) {
|
if (!alive) {
|
||||||
@@ -243,7 +244,7 @@ export function ReadingPane({ binding, context, backLabel, onClose, onReload, ca
|
|||||||
setMedia("pdf");
|
setMedia("pdf");
|
||||||
try {
|
try {
|
||||||
const bytes = await blob.arrayBuffer();
|
const bytes = await blob.arrayBuffer();
|
||||||
const pdfjs = await import("pdfjs-dist");
|
const pdfjs = await import("pdfjs-dist/legacy/build/pdf.mjs");
|
||||||
pdfjs.GlobalWorkerOptions.workerSrc = workerSrc;
|
pdfjs.GlobalWorkerOptions.workerSrc = workerSrc;
|
||||||
const doc = await pdfjs.getDocument({ data: bytes }).promise;
|
const doc = await pdfjs.getDocument({ data: bytes }).promise;
|
||||||
if (!alive) {
|
if (!alive) {
|
||||||
@@ -308,7 +309,8 @@ export function ReadingPane({ binding, context, backLabel, onClose, onReload, ca
|
|||||||
const canvas = canvasRef.current;
|
const canvas = canvasRef.current;
|
||||||
const context2d = canvas?.getContext("2d");
|
const context2d = canvas?.getContext("2d");
|
||||||
if (!canvas || !context2d) return;
|
if (!canvas || !context2d) return;
|
||||||
const viewport = pdfPage.getViewport({ scale });
|
const base = pdfPage.getViewport({ scale: 1 });
|
||||||
|
const viewport = pdfPage.getViewport({ scale: Math.min(scale, Math.sqrt(12_000_000 / (base.width * base.height))) });
|
||||||
canvas.width = viewport.width;
|
canvas.width = viewport.width;
|
||||||
canvas.height = viewport.height;
|
canvas.height = viewport.height;
|
||||||
const render = pdfPage.render({ canvasContext: context2d, viewport });
|
const render = pdfPage.render({ canvasContext: context2d, viewport });
|
||||||
@@ -584,7 +586,7 @@ export function ReadingPane({ binding, context, backLabel, onClose, onReload, ca
|
|||||||
<img className="reader-image" alt={element.title} src={blobUrl} style={{ width: `${Math.round(scale * 100)}%` }} />
|
<img className="reader-image" alt={element.title} src={blobUrl} style={{ width: `${Math.round(scale * 100)}%` }} />
|
||||||
) : null}
|
) : null}
|
||||||
{!isMail && media === "pdf" && pdf ? <canvas ref={canvasRef} className="reader-canvas" /> : null}
|
{!isMail && media === "pdf" && pdf ? <canvas ref={canvasRef} className="reader-canvas" /> : null}
|
||||||
{!isMail && media === "pdf-frame" && blobUrl ? <iframe className="reader-frame" title={element.title} src={blobUrl} /> : null}
|
{!isMail && media === "pdf-frame" && blobUrl ? <DocumentView title={element.title} url={blobUrl} /> : null}
|
||||||
{!isMail && media === "other" && blobUrl ? (
|
{!isMail && media === "other" && blobUrl ? (
|
||||||
<div className="reader-pane__state">
|
<div className="reader-pane__state">
|
||||||
<p>Die Datei ist geladen, aber hier nicht als Seite darstellbar.</p>
|
<p>Die Datei ist geladen, aber hier nicht als Seite darstellbar.</p>
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
import { DocumentView } from "./DocumentView.jsx";
|
||||||
import { useEffect, useRef, useState } from "react";
|
import { useEffect, useRef, useState } from "react";
|
||||||
import { Link, useNavigate } from "react-router-dom";
|
import { Link, useNavigate } from "react-router-dom";
|
||||||
import { api, fetchBlob } from "../api.js";
|
import { api, fetchBlob } from "../api.js";
|
||||||
@@ -355,7 +356,7 @@ export function PaperlessPicker({ context, placement, chapterId = "", chapterTit
|
|||||||
{picked.size ? `${picked.size} Belege einfügen` : "Belege auswählen"}
|
{picked.size ? `${picked.size} Belege einfügen` : "Belege auswählen"}
|
||||||
</button>
|
</button>
|
||||||
) : null}
|
) : null}
|
||||||
{previewUrl ? <iframe className="reader-frame" title="Paperless-Vorschau" src={previewUrl} /> : null}
|
{previewUrl ? <DocumentView title="Paperless-Vorschau" url={previewUrl} /> : null}
|
||||||
<div className="stack">
|
<div className="stack">
|
||||||
{held.map((item) => (
|
{held.map((item) => (
|
||||||
<div key={`held-${item.connector_id}:${item.id}`} className="paperless-pick">
|
<div key={`held-${item.connector_id}:${item.id}`} className="paperless-pick">
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
import { DocumentView } from "./DocumentView.jsx";
|
||||||
import { useEffect, useRef, useState } from "react";
|
import { useEffect, useRef, useState } from "react";
|
||||||
import { Link } from "react-router-dom";
|
import { Link } from "react-router-dom";
|
||||||
import { api, documentToText, downloadFile, fetchExternal, markDirty, textToDocument } from "../api.js";
|
import { api, documentToText, downloadFile, fetchExternal, markDirty, textToDocument } from "../api.js";
|
||||||
@@ -227,7 +228,7 @@ function Binding({ binding, context, canEdit, isOwner, onChange }) {
|
|||||||
onClose={() => setViewing(false)}
|
onClose={() => setViewing(false)}
|
||||||
>
|
>
|
||||||
<div className="viewer">
|
<div className="viewer">
|
||||||
{previewUrl ? <iframe ref={frameRef} title={element.title} src={previewUrl} /> : <p className="muted">Dokument wird geladen …</p>}
|
{previewUrl ? <DocumentView frameRef={frameRef} className="" title={element.title} url={previewUrl} /> : <p className="muted">Dokument wird geladen …</p>}
|
||||||
<div className="row">
|
<div className="row">
|
||||||
<button type="button" onClick={() => frameRef.current?.contentWindow?.print()} disabled={!previewUrl}>Drucken</button>
|
<button type="button" onClick={() => frameRef.current?.contentWindow?.print()} disabled={!previewUrl}>Drucken</button>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -1,10 +1,11 @@
|
|||||||
|
import { DocumentView } from "../pages/DocumentView.jsx";
|
||||||
import { useEffect, useRef, useState } from "react";
|
import { useEffect, useRef, useState } from "react";
|
||||||
import { api, documentToText, downloadMailPart, fetchExternal, fetchMailPart, markDirty, textToDocument } from "../api.js";
|
import { api, documentToText, downloadMailPart, fetchExternal, fetchMailPart, markDirty, textToDocument } from "../api.js";
|
||||||
import { MailAttachments, MailBody } from "../mail-view.jsx";
|
import { MailAttachments, MailBody } from "../mail-view.jsx";
|
||||||
import { MarkdownView } from "../markdown.jsx";
|
import { MarkdownView } from "../markdown.jsx";
|
||||||
import { NoteEditor } from "../notes.jsx";
|
import { NoteEditor } from "../notes.jsx";
|
||||||
import { DirectionColumn, directionWord } from "../pages/content.jsx";
|
import { DirectionColumn, directionWord } from "../pages/content.jsx";
|
||||||
import workerSrc from "pdfjs-dist/build/pdf.worker.min.mjs?url";
|
import workerSrc from "pdfjs-dist/legacy/build/pdf.worker.min.mjs?url";
|
||||||
import { editors, registerStage } from "./registry.jsx";
|
import { editors, registerStage } from "./registry.jsx";
|
||||||
|
|
||||||
function previewKind(type, filename) {
|
function previewKind(type, filename) {
|
||||||
@@ -181,7 +182,7 @@ export function MailStage({ element, context, shownDirection }) {
|
|||||||
setMedia("pdf");
|
setMedia("pdf");
|
||||||
try {
|
try {
|
||||||
const bytes = await blob.arrayBuffer();
|
const bytes = await blob.arrayBuffer();
|
||||||
const pdfjs = await import("pdfjs-dist");
|
const pdfjs = await import("pdfjs-dist/legacy/build/pdf.mjs");
|
||||||
pdfjs.GlobalWorkerOptions.workerSrc = workerSrc;
|
pdfjs.GlobalWorkerOptions.workerSrc = workerSrc;
|
||||||
const doc = await pdfjs.getDocument({ data: bytes }).promise;
|
const doc = await pdfjs.getDocument({ data: bytes }).promise;
|
||||||
if (!alive) {
|
if (!alive) {
|
||||||
@@ -260,7 +261,7 @@ export function MailStage({ element, context, shownDirection }) {
|
|||||||
{shownFile && phase === "loading" ? <p className="muted">Die Anlage wird geladen …</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 === "image" && blobUrl ? <img className="reader-image" alt={shownFile?.filename || element.title} src={blobUrl} /> : null}
|
||||||
{media === "pdf" && pdf ? <canvas ref={canvasRef} className="reader-canvas" /> : null}
|
{media === "pdf" && pdf ? <canvas ref={canvasRef} className="reader-canvas" /> : null}
|
||||||
{media === "pdf-frame" && blobUrl ? <iframe className="reader-frame" title={shownFile?.filename || element.title} src={blobUrl} /> : null}
|
{media === "pdf-frame" && blobUrl ? <DocumentView title={shownFile?.filename || element.title} url={blobUrl} /> : null}
|
||||||
{media === "other" && blobUrl ? <a href={blobUrl} target="_blank" rel="noreferrer">Anlage öffnen</a> : null}
|
{media === "other" && blobUrl ? <a href={blobUrl} target="_blank" rel="noreferrer">Anlage öffnen</a> : null}
|
||||||
{pages > 1 ? (
|
{pages > 1 ? (
|
||||||
<div className="reader-pages">
|
<div className="reader-pages">
|
||||||
|
|||||||
Reference in new issue
Block a user