diff --git a/src/renderer/components/Sidebar/Sidebar.tsx b/src/renderer/components/Sidebar/Sidebar.tsx index f376fc6..fae158c 100644 --- a/src/renderer/components/Sidebar/Sidebar.tsx +++ b/src/renderer/components/Sidebar/Sidebar.tsx @@ -17,10 +17,29 @@ export function Sidebar() { const setRootPath = useSidebarStore(s => s.setRootPath) const setTree = useSidebarStore(s => s.setTree) const isVisible = useSidebarStore(s => s.isVisible) + const expandDirs = useSidebarStore(s => s.expandDirs) // 计算当前活动标签的文件路径(用于文件树高亮) const activeFilePath = tabs.find(t => t.id === activeTabId)?.filePath ?? null + // 自动展开到活动文件所在的目录 + useEffect(() => { + if (!activeFilePath || !rootPath) return + if (!activeFilePath.startsWith(rootPath)) return + + // 提取文件的所有父目录路径 + const dirsToExpand: string[] = [] + let dir = activeFilePath.replace(/[/\\][^/\\]+$/, '') // 移除文件名 + while (dir && dir.length >= rootPath.length && dir !== rootPath) { + dirsToExpand.push(dir) + dir = dir.replace(/[/\\][^/\\]+$/, '') + } + + if (dirsToExpand.length > 0) { + expandDirs(dirsToExpand) + } + }, [activeFilePath, rootPath, expandDirs]) + const [isResizing, setIsResizing] = useState(false) const sidebarRef = useRef(null) diff --git a/src/renderer/stores/sidebarStore.ts b/src/renderer/stores/sidebarStore.ts index 59f8355..01ae671 100644 --- a/src/renderer/stores/sidebarStore.ts +++ b/src/renderer/stores/sidebarStore.ts @@ -12,6 +12,7 @@ interface SidebarState { setRootPath: (path: string | null) => void setTree: (tree: FileNode[]) => void toggleDir: (path: string) => void + expandDirs: (paths: string[]) => void setSidebarWidth: (width: number) => void } @@ -35,5 +36,17 @@ export const useSidebarStore = create((set) => ({ } return { expandedDirs: newSet } }), + expandDirs: (paths) => + set(state => { + const newSet = new Set(state.expandedDirs) + let changed = false + for (const p of paths) { + if (!newSet.has(p)) { + newSet.add(p) + changed = true + } + } + return changed ? { expandedDirs: newSet } : state + }), setSidebarWidth: (width) => set({ sidebarWidth: width }) }))