Skip to content

Commit 432f47e

Browse files
authored
Fix: splitChannels height (#3402)
* Fix: splitChannels height * Add a test
1 parent 83a0909 commit 432f47e

File tree

3 files changed

+31
-7
lines changed

3 files changed

+31
-7
lines changed

cypress/e2e/options.cy.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,30 @@ describe('WaveSurfer options tests', () => {
309309
})
310310
})
311311

312+
it('should split channels with individual channel heights', (done) => {
313+
cy.window().then((win) => {
314+
const wavesurfer = win.WaveSurfer.create({
315+
container: id,
316+
url: '../../examples/audio/stereo.mp3',
317+
splitChannels: [
318+
{
319+
waveColor: 'red',
320+
height: 60,
321+
},
322+
{
323+
waveColor: 'blue',
324+
height: 30,
325+
},
326+
],
327+
})
328+
329+
wrapReady(wavesurfer).then(() => {
330+
cy.get(id).matchImageSnapshot('split-channels-heights')
331+
done()
332+
})
333+
})
334+
})
335+
312336
it('should use plugins with Regions', (done) => {
313337
cy.window().then((win) => {
314338
const regions = win.Regions.create()

src/renderer.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -122,11 +122,11 @@ class Renderer extends EventEmitter<RendererEvents> {
122122
)
123123
}
124124

125-
private getHeight(): number {
125+
private getHeight(optionsHeight?: WaveSurferOptions['height']): number {
126126
const defaultHeight = 128
127-
if (this.options.height == null) return defaultHeight
128-
if (!isNaN(Number(this.options.height))) return Number(this.options.height)
129-
if (this.options.height === 'auto') return this.parent.clientHeight || defaultHeight
127+
if (optionsHeight == null) return defaultHeight
128+
if (!isNaN(Number(optionsHeight))) return Number(optionsHeight)
129+
if (optionsHeight === 'auto') return this.parent.clientHeight || defaultHeight
130130
return defaultHeight
131131
}
132132

@@ -164,7 +164,7 @@ class Renderer extends EventEmitter<RendererEvents> {
164164
z-index: 2;
165165
}
166166
:host .canvases {
167-
min-height: ${this.getHeight()}px;
167+
min-height: ${this.getHeight(this.options.height)}px;
168168
}
169169
:host .canvases > div {
170170
position: relative;
@@ -449,7 +449,7 @@ class Renderer extends EventEmitter<RendererEvents> {
449449
private renderChannel(channelData: Array<Float32Array | number[]>, options: WaveSurferOptions, width: number) {
450450
// A container for canvases
451451
const canvasContainer = document.createElement('div')
452-
const height = this.getHeight()
452+
const height = this.getHeight(options.height)
453453
canvasContainer.style.height = `${height}px`
454454
this.canvasWrapper.style.minHeight = `${height}px`
455455
this.canvasWrapper.appendChild(canvasContainer)

src/wavesurfer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ export type WaveSurferOptions = {
6262
/** Decoding sample rate. Doesn't affect the playback. Defaults to 8000 */
6363
sampleRate?: number
6464
/** Render each audio channel as a separate waveform */
65-
splitChannels?: WaveSurferOptions[]
65+
splitChannels?: Partial<WaveSurferOptions>[]
6666
/** Stretch the waveform to the full height */
6767
normalize?: boolean
6868
/** The list of plugins to initialize on start */

0 commit comments

Comments
 (0)