Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: dragstart and dragend events for drag to seek #3562

Merged
merged 1 commit into from
Feb 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions src/draggable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ export function makeDraggable(
element: HTMLElement | null,
onDrag: (dx: number, dy: number, x: number, y: number) => void,
onStart?: (x: number, y: number) => void,
onEnd?: () => void,
onEnd?: (x: number, y: number) => void,
threshold = 3,
mouseButton = 0,
): () => void {
Expand Down Expand Up @@ -45,17 +45,22 @@ export function makeDraggable(
}
}

const onPointerUp = () => {
const onPointerUp = (event: PointerEvent) => {
if (isDragging) {
onEnd?.()
const x = event.clientX
const y = event.clientY
const rect = element.getBoundingClientRect()
const { left, top } = rect

onEnd?.(x - left, y - top)
}
unsubscribeDocument()
}

const onPointerLeave = (e: PointerEvent) => {
// Listen to events only on the document and not on inner elements
if (!e.relatedTarget || e.relatedTarget === document.documentElement) {
onPointerUp()
onPointerUp(e)
}
}

Expand Down
14 changes: 11 additions & 3 deletions src/renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ type RendererEvents = {
click: [relativeX: number, relativeY: number]
dblclick: [relativeX: number, relativeY: number]
drag: [relativeX: number]
dragstart: [relativeX: number]
dragend: [relativeX: number]
scroll: [relativeStart: number, relativeEnd: number]
render: []
rendered: []
Expand Down Expand Up @@ -127,9 +129,15 @@ class Renderer extends EventEmitter<RendererEvents> {
this.emit('drag', Math.max(0, Math.min(1, x / this.wrapper.getBoundingClientRect().width)))
},
// On start drag
() => (this.isDragging = true),
(x) => {
this.isDragging = true
this.emit('dragstart', Math.max(0, Math.min(1, x / this.wrapper.getBoundingClientRect().width)))
},
// On end drag
() => (this.isDragging = false),
(x) => {
this.isDragging = false
this.emit('dragend', Math.max(0, Math.min(1, x / this.wrapper.getBoundingClientRect().width)))
},
)
}

Expand Down Expand Up @@ -255,7 +263,7 @@ class Renderer extends EventEmitter<RendererEvents> {

setScrollPercentage(percent: number) {
const { scrollWidth } = this.scrollContainer
const scrollStart = scrollWidth * percent
const scrollStart = scrollWidth * percent
this.setScroll(scrollStart)
}

Expand Down
14 changes: 14 additions & 0 deletions src/wavesurfer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,10 @@ export type WaveSurferEvents = {
dblclick: [relativeX: number, relativeY: number]
/** When the user drags the cursor */
drag: [relativeX: number]
/** When the user starts dragging the cursor */
dragstart: [relativeX: number]
/** When the user ends dragging the cursor */
dragend: [relativeX: number]
/** When the waveform is scrolled (panned) */
scroll: [visibleStartTime: number, visibleEndTime: number]
/** When the zoom level changes */
Expand Down Expand Up @@ -271,6 +275,16 @@ class WaveSurfer extends Player<WaveSurferEvents> {
this.renderer.on('rendered', () => {
this.emit('redrawcomplete')
}),

// DragStart
this.renderer.on('dragstart', (relativeX) => {
this.emit('dragstart', relativeX)
}),

// DragEnd
this.renderer.on('dragend', (relativeX) => {
this.emit('dragend', relativeX)
}),
)

// Drag
Expand Down
Loading