Website 1.7.0: move and zoom photos in their frame; delete unused photos

- A section's photo (hero, welcome, invitation, image-text) shows in the
  shape of its frame for the current template and layout, with a Move and
  zoom dialog: drag or arrow keys to choose what stays in view, zoom to
  300%. Saved with the section as data.framing ({ x, y, zoom } by media
  id), a draft until published. The site draws it with object-position
  and scale() around that point; Bold split/collage frames now clip, and
  a zoomed photo stays inside Heritage's mat. Framing for photos a section
  no longer shows is dropped on save.
- Media library: each photo says where it's used; one used nowhere can be
  deleted with its files (DELETE /admin/media/:id, 409 IN_USE otherwise,
  audited as media_deleted).

Checked: Website backend tests (135) and typecheck, frontend typecheck,
lint and production build, and an e2e test of both flows in Chromium.
Lighthouse not run.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-27 23:34:10 +02:00
co-authored by Claude Opus 5.5
parent 25100bd749
commit a9215ac4e4
22 changed files with 494 additions and 50 deletions
+152
View File
@@ -0,0 +1,152 @@
"use client";
/**
* Framing a photo: see it in the shape of the frame the site puts it in (the template and layout
* chosen), drag it to move it and zoom in to crop. Saved with the section (`data.framing`, by media
* id), so it's a draft like any other change: previewed, then published.
*/
import { createContext, useContext, useEffect, useRef, useState } from "react";
import { focusStyle } from "@/site/parts";
import type { Focus } from "@/site/types";
import { Btn, cx } from "./ui";
export type Frame = { ratio: number; round?: boolean; label: string };
export const CENTRE: Focus = { x: 50, y: 50, zoom: 1 };
type Design = { template?: string; variants?: Record<string, string> } | null | undefined;
/**
* The frame a section's photo is shown in, as wide as the site's desktop layout draws it
* (site/sections). `key` is the field (images, imageId, photoId); `index` the photo in a list.
*/
export function frameFor(design: Design, type: string, key: string, index = 0): Frame {
const t = design?.template ?? "heritage";
const v = design?.variants?.[type];
if (type === "hero") {
if (v === "text") return { ratio: 16 / 9, label: "Not shown: the top of the page is set to Text only" };
if (v === "split") return t === "bold" ? { ratio: 4 / 5, label: "Half the screen, beside the headline" } : { ratio: 4 / 3, label: "Beside the headline" };
if (v === "collage") {
if (t === "bold") return { ratio: 3 / 5, label: `Column ${index + 1} of 3, behind the headline` };
if (t === "community") return { ratio: 1, round: index === 2, label: `Photo ${index + 1} of the overlapping three` };
return { ratio: t === "heritage" ? 3 / 4 : 1, label: `Photo ${index + 1} of 3` };
}
if (t === "bold") return { ratio: 16 / 9, label: "The whole screen, behind the headline (taller on phones)" };
if (t === "heritage") return { ratio: 16 / 7, label: "Wide, above the headline" };
if (t === "community") return { ratio: 21 / 9, label: "Wide, behind the headline card (16:9 on phones)" };
return { ratio: 16 / 9, label: "Below the headline" };
}
if (type === "welcome") {
if (key === "photoId") return { ratio: 4 / 5, label: "The pastor's photo" };
return t === "bold" ? { ratio: 1, label: "Beside the welcome" } : { ratio: 4 / 3, label: "Beside the welcome" };
}
if (type === "invitation") return { ratio: 3, label: "Behind the invitation, darkened (the Image background layout)" };
return { ratio: 4 / 3, label: "Beside the text" };
}
/** What DataForm shares with each ImagePicker in a section: its framing, and the frame per field. */
export type Framing = {
get: (id: string) => Focus | undefined;
set: (id: string, f: Focus | null) => void;
frame: (key: string, index?: number) => Frame;
};
export const FramingContext = createContext<Framing | null>(null);
export const useFraming = () => useContext(FramingContext);
/** The photo drawn in its frame, as the site draws it (object-cover, point kept in view, zoom). */
export function Framed({ url, frame, focus, className, imgRef }: { url: string; frame: Frame; focus: Focus | undefined; className?: string; imgRef?: React.Ref<HTMLImageElement> }) {
return (
<div className={cx("relative overflow-hidden bg-gray-100", frame.round ? "rounded-full" : "rounded", className)} style={{ aspectRatio: String(frame.ratio) }}>
<img ref={imgRef} src={url} alt="" draggable={false} className="pointer-events-none absolute inset-0 h-full w-full select-none object-cover" style={focusStyle(focus)} />
</div>
);
}
const clamp = (n: number, lo: number, hi: number) => Math.min(hi, Math.max(lo, n));
/** Drag to move, slider (or wheel) to zoom; Done keeps it, Cancel leaves it as it was. */
export function FramingDialog({ url, alt, frame, initial, onDone, onClose }: {
url: string; alt: string; frame: Frame; initial: Focus | undefined; onDone: (f: Focus | null) => void; onClose: () => void;
}) {
const [f, setF] = useState<Focus>(initial ?? CENTRE);
const box = useRef<HTMLDivElement>(null);
const img = useRef<HTMLImageElement>(null);
const drag = useRef<{ x: number; y: number; start: Focus } | null>(null);
useEffect(() => {
const esc = (e: KeyboardEvent) => { if (e.key === "Escape") onClose(); };
window.addEventListener("keydown", esc);
return () => window.removeEventListener("keydown", esc);
}, [onClose]);
/** How far the photo can move (px) across 0–100%: its drawn size, zoomed, less the frame's. */
const room = () => {
const b = box.current?.getBoundingClientRect();
const i = img.current;
if (!b || !i?.naturalWidth) return { x: 0, y: 0 };
const cover = Math.max(b.width / i.naturalWidth, b.height / i.naturalHeight);
return { x: i.naturalWidth * cover * f.zoom - b.width, y: i.naturalHeight * cover * f.zoom - b.height };
};
const down = (e: React.PointerEvent) => {
(e.currentTarget as HTMLElement).setPointerCapture(e.pointerId);
drag.current = { x: e.clientX, y: e.clientY, start: f };
};
const move = (e: React.PointerEvent) => {
const d = drag.current;
if (!d) return;
const r = room();
// Dragging the photo right shows more of its left: the point in view moves left.
setF({
...d.start,
x: r.x > 1 ? clamp(d.start.x - ((e.clientX - d.x) / r.x) * 100, 0, 100) : d.start.x,
y: r.y > 1 ? clamp(d.start.y - ((e.clientY - d.y) / r.y) * 100, 0, 100) : d.start.y,
});
};
const key = (e: React.KeyboardEvent) => {
const step = e.shiftKey ? 10 : 2;
const by: Record<string, [number, number]> = { ArrowLeft: [step, 0], ArrowRight: [-step, 0], ArrowUp: [0, step], ArrowDown: [0, -step] };
const m = by[e.key];
if (!m) return;
e.preventDefault();
setF({ ...f, x: clamp(f.x + m[0], 0, 100), y: clamp(f.y + m[1], 0, 100) });
};
const round = (x: Focus): Focus => ({ x: Math.round(x.x * 10) / 10, y: Math.round(x.y * 10) / 10, zoom: Math.round(x.zoom * 100) / 100 });
const centred = f.x === 50 && f.y === 50 && f.zoom === 1;
return (
<div className="fixed inset-0 z-[60] flex items-center justify-center bg-black/50 p-4" role="dialog" aria-modal="true" aria-label="Move and zoom the photo">
<div className="max-h-[95vh] w-full max-w-2xl overflow-y-auto rounded-lg bg-white p-6 shadow-xl">
<h2 className="text-lg font-semibold">Move and zoom</h2>
<p className="mt-1 text-sm text-gray-600">{frame.label}. Drag the photo to choose what shows; zoom in to crop closer.</p>
<div
ref={box}
tabIndex={0}
aria-label={`${alt || "The photo"} in its frame. Arrow keys move it.`}
onPointerDown={down}
onPointerMove={move}
onPointerUp={() => { drag.current = null; }}
onPointerCancel={() => { drag.current = null; }}
onKeyDown={key}
onWheel={(e) => setF((x) => ({ ...x, zoom: clamp(x.zoom - e.deltaY * 0.002, 1, 3) }))}
className="mx-auto mt-4 cursor-grab touch-none outline-none ring-brand focus-visible:ring-2 active:cursor-grabbing"
style={{ maxWidth: frame.ratio < 1 ? `${Math.round(360 * frame.ratio)}px` : undefined }}
>
<Framed url={url} frame={frame} focus={f} imgRef={img} />
</div>
<label className="mt-5 flex items-center gap-3 text-sm text-gray-700">
Zoom
<input type="range" min={1} max={3} step={0.01} value={f.zoom} onChange={(e) => setF({ ...f, zoom: Number(e.target.value) })} className="flex-1" />
<span className="w-12 text-right tabular-nums">{Math.round(f.zoom * 100)}%</span>
</label>
<p className="mt-2 text-xs text-gray-500">On phones the frame can be a different shape; the part you chose stays in view.</p>
<div className="mt-5 flex flex-wrap items-center gap-3">
<Btn kind="primary" onClick={() => onDone(centred ? null : round(f))}>Done</Btn>
<Btn onClick={onClose}>Cancel</Btn>
<Btn kind="link" onClick={() => setF(CENTRE)} disabled={centred}>Reset</Btn>
</div>
</div>
</div>
);
}