-
Notifications
You must be signed in to change notification settings - Fork 91
Expand file tree
/
Copy pathindex.js
More file actions
196 lines (184 loc) · 6.14 KB
/
index.js
File metadata and controls
196 lines (184 loc) · 6.14 KB
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
import PropTypes from 'prop-types'
import React, { PureComponent } from 'react'
import { getLength, getAngle, getCursor } from '../utils'
import StyledRect from './StyledRect'
const zoomableMap = {
'n': 't',
's': 'b',
'e': 'r',
'w': 'l',
'ne': 'tr',
'nw': 'tl',
'se': 'br',
'sw': 'bl'
}
const MAIN_MOUSE_BUTTON = 0
export default class Rect extends PureComponent {
static propTypes = {
styles: PropTypes.object,
zoomable: PropTypes.string,
rotatable: PropTypes.bool,
onResizeStart: PropTypes.func,
onResize: PropTypes.func,
onResizeEnd: PropTypes.func,
onRotateStart: PropTypes.func,
onRotate: PropTypes.func,
onRotateEnd: PropTypes.func,
onDragStart: PropTypes.func,
onDrag: PropTypes.func,
onDragEnd: PropTypes.func,
parentRotateAngle: PropTypes.number
}
setElementRef = (ref) => { this.$element = ref }
// Drag
startDrag = (e) => {
if (e.button !== MAIN_MOUSE_BUTTON) return
let { clientX: startX, clientY: startY } = e
this.props.onDragStart && this.props.onDragStart()
this._isMouseDown = true
const onMove = (e) => {
if (!this._isMouseDown) return // patch: fix windows press win key during mouseup issue
e.stopImmediatePropagation()
const { clientX, clientY } = e
const deltaX = clientX - startX
const deltaY = clientY - startY
this.props.onDrag(deltaX, deltaY)
startX = clientX
startY = clientY
}
const onUp = () => {
document.removeEventListener('mousemove', onMove)
document.removeEventListener('mouseup', onUp)
if (!this._isMouseDown) return
this._isMouseDown = false
this.props.onDragEnd && this.props.onDragEnd()
}
document.addEventListener('mousemove', onMove)
document.addEventListener('mouseup', onUp)
}
// Rotate
startRotate = (e) => {
if (e.button !== MAIN_MOUSE_BUTTON) return
const { clientX, clientY } = e
const { styles: { transform: { rotateAngle: startAngle } } } = this.props
const rect = this.$element.getBoundingClientRect()
const center = {
x: rect.left + rect.width / 2,
y: rect.top + rect.height / 2
}
const startVector = {
x: clientX - center.x,
y: clientY - center.y
}
this.props.onRotateStart && this.props.onRotateStart()
this._isMouseDown = true
const onMove = (e) => {
if (!this._isMouseDown) return // patch: fix windows press win key during mouseup issue
e.stopImmediatePropagation()
const { clientX, clientY } = e
const rotateVector = {
x: clientX - center.x,
y: clientY - center.y
}
const angle = getAngle(startVector, rotateVector)
this.props.onRotate(angle, startAngle)
}
const onUp = () => {
document.removeEventListener('mousemove', onMove)
document.removeEventListener('mouseup', onUp)
if (!this._isMouseDown) return
this._isMouseDown = false
this.props.onRotateEnd && this.props.onRotateEnd()
}
document.addEventListener('mousemove', onMove)
document.addEventListener('mouseup', onUp)
}
// Resize
startResize = (e, cursor) => {
if (e.button !== MAIN_MOUSE_BUTTON) return
document.body.style.cursor = cursor
const { styles: { position: { centerX, centerY }, size: { width, height }, transform: { rotateAngle } } } = this.props
const { clientX: startX, clientY: startY } = e
const rect = { width, height, centerX, centerY, rotateAngle }
const type = e.target.getAttribute('class').split(' ')[ 0 ]
this.props.onResizeStart && this.props.onResizeStart()
this._isMouseDown = true
const onMove = (e) => {
if (!this._isMouseDown) return // patch: fix windows press win key during mouseup issue
e.stopImmediatePropagation()
const { clientX, clientY } = e
const deltaX = clientX - startX
const deltaY = clientY - startY
const alpha = Math.atan2(deltaY, deltaX)
const deltaL = getLength(deltaX, deltaY)
const isShiftKey = e.shiftKey
this.props.onResize(deltaL, alpha, rect, type, isShiftKey)
}
const onUp = () => {
document.body.style.cursor = 'auto'
document.removeEventListener('mousemove', onMove)
document.removeEventListener('mouseup', onUp)
if (!this._isMouseDown) return
this._isMouseDown = false
this.props.onResizeEnd && this.props.onResizeEnd()
}
document.addEventListener('mousemove', onMove)
document.addEventListener('mouseup', onUp)
}
render () {
const {
styles: {
position: { centerX, centerY },
size: { width, height },
transform: { rotateAngle }
},
zoomable,
rotatable,
parentRotateAngle
} = this.props
const style = {
width: Math.abs(width),
height: Math.abs(height),
transform: `rotate(${rotateAngle}deg)`,
left: centerX - Math.abs(width) / 2,
top: centerY - Math.abs(height) / 2
}
const direction = zoomable.split(',').map(d => d.trim()).filter(d => d) // TODO: may be speed up
return (
<StyledRect
ref={this.setElementRef}
onMouseDown={this.startDrag}
className="rect single-resizer"
style={style}
>
{
rotatable &&
<div className="rotate" onMouseDown={this.startRotate}>
<svg width="14" height="14" xmlns="http://www.w3.org/2000/svg">
<path
d="M10.536 3.464A5 5 0 1 0 11 10l1.424 1.425a7 7 0 1 1-.475-9.374L13.659.34A.2.2 0 0 1 14 .483V5.5a.5.5 0 0 1-.5.5H8.483a.2.2 0 0 1-.142-.341l2.195-2.195z"
fill="#eb5648"
fillRule="nonzero"
/>
</svg>
</div>
}
{
direction.map(d => {
const cursor = `${getCursor(rotateAngle + parentRotateAngle, d)}-resize`
return (
<div key={d} style={{ cursor }} className={`${zoomableMap[ d ]} resizable-handler`} onMouseDown={(e) => this.startResize(e, cursor)} />
)
})
}
{
direction.map(d => {
return (
<div key={d} className={`${zoomableMap[ d ]} square`} />
)
})
}
</StyledRect>
)
}
}