Skip to content

Commit 6f8bcdb

Browse files
committed
CHG: code sort
1 parent 7f2dcaf commit 6f8bcdb

File tree

4 files changed

+66
-53
lines changed

4 files changed

+66
-53
lines changed

README.md

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
# React-resizable-rotatable-draggable-rectangle
22

3-
>
4-
53
[![NPM](https://img.shields.io/npm/v/react-resizable-rotatable-draggable.svg)](https://www.npmjs.com/package/react-resizable-rotatable-draggable) [![JavaScript Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://standardjs.com)
64

75
A react widget that can be resized and rotated via a handler.
@@ -15,7 +13,7 @@ npm install --save react-resizable-rotatable-draggable`
1513
Then you will need to install peer dependency
1614

1715
```bash
18-
npm install --save styled-components@4"
16+
npm install --save styled-components@4
1917
```
2018

2119
### Usage
@@ -96,33 +94,32 @@ class App extends Component {
9694
}
9795
9896
export default App
99-
10097
```
10198
10299
### Props
103100
104101
| Props | Type | Default | Example |
105102
|:-----------:|:-----------------------:|:-------:|:-------------------------------------:|
106-
|left | number.isRequired | // | 10 |
107-
|top | number.isRequired | // | 10 |
108-
|width | number.isRequired | // | 100 |
109-
|height | number.isRequired | // | 100 |
103+
|left | number.isRequired | | 10 |
104+
|top | number.isRequired | | 10 |
105+
|width | number.isRequired | | 100 |
106+
|height | number.isRequired | | 100 |
110107
|rotateAngle | number | 0 | 0 |
111108
|rotatable | bool | true | true |
112109
|zoomable | string | '' | 'n, w, s, e, nw, ne, se, sw' |
113110
|minWidth | number | 10 | 0 |
114111
|minHeight | number | 10 | 0 |
115-
|aspectRatio | number (width / height) | // | 1(which makes the rectangle a square) |
116-
|onRotateStart| func | // | // |
117-
|onRotate | func | // | (rotateAngle) |
118-
|onRotateEnd | func | // | // |
119-
|onResizeStart| func | // | // |
120-
|onResize | func | // | (style, isShiftKey, type) |
121-
|onResizeEnd | func | // | // |
122-
|onDragStart | func | // | // |
123-
|onDrag | func | // | (deltaX, deltaY) |
124-
|onDragEnd | func | // | // |
112+
|aspectRatio | number (width / height) | | 1(which makes the rectangle a square) |
113+
|onRotateStart| func | | |
114+
|onRotate | func | | (rotateAngle) |
115+
|onRotateEnd | func | | |
116+
|onResizeStart| func | | |
117+
|onResize | func | | (style, isShiftKey, type) |
118+
|onResizeEnd | func | | |
119+
|onDragStart | func | | |
120+
|onDrag | func | | (deltaX, deltaY) |
121+
|onDragEnd | func | | |
125122
126123
## License
127124
128-
MIT © [Mockingbot](https://github.com/mockingbot)
125+
MIT © [MockingBot](https://github.com/mockingbot)

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
"build": "rollup -c",
2323
"build-dev": "rollup -c -w",
2424
"build-example": "cd example && npm install && npm run build",
25+
"publish-dev": "npm publish --tag dev",
2526
"start": "npm run build-dev",
2627
"deploy": "npm run build && npm run build-example && gh-pages -d example/build/dist",
2728
"prepack": "npm run build"

src/Rect/index.js

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,11 @@ export default class Rect extends PureComponent {
3131
parentRotateAngle: PropTypes.number
3232
}
3333

34-
setElementRef = ref => this.$element = ref
34+
setElementRef = (ref) => { this.$element = ref }
3535

3636
// Drag
3737
startDrag = (e) => {
38-
var { clientX: startX, clientY: startY } = e
38+
let { clientX: startX, clientY: startY } = e
3939
this.props.onDragStart && this.props.onDragStart()
4040
this._isMouseDown = true
4141
const onMove = (e) => {
@@ -69,7 +69,7 @@ export default class Rect extends PureComponent {
6969
x: rect.left + rect.width / 2,
7070
y: rect.top + rect.height / 2
7171
}
72-
var startVector = {
72+
const startVector = {
7373
x: clientX - center.x,
7474
y: clientY - center.y
7575
}
@@ -104,7 +104,7 @@ export default class Rect extends PureComponent {
104104
const { styles: { position: { centerX, centerY }, size: { width, height }, transform: { rotateAngle } } } = this.props
105105
const { clientX: startX, clientY: startY } = e
106106
const rect = { width, height, centerX, centerY, rotateAngle }
107-
const type = e.target.getAttribute('class').split(' ')[0]
107+
const type = e.target.getAttribute('class').split(' ')[ 0 ]
108108
this.props.onResizeStart && this.props.onResizeStart()
109109
this._isMouseDown = true
110110
const onMove = (e) => {
@@ -132,9 +132,24 @@ export default class Rect extends PureComponent {
132132
}
133133

134134
render () {
135-
const { styles: { position: { centerX, centerY }, size: { width, height }, transform: { rotateAngle } }, zoomable, rotatable, parentRotateAngle } = this.props
136-
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 }
137-
const direction = zoomable.split(',').map(d => d.trim()).filter(d => d)
135+
const {
136+
styles: {
137+
position: { centerX, centerY },
138+
size: { width, height },
139+
transform: { rotateAngle }
140+
},
141+
zoomable,
142+
rotatable,
143+
parentRotateAngle
144+
} = this.props
145+
const style = {
146+
width: Math.abs(width),
147+
height: Math.abs(height),
148+
transform: `rotate(${rotateAngle}deg)`,
149+
left: centerX - Math.abs(width) / 2,
150+
top: centerY - Math.abs(height) / 2
151+
}
152+
const direction = zoomable.split(',').map(d => d.trim()).filter(d => d) // TODO: may be speed up
138153

139154
return (
140155
<StyledRect
@@ -154,15 +169,15 @@ export default class Rect extends PureComponent {
154169
direction.map(d => {
155170
const cursor = `${getCursor(rotateAngle + parentRotateAngle, d)}-resize`
156171
return (
157-
<div key={d} style={{ cursor }} className={`${zoomableMap[d]} resizable-handler`} onMouseDown={(e) => this.startResize(e, cursor)} />
172+
<div key={d} style={{ cursor }} className={`${zoomableMap[ d ]} resizable-handler`} onMouseDown={(e) => this.startResize(e, cursor)} />
158173
)
159174
})
160175
}
161176

162177
{
163178
direction.map(d => {
164179
return (
165-
<div key={d} className={`${zoomableMap[d]} square`} />
180+
<div key={d} className={`${zoomableMap[ d ]} square`} />
166181
)
167182
})
168183
}

src/utils.js

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -188,34 +188,34 @@ export const getNewStyle = (type, rect, deltaW, deltaH, ratio, minWidth, minHeig
188188
}
189189
}
190190

191+
const cursorStartMap = { n: 0, ne: 1, e: 2, se: 3, s: 4, sw: 5, w: 6, nw: 7 }
192+
const cursorDirectionArray = [ 'n', 'ne', 'e', 'se', 's', 'sw', 'w', 'nw' ]
193+
const cursorMap = { 0: 0, 1: 1, 2: 2, 3: 2, 4: 3, 5: 4, 6: 4, 7: 5, 8: 6, 9: 6, 10: 7, 11: 8 }
191194
export const getCursor = (rotateAngle, d) => {
192-
const startMap = { n: 0, ne: 1, e: 2, se: 3, s: 4, sw: 5, w: 6, nw: 7 }
193-
const directionArray = ['n', 'ne', 'e', 'se', 's', 'sw', 'w', 'nw']
194-
const map = { 0: 0, 1: 1, 2: 2, 3: 2, 4: 3, 5: 4, 6: 4, 7: 5, 8: 6, 9: 6, 10: 7, 11: 8 }
195-
const increment = map[Math.floor(rotateAngle / 30)]
196-
const index = startMap[d]
195+
const increment = cursorMap[ Math.floor(rotateAngle / 30) ]
196+
const index = cursorStartMap[ d ]
197197
const newIndex = (index + increment) % 8
198-
return directionArray[newIndex]
198+
return cursorDirectionArray[ newIndex ]
199199
}
200200

201-
export const centerToTL = ({ centerX, centerY, width, height, rotateAngle }) => {
202-
const top = centerY - height / 2
203-
const left = centerX - width / 2
204-
return { top, left, width, height, rotateAngle }
205-
}
201+
export const centerToTL = ({ centerX, centerY, width, height, rotateAngle }) => ({
202+
top: centerY - height / 2,
203+
left: centerX - width / 2,
204+
width,
205+
height,
206+
rotateAngle
207+
})
206208

207-
export const tLToCenter = ({ top, left, width, height, rotateAngle }) => {
208-
return {
209-
position: {
210-
centerX: left + width / 2,
211-
centerY: top + height / 2
212-
},
213-
size: {
214-
width,
215-
height
216-
},
217-
transform: {
218-
rotateAngle
219-
}
209+
export const tLToCenter = ({ top, left, width, height, rotateAngle }) => ({
210+
position: {
211+
centerX: left + width / 2,
212+
centerY: top + height / 2
213+
},
214+
size: {
215+
width,
216+
height
217+
},
218+
transform: {
219+
rotateAngle
220220
}
221-
}
221+
})

0 commit comments

Comments
 (0)