import { type CSSProperties, type ReactNode, useEffect, useLayoutEffect, useRef, useState } from "react"; import { createPortal } from "react-dom"; import { Check, ChevronDown, Image as ImageIcon } from "lucide-react"; import { canvasThemes, type CanvasTheme } from "@/lib/canvas-theme"; import { useActiveTheme } from "@/stores/canvas/use-canvas-theme-store"; import type { CanvasNodeMetadata } from "@/types/canvas"; type VideoFrameOption = { nodeId: string; label: string; title: string; previewUrl?: string; }; type CompactMenuItem = { value: string; label: string; previewUrl?: string; }; type CanvasVideoPromptToolsProps = { metadata?: CanvasNodeMetadata; frameOptions: VideoFrameOption[]; onMetadataChange: (patch: Partial) => void; referenceMode?: "frames" | "all"; referenceSummary?: { imageCount: number; videoCount: number; audioCount: number }; }; const EMPTY_FRAME_VALUE = "__none__"; const MENU_GAP = 6; const MENU_MARGIN = 8; const MENU_ITEM_HEIGHT = 28; const CONTROL_TEXT_STYLE: CSSProperties = { fontFamily: "inherit", fontSize: 11, fontWeight: 400, letterSpacing: 0, lineHeight: 1 }; export function CanvasVideoPromptTools({ metadata, frameOptions, onMetadataChange, referenceMode = "frames", referenceSummary }: CanvasVideoPromptToolsProps) { const theme = canvasThemes[useActiveTheme()]; const startFrame = metadata?.videoStartFrameNodeId || EMPTY_FRAME_VALUE; const endFrame = metadata?.videoEndFrameNodeId || EMPTY_FRAME_VALUE; const setFrame = (key: "videoStartFrameNodeId" | "videoEndFrameNodeId", value: string) => { const next = value === EMPTY_FRAME_VALUE ? undefined : value; onMetadataChange(key === "videoStartFrameNodeId" ? { videoStartFrameNodeId: next } : { videoEndFrameNodeId: next }); }; if (referenceMode === "all") { const summary = referenceSummary ? [ referenceSummary.imageCount ? `${referenceSummary.imageCount} 图` : "", referenceSummary.videoCount ? `${referenceSummary.videoCount} 视频` : "", referenceSummary.audioCount ? `${referenceSummary.audioCount} 音频` : "", ].filter(Boolean).join(" · ") : ""; return (
event.stopPropagation()} onPointerDown={(event) => event.stopPropagation()} > 工作流参考 {summary || "尚未连接媒体"}
); } if (!frameOptions.length) return null; return (
event.stopPropagation()} onPointerDown={(event) => event.stopPropagation()} > setFrame("videoStartFrameNodeId", value)} /> setFrame("videoEndFrameNodeId", value)} />
); } function FrameMenu({ label, value, options, theme, onChange }: { label: string; value: string; options: VideoFrameOption[]; theme: CanvasTheme; onChange: (value: string) => void }) { const selected = options.find((item) => item.nodeId === value); const items = [{ value: EMPTY_FRAME_VALUE, label: "不指定" }, ...options.map((option) => ({ value: option.nodeId, label: `${option.label} · ${option.title}`, previewUrl: option.previewUrl }))]; return ( } value={value} items={items} menuWidth={220} maxMenuHeight={208} onSelect={onChange} /> ); } function CompactMenuButton({ theme, title, label, icon, value, items, menuWidth, maxMenuHeight, onSelect, }: { theme: CanvasTheme; title: string; label: string; icon: ReactNode; value?: string; items: CompactMenuItem[]; menuWidth: number; maxMenuHeight: number; onSelect: (value: string) => void; }) { const triggerRef = useRef(null); const menuRef = useRef(null); const [open, setOpen] = useState(false); const [position, setPosition] = useState<{ left: number; top: number; width: number; maxHeight: number } | null>(null); const updatePosition = () => { const rect = triggerRef.current?.getBoundingClientRect(); if (!rect) return; const menuHeight = Math.min(maxMenuHeight, items.length * MENU_ITEM_HEIGHT + 8); const effectiveMenuWidth = Math.max(menuWidth, Math.round(rect.width)); const openAbove = rect.bottom + MENU_GAP + menuHeight > window.innerHeight && rect.top > menuHeight + MENU_GAP; const maxLeft = Math.max(MENU_MARGIN, window.innerWidth - effectiveMenuWidth - MENU_MARGIN); const maxTop = Math.max(MENU_MARGIN, window.innerHeight - menuHeight - MENU_MARGIN); const left = Math.min(Math.max(MENU_MARGIN, rect.left), maxLeft); const top = openAbove ? Math.max(MENU_MARGIN, rect.top - menuHeight - MENU_GAP) : Math.min(Math.max(MENU_MARGIN, rect.bottom + MENU_GAP), maxTop); setPosition({ left, top, maxHeight: menuHeight, width: effectiveMenuWidth }); }; useLayoutEffect(() => { if (open) updatePosition(); }, [open, items.length, maxMenuHeight, menuWidth]); useEffect(() => { if (!open) return; const close = (event: PointerEvent) => { const target = event.target instanceof Node ? event.target : null; if (target && (triggerRef.current?.contains(target) || menuRef.current?.contains(target))) return; setOpen(false); }; const closeOnEscape = (event: KeyboardEvent) => { if (event.key === "Escape") setOpen(false); }; const reposition = () => updatePosition(); document.addEventListener("pointerdown", close, true); document.addEventListener("keydown", closeOnEscape, true); window.addEventListener("resize", reposition); window.addEventListener("scroll", reposition, true); return () => { document.removeEventListener("pointerdown", close, true); document.removeEventListener("keydown", closeOnEscape, true); window.removeEventListener("resize", reposition); window.removeEventListener("scroll", reposition, true); }; }, [open, items.length, maxMenuHeight, menuWidth]); const buttonStyle: CSSProperties = { ...CONTROL_TEXT_STYLE, color: theme.node.text }; const menuStyle: CSSProperties | undefined = position ? { left: position.left, top: position.top, width: position.width, maxHeight: position.maxHeight, background: theme.toolbar.panel, borderColor: theme.toolbar.border, color: theme.node.text, boxShadow: "0 10px 26px rgba(15,23,42,.14)", } : undefined; const listStyle: CSSProperties | undefined = position ? { maxHeight: Math.max(0, position.maxHeight - 8) } : undefined; const menu = open && position && typeof document !== "undefined" ? createPortal(
event.stopPropagation()} onPointerDown={(event) => event.stopPropagation()} onWheel={(event) => event.stopPropagation()} >
{items.map((item) => { const selected = value === item.value; return ( ); })}
, document.body, ) : null; return ( <> {menu} ); }