-
Notifications
You must be signed in to change notification settings - Fork 730
/
Copy pathBrushCorner.tsx
175 lines (160 loc) · 5.41 KB
/
BrushCorner.tsx
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
/* eslint react/jsx-handler-names: 0 */
import React from 'react';
import Drag, { HandlerArgs as DragArgs } from '@visx/drag/lib/Drag';
import { BaseBrushState as BrushState, UpdateBrush } from './BaseBrush';
import { ResizeTriggerAreas } from './types';
export type BrushCornerProps = {
stageWidth: number;
stageHeight: number;
brush: BrushState;
updateBrush: (update: UpdateBrush) => void;
onBrushEnd?: (brush: BrushState) => void;
type: ResizeTriggerAreas;
style?: React.CSSProperties;
corner: { x: number; y: number; width: number; height: number };
};
export type BrushCornerState = {};
export default class BrushCorner extends React.Component<BrushCornerProps, BrushCornerState> {
static defaultProps = {
style: {},
};
cornerDragMove = (drag: DragArgs) => {
const { updateBrush, type } = this.props;
if (!drag.isDragging) return;
updateBrush((prevBrush: Readonly<BrushState>) => {
const { start, end } = prevBrush;
const xMax = Math.max(start.x, end.x);
const xMin = Math.min(start.x, end.x);
const yMax = Math.max(start.y, end.y);
const yMin = Math.min(start.y, end.y);
let moveX = 0;
let moveY = 0;
switch (type) {
case 'topRight':
moveX = xMax + drag.dx;
moveY = yMin + drag.dy;
return {
...prevBrush,
activeHandle: type,
extent: {
...prevBrush.extent,
x0: Math.max(Math.min(moveX, start.x), prevBrush.bounds.x0),
x1: Math.min(Math.max(moveX, start.x), prevBrush.bounds.x1),
y0: Math.max(Math.min(moveY, end.y), prevBrush.bounds.y0),
y1: Math.min(Math.max(moveY, end.y), prevBrush.bounds.y1),
},
};
case 'topLeft':
moveX = xMin + drag.dx;
moveY = yMin + drag.dy;
return {
...prevBrush,
activeHandle: type,
extent: {
...prevBrush.extent,
x0: Math.max(Math.min(moveX, end.x), prevBrush.bounds.x0),
x1: Math.min(Math.max(moveX, end.x), prevBrush.bounds.x1),
y0: Math.max(Math.min(moveY, end.y), prevBrush.bounds.y0),
y1: Math.min(Math.max(moveY, end.y), prevBrush.bounds.y1),
},
};
case 'bottomLeft':
moveX = xMin + drag.dx;
moveY = yMax + drag.dy;
return {
...prevBrush,
activeHandle: type,
extent: {
...prevBrush.extent,
x0: Math.max(Math.min(moveX, end.x), prevBrush.bounds.x0),
x1: Math.min(Math.max(moveX, end.x), prevBrush.bounds.x1),
y0: Math.max(Math.min(moveY, start.y), prevBrush.bounds.y0),
y1: Math.min(Math.max(moveY, start.y), prevBrush.bounds.y1),
},
};
case 'bottomRight':
moveX = xMax + drag.dx;
moveY = yMax + drag.dy;
return {
...prevBrush,
activeHandle: type,
extent: {
...prevBrush.extent,
x0: Math.max(Math.min(moveX, start.x), prevBrush.bounds.x0),
x1: Math.min(Math.max(moveX, start.x), prevBrush.bounds.x1),
y0: Math.max(Math.min(moveY, start.y), prevBrush.bounds.y0),
y1: Math.min(Math.max(moveY, start.y), prevBrush.bounds.y1),
},
};
default:
return prevBrush;
}
});
};
cornerDragEnd = () => {
const { updateBrush, onBrushEnd } = this.props;
updateBrush((prevBrush: Readonly<BrushState>) => {
const { start, end, extent } = prevBrush;
start.x = Math.min(extent.x0, extent.x1);
start.y = Math.min(extent.y0, extent.y0);
end.x = Math.max(extent.x0, extent.x1);
end.y = Math.max(extent.y0, extent.y1);
const nextBrush = {
...prevBrush,
start,
end,
activeHandle: null,
domain: {
x0: Math.min(start.x, end.x),
x1: Math.max(start.x, end.x),
y0: Math.min(start.y, end.y),
y1: Math.max(start.y, end.y),
},
};
if (onBrushEnd) {
onBrushEnd(nextBrush);
}
return nextBrush;
});
};
render() {
const { type, brush, stageWidth, stageHeight, style: styleProp, corner } = this.props;
const cursor =
(styleProp && styleProp.cursor) ||
(type === 'topLeft' || type === 'bottomRight' ? 'nwse-resize' : 'nesw-resize');
const pointerEvents = brush.activeHandle || brush.isBrushing ? 'none' : 'all';
return (
<Drag
width={stageWidth}
height={stageHeight}
onDragMove={this.cornerDragMove}
onDragEnd={this.cornerDragEnd}
resetOnStart
>
{({ dragMove, dragEnd, dragStart, isDragging }) => (
<g>
{isDragging && (
<rect
fill="transparent"
width={stageWidth}
height={stageHeight}
style={{ cursor }}
onMouseMove={dragMove}
onMouseUp={dragEnd}
/>
)}
<rect
fill="transparent"
onMouseDown={dragStart}
onMouseMove={dragMove}
onMouseUp={dragEnd}
className={`visx-brush-corner-${type}`}
style={{ cursor, pointerEvents, ...styleProp }}
{...corner}
/>
</g>
)}
</Drag>
);
}
}