-
Notifications
You must be signed in to change notification settings - Fork 91
Expand file tree
/
Copy pathindex.js
More file actions
116 lines (104 loc) · 3.61 KB
/
index.js
File metadata and controls
116 lines (104 loc) · 3.61 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
import PropTypes from 'prop-types'
import React, { Component } from 'react'
import Rect from './Rect'
import { centerToTL, tLToCenter, getNewStyle, degToRadian } from './utils'
export default class ResizableRect extends Component {
static propTypes = {
left: PropTypes.number.isRequired,
top: PropTypes.number.isRequired,
width: PropTypes.number.isRequired,
height: PropTypes.number.isRequired,
rotatable: PropTypes.bool,
rotateAngle: PropTypes.number,
parentRotateAngle: PropTypes.number,
zoomable: PropTypes.string,
minWidth: PropTypes.number,
minHeight: PropTypes.number,
aspectRatio: PropTypes.oneOfType([
PropTypes.number,
PropTypes.bool
]),
dragStartThreshold: PropTypes.number,
dragStartTimeThreshold: PropTypes.number,
onRotateStart: PropTypes.func,
onRotate: PropTypes.func,
onRotateEnd: PropTypes.func,
onResizeStart: PropTypes.func,
onResize: PropTypes.func,
onResizeEnd: PropTypes.func,
onDragStart: PropTypes.func,
onDrag: PropTypes.func,
onDragEnd: PropTypes.func
}
static defaultProps = {
parentRotateAngle: 0,
rotateAngle: 0,
rotatable: true,
zoomable: '',
minWidth: 10,
minHeight: 10,
dragStartThreshold: 3,
dragStartTimeThreshold: 1000
}
handleRotate = (angle, startAngle) => {
if (!this.props.onRotate) return
let rotateAngle = Math.round(startAngle + angle)
if (rotateAngle >= 360) {
rotateAngle -= 360
} else if (rotateAngle < 0) {
rotateAngle += 360
}
if (rotateAngle > 356 || rotateAngle < 4) {
rotateAngle = 0
} else if (rotateAngle > 86 && rotateAngle < 94) {
rotateAngle = 90
} else if (rotateAngle > 176 && rotateAngle < 184) {
rotateAngle = 180
} else if (rotateAngle > 266 && rotateAngle < 274) {
rotateAngle = 270
}
this.props.onRotate(rotateAngle)
}
handleResize = (length, alpha, rect, type, isShiftKey) => {
if (!this.props.onResize) return
const { rotateAngle, aspectRatio, minWidth, minHeight, parentRotateAngle } = this.props
const beta = alpha - degToRadian(rotateAngle + parentRotateAngle)
const deltaW = length * Math.cos(beta)
const deltaH = length * Math.sin(beta)
const ratio = isShiftKey && !aspectRatio ? rect.width / rect.height : aspectRatio
const {
position: { centerX, centerY },
size: { width, height }
} = getNewStyle(type, { ...rect, rotateAngle }, deltaW, deltaH, ratio, minWidth, minHeight)
this.props.onResize(centerToTL({ centerX, centerY, width, height, rotateAngle }), isShiftKey, type)
}
handleDrag = (deltaX, deltaY) => {
this.props.onDrag && this.props.onDrag(deltaX, deltaY)
}
render () {
const {
top, left, width, height, rotateAngle, parentRotateAngle, zoomable, rotatable, dragStartThreshold, dragStartTimeThreshold,
onRotate, onResizeStart, onResizeEnd, onRotateStart, onRotateEnd, onDragStart, onDragEnd
} = this.props
const styles = tLToCenter({ top, left, width, height, rotateAngle })
return (
<Rect
styles={styles}
zoomable={zoomable}
rotatable={Boolean(rotatable && onRotate)}
parentRotateAngle={parentRotateAngle}
dragStartThreshold={dragStartThreshold}
dragStartTimeThreshold={dragStartTimeThreshold}
onResizeStart={onResizeStart}
onResize={this.handleResize}
onResizeEnd={onResizeEnd}
onRotateStart={onRotateStart}
onRotate={this.handleRotate}
onRotateEnd={onRotateEnd}
onDragStart={onDragStart}
onDrag={this.handleDrag}
onDragEnd={onDragEnd}
/>
)
}
}