wwf
3 天以前 a430284aa21e3ae1f0d5654e55b2ad2852519cc2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import {
  useCallback,
  useEffect,
  useRef,
  useState,
} from 'react'
 
export type UseResizePanelParams = {
  direction?: 'horizontal' | 'vertical' | 'both'
  triggerDirection?: 'top' | 'right' | 'bottom' | 'left' | 'top-right' | 'top-left' | 'bottom-right' | 'bottom-left'
  minWidth?: number
  maxWidth?: number
  minHeight?: number
  maxHeight?: number
  onResized?: (width: number, height: number) => void
  onResize?: (width: number, height: number) => void
}
export const useResizePanel = (params?: UseResizePanelParams) => {
  const {
    direction = 'both',
    triggerDirection = 'bottom-right',
    minWidth = -Infinity,
    maxWidth = Infinity,
    minHeight = -Infinity,
    maxHeight = Infinity,
    onResized,
    onResize,
  } = params || {}
  const triggerRef = useRef<HTMLDivElement>(null)
  const containerRef = useRef<HTMLDivElement>(null)
  const initXRef = useRef(0)
  const initYRef = useRef(0)
  const initContainerWidthRef = useRef(0)
  const initContainerHeightRef = useRef(0)
  const isResizingRef = useRef(false)
  const [prevUserSelectStyle, setPrevUserSelectStyle] = useState(getComputedStyle(document.body).userSelect)
 
  const handleStartResize = useCallback((e: MouseEvent) => {
    initXRef.current = e.clientX
    initYRef.current = e.clientY
    initContainerWidthRef.current = containerRef.current?.offsetWidth || minWidth
    initContainerHeightRef.current = containerRef.current?.offsetHeight || minHeight
    isResizingRef.current = true
    setPrevUserSelectStyle(getComputedStyle(document.body).userSelect)
    document.body.style.userSelect = 'none'
  }, [minWidth, minHeight])
 
  const handleResize = useCallback((e: MouseEvent) => {
    if (!isResizingRef.current)
      return
 
    if (!containerRef.current)
      return
 
    if (direction === 'horizontal' || direction === 'both') {
      const offsetX = e.clientX - initXRef.current
      let width = 0
      if (triggerDirection === 'left' || triggerDirection === 'top-left' || triggerDirection === 'bottom-left')
        width = initContainerWidthRef.current - offsetX
      else if (triggerDirection === 'right' || triggerDirection === 'top-right' || triggerDirection === 'bottom-right')
        width = initContainerWidthRef.current + offsetX
 
      if (width < minWidth)
        width = minWidth
      if (width > maxWidth)
        width = maxWidth
      containerRef.current.style.width = `${width}px`
      onResize?.(width, 0)
    }
 
    if (direction === 'vertical' || direction === 'both') {
      const offsetY = e.clientY - initYRef.current
      let height = 0
      if (triggerDirection === 'top' || triggerDirection === 'top-left' || triggerDirection === 'top-right')
        height = initContainerHeightRef.current - offsetY
      else if (triggerDirection === 'bottom' || triggerDirection === 'bottom-left' || triggerDirection === 'bottom-right')
        height = initContainerHeightRef.current + offsetY
 
      if (height < minHeight)
        height = minHeight
      if (height > maxHeight)
        height = maxHeight
 
      containerRef.current.style.height = `${height}px`
      onResize?.(0, height)
    }
  }, [
    direction,
    triggerDirection,
    minWidth,
    maxWidth,
    minHeight,
    maxHeight,
    onResize,
  ])
 
  const handleStopResize = useCallback(() => {
    isResizingRef.current = false
    document.body.style.userSelect = prevUserSelectStyle
 
    if (onResized && containerRef.current)
      onResized(containerRef.current.offsetWidth, containerRef.current.offsetHeight)
  }, [prevUserSelectStyle, onResized])
 
  useEffect(() => {
    const element = triggerRef.current
    element?.addEventListener('mousedown', handleStartResize)
    document.addEventListener('mousemove', handleResize)
    document.addEventListener('mouseup', handleStopResize)
    return () => {
      if (element)
        element.removeEventListener('mousedown', handleStartResize)
      document.removeEventListener('mousemove', handleResize)
    }
  }, [handleStartResize, handleResize, handleStopResize])
 
  return {
    triggerRef,
    containerRef,
  }
}