Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid unintentional move on clicking #34

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 32 additions & 6 deletions src/Rect/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ export default class Rect extends PureComponent {
styles: PropTypes.object,
zoomable: PropTypes.string,
rotatable: PropTypes.bool,
dragStartThreshold: PropTypes.number,
dragStartTimeThreshold: PropTypes.number,
onResizeStart: PropTypes.func,
onResize: PropTypes.func,
onResizeEnd: PropTypes.func,
Expand All @@ -35,24 +37,48 @@ export default class Rect extends PureComponent {

// Drag
startDrag = (e) => {
let { clientX: startX, clientY: startY } = e
this.props.onDragStart && this.props.onDragStart()
let { clientX: lastX, clientY: lastY } = e
this._isMouseDown = true

let dragStarted = false
const { dragStartThreshold, dragStartTimeThreshold } = this.props
const startDragTimer = setTimeout(() => {
dragStarted = true
this.props.onDragStart && this.props.onDragStart()
}, dragStartTimeThreshold)

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 deltaX = clientX - lastX
const deltaY = clientY - lastY
lastX = clientX
lastY = clientY

if (!dragStarted) {
if (
Math.abs(deltaX) < dragStartThreshold &&
Math.abs(deltaY) < dragStartThreshold
) {
return
}
dragStarted = true
clearTimeout(startDragTimer)
this.props.onDragStart && this.props.onDragStart()
}

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
if (!dragStarted) {
clearTimeout(startDragTimer)
return
}
this.props.onDragEnd && this.props.onDragEnd()
}
document.addEventListener('mousemove', onMove)
Expand Down
10 changes: 8 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ export default class ResizableRect extends Component {
PropTypes.number,
PropTypes.bool
]),
dragStartThreshold: PropTypes.number,
dragStartTimeThreshold: PropTypes.number,
onRotateStart: PropTypes.func,
onRotate: PropTypes.func,
onRotateEnd: PropTypes.func,
Expand All @@ -36,7 +38,9 @@ export default class ResizableRect extends Component {
rotatable: true,
zoomable: '',
minWidth: 10,
minHeight: 10
minHeight: 10,
dragStartThreshold: 3,
dragStartTimeThreshold: 1000
}

handleRotate = (angle, startAngle) => {
Expand Down Expand Up @@ -80,7 +84,7 @@ export default class ResizableRect extends Component {

render () {
const {
top, left, width, height, rotateAngle, parentRotateAngle, zoomable, rotatable,
top, left, width, height, rotateAngle, parentRotateAngle, zoomable, rotatable, dragStartThreshold, dragStartTimeThreshold,
onRotate, onResizeStart, onResizeEnd, onRotateStart, onRotateEnd, onDragStart, onDragEnd
} = this.props

Expand All @@ -92,6 +96,8 @@ export default class ResizableRect extends Component {
zoomable={zoomable}
rotatable={Boolean(rotatable && onRotate)}
parentRotateAngle={parentRotateAngle}
dragStartThreshold={dragStartThreshold}
dragStartTimeThreshold={dragStartTimeThreshold}

onResizeStart={onResizeStart}
onResize={this.handleResize}
Expand Down