"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 } | 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(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 {children}; } /** 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 }) { return (
); } 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(initial ?? CENTRE); const box = useRef(null); const img = useRef(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 = { 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 (

Move and zoom

{frame.label}. Drag the photo to choose what shows; zoom in to crop closer.

{ 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 }} >

On phones the frame can be a different shape; the part you chose stays in view.

onDone(centred ? null : round(f))}>Done Cancel setF(CENTRE)} disabled={centred}>Reset
); }