Skip to content

Commit

Permalink
Fix: add stopPropagation to draggable
Browse files Browse the repository at this point in the history
7.4.8

Fix: timeout to prevent clicks

Fix: lower drag theshold

7.4.9

Fix: prevent click only when dragging
  • Loading branch information
katspaugh committed Nov 26, 2023
1 parent e9cd82c commit f072a45
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "wavesurfer.js",
"version": "7.4.7",
"version": "7.4.9",
"license": "BSD-3-Clause",
"author": "katspaugh",
"description": "Navigable audio waveform player",
Expand Down
21 changes: 16 additions & 5 deletions src/draggable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,27 @@ export function makeDraggable(
onDrag: (dx: number, dy: number, x: number, y: number) => void,
onStart?: (x: number, y: number) => void,
onEnd?: () => void,
threshold = 5,
threshold = 3,
mouseButton = 0,
): () => void {
if (!element) return () => void 0

let unsubscribeDocument = () => void 0

const onPointerDown = (event: PointerEvent) => {
if (event.button !== 0) return
if (event.button !== mouseButton) return

event.preventDefault()
event.stopPropagation()

let startX = event.clientX
let startY = event.clientY
let isDragging = false

const onPointerMove = (event: PointerEvent) => {
event.preventDefault()
event.stopPropagation()

const x = event.clientX
const y = event.clientY
const dx = x - startX
Expand Down Expand Up @@ -46,8 +53,10 @@ export function makeDraggable(
}

const onClick = (event: MouseEvent) => {
event.stopPropagation()
event.preventDefault()
if (isDragging) {
event.stopPropagation()
event.preventDefault()
}
}

const onTouchMove = (event: TouchEvent) => {
Expand All @@ -67,7 +76,9 @@ export function makeDraggable(
document.removeEventListener('pointerup', onPointerUp)
document.removeEventListener('pointercancel', onPointerUp)
document.removeEventListener('touchmove', onTouchMove)
element.removeEventListener('click', onClick, { capture: true })
setTimeout(() => {
element.removeEventListener('click', onClick, { capture: true })
}, 10)
}
}

Expand Down

0 comments on commit f072a45

Please sign in to comment.