"use client"; import { useCallback, useEffect, useState } from "react"; import { GAP_LIMITS, editGaps } from "umtool-report-to-video/gaps"; import type { Span } from "./benchMedia"; // --------------------------------------------------------------------------- // GAPS, EDITED (lifted out of ClipBench.tsx; lane/reel-bench): seconds cut // out of the middle of a span (umtool-report-to-video gaps.mjs), so it plays // as one run of jump cuts. A span is dragged on the waveform with Alt held // (or after `g` / Select a gap) and written by Cut gap (or `g`); a click on a // gap's band puts it back -- or, where a gap has settings of its own (a // clip's: its mark and why, lane gap-cue), picks it, its settings and Remove // shown under the waveform; Undo writes the list back as it was. Written AT // ONCE, through the caller's writer -- a clip's window writer (`gapAdd` / // `gapRemove`) or a reel's beat writer -- which refuses what the build would; // the rule is checked here first (`refusal`), with the same sentences, so a // refusal is said before anything is sent. // --------------------------------------------------------------------------- /** A gap's own settings (common gapCue.mjs): its mark over the clip's, what it was cut for. */ export type GapOwn = { mark?: boolean; why?: "dead-air" | "idle" | "other"; reason?: string; corner?: "tl" | "tr" | "bl" | "br" }; /** A gap as saved: its seconds and its own settings. */ export type SavedGap = Span & GapOwn; /** One gap's own settings changed, its seconds as they are (the window writer's `gapEdit`; null drops a key). */ export type GapEdit = { at: number; mark?: boolean | null; why?: "dead-air" | "idle" | "other" | null; reason?: string | null; corner?: "tl" | "tr" | "bl" | "br" | null; }; export type GapOp = { add: Span } | { remove: Span } | { set: Span[] } | { edit: GapEdit }; /** What a grown gap kept of its own settings, as a note says it ("its mark on, dead air, its words, its corner tr"); "" for none. */ export function keptSaid(g: SavedGap): string { return [ typeof g.mark === "boolean" ? `its mark ${g.mark ? "on" : "off"}` : null, g.why ? (g.why === "dead-air" ? "dead air" : g.why) : null, g.reason ? `its words “${g.reason}”` : null, g.corner ? `its corner ${g.corner}` : null, ].filter(Boolean).join(", "); } /** * What the span being dragged for a gap would do, said WHILE it is dragged * (lane/clip-fixes, operator 6 Nov: "the gap tool gets blocked at random * invisible walls"): the writer's refusal (`why`, the same sentence the * writer would answer with), the wall the drag stopped at (`wall`), the * neighbour a touching span merges into (`merged`, the gap it would make), and * `short` under the shortest gap. */ export type GapLive = { why: string | null; wall: string | null; merged: Span | null; short: boolean }; const r2 = (n: number) => Number(n.toFixed(2)); export function useGapEditor({ key, gaps, span, refusal, write, said, setNote, fmt, wall, settings = false, }: { /** What the gaps belong to: another one starts with nothing selected and nothing to undo. */ key: string; /** The gaps as saved (a clip's with their own settings). */ gaps: SavedGap[]; /** Where a gap may go (the clip's cut or extent; a beat's span). */ span: Span; /** Why `next` cannot be written, or null. */ refusal: (next: Span[]) => string | null; /** Write the op (`next` the list it makes); resolves whether it was written. */ write: (op: GapOp, next: Span[]) => Promise; /** The sentence after a write, with the list it made ("the cut plays 6.20 s, one segment"). */ said: (next: Span[]) => string; setNote: (s: string | null) => void; fmt: (t: number) => string; /** The sentence for a drag stopped at `span`'s edge (the clip's cut, a beat's end); a plain one without. */ wall?: (side: "start" | "end", at: number) => string; /** * A gap has settings of its own (a clip's: its mark, its why; lane gap-cue): a click on its band PICKS it -- * its settings and Remove under the waveform -- instead of putting it back. A reel beat's has none. */ settings?: boolean; }) { const [sel, setSelState] = useState(null); const [live, setLive] = useState(null); const setSel = useCallback((s: Span | null | ((p: Span | null) => Span | null)) => { setSelState(s); if (s === null) setLive(null); }, []); const [pick, setPick] = useState(false); const [undo, setUndo] = useState<{ gaps: Span[]; what: string } | null>(null); // The saved gap whose settings are shown (a click on its band, `settings` only): by its seconds. const [picked, setPicked] = useState(null); useEffect(() => { setSel(null); setPick(false); setUndo(null); setPicked(null); }, [key, setSel]); // The gap picked as it is saved now; none once it is gone (put back, merged into another). const pickedGap = picked ? (gaps.find((g) => g.from === picked.from && g.to === picked.to) ?? null) : null; useEffect(() => { if (picked && !pickedGap) setPicked(null); }, [picked, pickedGap]); const run = useCallback( async (op: GapOp, next: Span[], what: string) => { const why = refusal(next); if (why) { setNote(`not cut: ${why}`); return false; } const before = gaps; const ok = await write(op, next); if (ok) { setUndo({ gaps: before, what }); setNote(`${what} — ${said(next)}`); } return ok; }, [refusal, write, said, setNote, gaps], ); const cut = useCallback(async () => { const s = sel; if (!s) return; const g = { from: r2(s.from), to: r2(s.to) }; let next: Span[]; try { next = editGaps(gaps, { add: g }); } catch (e) { setNote(e instanceof Error ? e.message : String(e)); return; } // A span touching a gap GROWS it (editGaps): say which one it became, and what of the gaps it took in it kept // (gaps.mjs mergedOwnKeys: their why and words when they agree, the first corner, the mark on if any was). const grown = next.find((x) => x.from <= g.from + 1e-6 && x.to >= g.to - 1e-6) as SavedGap | undefined; const merged = grown && (Math.abs(grown.from - g.from) > 0.005 || Math.abs(grown.to - g.to) > 0.005) ? grown : null; const kept = merged ? keptSaid(merged) : ""; const ok = await run( { add: g }, next, merged ? `cut ${fmt(g.from)}–${fmt(g.to)} out, merged with the gap beside it into ${fmt(merged.from)}–${fmt(merged.to)}${kept ? ` — kept ${kept}` : ""}` : `cut ${fmt(g.from)}–${fmt(g.to)} out`, ); if (ok) { setSel(null); setPick(false); } }, [sel, gaps, run, setNote, fmt, setSel]); const remove = useCallback( (g: Span) => { const next = gaps.filter((x) => x.from !== g.from || x.to !== g.to); void run({ remove: g }, next, `put ${fmt(g.from)}–${fmt(g.to)} back`); }, [gaps, run, fmt], ); /** * A click on a gap's band: with `settings`, it picks the gap (again: lets it go) and says so; without, * it puts the gap back, as it always has. */ const band = useCallback( (g: Span) => { if (!settings) { remove(g); return; } setSel(null); setPick(false); setPicked((p) => (p && p.from === g.from && p.to === g.to ? null : { from: g.from, to: g.to })); }, [settings, remove, setSel], ); /** One of the picked gap's own settings changed (its mark, its why): one write, an undo point, Undo here. */ const editPicked = useCallback( (change: Omit) => { const g = pickedGap; if (!g) return Promise.resolve(false); const edit: GapEdit = { at: Number(((g.from + g.to) / 2).toFixed(3)), ...change }; let next: Span[]; try { next = editGaps(gaps, { edit }); } catch (e) { setNote(e instanceof Error ? e.message : String(e)); return Promise.resolve(false); } const said = [ "mark" in change ? `its mark ${change.mark === true ? "on" : change.mark === false ? "off" : "as the clip's"}` : null, "why" in change ? `cut for ${change.why === "dead-air" ? "dead air" : change.why ?? "no reason given"}` : null, "reason" in change ? (change.reason ? `its mark says “${change.reason}”` : "its own words dropped") : null, "corner" in change ? `its mark at ${change.corner ?? "the clip's corner"}` : null, ].filter(Boolean).join(", "); return run({ edit }, next, `the gap ${fmt(g.from)}–${fmt(g.to)}: ${said}`); }, [pickedGap, gaps, run, setNote, fmt], ); /** `G`: the gap the playhead is in (or at the edge of). */ const removeAt = useCallback( (t: number | null) => { const g = t == null ? null : gaps.find((x) => t >= x.from - 0.05 && t <= x.to + 0.05); if (!g) { setNote(settings ? "the playhead is not on a gap — play to it, or pick its band and Remove" : "the playhead is not on a gap — play to it, or click its band"); return; } remove(g); }, [gaps, remove, setNote, settings], ); const undoLast = useCallback(() => { const u = undo; if (!u) return; void write({ set: u.gaps }, u.gaps).then((ok) => { if (!ok) return; setUndo(null); setNote(`undone — ${u.what} is reversed`); }); }, [undo, write, setNote]); /** * A span dragged for a gap: kept inside the span, and checked on EVERY move * with the writer's own rule (`refusal`, the full validateGaps), so a wall is * said while the span is being dragged into it, not after release. Under the * shortest gap on release, dropped and said. */ const select = useCallback( (s: Span, done: boolean) => { const from = Math.max(span.from, s.from); const to = Math.min(span.to, s.to); // The clamp biting: the drag went past where a gap may go. const side = s.from < span.from - 0.01 ? "start" : s.to > span.to + 0.01 ? "end" : null; const at = side === "start" ? span.from : span.to; const wallSaid = side ? (wall ? wall(side, at) : `stopped at the ${side} of where a gap may go (${fmt(at)})`) : null; const short = to - from < GAP_LIMITS.min; let why: string | null = null; let merged: Span | null = null; if (!short) { const g = { from: r2(from), to: r2(to) }; try { const next = editGaps(gaps, { add: g }); why = refusal(next); const grown = next.find((x) => x.from <= g.from + 1e-6 && x.to >= g.to - 1e-6); if (grown && (Math.abs(grown.from - g.from) > 0.005 || Math.abs(grown.to - g.to) > 0.005)) merged = grown; } catch (e) { why = e instanceof Error ? e.message : String(e); } } if (!done) { setSelState(to > from ? { from, to } : null); setLive(to > from ? { why: short ? `a gap is at least ${GAP_LIMITS.min} s` : why, wall: wallSaid, merged, short } : null); return; } if (short) { setSel(null); if (to - from > 0.01) setNote(`a gap is at least ${GAP_LIMITS.min} s — drag a longer span${wallSaid ? `; ${wallSaid}` : ""}`); else if (wallSaid) setNote(wallSaid); return; } setSelState({ from, to }); setLive({ why, wall: wallSaid, merged, short }); const head = why ? `this span cannot be cut: ${why}` : merged ? `${(to - from).toFixed(2)} s selected — it touches a gap, so Cut gap (g) makes them one: ${fmt(merged.from)}–${fmt(merged.to)}` : `${(to - from).toFixed(2)} s selected — Cut gap (g) to take it out, Hear it to listen first`; setNote(wallSaid ? `${head} · ${wallSaid}` : head); }, [span.from, span.to, gaps, refusal, setNote, fmt, wall, setSel], ); return { sel, setSel, live, pick, setPick, undo, cut, remove, removeAt, undoLast, select, settings, picked: pickedGap, setPicked, band, editPicked }; } export type GapEditor = ReturnType;