Files
crosscode-platform/apps/website/frontend/src/admin/framing.tsx
T
joshuaandClaude Opus 5.5 f5f778f0d7 website 1.11.0: layouts and backgrounds for the blocks; a video for the top of the page
- Blocks get layouts (BLOCK_VARIANTS: text standard/centred; image and text photo left,
  right or above; quote centred/with a line; call to action centred/banner), stored on
  Section.variant and checked against the type; an unknown one draws as the default, as
  every block looked before. The demo's image-text block id is now image-left.
- Each block has its own background (Section.background, migration
  20261201000000_block_background), in the published source only when set, put back by
  Discard and Restore, drawn as the homepage's (data-bg).
- MP4 uploads up to 20 MB for the hero's Video layout: QuickTime refused, metadata boxes
  (udta, meta, uuid) blanked into free boxes of the same size so where it was filmed is
  gone and the frame offsets hold; size read from tkhd. GET /media lists mime.
- Admin: a block's Layout and Background when open; the hero's Video field (only videos;
  photo fields only photos); the media library shows and takes videos. An upload over the
  limit says so instead of "That request isn't valid."
- Tests: backend (152, three new); e2e for block layouts and a video upload.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-01 09:57:02 +02:00

177 lines
9.7 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"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, useAdmin } 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" };
}
// A ministry's, leader's or event's photo: the home page section's frame for the layout chosen.
if (type === "ministry") {
const vm = design?.variants?.ministries;
if (vm === "bands") return t === "bold" ? { ratio: 4 / 3, label: "Beside its name on the home page (4:3 on the Ministries page)" } : { ratio: 16 / 10, label: "Beside its name on the home page (4:3 on the Ministries page)" };
return { ratio: 4 / 3, label: "On its card on the home page and beside it on the Ministries page" };
}
if (type === "leader") {
const round = t === "heritage" || t === "community";
if (t === "bold") return { ratio: 3 / 4, label: "Their photo on the home page (square on the Leadership page)" };
return { ratio: 1, round, label: round ? "Their photo, in a circle" : "Their photo" };
}
if (type === "event") return { ratio: 16 / 10, label: "On the event's card (a little taller on its own page)" };
if (type === "invitation") return { ratio: 3, label: "Behind the invitation, darkened (the Image background layout)" };
// A block's own layout (passed in as the design's variant for its type).
if (type === "image-text" && v === "image-above") return { ratio: 16 / 9, label: "Above the text, the page's full width" };
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);
/**
* Framing for a row with one photo (a ministry, a leader, an event): saved as the row's `framing`,
* in the frame of `type` (frameFor) for the site's current template and layouts.
*/
export function RowFraming({ type, value, onChange, children }: { type: "ministry" | "leader" | "event"; value: Focus | null | undefined; onChange: (f: Focus | null) => void; children: React.ReactNode }) {
const design = useAdmin<{ design: Design }>("/design");
const ctx: Framing = { get: () => value ?? undefined, set: (_, f) => onChange(f), frame: (key, i) => frameFor(design.data?.design, type, key, i) };
return <FramingContext.Provider value={ctx}>{children}</FramingContext.Provider>;
}
/** 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>
);
}