Merge pull request #4655 from atom/bo-fix-dragging

Handle the case when the editor is destroyed while dragging
This commit is contained in:
Ben Ogle
2014-12-16 15:05:58 -08:00
2 changed files with 23 additions and 2 deletions

View File

@@ -1612,6 +1612,22 @@ describe "TextEditorComponent", ->
expect(nextAnimationFrame).toBe noAnimationFrame
expect(editor.getSelectedScreenRange()).toEqual [[2, 4], [6, 8]]
describe "when the editor is destroyed while dragging", ->
it "cleans up the handlers for window.mouseup and window.mousemove", ->
linesNode.dispatchEvent(buildMouseEvent('mousedown', clientCoordinatesForScreenPosition([2, 4]), which: 1))
linesNode.dispatchEvent(buildMouseEvent('mousemove', clientCoordinatesForScreenPosition([6, 8]), which: 1))
nextAnimationFrame()
spyOn(window, 'removeEventListener').andCallThrough()
linesNode.dispatchEvent(buildMouseEvent('mousemove', clientCoordinatesForScreenPosition([6, 10]), which: 1))
editor.destroy()
nextAnimationFrame()
call.args.pop() for call in window.removeEventListener.calls
expect(window.removeEventListener).toHaveBeenCalledWith('mouseup')
expect(window.removeEventListener).toHaveBeenCalledWith('mousemove')
describe "when a line is folded", ->
beforeEach ->
editor.foldBufferRow 4

View File

@@ -750,10 +750,12 @@ TextEditorComponent = React.createClass
lastMousePosition = {}
animationLoop = =>
@requestAnimationFrame =>
if dragging
if dragging and @isMounted()
screenPosition = @screenPositionForMouseEvent(lastMousePosition)
dragHandler(screenPosition)
animationLoop()
else if not @isMounted()
stopDragging()
onMouseMove = (event) ->
lastMousePosition.clientX = event.clientX
@@ -768,10 +770,13 @@ TextEditorComponent = React.createClass
onMouseUp() if event.which is 0
onMouseUp = ->
stopDragging()
editor.finalizeSelections()
stopDragging = ->
dragging = false
window.removeEventListener('mousemove', onMouseMove)
window.removeEventListener('mouseup', onMouseUp)
editor.finalizeSelections()
window.addEventListener('mousemove', onMouseMove)
window.addEventListener('mouseup', onMouseUp)