-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathRect.jsx
301 lines (256 loc) · 8.07 KB
/
Rect.jsx
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
import * as React from "react";
import { getAngle, getLength } from "./utils";
import { Box } from "@mui/material";
import PropTypes from "prop-types";
import ResizeHandle from "./ResizeHandle";
import RotateHandle from "./RotateHandle";
export default function Rect({
children,
onClick,
onDrag,
onResize,
onRotate,
selected,
styles: {
position: { centerX, centerY },
size: { height, width },
transform: { rotateAngle }
},
resizeDirection,
zIndex
}) {
// ref to the element
const ref = React.useRef();
// track whether the mouse is down
const isMouseDown = React.useRef(false);
/**
* Callback for when the mouse is pressed down on the rotate handle. Initializes the rotation.
* @param {React.MouseEvent} e Mouse event
*/
const startRotate = e => {
// only accept left clicks
if (e.button !== 0) return;
// dont propagate event
e.stopPropagation();
// get mouse position
const { clientX, clientY } = e;
// get the current rectangle layout
const rect = ref.current.getBoundingClientRect();
const startAngle = rotateAngle;
// get the center of the rectangle
const center = {
x: rect.left + rect.width / 2,
y: rect.top + rect.height / 2
};
// ge the mouse move vector
const startVector = {
x: clientX - center.x,
y: clientY - center.y
};
// mouse is down
isMouseDown.current = true;
/**
* Callback for mouse move. Calculates the new rotation angle and calls the user callback.
* @param {React.MouseEvent} e Mouse event
*/
const onMove = e => {
// abort if mouse is not down
if (!isMouseDown.current) return; // patch: fix windows press win key during mouseup issue
// stop event propagation
e.stopPropagation();
// get the mouse move vector
const { clientX, clientY } = e;
const rotateVector = {
x: clientX - center.x,
y: clientY - center.y
};
// calculate the new rotation angle
const angle = getAngle(startVector, rotateVector);
// call user callback
onRotate(angle, startAngle);
};
/**
* Callback for mouse up. Stops listening for mouse move events.
*/
const onUp = () => {
document.removeEventListener("mousemove", onMove);
document.removeEventListener("mouseup", onUp);
if (!isMouseDown.current) return;
isMouseDown.current = false;
};
// add mouse move and mouse up listeners
document.addEventListener("mousemove", onMove);
document.addEventListener("mouseup", onUp);
};
/**
* Callback for when the mouse is pressed down on the resize handle. Initializes the resize.
* @param {React.MouseEvent} e Mouse event
* @param {string} cursor Cursor type to add to document
* @param {string} type Resize handle type
*/
const startResize = (e, cursor, type) => {
// set the cursor to the document
document.body.style.cursor = cursor;
// stop event propagation
e.stopPropagation();
// get mouse start position
const { clientX: startX, clientY: startY } = e;
// define the start rectangle layout
const rect = {
centerX,
centerY,
height,
rotateAngle,
width
};
// track whether the mouse is down
isMouseDown.current = true;
/**
* Callback for mouse move. Calculates the new rectangle layout and calls the user callback.
* @param {React.MouseEvent} e Mouse event
*/
const onMove = e => {
// abort if mouse is not down
if (!isMouseDown.current) return; // patch: fix windows press win key during mouseup issue
// stop event propagation
e.stopPropagation();
// calculate angle and distance of mouse movement
const { clientX, clientY } = e;
const deltaX = clientX - startX;
const deltaY = clientY - startY;
const alpha = Math.atan2(deltaY, deltaX);
const deltaL = getLength(deltaX, deltaY);
// is shift key down?
const isShiftKey = e.shiftKey;
// call user callback
onResize(deltaL, alpha, rect, type, isShiftKey);
};
/**
* Callback for mouse up. Stops listening for mouse move events.
*/
const onUp = () => {
// return cursor to default
document.body.style.cursor = "auto";
// remove mouse move and mouse up listeners
document.removeEventListener("mousemove", onMove);
document.removeEventListener("mouseup", onUp);
// reset mouse down state
if (!isMouseDown.current) return;
isMouseDown.current = false;
};
// add mouse move and mouse up listeners
document.addEventListener("mousemove", onMove);
document.addEventListener("mouseup", onUp);
};
/**
* Callback for when the mouse is pressed down on the rectangle. Initializes the drag.
* @param {React.MouseEvent} e Mouse event
*/
const startDrag = e => {
// get mouse start position
const { clientX: startX, clientY: startY } = e;
const startCenterX = centerX;
const startCenterY = centerY;
// track whether the mouse is down
isMouseDown.current = true;
// stop event propagation
e.stopPropagation();
/**
* Callback for mouse move. Calculates the new rectangle center coordinates and calls the user callback.
* @param {React.MouseEvent} e Mouse event
*/
const onMove = e => {
// abort if mouse is not down
if (!isMouseDown.current) return; // patch: fix windows press win key during mouseup issue
// stop event propagation
e.stopPropagation();
// calculate the new center coordinates
const { clientX, clientY } = e;
const deltaX = clientX - startX;
const deltaY = clientY - startY;
const newCenterX = startCenterX + deltaX;
const newCenterY = startCenterY + deltaY;
// call user callback
onDrag(newCenterX, newCenterY);
};
/**
* Callback for mouse up. Stops listening for mouse move events.
*/
const onUp = () => {
// remove mouse move and mouse up listeners
document.removeEventListener("mousemove", onMove);
document.removeEventListener("mouseup", onUp);
// reset mouse down state
if (!isMouseDown.current) return;
isMouseDown.current = false;
};
// add mouse move and mouse up listeners
document.addEventListener("mousemove", onMove);
document.addEventListener("mouseup", onUp);
};
// convert the single string format to array of resize handle types
const allowableDirection = ["n", "e", "s", "w", "nw", "ne", "se", "sw"];
resizeDirection = resizeDirection.filter(d => allowableDirection.includes(d));
return (
<Box
ref={ref}
sx={{
cursor: selected && onDrag ? "move" : "pointer",
height: `${Math.abs(height)}px`,
left: `${centerX - Math.abs(width) / 2}px`,
outline: theme =>
selected ? `1px solid ${theme.palette.primary.main}` : 0,
position: "absolute",
top: `${centerY - Math.abs(height) / 2}px`,
transform: `rotate(${rotateAngle}deg)`,
width: `${Math.abs(width)}px`,
zIndex
}}
onMouseDown={startDrag}
onClick={onClick}
className="canvas-item"
>
{children}
{selected && onRotate && <RotateHandle onRotate={startRotate} />}
{selected &&
resizeDirection.map(d => {
return (
<ResizeHandle
key={d}
direction={d}
onResize={startResize}
rotateAngle={rotateAngle}
/>
);
})}
</Box>
);
}
Rect.propTypes = {
/**
* Callback for rectangle click
*/
onClick: PropTypes.func,
/**
* Callback for rectangle resize
*/
onResize: PropTypes.func,
/**
* Callback for rectangle rotation
*/
onRotate: PropTypes.func,
/**
* The allowable resize directions of the item. Array of 'n', 's', 'e', 'w', 'ne', 'nw', 'se', 'sw'.
*/
resizeDirection: PropTypes.arrayOf(
PropTypes.oneOf(["n", "ne", "e", "se", "s", "sw", "w", "nw"])
),
/**
* Styles object
*/
styles: PropTypes.object,
/**
* Z-index of the rectangle
*/
zIndex: PropTypes.number
};