From 2548891b99fdd8ae6a7bfaa6ea34a77fc9793897 Mon Sep 17 00:00:00 2001 From: Nathan Sobo Date: Sat, 31 May 2014 18:36:59 +0900 Subject: [PATCH] Clear the mousewheelScreenRow even if the event does not cause scrolling If a mousewheel event is triggered when the editor can't be scrolled, we still want to clear the mouseWheelScreenRow. This is typically done when we stop scrolling, but if we never start scrolling it will never happen. This commit adds another timeout to cover that case. --- spec/editor-component-spec.coffee | 16 ++++++++++++++++ src/editor-component.coffee | 10 ++++++++++ 2 files changed, 26 insertions(+) diff --git a/spec/editor-component-spec.coffee b/spec/editor-component-spec.coffee index 271ea856a..e01fbb853 100644 --- a/spec/editor-component-spec.coffee +++ b/spec/editor-component-spec.coffee @@ -790,6 +790,22 @@ describe "EditorComponent", -> expect(component.mouseWheelScreenRow).toBe null + it "clears the mouseWheelScreenRow after a delay even if the event does not cause scrolling", -> + spyOn(_._, 'now').andCallFake -> window.now # Ensure _.debounce is based on our fake spec timeline + + expect(editor.getScrollTop()).toBe 0 + + lineNode = node.querySelector('.line') + wheelEvent = new WheelEvent('mousewheel', wheelDeltaX: 0, wheelDeltaY: 10) + Object.defineProperty(wheelEvent, 'target', get: -> lineNode) + node.dispatchEvent(wheelEvent) + + expect(editor.getScrollTop()).toBe 0 + + expect(component.mouseWheelScreenRow).toBe 0 + advanceClock(component.mouseWheelScreenRowClearDelay) + expect(component.mouseWheelScreenRow).toBe null + it "does not preserve the line if it is on screen", -> expect(node.querySelectorAll('.line-number').length).toBe 14 # dummy line lineNodes = node.querySelectorAll('.line') diff --git a/src/editor-component.coffee b/src/editor-component.coffee index de2e40ddb..ede23e203 100644 --- a/src/editor-component.coffee +++ b/src/editor-component.coffee @@ -29,6 +29,7 @@ EditorComponent = React.createClass pendingVerticalScrollDelta: 0 pendingHorizontalScrollDelta: 0 mouseWheelScreenRow: null + mouseWheelScreenRowClearDelay: 150 render: -> {focused, fontSize, lineHeight, fontFamily, showIndentGuide, showInvisibles, visible} = @state @@ -370,6 +371,8 @@ EditorComponent = React.createClass # Scrolling vertically @pendingVerticalScrollDelta -= wheelDeltaY @mouseWheelScreenRow = @screenRowForNode(event.target) + @clearMouseWheelScreenRowAfterDelay ?= debounce(@clearMouseWheelScreenRow, @mouseWheelScreenRowClearDelay) + @clearMouseWheelScreenRowAfterDelay() unless animationFramePending requestAnimationFrame => @@ -379,6 +382,13 @@ EditorComponent = React.createClass @pendingVerticalScrollDelta = 0 @pendingHorizontalScrollDelta = 0 + clearMouseWheelScreenRow: -> + if @mouseWheelScreenRow? + @mouseWheelScreenRow = null + @requestUpdate() + + clearMouseWheelScreenRowAfterDelay: null # created lazily + screenRowForNode: (node) -> while node isnt document if screenRow = node.dataset.screenRow