"use client"; import { useEffect, useRef } from "react"; import { AnimatePresence, motion, useReducedMotion } from "motion/react"; import { createPortal } from "react-dom"; import { FileText, ImagePlus, StickyNote, X } from "lucide-react"; import type { XYPosition } from "@xyflow/react"; import type { NodeDirection } from "@/lib/canvas"; import styles from "./canvas.module.css"; export type BranchRequest = { id: string; direction: NodeDirection; point: XYPosition; at?: XYPosition }; export function BranchMenu({ request, close, choose }: { request: BranchRequest | null; close: () => void; choose: (kind: "text" | "image" | "script") => void }) { const ref = useRef(null); const reduce = useReducedMotion(); useEffect(() => { if (!request) return; const focus = document.activeElement as HTMLElement | null; ref.current?.querySelector("button[data-choice]")?.focus(); const outside = (event: PointerEvent) => { if (!ref.current?.contains(event.target as Node)) close(); }; const escape = (event: KeyboardEvent) => { if (event.key === "Escape") { event.preventDefault(); event.stopPropagation(); close(); focus?.focus(); } }; window.addEventListener("pointerdown", outside); window.addEventListener("keydown", escape, true); return () => { window.removeEventListener("pointerdown", outside); window.removeEventListener("keydown", escape, true); }; }, [request, close]); if (typeof document === "undefined") return null; return createPortal({request && { event.stopPropagation(); if (event.key === "ArrowDown" || event.key === "ArrowUp") { event.preventDefault(); const buttons = Array.from(ref.current?.querySelectorAll("button") || []); const index = buttons.indexOf(document.activeElement as HTMLButtonElement); buttons[(index + (event.key === "ArrowDown" ? 1 : -1) + buttons.length) % buttons.length]?.focus(); } }}>
添加连接节点
{([["text", StickyNote, "文本"], ["image", ImagePlus, "图片"], ["script", FileText, "脚本"]] as const).map(([kind, Icon, label]) => )}
}
, document.body); }