fix: 修复侧边栏拖动条位置偏移问题
This commit is contained in:
@@ -4,7 +4,7 @@ import type { FileItem } from '@/lib/api'
|
|||||||
import type { TOCItem } from '@/lib/utils'
|
import type { TOCItem } from '@/lib/utils'
|
||||||
import { useWallpaper } from '@/stores'
|
import { useWallpaper } from '@/stores'
|
||||||
import { ContextMenu } from '@/components/common/ContextMenu'
|
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 { useFileSystemContext } from '@/contexts/FileSystemContext'
|
||||||
import { DragContextProvider } from '@/contexts/DragContext'
|
import { DragContextProvider } from '@/contexts/DragContext'
|
||||||
import { useDropTarget } from '@/hooks/domain/useDragDrop'
|
import { useDropTarget } from '@/hooks/domain/useDragDrop'
|
||||||
@@ -47,7 +47,7 @@ const renderTOCItem = (item: TOCItem, onTOCItemClick: (id: string) => void) => {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
const SidebarContent = ({
|
const SidebarContent = forwardRef<HTMLDivElement, SidebarProps>(({
|
||||||
isOpen,
|
isOpen,
|
||||||
width,
|
width,
|
||||||
refreshKey,
|
refreshKey,
|
||||||
@@ -60,9 +60,20 @@ const SidebarContent = ({
|
|||||||
tocItems,
|
tocItems,
|
||||||
onTOCItemClick,
|
onTOCItemClick,
|
||||||
onTOCClose,
|
onTOCClose,
|
||||||
}: SidebarProps) => {
|
}, ref) => {
|
||||||
const { opacity } = useWallpaper()
|
const { opacity } = useWallpaper()
|
||||||
const { openCreateDirectoryDialog, openCreateFileDialog, openRenameDialog, openDeleteDialog } = useFileSystemContext()
|
const { openCreateDirectoryDialog, openCreateFileDialog, openRenameDialog, openDeleteDialog } = useFileSystemContext()
|
||||||
|
const containerRef = useRef<HTMLDivElement>(null)
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (ref) {
|
||||||
|
if (typeof ref === 'function') {
|
||||||
|
ref(containerRef.current)
|
||||||
|
} else {
|
||||||
|
ref.current = containerRef.current
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}, [ref])
|
||||||
|
|
||||||
const [contextMenuOpen, setContextMenuOpen] = useState(false)
|
const [contextMenuOpen, setContextMenuOpen] = useState(false)
|
||||||
const [contextMenuPosition, setContextMenuPosition] = useState({ x: 0, y: 0 })
|
const [contextMenuPosition, setContextMenuPosition] = useState({ x: 0, y: 0 })
|
||||||
@@ -132,7 +143,9 @@ const SidebarContent = ({
|
|||||||
|
|
||||||
return (
|
return (
|
||||||
<div
|
<div
|
||||||
|
ref={containerRef}
|
||||||
className={`
|
className={`
|
||||||
|
sidebar-container
|
||||||
backdrop-blur-sm border-r border-gray-200 dark:border-gray-700/60 transition-none flex flex-col shrink-0 relative z-20
|
backdrop-blur-sm border-r border-gray-200 dark:border-gray-700/60 transition-none flex flex-col shrink-0 relative z-20
|
||||||
${isOpen ? '' : 'hidden'}
|
${isOpen ? '' : 'hidden'}
|
||||||
`}
|
`}
|
||||||
@@ -221,14 +234,14 @@ const SidebarContent = ({
|
|||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
)
|
)
|
||||||
}
|
})
|
||||||
|
|
||||||
export const Sidebar = (props: SidebarProps) => {
|
export const Sidebar = forwardRef<HTMLDivElement, SidebarProps>((props, ref) => {
|
||||||
const { moveItem } = useFileSystemContext()
|
const { moveItem } = useFileSystemContext()
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<DragContextProvider moveItem={moveItem}>
|
<DragContextProvider moveItem={moveItem}>
|
||||||
<SidebarContent {...props} />
|
<SidebarContent {...props} ref={ref} />
|
||||||
</DragContextProvider>
|
</DragContextProvider>
|
||||||
)
|
)
|
||||||
}
|
})
|
||||||
|
|||||||
@@ -3,13 +3,16 @@ import { useCallback, useEffect, useRef, useState } from 'react'
|
|||||||
export const useSidebarResize = (initialWidth: number = 250) => {
|
export const useSidebarResize = (initialWidth: number = 250) => {
|
||||||
const [sidebarWidth, setSidebarWidth] = useState(initialWidth)
|
const [sidebarWidth, setSidebarWidth] = useState(initialWidth)
|
||||||
const [isResizing, setIsResizing] = useState(false)
|
const [isResizing, setIsResizing] = useState(false)
|
||||||
const offsetX = useRef(0)
|
const sidebarLeft = useRef(0)
|
||||||
|
|
||||||
const startResizing = useCallback((e: React.MouseEvent) => {
|
const startResizing = useCallback((e: React.MouseEvent) => {
|
||||||
e.preventDefault()
|
e.preventDefault()
|
||||||
const rect = (e.target as HTMLElement).getBoundingClientRect()
|
const sidebar = (e.target as HTMLElement).closest('.sidebar-container') as HTMLElement
|
||||||
offsetX.current = e.clientX - rect.left
|
if (sidebar) {
|
||||||
setIsResizing(true)
|
const rect = sidebar.getBoundingClientRect()
|
||||||
|
sidebarLeft.current = rect.left
|
||||||
|
setIsResizing(true)
|
||||||
|
}
|
||||||
}, [])
|
}, [])
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
@@ -17,7 +20,7 @@ export const useSidebarResize = (initialWidth: number = 250) => {
|
|||||||
|
|
||||||
const stopResizing = () => setIsResizing(false)
|
const stopResizing = () => setIsResizing(false)
|
||||||
const resize = (e: MouseEvent) => {
|
const resize = (e: MouseEvent) => {
|
||||||
const newWidth = e.clientX - offsetX.current
|
const newWidth = e.clientX - sidebarLeft.current
|
||||||
if (newWidth > 150 && newWidth < 600) {
|
if (newWidth > 150 && newWidth < 600) {
|
||||||
setSidebarWidth(newWidth)
|
setSidebarWidth(newWidth)
|
||||||
}
|
}
|
||||||
@@ -33,4 +36,3 @@ export const useSidebarResize = (initialWidth: number = 250) => {
|
|||||||
|
|
||||||
return { sidebarWidth, startResizing }
|
return { sidebarWidth, startResizing }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user