From d628c14c2fb3557fed9931eb768ce653cce04387 Mon Sep 17 00:00:00 2001 From: Jeremy Press Date: Wed, 19 Jul 2017 16:52:43 -0700 Subject: [PATCH] Fix: Don't swallow touch start event to prevent iOS inertia scrolling (#233) --- src/lib/viewers/doc/PresentationViewer.js | 6 ++++-- src/lib/viewers/doc/__tests__/PresentationViewer-test.js | 9 +++++++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/src/lib/viewers/doc/PresentationViewer.js b/src/lib/viewers/doc/PresentationViewer.js index bd69df749..2e014d479 100644 --- a/src/lib/viewers/doc/PresentationViewer.js +++ b/src/lib/viewers/doc/PresentationViewer.js @@ -157,6 +157,7 @@ class PresentationViewer extends DocBaseViewer { this.docEl.addEventListener('wheel', this.wheelHandler()); if (this.hasTouch) { this.docEl.addEventListener('touchstart', this.mobileScrollHandler); + this.docEl.addEventListener('touchmove', this.mobileScrollHandler); this.docEl.addEventListener('touchend', this.mobileScrollHandler); } } @@ -174,6 +175,7 @@ class PresentationViewer extends DocBaseViewer { this.docEl.removeEventListener('wheel', this.wheelHandler()); if (this.hasTouch) { this.docEl.removeEventListener('touchstart', this.mobileScrollHandler); + this.docEl.removeEventListener('touchmove', this.mobileScrollHandler); this.docEl.removeEventListener('touchend', this.mobileScrollHandler); } } @@ -228,10 +230,10 @@ class PresentationViewer extends DocBaseViewer { return; } - event.preventDefault(); - if (event.type === 'touchstart') { this.scrollStart = event.changedTouches[0].clientY; + } else if (event.type === 'touchmove') { + event.preventDefault(); } else { const scrollEnd = event.changedTouches[0].clientY; diff --git a/src/lib/viewers/doc/__tests__/PresentationViewer-test.js b/src/lib/viewers/doc/__tests__/PresentationViewer-test.js index 2b8c14f16..9c0207d4d 100644 --- a/src/lib/viewers/doc/__tests__/PresentationViewer-test.js +++ b/src/lib/viewers/doc/__tests__/PresentationViewer-test.js @@ -273,6 +273,7 @@ describe('lib/viewers/doc/PresentationViewer', () => { presentation.bindDOMListeners(); expect(stubs.addEventListener).to.be.calledWith('touchstart', presentation.mobileScrollHandler); + expect(stubs.addEventListener).to.be.calledWith('touchmove', presentation.mobileScrollHandler); expect(stubs.addEventListener).to.be.calledWith('touchend', presentation.mobileScrollHandler); }); }); @@ -292,6 +293,7 @@ describe('lib/viewers/doc/PresentationViewer', () => { presentation.unbindDOMListeners(); expect(stubs.removeEventListener).to.be.calledWith('touchstart', presentation.mobileScrollHandler); + expect(stubs.removeEventListener).to.be.calledWith('touchmove', presentation.mobileScrollHandler); expect(stubs.removeEventListener).to.be.calledWith('touchend', presentation.mobileScrollHandler); }); }); @@ -374,6 +376,13 @@ describe('lib/viewers/doc/PresentationViewer', () => { presentation.mobileScrollHandler(stubs.event); expect(presentation.scrollStart).to.equal(100); + }); + + it('should prevent default behaviour if the event is touchmove', () => { + stubs.event.type = 'touchmove'; + stubs.event.changedTouches[0].clientY = 100; + + presentation.mobileScrollHandler(stubs.event); expect(stubs.event.preventDefault).to.be.called; });