import { Component, createMemo, createSignal, Show } from "solid-js"; import { type FileNode, type TocNode } from "../data-loader"; import { useNavigate } from "@solidjs/router"; /** * 检查当前文件路径是否在文件夹内 */ function isPathInDir(currentPath: string, dirPath: string): boolean { // 确保 dirPath 以 / 结尾,用于前缀匹配 const dirPathPrefix = dirPath.endsWith('/') ? dirPath : dirPath + '/'; return currentPath.startsWith(dirPathPrefix); } /** * 文件树节点组件 */ export const FileTreeNode: Component<{ node: FileNode; currentPath: string; pathHeadings: Record; depth: number; onClose: () => void; }> = (props) => { const navigate = useNavigate(); const isDir = !!props.node.children; const isActive = createMemo(() => props.currentPath === props.node.path); // 默认收起,除非当前文件在该文件夹内 const [isExpanded, setIsExpanded] = createSignal(isDir && isPathInDir(props.currentPath, props.node.path)); const handleClick = () => { if (isDir) { setIsExpanded(!isExpanded()); } else { navigate(props.node.path); props.onClose(); } }; const indent = props.depth * 12; return (
{isExpanded() ? "📂" : "📁"} 📄 {props.node.name}
{props.node.children!.map((child) => ( ))}
); }; /** * 标题节点组件 */ export const HeadingNode: Component<{ node: TocNode; basePath: string; depth: number; }> = (props) => { const navigate = useNavigate(); const anchor = props.node.title.toLowerCase().replace(/\s+/g, "-"); const href = `${props.basePath}#${anchor}`; const handleClick = (e: MouseEvent) => { e.preventDefault(); navigate(href); }; const indent = props.depth * 12; return (
{props.node.title}
{props.node.children!.map((child) => ( ))}
); };