diff --git a/spec/editor-component-spec.coffee b/spec/editor-component-spec.coffee index 00f71c3ed..c7ef4a726 100644 --- a/spec/editor-component-spec.coffee +++ b/spec/editor-component-spec.coffee @@ -1687,6 +1687,42 @@ describe "EditorComponent", -> expect(componentNode.contains(lineNumberNode)).toBe true + it "only prevents the default action of the mousewheel event if it actually lead to scrolling", -> + spyOn(WheelEvent::, 'preventDefault').andCallThrough() + + wrapperNode.style.height = 4.5 * lineHeightInPixels + 'px' + wrapperNode.style.width = 20 * charWidth + 'px' + component.measureHeightAndWidth() + runSetImmediateCallbacks() + + componentNode.dispatchEvent(new WheelEvent('mousewheel', wheelDeltaX: 0, wheelDeltaY: 50)) + expect(editor.getScrollTop()).toBe 0 + expect(WheelEvent::preventDefault).not.toHaveBeenCalled() + + componentNode.dispatchEvent(new WheelEvent('mousewheel', wheelDeltaX: 0, wheelDeltaY: -3000)) + runSetImmediateCallbacks() + expect(editor.getScrollTop()).toBe editor.getScrollHeight() - editor.getHeight() + 15 + expect(WheelEvent::preventDefault).toHaveBeenCalled() + WheelEvent::preventDefault.reset() + + componentNode.dispatchEvent(new WheelEvent('mousewheel', wheelDeltaX: 0, wheelDeltaY: -30)) + expect(editor.getScrollTop()).toBe editor.getScrollHeight() - editor.getHeight() + 15 + expect(WheelEvent::preventDefault).not.toHaveBeenCalled() + + componentNode.dispatchEvent(new WheelEvent('mousewheel', wheelDeltaX: 50, wheelDeltaY: 0)) + expect(editor.getScrollLeft()).toBe 0 + expect(WheelEvent::preventDefault).not.toHaveBeenCalled() + + componentNode.dispatchEvent(new WheelEvent('mousewheel', wheelDeltaX: -3000, wheelDeltaY: 0)) + runSetImmediateCallbacks() + expect(editor.getScrollLeft()).toBe editor.getScrollWidth() - editor.getWidth() + 15 + expect(WheelEvent::preventDefault).toHaveBeenCalled() + WheelEvent::preventDefault.reset() + + componentNode.dispatchEvent(new WheelEvent('mousewheel', wheelDeltaX: -30, wheelDeltaY: 0)) + expect(editor.getScrollLeft()).toBe editor.getScrollWidth() - editor.getWidth() + 15 + expect(WheelEvent::preventDefault).not.toHaveBeenCalled() + describe "input events", -> inputNode = null diff --git a/src/editor-component.coffee b/src/editor-component.coffee index ea281a24d..993dbe8c7 100644 --- a/src/editor-component.coffee +++ b/src/editor-component.coffee @@ -567,20 +567,23 @@ EditorComponent = React.createClass @pendingScrollLeft = null onMouseWheel: (event) -> - event.preventDefault() {editor} = @props # Only scroll in one direction at a time {wheelDeltaX, wheelDeltaY} = event if Math.abs(wheelDeltaX) > Math.abs(wheelDeltaY) # Scrolling horizontally - editor.setScrollLeft(editor.getScrollLeft() - Math.round(wheelDeltaX * @scrollSensitivity)) + previousScrollLeft = editor.getScrollLeft() + editor.setScrollLeft(previousScrollLeft - Math.round(wheelDeltaX * @scrollSensitivity)) + event.preventDefault() unless previousScrollLeft is editor.getScrollLeft() else # Scrolling vertically @mouseWheelScreenRow = @screenRowForNode(event.target) @clearMouseWheelScreenRowAfterDelay ?= debounce(@clearMouseWheelScreenRow, @mouseWheelScreenRowClearDelay) @clearMouseWheelScreenRowAfterDelay() - editor.setScrollTop(editor.getScrollTop() - Math.round(wheelDeltaY * @scrollSensitivity)) + previousScrollTop = editor.getScrollTop() + editor.setScrollTop(previousScrollTop - Math.round(wheelDeltaY * @scrollSensitivity)) + event.preventDefault() unless previousScrollTop is editor.getScrollTop() onScrollViewScroll: -> if @isMounted()