Skip to content

Commit

Permalink
Merge branch 'main' into feat-part-attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
Brian Byassee committed Jan 24, 2024
2 parents f9d8e04 + affe115 commit 0b8c041
Show file tree
Hide file tree
Showing 14 changed files with 206 additions and 112 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ jobs:
if [ $NEW_VERSION != $OLD_VERSION ]; then
echo "New version $NEW_VERSION detected"
echo "version=$NEW_VERSION" >> $GITHUB_OUTPUT
git log "$OLD_VERSION"..HEAD --pretty=format:"* %s" > TEMP_CHANGELOG.md
git log "$OLD_VERSION"..HEAD --pretty=format:"* %s by %ae" | sed -E 's/by [0-9]+\+(.+)@users.noreply.github.com/by @\1/g' | sed -E 's/ by @?katspaugh.*//g' > TEMP_CHANGELOG.md
else
echo "Version $OLD_VERSION hasn't changed, skipping the release"
fi
Expand Down
19 changes: 19 additions & 0 deletions cypress/e2e/basic.cy.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,25 @@ describe('WaveSurfer basic tests', () => {
cy.window().its('wavesurfer').should('be.an', 'object')
})

it('should emit a redrawcomplete event', () => {
cy.window().then((win) => {
const { wavesurfer } = win
expect(wavesurfer.getDuration().toFixed(2)).to.equal('21.77')

wavesurfer.options.minPxPerSec = 200
wavesurfer.load('../../examples/audio/audio.wav')

return new Promise((resolve) => {
wavesurfer.once('redrawcomplete', () => {
wavesurfer.zoom(100)
wavesurfer.once('redrawcomplete', () => {
resolve()
})
})
})
})
})

it('should load an audio file without errors', () => {
cy.window().then((win) => {
expect(win.wavesurfer.getDuration().toFixed(2)).to.equal('21.77')
Expand Down
11 changes: 8 additions & 3 deletions examples/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,14 @@ wavesurfer.on('ready', (duration) => {
console.log('Ready', duration + 's')
})

/** When a waveform is drawn */
wavesurfer.on('redraw', () => {
console.log('Redraw')
/** When visible waveform is drawn */
wavesurfer.on('redrawcomplete', () => {
console.log('Redraw began')
})

/** When all audio channel chunks of the waveform have drawn */
wavesurfer.on('redrawcomplete', () => {
console.log('Redraw complete')
})

/** When the audio starts playing */
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "wavesurfer.js",
"version": "7.6.3",
"version": "7.7.0",
"license": "BSD-3-Clause",
"author": "katspaugh",
"description": "Audio waveform player",
Expand Down
8 changes: 4 additions & 4 deletions src/dom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ function renderNode(tagName: string, content: TreeNode): HTMLElement | SVGElemen
return element
}

function render(tagName: string, content: TreeNode & { xmlns: string }, container?: Node): SVGElement
function render(tagName: string, content?: TreeNode, container?: Node): HTMLElement
function render(tagName: string, content?: TreeNode, container?: Node): HTMLElement | SVGElement {
export function createElement(tagName: string, content: TreeNode & { xmlns: string }, container?: Node): SVGElement
export function createElement(tagName: string, content?: TreeNode, container?: Node): HTMLElement
export function createElement(tagName: string, content?: TreeNode, container?: Node): HTMLElement | SVGElement {
const el = renderNode(tagName, content || {})
container?.appendChild(el)
return el
}

export default render
export default createElement
8 changes: 7 additions & 1 deletion src/player.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@ class Player<T extends GeneralEventTypes> extends EventEmitter<T> {
}

/** Start playing the audio */
public play(): Promise<void> {
public async play(): Promise<void> {
if (!this.media.src) return
return this.media.play()
}

Expand Down Expand Up @@ -145,6 +146,11 @@ class Player<T extends GeneralEventTypes> extends EventEmitter<T> {
return this.media.playbackRate
}

/** Check if the audio is seeking */
public isSeeking(): boolean {
return this.media.seeking
}

/** Set the playback speed, pass an optional false to NOT preserve the pitch */
public setPlaybackRate(rate: number, preservePitch?: boolean) {
// preservePitch is true by default in most browsers
Expand Down
8 changes: 4 additions & 4 deletions src/plugins/envelope.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import BasePlugin, { type BasePluginEvents } from '../base-plugin.js'
import { makeDraggable } from '../draggable.js'
import EventEmitter from '../event-emitter.js'
import render from '../dom.js'
import createElement from '../dom.js'

export type EnvelopePoint = {
id?: string
Expand Down Expand Up @@ -66,7 +66,7 @@ class Polyline extends EventEmitter<{
const height = wrapper.clientHeight

// SVG element
const svg = render(
const svg = createElement(
'svg',
{
xmlns: 'http://www.w3.org/2000/svg',
Expand All @@ -88,7 +88,7 @@ class Polyline extends EventEmitter<{
this.svg = svg

// A polyline representing the envelope
const polyline = render(
const polyline = createElement(
'polyline',
{
xmlns: 'http://www.w3.org/2000/svg',
Expand Down Expand Up @@ -173,7 +173,7 @@ class Polyline extends EventEmitter<{
private createCircle(x: number, y: number) {
const size = this.options.dragPointSize
const radius = size / 2
return render(
return createElement(
'ellipse',
{
xmlns: 'http://www.w3.org/2000/svg',
Expand Down
6 changes: 3 additions & 3 deletions src/plugins/hover.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*/

import BasePlugin, { type BasePluginEvents } from '../base-plugin.js'
import render from '../dom.js'
import createElement from '../dom.js'

export type HoverPluginOptions = {
lineColor?: string
Expand Down Expand Up @@ -33,8 +33,8 @@ class HoverPlugin extends BasePlugin<HoverPluginEvents, HoverPluginOptions> {
this.options = Object.assign({}, defaultOptions, options)

// Create the plugin elements
this.wrapper = render('div', { part: 'hover' })
this.label = render('span', { part: 'hover-label' }, this.wrapper)
this.wrapper = createElement('div', { part: 'hover' })
this.label = createElement('span', { part: 'hover-label' }, this.wrapper)
}

public static create(options?: HoverPluginOptions) {
Expand Down
7 changes: 4 additions & 3 deletions src/plugins/minimap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import BasePlugin, { type BasePluginEvents } from '../base-plugin.js'
import WaveSurfer, { type WaveSurferOptions } from '../wavesurfer.js'
import render from '../dom.js'
import createElement from '../dom.js'

export type MinimapPluginOptions = {
overlayColor?: string
Expand Down Expand Up @@ -63,7 +63,7 @@ class MinimapPlugin extends BasePlugin<MinimapPluginEvents, MinimapPluginOptions
}

private initMinimapWrapper(): HTMLElement {
return render('div', {
return createElement('div', {
part: 'minimap',
style: {
position: 'relative',
Expand All @@ -72,9 +72,10 @@ class MinimapPlugin extends BasePlugin<MinimapPluginEvents, MinimapPluginOptions
}

private initOverlay(): HTMLElement {
return render(
return createElement(
'div',
{
part: 'minimap-overlay',
style: {
position: 'absolute',
zIndex: '2',
Expand Down
25 changes: 14 additions & 11 deletions src/plugins/regions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import BasePlugin, { type BasePluginEvents } from '../base-plugin.js'
import { makeDraggable } from '../draggable.js'
import EventEmitter from '../event-emitter.js'
import render from '../dom.js'
import createElement from '../dom.js'

export type RegionsPluginOptions = undefined

Expand All @@ -25,7 +25,7 @@ export type RegionEvents = {
/** Before the region is removed */
remove: []
/** When the region's parameters are being updated */
update: []
update: [side?: 'start' | 'end']
/** When dragging or resizing is finished */
'update-end': []
/** On play */
Expand Down Expand Up @@ -120,7 +120,7 @@ class SingleRegion extends EventEmitter<RegionEvents> {
wordBreak: 'keep-all',
}

const leftHandle = render(
const leftHandle = createElement(
'div',
{
part: 'region-handle region-handle-left',
Expand All @@ -134,7 +134,7 @@ class SingleRegion extends EventEmitter<RegionEvents> {
element,
)

const rightHandle = render(
const rightHandle = createElement(
'div',
{
part: 'region-handle region-handle-right',
Expand Down Expand Up @@ -188,7 +188,7 @@ class SingleRegion extends EventEmitter<RegionEvents> {
elementTop = elementHeight * this.channelIdx
}

const element = render('div', {
const element = createElement('div', {
style: {
position: 'absolute',
top: `${elementTop}%`,
Expand Down Expand Up @@ -219,7 +219,7 @@ class SingleRegion extends EventEmitter<RegionEvents> {
}

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

Expand Down Expand Up @@ -270,7 +270,7 @@ class SingleRegion extends EventEmitter<RegionEvents> {
this.end = newEnd

this.renderPosition()
this.emit('update')
this.emit('update', side)
}
}

Expand Down Expand Up @@ -319,7 +319,7 @@ class SingleRegion extends EventEmitter<RegionEvents> {
}
if (typeof content === 'string') {
const isMarker = this.start === this.end
this.content = render('div', {
this.content = createElement('div', {
style: {
padding: `0.2em ${isMarker ? 0.2 : 0.4}em`,
display: 'inline-block',
Expand Down Expand Up @@ -439,7 +439,7 @@ class RegionsPlugin extends BasePlugin<RegionsPluginEvents, RegionsPluginOptions
}

private initRegionsContainer(): HTMLElement {
return render('div', {
return createElement('div', {
style: {
position: 'absolute',
top: '0',
Expand Down Expand Up @@ -502,8 +502,11 @@ class RegionsPlugin extends BasePlugin<RegionsPluginEvents, RegionsPluginOptions
this.regions.push(region)

const regionSubscriptions = [
region.on('update', () => {
this.adjustScroll(region)
region.on('update', (side) => {
// Undefined side indicates that we are dragging not resizing
if (!side) {
this.adjustScroll(region)
}
}),

region.on('update-end', () => {
Expand Down
8 changes: 4 additions & 4 deletions src/plugins/spectrogram.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ function FFT(bufferSize: number, sampleRate: number, windowFunc: string, alpha:
* Spectrogram plugin for wavesurfer.
*/
import BasePlugin, { type BasePluginEvents } from '../base-plugin.js'
import render from '../dom.js'
import createElement from '../dom.js'

export type SpectrogramPluginOptions = {
/** Selector of element or element in which to render */
Expand Down Expand Up @@ -347,7 +347,7 @@ class SpectrogramPlugin extends BasePlugin<SpectrogramPluginEvents, SpectrogramP
}

private createWrapper() {
this.wrapper = render('div', {
this.wrapper = createElement('div', {
style: {
display: 'block',
position: 'relative',
Expand All @@ -357,7 +357,7 @@ class SpectrogramPlugin extends BasePlugin<SpectrogramPluginEvents, SpectrogramP

// if labels are active
if (this.options.labels) {
this.labelsEl = render(
this.labelsEl = createElement(
'canvas',
{
part: 'spec-labels',
Expand All @@ -376,7 +376,7 @@ class SpectrogramPlugin extends BasePlugin<SpectrogramPluginEvents, SpectrogramP
}

private createCanvas() {
this.canvas = render(
this.canvas = createElement(
'canvas',
{
style: {
Expand Down
8 changes: 4 additions & 4 deletions src/plugins/timeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*/

import BasePlugin, { type BasePluginEvents } from '../base-plugin.js'
import render from '../dom.js'
import createElement from '../dom.js'

export type TimelinePluginOptions = {
/** The height of the timeline in pixels, defaults to 20 */
Expand Down Expand Up @@ -104,7 +104,7 @@ class TimelinePlugin extends BasePlugin<TimelinePluginEvents, TimelinePluginOpti
}

private initTimelineWrapper(): HTMLElement {
return render('div', { part: 'timeline-wrapper', style: { pointerEvents: 'none' } })
return createElement('div', { part: 'timeline-wrapper', style: { pointerEvents: 'none' } })
}

// Return how many seconds should be between each notch
Expand Down Expand Up @@ -153,7 +153,7 @@ class TimelinePlugin extends BasePlugin<TimelinePluginEvents, TimelinePluginOpti
const secondaryLabelSpacing = this.options.secondaryLabelSpacing
const isTop = this.options.insertPosition === 'beforebegin'

const timeline = render('div', {
const timeline = createElement('div', {
style: {
height: `${this.options.height}px`,
overflow: 'hidden',
Expand Down Expand Up @@ -181,7 +181,7 @@ class TimelinePlugin extends BasePlugin<TimelinePluginEvents, TimelinePluginOpti
Object.assign(timeline.style, this.options.style)
}

const notchEl = render('div', {
const notchEl = createElement('div', {
style: {
width: '0',
height: '50%',
Expand Down
Loading

0 comments on commit 0b8c041

Please sign in to comment.