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

Fix: [Regions] grabbing cursor on mousedown #3423

Merged
merged 1 commit into from
Dec 18, 2023
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ However, please keep in mind that this forum is dedicated to wavesurfer-specific
If you're using a VBR (variable bit rate) audio file, there might be a mismatch between the audio and the waveform. This can be fixed by converting your file to CBR (constant bit rate).
<p>Alternatively, you can use the <a href="https://wavesurfer.xyz/examples/?webaudio-shim.js">Web Audio shim</a> which is more accurate.</p>
</details>

## v7 – a new TypeScript version

Wavesurfer.js v7 is a TypeScript rewrite of wavesurfer.js that brings several improvements:
Expand Down
25 changes: 12 additions & 13 deletions src/plugins/regions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,11 @@ class SingleRegion extends EventEmitter<RegionEvents> {
this.element.style.right = `${end * 100}%`
}

private toggleCursor(toggle: boolean) {
if (!this.drag) return
this.element.style.cursor = toggle ? 'grabbing' : 'grab'
}

private initMouseEvents() {
const { element } = this
if (!element) return
Expand All @@ -215,27 +220,21 @@ class SingleRegion extends EventEmitter<RegionEvents> {
element.addEventListener('mouseenter', (e) => this.emit('over', e))
element.addEventListener('mouseleave', (e) => this.emit('leave', e))
element.addEventListener('dblclick', (e) => this.emit('dblclick', e))
element.addEventListener('pointerdown', () => this.toggleCursor(true))
element.addEventListener('pointerup', () => this.toggleCursor(false))

// Drag
makeDraggable(
element,
(dx) => this.onMove(dx),
() => this.onStartMoving(),
() => this.onEndMoving(),
() => this.toggleCursor(true),
() => {
this.toggleCursor(false)
this.drag && this.emit('update-end')
},
)
}

private onStartMoving() {
if (!this.drag) return
this.element.style.cursor = 'grabbing'
}

private onEndMoving() {
if (!this.drag) return
this.element.style.cursor = 'grab'
this.emit('update-end')
}

public _onUpdate(dx: number, side?: 'start' | 'end') {
if (!this.element.parentElement) return
const deltaSeconds = (dx / this.element.parentElement.clientWidth) * this.totalDuration
Expand Down