Skip to content

Commit

Permalink
feat: add a destroy method (#404)
Browse files Browse the repository at this point in the history
  • Loading branch information
karlito40 authored and timmywil committed Sep 6, 2019
1 parent ec6e42e commit c88ef75
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
12 changes: 12 additions & 0 deletions src/events.ts
Expand Up @@ -31,3 +31,15 @@ export function onPointer(
>(name as any, handler, eventOpts)
})
}

export function destroyPointer(
event: 'down' | 'move' | 'up',
elem: HTMLElement | SVGElement | Document,
handler: (event: PointerEvent) => void
) {
events[event].split(' ').forEach((name) => {
;(elem as HTMLElement).removeEventListener<
'pointerdown' | 'pointermove' | 'pointerup' | 'pointerleave' | 'pointercancel'
>(name as any, handler)
})
}
9 changes: 8 additions & 1 deletion src/panzoom.ts
Expand Up @@ -8,7 +8,7 @@
*
*/
import { getDimensions, setStyle, setTransform } from './css'
import { onPointer } from './events'
import { destroyPointer, onPointer } from './events'
import isAttached from './isAttached'
import isSVGElement from './isSVGElement'
import { addPointer, getDistance, getMiddle, removePointer } from './pointers'
Expand Down Expand Up @@ -407,7 +407,14 @@ function Panzoom(elem: HTMLElement | SVGElement, options?: PanzoomOptions): Panz
onPointer('up', document, handleUp, { passive: true })
}

function destroy() {
destroyPointer('down', elem, handleDown)
destroyPointer('move', document, move)
destroyPointer('up', document, handleUp)
}

return {
destroy,
getPan: () => ({ x, y }),
getScale: () => scale,
getOptions: () => shallowClone(options),
Expand Down
2 changes: 2 additions & 0 deletions src/types.ts
Expand Up @@ -122,6 +122,8 @@ export interface CurrentValues {
}

export interface PanzoomObject {
/** Remove all event listeners bind to the the Panzoom element */
destroy: () => void
/** Get the current x/y translation */
getPan: () => { x: number; y: number }
/** Get the current scale */
Expand Down

0 comments on commit c88ef75

Please sign in to comment.