From 8531d916a3f8358a5971cf25086bdb106f8ac6b0 Mon Sep 17 00:00:00 2001 From: ssdfasd <2156608475@qq.com> Date: Sun, 8 Mar 2026 17:35:18 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E4=BE=A7=E8=BE=B9?= =?UTF-8?q?=E6=A0=8F=E6=8B=96=E5=8A=A8=E6=9D=A1=E4=BD=8D=E7=BD=AE=E5=81=8F?= =?UTF-8?q?=E7=A7=BB=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../file-system/Sidebar/Sidebar.tsx | 27 ++++++++++++++----- src/hooks/ui/useSidebarResize.ts | 14 +++++----- 2 files changed, 28 insertions(+), 13 deletions(-) diff --git a/src/components/file-system/Sidebar/Sidebar.tsx b/src/components/file-system/Sidebar/Sidebar.tsx index a8ba7d1..45cf34d 100644 --- a/src/components/file-system/Sidebar/Sidebar.tsx +++ b/src/components/file-system/Sidebar/Sidebar.tsx @@ -4,7 +4,7 @@ import type { FileItem } from '@/lib/api' import type { TOCItem } from '@/lib/utils' import { useWallpaper } from '@/stores' import { ContextMenu } from '@/components/common/ContextMenu' -import React, { useState } from 'react' +import React, { useState, forwardRef, useRef, useEffect } from 'react' import { useFileSystemContext } from '@/contexts/FileSystemContext' import { DragContextProvider } from '@/contexts/DragContext' import { useDropTarget } from '@/hooks/domain/useDragDrop' @@ -47,7 +47,7 @@ const renderTOCItem = (item: TOCItem, onTOCItemClick: (id: string) => void) => { ) } -const SidebarContent = ({ +const SidebarContent = forwardRef(({ isOpen, width, refreshKey, @@ -60,9 +60,20 @@ const SidebarContent = ({ tocItems, onTOCItemClick, onTOCClose, -}: SidebarProps) => { +}, ref) => { const { opacity } = useWallpaper() const { openCreateDirectoryDialog, openCreateFileDialog, openRenameDialog, openDeleteDialog } = useFileSystemContext() + const containerRef = useRef(null) + + useEffect(() => { + if (ref) { + if (typeof ref === 'function') { + ref(containerRef.current) + } else { + ref.current = containerRef.current + } + } + }, [ref]) const [contextMenuOpen, setContextMenuOpen] = useState(false) const [contextMenuPosition, setContextMenuPosition] = useState({ x: 0, y: 0 }) @@ -132,7 +143,9 @@ const SidebarContent = ({ return (
) -} +}) -export const Sidebar = (props: SidebarProps) => { +export const Sidebar = forwardRef((props, ref) => { const { moveItem } = useFileSystemContext() return ( - + ) -} +}) diff --git a/src/hooks/ui/useSidebarResize.ts b/src/hooks/ui/useSidebarResize.ts index cae74c2..c5b35d9 100644 --- a/src/hooks/ui/useSidebarResize.ts +++ b/src/hooks/ui/useSidebarResize.ts @@ -3,13 +3,16 @@ import { useCallback, useEffect, useRef, useState } from 'react' export const useSidebarResize = (initialWidth: number = 250) => { const [sidebarWidth, setSidebarWidth] = useState(initialWidth) const [isResizing, setIsResizing] = useState(false) - const offsetX = useRef(0) + const sidebarLeft = useRef(0) const startResizing = useCallback((e: React.MouseEvent) => { e.preventDefault() - const rect = (e.target as HTMLElement).getBoundingClientRect() - offsetX.current = e.clientX - rect.left - setIsResizing(true) + const sidebar = (e.target as HTMLElement).closest('.sidebar-container') as HTMLElement + if (sidebar) { + const rect = sidebar.getBoundingClientRect() + sidebarLeft.current = rect.left + setIsResizing(true) + } }, []) useEffect(() => { @@ -17,7 +20,7 @@ export const useSidebarResize = (initialWidth: number = 250) => { const stopResizing = () => setIsResizing(false) const resize = (e: MouseEvent) => { - const newWidth = e.clientX - offsetX.current + const newWidth = e.clientX - sidebarLeft.current if (newWidth > 150 && newWidth < 600) { setSidebarWidth(newWidth) } @@ -33,4 +36,3 @@ export const useSidebarResize = (initialWidth: number = 250) => { return { sidebarWidth, startResizing } } -