From 1501ba5c0de88cb861077e68d78f96c933cfd2d8 Mon Sep 17 00:00:00 2001 From: Tobias Gutmann Date: Tue, 18 Jun 2019 10:43:33 +0200 Subject: [PATCH] fix(issue#935): Prevent WheelEvent from propagating --- src/eventListeners/wheelEventListener.js | 7 +++++++ src/eventListeners/wheelEventListener.test.js | 19 +++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/src/eventListeners/wheelEventListener.js b/src/eventListeners/wheelEventListener.js index 594d147c1..0fcab799a 100644 --- a/src/eventListeners/wheelEventListener.js +++ b/src/eventListeners/wheelEventListener.js @@ -23,6 +23,13 @@ function wheelEventHandler(evt) { return; } + // Prevent triggering MouseWheel events that are not real scroll events: + // E.g. when clicking the MiddleMouseWheelButton, a deltaY of 0 is emitted. + // See https://github.com/cornerstonejs/cornerstoneTools/issues/935 + if (evt.deltaY > -1 && evt.deltaY < 1) { + return; + } + evt.preventDefault(); const { pageX, pageY } = evt; diff --git a/src/eventListeners/wheelEventListener.test.js b/src/eventListeners/wheelEventListener.test.js index 996e51e5e..50ab6068e 100644 --- a/src/eventListeners/wheelEventListener.test.js +++ b/src/eventListeners/wheelEventListener.test.js @@ -103,4 +103,23 @@ describe('eventListeners/wheelEventListener.js', () => { // Fire Away! localElement.dispatchEvent(new Event('wheel')); }); + + it('does not emit an `cornerstonetoolsmousewheel` event when scrolling less than one pixel', () => { + // Setup + const localElement = document.createElement('div'); + const scrollCloseToZeroPixel = 0.00001; + const middleMouseButtonClickedEvent = new WheelEvent('wheel', { deltaY: scrollCloseToZeroPixel }); + + // Assert + localElement.addEventListener('cornerstonetoolsmousewheel', (evt) => { + expect(false).toBe(true, 'Unwanted event fired'); + }); + + // SUT + wheelEventListener.enable(localElement); + + // Fire Away! + localElement.dispatchEvent(middleMouseButtonClickedEvent); + }); + });