"use client"; import { useEffect, useRef, useState } from "react"; import { bandBox } from "umtool-report-to-video/post-censor"; // THE CENSOR BOXES, DRAWN (post-censor.mjs; knobs.md, "A post item"): the // attachment on a still frame -- a picture as it is, a video paused at the // bench's playhead (or its in point) -- shown as the cut shows it (its crop), // each box over it; drag to draw a new one, × to take one away. The boxes are // fractions of the picture as shown, which is what the render blurs. // // THE BAND (post-censor.mjs `band`): its line (two, between) drawn across the // frame -- an upright one for left/right -- each dragged to where the censor // starts; the region it censors tinted. A drag writes the knob (`onBand`). type Box = number[]; export type Band = { below?: number; above?: number; between?: number[]; left?: number; right?: number }; type Props = { project: string; media: { src: string; kind: string; width?: number; height?: number; crop?: number[] }; /** The video's source second to hold still. */ at: number; boxes: Box[]; onChange: (boxes: Box[]) => void; index: number; /** The band (null: none), and its writer: each line dragged sets its edge. */ band?: Band | null; onBand?: (band: Band) => void; }; /** A band's lines: `[{ edge, at, upright }]` (between: its two). */ const linesOf = (band: Band | null | undefined) => { if (!band) return [] as Array<{ edge: string; at: number; upright: boolean; k?: number }>; const out: Array<{ edge: string; at: number; upright: boolean; k?: number }> = []; if (typeof band.below === "number") out.push({ edge: "below", at: band.below, upright: false }); else if (typeof band.above === "number") out.push({ edge: "above", at: band.above, upright: false }); else if (Array.isArray(band.between)) band.between.forEach((at, k) => out.push({ edge: "between", at, upright: false, k })); if (typeof band.left === "number") out.push({ edge: "left", at: band.left, upright: true }); else if (typeof band.right === "number") out.push({ edge: "right", at: band.right, upright: true }); return out; }; const r3 = (v: number) => Math.round(v * 1000) / 1000; const r4 = (v: number) => Math.round(v * 10000) / 10000; const clamp = (v: number) => Math.min(1, Math.max(0, v)); export default function BlurBoxPad({ project, media, at, boxes, onChange, index, band = null, onBand }: Props) { const pad = useRef(null); const [line, setLine] = useState<{ edge: string; k?: number } | null>(null); const region = bandBox(band) as number[] | null; const video = useRef(null); const [drag, setDrag] = useState<{ x: number; y: number; x2: number; y2: number } | null>(null); const crop = Array.isArray(media.crop) && media.crop.length === 4 ? media.crop : [0, 0, 1, 1]; const aspect = media.width && media.height ? (media.width * crop[2]) / (media.height * crop[3]) : 16 / 9; const src = `/api/report/post-item?project=${encodeURIComponent(project)}&file=${encodeURIComponent(media.src)}`; const inner: React.CSSProperties = { position: "absolute", width: `${100 / crop[2]}%`, height: `${100 / crop[3]}%`, left: `${(-crop[0] / crop[2]) * 100}%`, top: `${(-crop[1] / crop[3]) * 100}%`, maxWidth: "none", objectFit: "fill", }; useEffect(() => { const v = video.current; if (!v) return; try { v.currentTime = at; } catch { // not seekable yet } }, [at]); const where = (ev: React.PointerEvent) => { const r = pad.current!.getBoundingClientRect(); return { x: clamp((ev.clientX - r.left) / r.width), y: clamp((ev.clientY - r.top) / r.height) }; }; const box = (d: { x: number; y: number; x2: number; y2: number }): Box => { const x = Math.min(d.x, d.x2); const y = Math.min(d.y, d.y2); return [r4(x), r4(y), r4(Math.abs(d.x2 - d.x)), r4(Math.abs(d.y2 - d.y))]; }; return (
{ if ((ev.target as HTMLElement).closest("button")) return; // A band's line: dragged, not a box drawn. const handle = (ev.target as HTMLElement).closest("[data-post-bench-band-line]") as HTMLElement | null; if (handle && band && onBand) { (ev.currentTarget as HTMLDivElement).setPointerCapture(ev.pointerId); const k = handle.getAttribute("data-band-k"); setLine({ edge: handle.getAttribute("data-post-bench-band-line") ?? "", ...(k ? { k: Number(k) } : {}) }); return; } (ev.currentTarget as HTMLDivElement).setPointerCapture(ev.pointerId); const p = where(ev); setDrag({ x: p.x, y: p.y, x2: p.x, y2: p.y }); }} onPointerMove={(ev) => { if (line && band && onBand) { const p = where(ev); const upright = line.edge === "left" || line.edge === "right"; const v = r3(Math.min(0.98, Math.max(0.02, upright ? p.x : p.y))); if (line.edge === "between") { const b = [...(band.between ?? [0.3, 0.6])]; b[line.k ?? 0] = v; onBand({ ...band, between: [Math.min(b[0], b[1]), Math.max(b[0], b[1])].map(r3) }); } else onBand({ ...band, [line.edge]: v }); return; } if (!drag) return; const p = where(ev); setDrag({ ...drag, x2: p.x, y2: p.y }); }} onPointerUp={() => { if (line) { setLine(null); return; } if (!drag) return; const b = box(drag); setDrag(null); if (b[2] >= 0.01 && b[3] >= 0.01 && boxes.length < 8) onChange([...boxes, b]); }} > {media.kind === "image" ? ( // eslint-disable-next-line @next/next/no-img-element ) : (