Lade Bilder aus E-Mails auf Wunsch über den Server nach und verkleinere den Kopf der Leseansicht auf dem Handy.
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
075525ee17
commit
a3f328832e
11 files changed
+365
-11
No files matched your search
@@ -4,9 +4,34 @@ import { SessionContext } from "../session.js";
|
||||
import { ErrorNote } from "../pages/widgets.jsx";
|
||||
import { formatWhen } from "./admin-core.js";
|
||||
import { DownloadPolicy } from "./DownloadPolicy.jsx";
|
||||
import { deviceClass, readImageChoice, writeImageChoice } from "../mail-images.js";
|
||||
import { isEmployee } from "./admin-core.js";
|
||||
import { PasswordForm } from "./widgets.jsx";
|
||||
|
||||
function MailImages() {
|
||||
const [choice, setChoice] = useState(() => ({ desktop: readImageChoice("desktop"), touch: readImageChoice("touch") }));
|
||||
const here = deviceClass();
|
||||
const rows = [["desktop", "Am Computer"], ["touch", "Auf Handy und Tablet"]];
|
||||
return (
|
||||
<section className="card stack" aria-label="Bilder in E-Mails">
|
||||
<h2>Bilder in E-Mails</h2>
|
||||
<p className="muted">
|
||||
Bilder liegen beim Absender. Beim Nachladen holt unser Server sie für dich (nur öffentliche https-Adressen, nur echte Bilddateien), dein Gerät nimmt keinen Kontakt auf.
|
||||
Der Absender kann trotzdem erkennen, dass die Nachricht geöffnet wurde. Die Wahl gilt nur für diesen Browser; dieses Gerät zählt als „{here === "touch" ? "Handy und Tablet" : "Computer"}“.
|
||||
</p>
|
||||
{rows.map(([key, label]) => (
|
||||
<label key={key}>
|
||||
{label}
|
||||
<select value={choice[key]} onChange={(event) => setChoice((current) => ({ ...current, [key]: writeImageChoice(key, event.target.value) }))}>
|
||||
<option value="ask">Nur auf Klick nachladen</option>
|
||||
<option value="auto">Automatisch nachladen</option>
|
||||
</select>
|
||||
</label>
|
||||
))}
|
||||
</section>
|
||||
);
|
||||
}
|
||||
|
||||
export function Account() {
|
||||
const { session, logout } = useContext(SessionContext);
|
||||
const [sessions, setSessions] = useState([]);
|
||||
@@ -56,6 +81,7 @@ export function Account() {
|
||||
</header>
|
||||
<ErrorNote error={error} />
|
||||
{notice ? <p className="notice" role="status">{notice}</p> : null}
|
||||
<MailImages />
|
||||
{isEmployee(session) ? null : (
|
||||
<section className="card stack" aria-label="Herunterladen">
|
||||
<h2>Herunterladen von Originalen</h2>
|
||||
|
||||
@@ -153,3 +153,15 @@ body.is-resizing .library__resizer { background: color-mix(in srgb, var(--accent
|
||||
|
||||
.mail-frame { display: block; width: 100%; height: 60vh; min-height: 260px; border: 1px solid var(--border); border-radius: 8px; background: #fff; }
|
||||
.mail-reading .segmented { display: inline-flex; gap: 6px; margin-bottom: 8px; }
|
||||
|
||||
/* Reader header on the phone: back link and tools on one line, a modest title below */
|
||||
@media (max-width: 759px) {
|
||||
.reader-pane__bar { padding: 8px 12px 4px; gap: 2px 8px; flex-wrap: wrap; }
|
||||
.reader-pane__bar .reader-back { order: 1; }
|
||||
.reader-pane__bar .reader-pane__tools { order: 2; margin-left: auto; }
|
||||
.reader-pane__bar .reader-pane__heading { order: 3; flex: 1 1 100%; }
|
||||
.reader-pane__heading h2 { font-size: 17px; line-height: 1.25; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
|
||||
.reader-pane__heading .direction-column__icon { width: 18px; height: 18px; }
|
||||
.reader-pane__heading .reader-facts { margin: 2px 0 0; font-size: 12px; }
|
||||
}
|
||||
.mail-images { display: grid; gap: 4px; margin: 4px 0; }
|
||||
@@ -0,0 +1,30 @@
|
||||
// Pictures in a mail come from the sender's server. The app loads them on request, through its own server.
|
||||
// The choice lives in the browser: one value for pointer devices (computer) and one for touch devices (phone, tablet).
|
||||
|
||||
export const IMAGE_KEYS = { desktop: "tsumugu-mail-images-desktop", touch: "tsumugu-mail-images-touch" };
|
||||
|
||||
export function deviceClass(match = typeof window === "undefined" ? null : window.matchMedia) {
|
||||
try {
|
||||
return match && match.call(typeof window === "undefined" ? undefined : window, "(pointer: coarse)").matches ? "touch" : "desktop";
|
||||
} catch {
|
||||
return "desktop";
|
||||
}
|
||||
}
|
||||
|
||||
export function readImageChoice(device, storage = typeof window === "undefined" ? null : window.localStorage) {
|
||||
try {
|
||||
return storage?.getItem(IMAGE_KEYS[device]) === "auto" ? "auto" : "ask";
|
||||
} catch {
|
||||
return "ask";
|
||||
}
|
||||
}
|
||||
|
||||
export function writeImageChoice(device, choice, storage = typeof window === "undefined" ? null : window.localStorage) {
|
||||
const value = choice === "auto" ? "auto" : "ask";
|
||||
try {
|
||||
storage?.setItem(IMAGE_KEYS[device], value);
|
||||
} catch {
|
||||
/* The choice then only lasts for this visit. */
|
||||
}
|
||||
return value;
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
import assert from "node:assert/strict";
|
||||
import test from "node:test";
|
||||
import { IMAGE_KEYS, deviceClass, readImageChoice, writeImageChoice } from "./mail-images.js";
|
||||
|
||||
function memory() {
|
||||
const data = {};
|
||||
return { getItem: (key) => (key in data ? data[key] : null), setItem: (key, value) => { data[key] = value; } };
|
||||
}
|
||||
|
||||
test("pictures are asked for by default and the choice is kept per device kind", () => {
|
||||
const store = memory();
|
||||
assert.equal(readImageChoice("touch", store), "ask");
|
||||
assert.equal(writeImageChoice("desktop", "auto", store), "auto");
|
||||
assert.equal(readImageChoice("desktop", store), "auto");
|
||||
assert.equal(readImageChoice("touch", store), "ask");
|
||||
assert.equal(writeImageChoice("touch", "anything", store), "ask");
|
||||
assert.ok(IMAGE_KEYS.desktop !== IMAGE_KEYS.touch);
|
||||
});
|
||||
|
||||
test("a blocked store and a missing media query fall back to the safe choice", () => {
|
||||
const broken = { getItem() { throw new Error("blocked"); }, setItem() { throw new Error("blocked"); } };
|
||||
assert.equal(readImageChoice("desktop", broken), "ask");
|
||||
assert.equal(writeImageChoice("desktop", "auto", broken), "auto");
|
||||
assert.equal(readImageChoice("desktop", null), "ask");
|
||||
assert.equal(deviceClass(null), "desktop");
|
||||
assert.equal(deviceClass(function () { return { matches: true }; }), "touch");
|
||||
assert.equal(deviceClass(function () { throw new Error("x"); }), "desktop");
|
||||
});
|
||||
@@ -1,4 +1,4 @@
|
||||
import { useState } from "react";
|
||||
import { useEffect, useState } from "react";
|
||||
|
||||
// The cleaned HTML of a mail is shown in a frame without scripts and without any reach into the app:
|
||||
// an empty `sandbox` gives it its own origin, and the policy inside allows no loads except inline images.
|
||||
@@ -12,9 +12,33 @@ export function mailDocument(html) {
|
||||
+ `</head><body>${html}</body></html>`;
|
||||
}
|
||||
|
||||
export function MailBody({ html, text, truncated, notices = [] }) {
|
||||
// `remote` describes the pictures of other servers: { key, count, auto, load }. `load` asks the app's server for them.
|
||||
export function MailBody({ html, text, truncated, notices = [], remote = null }) {
|
||||
const [view, setView] = useState(html ? "html" : "text");
|
||||
const [withImages, setWithImages] = useState(null);
|
||||
const [busy, setBusy] = useState(false);
|
||||
const [problem, setProblem] = useState("");
|
||||
const mode = html ? view : "text";
|
||||
const shownHtml = withImages ? withImages.html : html;
|
||||
|
||||
async function loadImages() {
|
||||
setBusy(true);
|
||||
setProblem("");
|
||||
try {
|
||||
setWithImages(await remote.load());
|
||||
} catch (err) {
|
||||
setProblem(err.message || "Die Bilder konnten nicht geladen werden.");
|
||||
} finally {
|
||||
setBusy(false);
|
||||
}
|
||||
}
|
||||
|
||||
useEffect(() => {
|
||||
setWithImages(null);
|
||||
setProblem("");
|
||||
if (remote?.auto && remote.count > 0) loadImages();
|
||||
}, [remote?.key]);
|
||||
|
||||
return (
|
||||
<div className="mail-reading">
|
||||
{html ? (
|
||||
@@ -25,8 +49,16 @@ export function MailBody({ html, text, truncated, notices = [] }) {
|
||||
) : null}
|
||||
{mode === "html" ? (
|
||||
<>
|
||||
<iframe className="mail-frame" title="Nachricht" sandbox="allow-popups allow-popups-to-escape-sandbox" referrerPolicy="no-referrer" srcDoc={mailDocument(html)} />
|
||||
<p className="muted">Aus Sicherheitsgründen sind Skripte, eingebettete Formulare und Bilder von fremden Servern entfernt. Links öffnen externe Seiten: nur bekannten Absendern vertrauen.</p>
|
||||
{remote && remote.count > 0 && !withImages ? (
|
||||
<p className="mail-images">
|
||||
<button type="button" className="quiet" disabled={busy} onClick={loadImages}>{busy ? "Bilder werden geladen …" : `Bilder nachladen (${remote.count})`}</button>
|
||||
<span className="muted">Unsere Server laden die Bilder für dich, der Absender sieht deine Adresse nicht. Er kann aber erkennen, dass die Nachricht geöffnet wurde.</span>
|
||||
</p>
|
||||
) : null}
|
||||
{withImages ? <p className="muted">{withImages.loaded} Bild(er) geladen{withImages.failed ? `, ${withImages.failed} nicht verfügbar oder nicht sicher` : ""}.</p> : null}
|
||||
{problem ? <p className="error">{problem}</p> : null}
|
||||
<iframe className="mail-frame" title="Nachricht" sandbox="allow-popups allow-popups-to-escape-sandbox" referrerPolicy="no-referrer" srcDoc={mailDocument(shownHtml)} />
|
||||
<p className="muted">Aus Sicherheitsgründen sind Skripte und eingebettete Formulare entfernt, Bilder von fremden Servern erst nach deiner Freigabe sichtbar. Links öffnen externe Seiten: nur bekannten Absendern vertrauen.</p>
|
||||
</>
|
||||
) : (
|
||||
<>
|
||||
|
||||
@@ -2,6 +2,7 @@ import { DocumentView } from "../pages/DocumentView.jsx";
|
||||
import { useEffect, useRef, useState } from "react";
|
||||
import { api, documentToText, downloadMailPart, fetchExternal, fetchMailPart, markDirty, textToDocument } from "../api.js";
|
||||
import { MailAttachments, MailBody } from "../mail-view.jsx";
|
||||
import { deviceClass, readImageChoice } from "../mail-images.js";
|
||||
import { MarkdownView } from "../markdown.jsx";
|
||||
import { NoteEditor } from "../notes.jsx";
|
||||
import { DirectionColumn, directionWord } from "../pages/content.jsx";
|
||||
@@ -270,7 +271,18 @@ export function MailStage({ element, context, binding, shownDirection }) {
|
||||
<button type="button" disabled={page >= pages} onClick={() => setPage((current) => Math.min(pages, current + 1))}>Nächste Seite</button>
|
||||
</div>
|
||||
) : null}
|
||||
<MailBody html={letter.html} text={letter.text} truncated={letter.text_truncated} notices={letter.notices} />
|
||||
<MailBody
|
||||
html={letter.html}
|
||||
text={letter.text}
|
||||
truncated={letter.text_truncated}
|
||||
notices={letter.notices}
|
||||
remote={{
|
||||
key: element.id,
|
||||
count: letter.remote_images || 0,
|
||||
auto: readImageChoice(deviceClass()) === "auto",
|
||||
load: () => api(`/api/v1/external-refs/${element.id}/mail/html-with-images${context?.kind && context?.id ? `?context_kind=${context.kind}&context_id=${context.id}` : ""}`),
|
||||
}}
|
||||
/>
|
||||
{binding?.rights?.download ? <button type="button" onClick={openOriginal}>Vollständige E-Mail herunterladen</button> : null}
|
||||
</>
|
||||
) : (
|
||||
|
||||
Reference in new issue
Block a user