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: [Regions] scroll container when dragging a region #3424

Merged
merged 1 commit into from
Dec 18, 2023
Merged
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
20 changes: 20 additions & 0 deletions src/plugins/regions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -450,12 +450,32 @@ class RegionsPlugin extends BasePlugin<RegionsPluginEvents, RegionsPluginOptions
div.style.marginTop = `${overlap}px`
}

private adjustScroll(region: Region) {
const scrollContainer = this.wavesurfer?.getWrapper()?.parentElement
if (!scrollContainer) return
const { clientWidth, scrollWidth } = scrollContainer
if (scrollWidth <= clientWidth) return
const scrollBbox = scrollContainer.getBoundingClientRect()
const bbox = region.element.getBoundingClientRect()
const left = bbox.left - scrollBbox.left
const right = bbox.right - scrollBbox.left
if (left < 0) {
scrollContainer.scrollLeft += left
} else if (right > clientWidth) {
scrollContainer.scrollLeft += right - clientWidth
}
}

private saveRegion(region: Region) {
this.regionsContainer.appendChild(region.element)
this.avoidOverlapping(region)
this.regions.push(region)

const regionSubscriptions = [
region.on('update', () => {
this.adjustScroll(region)
}),

region.on('update-end', () => {
this.avoidOverlapping(region)
this.emit('region-updated', region)
Expand Down