From f23e0fa1eb92f96d12c1efc2283f454f14964d5d Mon Sep 17 00:00:00 2001 From: Timmy Willison <4timmywil@gmail.com> Date: Fri, 22 Nov 2019 12:19:52 -0500 Subject: [PATCH] fix(events): fix triggering panzoomend for one pointer event Fixes gh-428 --- src/panzoom.ts | 9 +++++---- test/unit/panzoom.test.ts | 9 +++++++++ 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/src/panzoom.ts b/src/panzoom.ts index 04781062..0c1ca57f 100644 --- a/src/panzoom.ts +++ b/src/panzoom.ts @@ -435,6 +435,11 @@ function Panzoom( } function handleUp(event: PointerEvent) { + // Don't call panzoomend when panning with 2 touches + // until both touches end + if (pointers.length === 1) { + trigger('panzoomend', { x, y, scale }, options) + } // Note: don't remove all pointers // Can restart without having to reinitiate all of them // Remove the pointer regardless of the isPanning state @@ -442,10 +447,6 @@ function Panzoom( if (!isPanning) { return } - // Only call panzoomend once - if (pointers.length === 1) { - trigger('panzoomend', { x, y, scale }, options) - } isPanning = false origX = origY = startClientX = startClientY = undefined } diff --git a/test/unit/panzoom.test.ts b/test/unit/panzoom.test.ts index a608925c..e3255c54 100644 --- a/test/unit/panzoom.test.ts +++ b/test/unit/panzoom.test.ts @@ -71,10 +71,19 @@ describe('Panzoom', () => { } const panzoom = Panzoom(div) assert(Object.keys(events).length > 0) + const endListener = () => { + console.log('panzoomend called') + assert.ok('panzoomend called on pan') + } + div.addEventListener('panzoomend', endListener) + div.dispatchEvent(new PointerEvent('pointerdown')) + document.dispatchEvent(new PointerEvent('pointerup')) panzoom.destroy() + div.removeEventListener('panzoomend', endListener) assert(Object.keys(events).length === 0) Element.prototype.addEventListener = addEvent Element.prototype.removeEventListener = removeEvent + document.body.removeChild(div) }) it('sets the expected transform-origin on SVG', () => { const elem = document.createElementNS('http://www.w3.org/2000/svg', 'g')