yanjinhui
10 天以前 c5de0d98241f8c8349fa38851b77efcfc61e4d26
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import { ref } from 'vue'
 
export const useResize = (props?: {
  minHeightPx?: number
  minWidthPx?: number
  initHeight?: number
  initWidth?: number
}) => {
  const {
    minHeightPx = 400,
    minWidthPx = window.innerWidth / 2,
    initHeight = 400,
    initWidth = window.innerWidth / 2
  } = props || {}
  // 屏幕宽度的 50% 作为最小宽度
  //   const minWidthPx = window.innerWidth / 2
  // 固定的最小高度 400px
  //   const minHeightPx = 400
  // 初始高度限制为 400px
  const maxHeight = ref(initHeight + 'px')
  // 初始宽度限制为 50%
  const minWidth = ref(initWidth + 'px')
  const setupDrag = (elDialog: any, el: any) => {
    // 获取对话框元素
    // 是否正在调整大小的标志
    let isResizing = false
    // 当前调整的方向
    let currentResizeDirection = ''
 
    // 鼠标移动时的事件处理器,用于检测鼠标位置并设置相应的光标样式
    const handleMouseMove = (e: any) => {
      const rect = elDialog.getBoundingClientRect()
      // 鼠标相对于对话框左侧的偏移量
      const offsetX = e.clientX - rect.left
      // 鼠标相对于对话框顶部的偏移量
      const offsetY = e.clientY - rect.top
      const width = elDialog.clientWidth
      const height = elDialog.clientHeight
 
      // 获取对话框的内边距
      const computedStyle = window.getComputedStyle(elDialog)
      const paddingLeft = parseFloat(computedStyle.paddingLeft)
      const paddingRight = parseFloat(computedStyle.paddingRight)
      const paddingBottom = parseFloat(computedStyle.paddingBottom)
      const paddingTop = parseFloat(computedStyle.paddingTop)
 
      // 根据鼠标位置设置相应的光标样式和调整方向
      if (!isResizing) {
        if (offsetX < paddingLeft && offsetY > paddingTop && offsetY < height - paddingBottom) {
          elDialog.style.cursor = 'ew-resize' // 左右箭头
          currentResizeDirection = 'left'
        } else if (
          offsetX > width - paddingRight &&
          offsetY > paddingTop &&
          offsetY < height - paddingBottom
        ) {
          elDialog.style.cursor = 'ew-resize' // 左右箭头
          currentResizeDirection = 'right'
        } else if (
          offsetY < paddingTop &&
          offsetX > paddingLeft &&
          offsetX < width - paddingRight
        ) {
          elDialog.style.cursor = 'ns-resize' // 上下箭头
          currentResizeDirection = 'top'
        } else if (
          offsetY > height - paddingBottom &&
          offsetX > paddingLeft &&
          offsetX < width - paddingRight
        ) {
          elDialog.style.cursor = 'ns-resize' // 上下箭头
          currentResizeDirection = 'bottom'
        } else if (offsetX < paddingLeft && offsetY < paddingTop) {
          elDialog.style.cursor = 'nwse-resize' // 左上右下箭头
          currentResizeDirection = 'top-left'
        } else if (offsetX > width - paddingRight && offsetY < paddingTop) {
          elDialog.style.cursor = 'nesw-resize' // 右上左下箭头
          currentResizeDirection = 'top-right'
        } else if (offsetX < paddingLeft && offsetY > height - paddingBottom) {
          elDialog.style.cursor = 'nesw-resize' // 右上左下箭头
          currentResizeDirection = 'bottom-left'
        } else if (offsetX > width - paddingRight && offsetY > height - paddingBottom) {
          elDialog.style.cursor = 'nwse-resize' // 左上右下箭头
          currentResizeDirection = 'bottom-right'
        } else {
          elDialog.style.cursor = 'default'
          currentResizeDirection = ''
        }
      }
    }
 
    // 鼠标按下时的事件处理器,开始调整对话框大小
    const handleMouseDown = (e) => {
      if (currentResizeDirection) {
        isResizing = true
 
        const initialX = e.clientX
        const initialY = e.clientY
        const initialWidth = elDialog.clientWidth
        const initialHeight = el.querySelector('.el-dialog__body').clientHeight
 
        // 调整大小的事件处理器
        const handleResizing = (e: any) => {
          if (!isResizing) return
 
          let newWidth = initialWidth
          let newHeight = initialHeight
 
          // 根据当前调整方向计算新的宽度和高度
          if (currentResizeDirection.includes('right')) {
            newWidth = Math.max(minWidthPx, initialWidth + (e.clientX - initialX) * 2)
            minWidth.value = `${newWidth}px`
          }
 
          if (currentResizeDirection.includes('left')) {
            newWidth = Math.max(minWidthPx, initialWidth - (e.clientX - initialX) * 2)
            minWidth.value = `${newWidth}px`
          }
 
          if (currentResizeDirection.includes('bottom')) {
            newHeight = Math.max(minHeightPx, initialHeight + (e.clientY - initialY) * 2 - 20)
            maxHeight.value = `${Math.min(newHeight, window.innerHeight - 165)}px`
          }
 
          if (currentResizeDirection.includes('top')) {
            newHeight = Math.max(minHeightPx, initialHeight - (e.clientY - initialY) * 2 - 20)
            maxHeight.value = `${Math.min(newHeight, window.innerHeight - 165)}px`
          }
 
          if (currentResizeDirection === 'top-left') {
            newWidth = Math.max(minWidthPx, initialWidth - (e.clientX - initialX) * 2)
            minWidth.value = `${newWidth}px`
            newHeight = Math.max(minHeightPx, initialHeight - (e.clientY - initialY) * 2 - 20)
            maxHeight.value = `${Math.min(newHeight, window.innerHeight - 165)}px`
          }
 
          if (currentResizeDirection === 'top-right') {
            newWidth = Math.max(minWidthPx, initialWidth + (e.clientX - initialX) * 2)
            minWidth.value = `${newWidth}px`
            newHeight = Math.max(minHeightPx, initialHeight - (e.clientY - initialY) * 2 - 20)
            maxHeight.value = `${Math.min(newHeight, window.innerHeight - 165)}px`
          }
 
          if (currentResizeDirection === 'bottom-left') {
            newWidth = Math.max(minWidthPx, initialWidth - (e.clientX - initialX) * 2)
            minWidth.value = `${newWidth}px`
            newHeight = Math.max(minHeightPx, initialHeight + (e.clientY - initialY) * 2 - 20)
            maxHeight.value = `${Math.min(newHeight, window.innerHeight - 165)}px`
          }
 
          if (currentResizeDirection === 'bottom-right') {
            newWidth = Math.max(minWidthPx, initialWidth + (e.clientX - initialX) * 2)
            minWidth.value = `${newWidth}px`
            newHeight = Math.max(minHeightPx, initialHeight + (e.clientY - initialY) * 2 - 20)
            maxHeight.value = `${Math.min(newHeight, window.innerHeight - 165)}px`
          }
        }
        // 停止调整大小的事件处理器
        const stopResizing = () => {
          isResizing = false
          document.removeEventListener('mousemove', handleResizing)
          document.removeEventListener('mouseup', stopResizing)
        }
 
        document.addEventListener('mousemove', handleResizing)
        document.addEventListener('mouseup', stopResizing)
      }
    }
    elDialog.addEventListener('mousemove', handleMouseMove)
    elDialog.addEventListener('mousedown', handleMouseDown)
  }
 
  return {
    setupDrag,
    maxHeight,
    minWidth
  }
}