Skip to content

Commit f049862

Browse files
authored
Fix: don't reset scroll on setOptions (#3372)
1 parent a92caf7 commit f049862

File tree

1 file changed

+11
-9
lines changed

1 file changed

+11
-9
lines changed

src/renderer.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class Renderer extends EventEmitter<RendererEvents> {
2121
private progressWrapper: HTMLElement
2222
private cursor: HTMLElement
2323
private timeouts: Array<{ timeout?: ReturnType<typeof setTimeout> }> = []
24-
private isScrolling = false
24+
private isScrollable = false
2525
private audioData: AudioBuffer | null = null
2626
private resizeObserver: ResizeObserver | null = null
2727
private isDragging = false
@@ -526,7 +526,6 @@ class Renderer extends EventEmitter<RendererEvents> {
526526
// Clear the canvases
527527
this.canvasWrapper.innerHTML = ''
528528
this.progressWrapper.innerHTML = ''
529-
this.wrapper.style.width = ''
530529

531530
// Width
532531
if (this.options.width != null) {
@@ -540,16 +539,16 @@ class Renderer extends EventEmitter<RendererEvents> {
540539
const scrollWidth = Math.ceil(audioData.duration * (this.options.minPxPerSec || 0))
541540

542541
// Whether the container should scroll
543-
this.isScrolling = scrollWidth > parentWidth
544-
const useParentWidth = this.options.fillParent && !this.isScrolling
542+
this.isScrollable = scrollWidth > parentWidth
543+
const useParentWidth = this.options.fillParent && !this.isScrollable
545544
// Width of the waveform in pixels
546545
const width = (useParentWidth ? parentWidth : scrollWidth) * pixelRatio
547546

548547
// Set the width of the wrapper
549548
this.wrapper.style.width = useParentWidth ? '100%' : `${scrollWidth}px`
550549

551550
// Set additional styles
552-
this.scrollContainer.style.overflowX = this.isScrolling ? 'auto' : 'hidden'
551+
this.scrollContainer.style.overflowX = this.isScrollable ? 'auto' : 'hidden'
553552
this.scrollContainer.classList.toggle('noScrollbar', !!this.options.hideScrollbar)
554553
this.cursor.style.backgroundColor = `${this.options.cursorColor || this.options.progressColor}`
555554
this.cursor.style.width = `${this.options.cursorWidth}px`
@@ -578,14 +577,17 @@ class Renderer extends EventEmitter<RendererEvents> {
578577
if (!this.audioData) return
579578

580579
// Remember the current cursor position
580+
const { scrollWidth } = this.scrollContainer
581581
const oldCursorPosition = this.progressWrapper.clientWidth
582582

583-
// Set the new zoom level and re-render the waveform
583+
// Re-render the waveform
584584
this.render(this.audioData)
585585

586586
// Adjust the scroll position so that the cursor stays in the same place
587-
const newCursortPosition = this.progressWrapper.clientWidth
588-
this.scrollContainer.scrollLeft += newCursortPosition - oldCursorPosition
587+
if (this.isScrollable && scrollWidth !== this.scrollContainer.scrollWidth) {
588+
const newCursorPosition = this.progressWrapper.clientWidth
589+
this.scrollContainer.scrollLeft += newCursorPosition - oldCursorPosition
590+
}
589591
}
590592

591593
zoom(minPxPerSec: number) {
@@ -638,7 +640,7 @@ class Renderer extends EventEmitter<RendererEvents> {
638640
this.cursor.style.left = `${percents}%`
639641
this.cursor.style.marginLeft = Math.round(percents) === 100 ? `-${this.options.cursorWidth}px` : ''
640642

641-
if (this.isScrolling && this.options.autoScroll) {
643+
if (this.isScrollable && this.options.autoScroll) {
642644
this.scrollIntoView(progress, isPlaying)
643645
}
644646
}

0 commit comments

Comments
 (0)