1
- import { useState } from "react" ;
1
+ import { useRef , useState } from "react" ;
2
2
import { Button , Popover , Input } from "@douyinfe/semi-ui" ;
3
3
import { IconEdit , IconDeleteStroked } from "@douyinfe/semi-icons" ;
4
4
import {
@@ -9,22 +9,33 @@ import {
9
9
State ,
10
10
} from "../../data/constants" ;
11
11
import {
12
+ useCanvas ,
12
13
useLayout ,
13
14
useSettings ,
14
15
useUndoRedo ,
15
16
useSelect ,
16
17
useAreas ,
17
18
useSaveState ,
18
- useTransform ,
19
19
} from "../../hooks" ;
20
20
import ColorPalette from "../ColorPicker" ;
21
21
import { useTranslation } from "react-i18next" ;
22
+ import { useHover } from "usehooks-ts" ;
22
23
23
- export default function Area ( { data, onMouseDown, setResize, setInitCoords } ) {
24
- const [ hovered , setHovered ] = useState ( false ) ;
24
+ export default function Area ( {
25
+ data,
26
+ onPointerDown,
27
+ setResize,
28
+ setInitCoords,
29
+ } ) {
30
+ const ref = useRef ( null ) ;
31
+ const isHovered = useHover ( ref ) ;
32
+ const {
33
+ pointer : {
34
+ spaces : { diagram : pointer } ,
35
+ } ,
36
+ } = useCanvas ( ) ;
25
37
const { layout } = useLayout ( ) ;
26
38
const { settings } = useSettings ( ) ;
27
- const { transform } = useTransform ( ) ;
28
39
const { setSaveState } = useSaveState ( ) ;
29
40
const { selectedElement, setSelectedElement } = useSelect ( ) ;
30
41
@@ -35,8 +46,8 @@ export default function Area({ data, onMouseDown, setResize, setInitCoords }) {
35
46
y : data . y ,
36
47
width : data . width ,
37
48
height : data . height ,
38
- mouseX : e . clientX / transform . zoom ,
39
- mouseY : e . clientY / transform . zoom ,
49
+ pointerX : pointer . x ,
50
+ pointerY : pointer . y ,
40
51
} ) ;
41
52
} ;
42
53
@@ -84,21 +95,18 @@ export default function Area({ data, onMouseDown, setResize, setInitCoords }) {
84
95
selectedElement . open ;
85
96
86
97
return (
87
- < g
88
- onMouseEnter = { ( ) => setHovered ( true ) }
89
- onMouseLeave = { ( ) => setHovered ( false ) }
90
- >
98
+ < g ref = { ref } >
91
99
< foreignObject
92
100
key = { data . id }
93
101
x = { data . x }
94
102
y = { data . y }
95
103
width = { data . width > 0 ? data . width : 0 }
96
104
height = { data . height > 0 ? data . height : 0 }
97
- onMouseDown = { onMouseDown }
105
+ onPointerDown = { onPointerDown }
98
106
>
99
107
< div
100
108
className = { `border-2 ${
101
- hovered
109
+ isHovered
102
110
? "border-dashed border-blue-500"
103
111
: selectedElement . element === ObjectType . AREA &&
104
112
selectedElement . id === data . id
@@ -114,7 +122,7 @@ export default function Area({ data, onMouseDown, setResize, setInitCoords }) {
114
122
< div className = "text-color select-none overflow-hidden text-ellipsis" >
115
123
{ data . name }
116
124
</ div >
117
- { ( hovered || ( areaIsSelected ( ) && ! layout . sidebar ) ) && (
125
+ { ( isHovered || ( areaIsSelected ( ) && ! layout . sidebar ) ) && (
118
126
< Popover
119
127
visible = { areaIsSelected ( ) && ! layout . sidebar }
120
128
onClickOutSide = { onClickOutSide }
@@ -139,7 +147,7 @@ export default function Area({ data, onMouseDown, setResize, setInitCoords }) {
139
147
</ div >
140
148
</ div >
141
149
</ foreignObject >
142
- { hovered && (
150
+ { isHovered && (
143
151
< >
144
152
< circle
145
153
cx = { data . x }
@@ -149,7 +157,7 @@ export default function Area({ data, onMouseDown, setResize, setInitCoords }) {
149
157
stroke = "#5891db"
150
158
strokeWidth = { 2 }
151
159
cursor = "nwse-resize"
152
- onMouseDown = { ( e ) => handleResize ( e , "tl" ) }
160
+ onPointerDown = { ( e ) => e . isPrimary && handleResize ( e , "tl" ) }
153
161
/>
154
162
< circle
155
163
cx = { data . x + data . width }
@@ -159,7 +167,7 @@ export default function Area({ data, onMouseDown, setResize, setInitCoords }) {
159
167
stroke = "#5891db"
160
168
strokeWidth = { 2 }
161
169
cursor = "nesw-resize"
162
- onMouseDown = { ( e ) => handleResize ( e , "tr" ) }
170
+ onPointerDown = { ( e ) => e . isPrimary && handleResize ( e , "tr" ) }
163
171
/>
164
172
< circle
165
173
cx = { data . x }
@@ -169,7 +177,7 @@ export default function Area({ data, onMouseDown, setResize, setInitCoords }) {
169
177
stroke = "#5891db"
170
178
strokeWidth = { 2 }
171
179
cursor = "nesw-resize"
172
- onMouseDown = { ( e ) => handleResize ( e , "bl" ) }
180
+ onPointerDown = { ( e ) => e . isPrimary && handleResize ( e , "bl" ) }
173
181
/>
174
182
< circle
175
183
cx = { data . x + data . width }
@@ -179,7 +187,7 @@ export default function Area({ data, onMouseDown, setResize, setInitCoords }) {
179
187
stroke = "#5891db"
180
188
strokeWidth = { 2 }
181
189
cursor = "nwse-resize"
182
- onMouseDown = { ( e ) => handleResize ( e , "br" ) }
190
+ onPointerDown = { ( e ) => e . isPrimary && handleResize ( e , "br" ) }
183
191
/>
184
192
</ >
185
193
) }
0 commit comments