From 09a0773043991afa4620d5bf51e896682e3b4427 Mon Sep 17 00:00:00 2001 From: Ben Ogle Date: Tue, 16 Dec 2014 14:27:11 -0800 Subject: [PATCH] Handle editor destruction when dragging. Closes #4622 --- spec/text-editor-component-spec.coffee | 16 ++++++++++++++++ src/text-editor-component.coffee | 9 +++++++-- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/spec/text-editor-component-spec.coffee b/spec/text-editor-component-spec.coffee index 7e0810ceb..bfc343c03 100644 --- a/spec/text-editor-component-spec.coffee +++ b/spec/text-editor-component-spec.coffee @@ -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 diff --git a/src/text-editor-component.coffee b/src/text-editor-component.coffee index 09df150df..51c4447b6 100644 --- a/src/text-editor-component.coffee +++ b/src/text-editor-component.coffee @@ -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)