From cbad8a56ecee32fca9f2090d24c75cc40ddc47e3 Mon Sep 17 00:00:00 2001 From: Nathan Sobo Date: Mon, 7 Apr 2014 11:09:44 -0600 Subject: [PATCH] Don't start the animation loop until the mouse starts dragging Previously, the animation loop would run multiple times prior to the the mouseup event on click. We only want to select to the current mouse position if the mouse is actually dragged. --- src/editor-component.coffee | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/src/editor-component.coffee b/src/editor-component.coffee index 9bf0a6215..4ae9815df 100644 --- a/src/editor-component.coffee +++ b/src/editor-component.coffee @@ -284,16 +284,28 @@ EditorCompont = React.createClass @selectToMousePositionUntilMouseUp(event) selectToMousePositionUntilMouseUp: (event) -> - dragging = true - lastMousePosition = {clientX: event.clientX, clientY: event.clientY} + {editor} = @props + dragging = false + lastMousePosition = {} + + animationLoop = => + requestAnimationFrame => + if dragging + @selectToMousePosition(lastMousePosition) + animationLoop() onMouseMove = (event) -> - # Stop dragging when cursor enters dev tools because we can't detect mouseup - dragging = false if event.which is 0 - lastMousePosition.clientX = event.clientX lastMousePosition.clientY = event.clientY + # Start the animation loop when the mouse moves prior to a mouseup event + unless dragging + dragging = true + animationLoop() + + # Stop dragging when cursor enters dev tools because we can't detect mouseup + onMouseUp() if event.which is 0 + onMouseUp = -> dragging = false window.removeEventListener('mousemove', onMouseMove) @@ -302,14 +314,6 @@ EditorCompont = React.createClass window.addEventListener('mousemove', onMouseMove) window.addEventListener('mouseup', onMouseUp) - animationLoop = => - requestAnimationFrame => - if dragging - @selectToMousePosition(lastMousePosition) - animationLoop() - - animationLoop() - selectToMousePosition: (event) -> @props.editor.selectToScreenPosition(@screenPositionForMouseEvent(event))