"use client"; import Link from "next/link"; import { useEffect, useLayoutEffect, useMemo, useRef, useState } from "react"; import { createPortal } from "react-dom"; import { blockVerbs, commandOf, fromBlockValues } from "@/lib/cli/verbs.mjs"; import { clipBenchHref, ingredientHref, postBenchHref } from "@/lib/hrefs"; import { openCommand } from "@/components/shell/JumpPalette"; import type { CliVerb } from "@/components/shell/CommandForm"; import { cn } from "@/lib/utils"; // --------------------------------------------------------------------------- // A BLOCK'S MENU (umtool redesign L8b): right-click a block of the workspace's // strip, or its ⋯, or Shift+F10 / the context-menu key on it. On top, what needs // no form -- Edit in place (E), Play from here, Move left / right (Alt+← →), // Remove (the top bar's Undo takes it back), the full bench, the recording's // life cycle -- the strip's own writers. Under them, every verb of the registry // whose row names this block's kind (lib/cli/verbs.mjs `blocks`), grouped by its // command; choosing one opens THE form ⌘K opens (shell/CommandForm, in the // palette), filled in from the block by the row's own `fromBlock` rules. A row // with `nav` is a page, not a run: it is a link here. // // Keys: ↑ ↓ (Home, End, a letter) move, Enter or Space chooses, Esc or Tab // closes; the focus goes back to the block. // --------------------------------------------------------------------------- /** What a block tells its menu: verbs.mjs BLOCK_FIELDS (the fromBlock templates read these). */ export type BlockInfo = { project: string; id: string; type: string; /** Its own index in the timeline (null: the intro, or a twin the view did not place). */ at: number | null; /** Its id is in the timeline more than once and the view did not say which it is. */ twin?: boolean; start?: number | null; end?: number | null; cutStart?: number | null; cutEnd?: number | null; second?: number | null; video?: string | null; channel?: string | null; }; /** The strip's own writers, as the workspace does them. */ export type BlockActions = { /** The block at this place in the strip (two blocks may share an id). */ info: (place: number) => BlockInfo | null; /** The timeline's length (Move right stops at its end). */ entries: number; busy: boolean; edit: (b: BlockInfo) => void; play: (b: BlockInfo) => void; /** By the block's own index (`at`), never by its id. */ move: (b: BlockInfo, d: 1 | -1) => void; remove: (b: BlockInfo) => void; }; /** Where it opens: at the pointer, or over the block (the keyboard, the ⋯). */ export type MenuAnchor = { x: number; y: number } | { rect: { left: number; top: number; bottom: number } }; type Item = | { key: string; label: string; hint?: string; kind: "action"; disabled?: boolean; why?: string; run: () => void } | { key: string; label: string; hint?: string; kind: "link"; href: string } | { key: string; label: string; hint?: string; kind: "verb"; row: CliVerb }; type Group = { key: string; label: string; items: Item[] }; const KIND_WORD: Record = { clip: "Clip", post: "Post item", teaser: "Card", card: "Chapter card", intro: "Intro", video: "Video" }; const COMMAND_WORD: Record = { timeline: "The cut", posts: "Post item", "trailer-beats": "Trailer" }; /** A nav row's page, from what the block filled in. */ function navHref(row: CliVerb, values: Record): string | null { if (row.nav === "ingredient") { const [ch, id] = String(values.recording ?? "").split("/"); return ch && id ? ingredientHref(ch, id) : null; } return null; } /** Why a timeline entry with no index of its own is not moved or removed here. */ export const TWIN_WHY = "this entry has a twin — use the Cut tab"; function groupsOf(info: BlockInfo, actions: BlockActions): Group[] { const entry = info.type !== "intro"; const placed = info.at !== null; const why = placed ? undefined : TWIN_WHY; const direct: Item[] = []; if (info.type === "clip" || info.type === "post") direct.push({ key: "edit", label: "Edit in place", hint: "E", kind: "action", run: () => actions.edit(info) }); direct.push({ key: "play", label: "Play from here", kind: "action", run: () => actions.play(info) }); if (entry) { // Each by the block's OWN index: a twin the view did not place is never guessed at. direct.push({ key: "move-left", label: "Move left", hint: "Alt+←", kind: "action", disabled: actions.busy || !placed || info.at === 0, why, run: () => actions.move(info, -1) }); direct.push({ key: "move-right", label: "Move right", hint: "Alt+→", kind: "action", disabled: actions.busy || !placed || (info.at ?? 0) >= actions.entries - 1, why, run: () => actions.move(info, 1) }); direct.push({ key: "remove", label: "Remove", hint: placed ? "Undo takes it back" : "a twin", kind: "action", disabled: actions.busy || !placed, why, run: () => actions.remove(info) }); } if (info.type === "clip") direct.push({ key: "bench", label: "Full bench", kind: "link", href: clipBenchHref(info.project, info.id) }); if (info.type === "post") direct.push({ key: "bench", label: "Full bench", kind: "link", href: postBenchHref(info.project, info.id) }); const rows = blockVerbs(info.type) as CliVerb[]; for (const row of rows) { if (!row.nav) continue; const href = navHref(row, fromBlockValues(row, info)); if (href) direct.push({ key: `nav:${row.verb}`, label: row.nav === "ingredient" ? "Life cycle" : row.verb, hint: `umtool ${row.verb}`, kind: "link", href }); } // The verbs, by command: the kind's own verbs (one each) first, then each command's sub-verbs. const byCmd = new Map(); for (const row of rows) { if (row.nav) continue; const c = commandOf(row.verb); byCmd.set(c, [...(byCmd.get(c) ?? []), row]); } const own: CliVerb[] = []; const multi: Group[] = []; for (const [c, list] of byCmd) { if (list.length === 1 && list[0].verb === c) own.push(list[0]); else multi.push({ key: `cmd:${c}`, label: COMMAND_WORD[c] ?? c, items: list.map((row) => ({ key: `verb:${row.verb}`, label: row.verb, hint: row.summary, kind: "verb", row })) }); } const verbGroups: Group[] = []; if (own.length) verbGroups.push({ key: "own", label: KIND_WORD[info.type] ?? info.type, items: own.map((row) => ({ key: `verb:${row.verb}`, label: row.verb, hint: row.summary, kind: "verb", row })) }); // A group of one sub-verb (trailer-beats add) joins the kind's own. for (const g of multi) { if (g.items.length === 1 && verbGroups[0]?.key === "own") verbGroups[0].items.push(g.items[0]); else verbGroups.push(g); } return [{ key: "direct", label: `${KIND_WORD[info.type] ?? info.type} ${info.id}`, items: direct }, ...verbGroups]; } export default function BlockMenu({ place, info, actions, anchor, onClose, }: { /** The block's place in the strip (its ⋯ toggles the menu: a press on it is not "outside"). */ place: number; info: BlockInfo; actions: BlockActions; anchor: MenuAnchor; /** Closed: `refocus` -- the focus goes back to the block (Esc, Tab, a choice). */ onClose: (refocus: boolean) => void; }) { const groups = useMemo(() => groupsOf(info, actions), [info, actions]); const flat = useMemo(() => groups.flatMap((g) => g.items), [groups]); const usable = (i: number) => !(flat[i]?.kind === "action" && flat[i].disabled); const [at, setAt] = useState(() => Math.max(0, flat.findIndex((_, i) => usable(i)))); const box = useRef(null); const els = useRef>([]); const [pos, setPos] = useState<{ left: number; top: number } | null>(null); // Placed inside the window: at the pointer (flipped up when there is no room below), or above the block. useLayoutEffect(() => { const el = box.current; if (!el) return; const w = el.offsetWidth; const h = el.offsetHeight; const W = window.innerWidth; const H = window.innerHeight; let left: number; let top: number; if ("rect" in anchor) { left = anchor.rect.left; top = anchor.rect.top - h - 4 >= 8 ? anchor.rect.top - h - 4 : anchor.rect.bottom + 4 + h <= H - 8 ? anchor.rect.bottom + 4 : 8; } else { left = anchor.x; top = anchor.y + h <= H - 8 ? anchor.y : Math.max(8, anchor.y - h); } setPos({ left: Math.max(8, Math.min(left, W - w - 8)), top: Math.max(8, top) }); }, [anchor]); useEffect(() => { if (pos) els.current[at]?.focus({ preventScroll: false }); }, [at, pos]); // A press outside closes it (the focus stays where that press put it); so does the window moving under it. useEffect(() => { const down = (e: PointerEvent) => { const t = e.target as Element | null; if (t?.closest?.(`[data-ws-block-more-i="${place}"]`)) return; if (box.current && !box.current.contains(t as Node)) onClose(false); }; const away = () => onClose(false); document.addEventListener("pointerdown", down, true); window.addEventListener("resize", away); window.addEventListener("blur", away); return () => { document.removeEventListener("pointerdown", down, true); window.removeEventListener("resize", away); window.removeEventListener("blur", away); }; }, [onClose, place]); const choose = (it: Item) => { if (it.kind === "action") { if (it.disabled) return; onClose(true); it.run(); } else if (it.kind === "verb") { // The focus back on the block, then the palette's dialog opened at once -- in this same event, so no key typed // in between reaches the workspace -- and closing it returns the focus to the block. onClose(true); openCommand({ row: it.row, values: fromBlockValues(it.row, info) }); } else { onClose(false); } }; const step = (d: 1 | -1, from = at) => { for (let n = 1; n <= flat.length; n++) { const i = (from + d * n + flat.length * 2) % flat.length; if (usable(i)) return i; } return from; }; const onKey = (e: React.KeyboardEvent) => { const k = e.key; // The menu's keys are its own: not the workspace's (data-ws-own-keys), not the page's. e.stopPropagation(); if (k === "ArrowDown") setAt((i) => step(1, i)); else if (k === "ArrowUp") setAt((i) => step(-1, i)); else if (k === "Home") setAt(step(1, -1)); else if (k === "End") setAt(step(-1, flat.length)); else if (k === "Escape") onClose(true); else if (k === "Tab") onClose(true); else if (k === "Enter" || k === " ") { const it = flat[at]; if (it?.kind === "link") { els.current[at]?.click(); } else if (it) choose(it); } else if (k.length === 1 && /\S/.test(k) && !e.ctrlKey && !e.metaKey && !e.altKey) { // A letter: the next item whose label starts with it. const c = k.toLowerCase(); for (let n = 1; n <= flat.length; n++) { const i = (at + n) % flat.length; if (usable(i) && flat[i].label.toLowerCase().startsWith(c)) { setAt(i); break; } } } else return; e.preventDefault(); }; let n = -1; const menu = (
e.preventDefault()} style={pos ? { left: pos.left, top: pos.top } : { left: -9999, top: 0 }} className="fixed z-50 max-h-[min(600px,calc(100dvh-16px))] w-[min(380px,calc(100vw-16px))] overflow-y-auto rounded-[3px] border border-seam-2 bg-panel py-1 text-[12.5px] text-text shadow-2xl" > {groups.map((g, gi) => (
{gi > 0 &&
}
{g.label}
{g.items.map((it) => { n += 1; const i = n; const on = i === at; const cls = cn( "flex w-full min-w-0 items-baseline gap-3 px-3 py-[5px] text-left outline-none", on && "bg-panel-2 text-text", it.kind === "action" && it.disabled && "cursor-default opacity-40", ); const body = ( <> {it.label} {it.hint && {it.hint}} ); const common = { ref: (el: HTMLElement | null) => void (els.current[i] = el), role: "menuitem", tabIndex: on ? 0 : -1, onMouseMove: () => i !== at && usable(i) && setAt(i), className: cls, } as const; if (it.kind === "link") { return ( onClose(false)}> {body} ); } return ( ); })}
))}
); return typeof document === "undefined" ? null : createPortal(menu, document.body); }