ttrpg-tools/src/components/Sidebar.tsx

179 lines
5.3 KiB
TypeScript
Raw Normal View History

2026-02-26 15:10:03 +08:00
import { Component, createMemo, createSignal, onMount, Show } from "solid-js";
2026-02-26 14:24:48 +08:00
import { generateToc, type FileNode, type TocNode } from "../data-loader";
2026-02-26 15:04:17 +08:00
import { useLocation } from "@solidjs/router";
import { FileTreeNode, HeadingNode } from "./FileTree";
2026-02-26 14:24:48 +08:00
2026-02-27 12:34:55 +08:00
export interface SidebarProps {
2026-02-26 14:24:48 +08:00
isOpen: boolean;
onClose: () => void;
fileTree?: FileNode[];
pathHeadings?: Record<string, TocNode[]>;
onDataSourceOpen?: () => void;
2026-02-26 14:24:48 +08:00
}
2026-02-27 12:34:55 +08:00
interface SidebarContentProps {
2026-02-26 15:22:40 +08:00
fileTree: FileNode[];
pathHeadings: Record<string, TocNode[]>;
currentPath: string;
onClose: () => void;
2026-02-27 12:34:55 +08:00
isDesktop?: boolean;
onDataSourceOpen?: () => void;
2026-02-27 12:34:55 +08:00
}
/**
*
*/
const SidebarContent: Component<SidebarContentProps> = (props) => {
2026-02-26 15:22:40 +08:00
const location = useLocation();
// 响应式获取当前文件的标题列表
const currentFileHeadings = createMemo(() => {
2026-03-22 10:11:41 +08:00
const pathname = decodeURIComponent(location.pathname);
return (
props.pathHeadings[pathname] || props.pathHeadings[`${pathname}.md`] || []
);
2026-02-26 15:22:40 +08:00
});
return (
2026-03-23 19:01:42 +08:00
<div class="flex flex-col h-full">
2026-03-25 09:42:58 +08:00
<div class="p-4 border-b border-b-gray-200">
2026-03-23 19:01:42 +08:00
<div class="flex items-center justify-between">
<h2 class="text-lg font-bold text-gray-900"></h2>
<div class="flex items-center gap-1">
2026-03-23 19:01:42 +08:00
<button
onClick={props.onDataSourceOpen}
class="text-gray-400 hover:text-gray-600 p-1 rounded hover:bg-gray-100"
title="Content Source"
2026-03-23 19:01:42 +08:00
>
📂
2026-03-23 19:01:42 +08:00
</button>
<Show when={!props.isDesktop}>
<button
onClick={props.onClose}
class="text-gray-500 hover:text-gray-700"
title="关闭"
>
</button>
</Show>
</div>
2026-03-23 19:01:42 +08:00
</div>
2026-02-26 15:22:40 +08:00
</div>
2026-03-23 19:01:42 +08:00
{/* 文件树滚动区域 */}
<div class="flex-1 overflow-y-auto p-4 min-h-0">
2026-02-26 15:22:40 +08:00
<h3 class="text-xs font-semibold text-gray-500 uppercase mb-2 px-2">
</h3>
{props.fileTree.map((node) => (
<FileTreeNode
node={node}
currentPath={props.currentPath}
pathHeadings={props.pathHeadings}
depth={0}
onClose={props.onClose}
/>
))}
</div>
2026-03-23 19:01:42 +08:00
{/* 当前文件标题滚动区域 */}
2026-02-26 15:22:40 +08:00
<Show when={currentFileHeadings().length > 0}>
2026-03-25 09:42:58 +08:00
<div class="flex-1 border-t-gray-200 border-t overflow-y-auto p-4 min-h-0">
2026-02-26 15:22:40 +08:00
<h3 class="text-xs font-semibold text-gray-500 uppercase mb-2 px-2">
</h3>
{currentFileHeadings().map((node) => (
<HeadingNode node={node} basePath={location.pathname} depth={0} />
2026-02-26 15:22:40 +08:00
))}
</div>
</Show>
</div>
);
};
/**
*
*/
export const MobileSidebar: Component<SidebarProps> = (props) => {
2026-02-26 14:24:48 +08:00
const location = useLocation();
const [selfFileTree, setSelfFileTree] = createSignal<FileNode[]>([]);
const [selfPathHeadings, setSelfPathHeadings] = createSignal<
Record<string, TocNode[]>
>({});
2026-02-26 14:24:48 +08:00
const fileTree = () => props.fileTree ?? selfFileTree();
const pathHeadings = () => props.pathHeadings ?? selfPathHeadings();
// 加载目录数据 (only if props not provided)
2026-02-26 14:30:09 +08:00
onMount(async () => {
if (props.fileTree && props.pathHeadings) return;
2026-02-26 14:30:09 +08:00
const toc = await generateToc();
setSelfFileTree(toc.fileTree);
setSelfPathHeadings(toc.pathHeadings);
2026-02-26 14:24:48 +08:00
});
return (
2026-02-26 15:02:28 +08:00
<>
2026-02-26 14:24:48 +08:00
{/* 遮罩层 */}
<div
2026-02-26 15:22:40 +08:00
class={`fixed inset-0 bg-black/50 z-40 transition-opacity duration-300 ease-in-out md:hidden ${
2026-02-26 15:02:28 +08:00
props.isOpen ? "opacity-100" : "opacity-0 pointer-events-none"
}`}
2026-02-26 14:24:48 +08:00
onClick={props.onClose}
/>
{/* 侧边栏 */}
2026-02-26 15:02:28 +08:00
<aside
2026-03-23 19:01:42 +08:00
class={`fixed top-0 left-0 h-full w-64 bg-white shadow-lg z-50 transform transition-transform duration-300 ease-in-out md:hidden ${
2026-02-26 15:02:28 +08:00
props.isOpen ? "translate-x-0" : "-translate-x-full"
}`}
>
2026-02-26 15:22:40 +08:00
<SidebarContent
fileTree={fileTree()}
pathHeadings={pathHeadings()}
currentPath={location.pathname}
onClose={props.onClose}
onDataSourceOpen={props.onDataSourceOpen}
2026-02-26 15:22:40 +08:00
/>
2026-02-26 14:24:48 +08:00
</aside>
2026-02-26 15:02:28 +08:00
</>
2026-02-26 14:24:48 +08:00
);
};
2026-02-26 15:22:40 +08:00
/**
*
*/
export const DesktopSidebar: Component<{
fileTree?: FileNode[];
pathHeadings?: Record<string, TocNode[]>;
onDataSourceOpen?: () => void;
}> = (props) => {
2026-02-26 15:22:40 +08:00
const location = useLocation();
const [selfFileTree, setSelfFileTree] = createSignal<FileNode[]>([]);
const [selfPathHeadings, setSelfPathHeadings] = createSignal<
Record<string, TocNode[]>
>({});
const fileTree = () => props.fileTree ?? selfFileTree();
const pathHeadings = () => props.pathHeadings ?? selfPathHeadings();
2026-02-26 15:22:40 +08:00
onMount(async () => {
if (props.fileTree && props.pathHeadings) return;
2026-02-26 15:22:40 +08:00
const toc = await generateToc();
setSelfFileTree(toc.fileTree);
setSelfPathHeadings(toc.pathHeadings);
2026-02-26 15:22:40 +08:00
});
return (
2026-03-23 19:01:42 +08:00
<aside class="hidden md:block fixed top-0 left-0 h-full w-64 bg-white shadow-lg z-30 overflow-hidden pt-16">
2026-02-26 15:22:40 +08:00
<SidebarContent
fileTree={fileTree()}
pathHeadings={pathHeadings()}
currentPath={location.pathname}
onClose={() => {}}
2026-02-27 12:34:55 +08:00
isDesktop
onDataSourceOpen={props.onDataSourceOpen}
2026-02-26 15:22:40 +08:00
/>
</aside>
);
};