1
1
import { useEffect , useRef , useState } from 'react'
2
2
import { AttrType } from '../types'
3
3
import { onMove } from '@/utils/util'
4
+ import { DraggableBoxProps } from '../components/DraggableBox'
4
5
5
- export default function useMouse (
6
- defaultAttrs : AttrType ,
7
- scale : number ,
8
- parent : boolean
9
- ) {
6
+ export default function useMouse ( props : DraggableBoxProps ) {
7
+ const {
8
+ attrs : defaultAttrs ,
9
+ scale,
10
+ parent,
11
+ draggable,
12
+ resizable,
13
+ onDragStart,
14
+ onDrag,
15
+ onDragEnd,
16
+ onResizeStart,
17
+ onResize,
18
+ onResizeEnd
19
+ } = props
10
20
const [ attrs , setAttrs ] = useState ( defaultAttrs )
21
+ const [ isDragging , setIsDrag ] = useState ( false )
22
+ const [ isResizing , setIsResize ] = useState ( false )
11
23
const dragBoxRef = useRef < HTMLDivElement | null > ( null )
12
24
let mouseEventRemove = ( ) => { }
13
- function onPonitMouseHandle ( mouseDownEvent : any , point : string ) {
25
+ function onPonitMouseHandle ( mouseDownEvent : React . MouseEvent , point : string ) {
14
26
mouseDownEvent . stopPropagation ( )
15
27
mouseDownEvent . preventDefault ( )
28
+ if ( ! draggable ) return
29
+ const resizeStartReturn = onResizeStart ?.( mouseDownEvent , point )
30
+ if ( resizeStartReturn === false ) return
16
31
// 记录初始位置和大小
17
32
const itemAttrX = attrs . x
18
33
const itemAttrY = attrs . y
@@ -25,47 +40,58 @@ export default function useMouse(
25
40
. offsetWidth
26
41
const parentHeight = ( dragBoxRef . current ! . parentNode ! as HTMLElement )
27
42
. offsetHeight
28
- mouseEventRemove = onMove ( ( moveEvent ) => {
29
- let currX = Math . round ( ( moveEvent . screenX - startX ) / scale )
30
- let currY = Math . round ( ( moveEvent . screenY - startY ) / scale )
31
- const isTop = / t / . test ( point )
32
- const isBottom = / b / . test ( point )
33
- const isLeft = / l / . test ( point )
34
- const isRight = / r / . test ( point )
35
- const newHeight = itemAttrH + ( isTop ? - currY : isBottom ? currY : 0 )
36
- const newWidth = itemAttrW + ( isLeft ? - currX : isRight ? currX : 0 )
37
- let h = Math . abs ( newHeight )
38
- let w = Math . abs ( newWidth )
39
- let x =
40
- newWidth > 0
41
- ? itemAttrX + ( isLeft ? currX : 0 )
42
- : itemAttrX + ( isLeft ? itemAttrW : newWidth )
43
- let y =
44
- newHeight > 0
45
- ? itemAttrY + ( isTop ? currY : 0 )
46
- : itemAttrY + ( isTop ? itemAttrH : newHeight )
47
- if ( parent ) {
48
- if ( x < 0 ) {
49
- w = w + x
50
- x = 0
51
- }
52
- if ( y < 0 ) {
53
- h = h + y
54
- y = 0
55
- }
56
- if ( x + w > parentWidth ) {
57
- w = parentWidth - x
58
- }
59
- if ( y + h > parentHeight ) {
60
- h = parentHeight - y
43
+ mouseEventRemove = onMove (
44
+ ( moveEvent ) => {
45
+ setIsResize ( true )
46
+ let currX = Math . round ( ( moveEvent . screenX - startX ) / scale ! )
47
+ let currY = Math . round ( ( moveEvent . screenY - startY ) / scale ! )
48
+ const isTop = / t / . test ( point )
49
+ const isBottom = / b / . test ( point )
50
+ const isLeft = / l / . test ( point )
51
+ const isRight = / r / . test ( point )
52
+ const newHeight = itemAttrH + ( isTop ? - currY : isBottom ? currY : 0 )
53
+ const newWidth = itemAttrW + ( isLeft ? - currX : isRight ? currX : 0 )
54
+ let h = Math . abs ( newHeight )
55
+ let w = Math . abs ( newWidth )
56
+ let x =
57
+ newWidth > 0
58
+ ? itemAttrX + ( isLeft ? currX : 0 )
59
+ : itemAttrX + ( isLeft ? itemAttrW : newWidth )
60
+ let y =
61
+ newHeight > 0
62
+ ? itemAttrY + ( isTop ? currY : 0 )
63
+ : itemAttrY + ( isTop ? itemAttrH : newHeight )
64
+ if ( parent ) {
65
+ if ( x < 0 ) {
66
+ w = w + x
67
+ x = 0
68
+ }
69
+ if ( y < 0 ) {
70
+ h = h + y
71
+ y = 0
72
+ }
73
+ if ( x + w > parentWidth ) {
74
+ w = parentWidth - x
75
+ }
76
+ if ( y + h > parentHeight ) {
77
+ h = parentHeight - y
78
+ }
61
79
}
80
+ onResize ?.( { x, y, w, h } , point )
81
+ setAttrs ( { x, y, w, h } )
82
+ } ,
83
+ ( ) => {
84
+ onResizeEnd ?.( attrs , point )
85
+ setIsResize ( false )
62
86
}
63
- setAttrs ( { x, y, w, h } )
64
- } )
87
+ )
65
88
}
66
- function onBoxMouseHandle ( e : any , distance = 10 ) {
89
+ function onBoxMouseHandle ( e : React . MouseEvent , distance = 0 ) {
67
90
e . preventDefault ( )
68
91
e . stopPropagation ( )
92
+ if ( ! resizable ) return
93
+ const dragStartReturn = onDragStart ?.( e )
94
+ if ( dragStartReturn === false ) return
69
95
// 记录初始位置和大小
70
96
const itemAttrX = attrs . x
71
97
const itemAttrY = attrs . y
@@ -78,30 +104,43 @@ export default function useMouse(
78
104
. offsetWidth
79
105
const parentHeight = ( dragBoxRef . current ! . parentNode ! as HTMLElement )
80
106
. offsetHeight
81
- mouseEventRemove = onMove ( ( moveEvent ) => {
82
- let currX = Math . round ( itemAttrX + ( moveEvent . screenX - startX ) / scale )
83
- let currY = Math . round ( itemAttrY + ( moveEvent . screenY - startY ) / scale )
84
- // 要预留的距离
85
- if ( parent ) {
86
- // 基于左上角位置检测
87
- currX = currX < 0 ? 0 : currX
88
- currY = currY < 0 ? 0 : currY
89
- // 基于右下角位置检测
90
- currX =
91
- currX > parentWidth - itemAttrW ? parentWidth - itemAttrW : currX
92
- currY =
93
- currY > parentHeight - itemAttrH ? parentHeight - itemAttrH : currY
94
- } else {
95
- // 基于左上角位置检测
96
- currX = currX < - itemAttrW + distance ? - itemAttrW + distance : currX
97
- currY = currY < - itemAttrH + distance ? - itemAttrH + distance : currY
98
- // 基于右下角位置检测
99
- currX = currX > parentWidth - distance ? parentWidth - distance : currX
100
- currY =
101
- currY > parentHeight - distance ? parentHeight - distance : currY
107
+ mouseEventRemove = onMove (
108
+ ( moveEvent ) => {
109
+ setIsDrag ( true )
110
+ let currX = Math . round (
111
+ itemAttrX + ( moveEvent . screenX - startX ) / scale !
112
+ )
113
+ let currY = Math . round (
114
+ itemAttrY + ( moveEvent . screenY - startY ) / scale !
115
+ )
116
+ // 要预留的距离
117
+ if ( parent ) {
118
+ // 基于左上角位置检测
119
+ currX = currX < 0 ? 0 : currX
120
+ currY = currY < 0 ? 0 : currY
121
+ // 基于右下角位置检测
122
+ currX =
123
+ currX > parentWidth - itemAttrW ? parentWidth - itemAttrW : currX
124
+ currY =
125
+ currY > parentHeight - itemAttrH ? parentHeight - itemAttrH : currY
126
+ } else {
127
+ // 基于左上角位置检测
128
+ currX = currX < - itemAttrW + distance ? - itemAttrW + distance : currX
129
+ currY = currY < - itemAttrH + distance ? - itemAttrH + distance : currY
130
+ // 基于右下角位置检测
131
+ currX =
132
+ currX > parentWidth - distance ? parentWidth - distance : currX
133
+ currY =
134
+ currY > parentHeight - distance ? parentHeight - distance : currY
135
+ }
136
+ onDrag ?.( { ...attrs , x : currX , y : currY } )
137
+ setAttrs ( { ...attrs , x : currX , y : currY } )
138
+ } ,
139
+ ( ) => {
140
+ onDragEnd ?.( attrs )
141
+ setIsDrag ( false )
102
142
}
103
- setAttrs ( { ...attrs , x : currX , y : currY } )
104
- } )
143
+ )
105
144
}
106
145
useEffect ( ( ) => {
107
146
return mouseEventRemove ( )
@@ -110,6 +149,8 @@ export default function useMouse(
110
149
attrs,
111
150
onPonitMouseHandle,
112
151
onBoxMouseHandle,
113
- dragBoxRef
152
+ dragBoxRef,
153
+ isDragging,
154
+ isResizing
114
155
}
115
156
}
0 commit comments