"use client"; import { ChevronRight, FileText, Folder, Paperclip, Plus } from "lucide-react"; import { cn } from "@/lib/utils"; import type { FileTreeRow } from "@/lib/file-tree/types"; import { getFileTreeRowLabel } from "@/lib/file-tree/types"; import { isRealFileAsset } from "@/lib/file-tree/asset"; interface FileTreeProps { rows: FileTreeRow[]; activeId: string; selectedRowIds: Set; onRowClick: (row: FileTreeRow, event: React.MouseEvent) => void; onRowDoubleClick: (row: FileTreeRow, event: React.MouseEvent) => void; onRowContextMenu: (row: FileTreeRow, event: React.MouseEvent) => void; onRowDragStart?: (row: FileTreeRow, event: React.DragEvent) => void; onToggleExpand: (docId: string) => void; onCreateChild: (parentId: string | null) => void; onBlankMouseDown?: (event: React.MouseEvent) => void; onDropFiles?: (docId: string, files: FileList) => void; onInternalDrop?: (args: { targetRow: FileTreeRow; rowIds: string[]; copy: boolean }) => void; } const INDENT = 16; export function FileTree({ rows, activeId, selectedRowIds, onRowClick, onRowDoubleClick, onRowContextMenu, onRowDragStart, onToggleExpand, onCreateChild, onBlankMouseDown, onDropFiles, onInternalDrop, }: FileTreeProps) { if (rows.length === 0) { return
暂无页面,点击下方按钮创建
; } return (
{ if (onDropFiles) event.preventDefault(); }} onDrop={(event) => { if (onDropFiles && event.dataTransfer.files?.length) { event.preventDefault(); const files = event.dataTransfer.files; const firstDocRow = rows.find((row) => row.kind === "doc"); const targetDocId = firstDocRow?.docId ?? ""; onDropFiles(targetDocId, files); } }} onMouseDown={(event) => { if (event.target === event.currentTarget) { onBlankMouseDown?.(event); } }} > {rows.map((row) => { const label = getFileTreeRowLabel(row); const selected = selectedRowIds.has(row.rowId); const active = row.kind !== "asset" && row.docId === activeId; const draggable = row.kind === "doc" || (row.kind === "asset" && isRealFileAsset(row.asset)); const baseClass = "flex items-center gap-2 rounded-md px-2 py-1.5 text-sm text-gray-700 hover:bg-[#f5f7fb]"; const activeClass = row.kind === "index" ? "text-[#2563eb] font-medium" : row.kind === "doc" ? "text-[#2563eb]" : ""; const paddingLeft = row.kind === "doc" ? row.depth * INDENT + 8 : row.depth * INDENT + 32; return (
onRowClick(row, event)} onDoubleClick={(event) => onRowDoubleClick(row, event)} onContextMenu={(event) => onRowContextMenu(row, event)} draggable={draggable} onDragStart={(event) => { if (!draggable) return; if (!selectedRowIds.has(row.rowId)) { onRowDragStart?.(row, event); } const rowIds = selectedRowIds.has(row.rowId) ? Array.from(selectedRowIds) : [row.rowId]; const payload = JSON.stringify({ type: "mnote-file-tree-dnd", version: 1, rowIds }); try { event.dataTransfer.setData("application/x-mnote-file-tree", payload); } catch { // ignore } event.dataTransfer.setData("text/plain", payload); event.dataTransfer.effectAllowed = event.altKey ? "copyMove" : "move"; }} onDragOver={(event) => { const types = Array.from(event.dataTransfer.types ?? []); const hasInternal = types.includes("application/x-mnote-file-tree"); if (hasInternal && onInternalDrop) { event.preventDefault(); event.dataTransfer.dropEffect = event.altKey ? "copy" : "move"; return; } if (onDropFiles) event.preventDefault(); }} onDrop={(event) => { const types = Array.from(event.dataTransfer.types ?? []); const isInternal = types.includes("application/x-mnote-file-tree"); if (isInternal && onInternalDrop) { const raw = event.dataTransfer.getData("application/x-mnote-file-tree") || ""; try { const parsed = JSON.parse(raw) as { type?: string; version?: number; rowIds?: unknown }; if (parsed?.type === "mnote-file-tree-dnd" && parsed.version === 1 && Array.isArray(parsed.rowIds)) { event.preventDefault(); onInternalDrop({ targetRow: row, rowIds: parsed.rowIds.filter((id) => typeof id === "string") as string[], copy: event.altKey, }); return; } } catch { // ignore } } if (onDropFiles && event.dataTransfer.files?.length) { event.preventDefault(); onDropFiles(row.docId, event.dataTransfer.files); } }} role="button" tabIndex={0} > {row.kind === "doc" ? ( <> {row.hasChildren ? ( ) : ( )} {label} ) : row.kind === "index" ? ( <> {label} ) : ( <> {label} )}
); })}
); }