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: exports for Base Plugin #3495

Merged
merged 9 commits into from
Jan 19, 2024
Merged
Show file tree
Hide file tree
Changes from 8 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
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 {
function createElement(tagName: string, content: TreeNode & { xmlns: string }, container?: Node): SVGElement
function createElement(tagName: string, content?: TreeNode, container?: Node): HTMLElement
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: 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
6 changes: 3 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,7 +72,7 @@ class MinimapPlugin extends BasePlugin<MinimapPluginEvents, MinimapPluginOptions
}

private initOverlay(): HTMLElement {
return render(
return createElement(
'div',
{
part: 'minimap-overlay',
Expand Down
12 changes: 6 additions & 6 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 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 @@ -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
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 All @@ -179,7 +179,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
6 changes: 5 additions & 1 deletion src/wavesurfer.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { GenericPlugin } from './base-plugin.js'
import BasePlugin, { type GenericPlugin } from './base-plugin.js'
import Decoder from './decoder.js'
import createElement from './dom.js'
import Fetcher from './fetcher.js'
import Player from './player.js'
import Renderer from './renderer.js'
Expand Down Expand Up @@ -140,6 +141,9 @@ class WaveSurfer extends Player<WaveSurferEvents> {
protected subscriptions: Array<() => void> = []
protected mediaSubscriptions: Array<() => void> = []

public static readonly BasePlugin = BasePlugin
public static readonly createElement = createElement
Copy link
Owner

@katspaugh katspaugh Jan 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the back and forth on this, but now that it's a static method of WaveSurfer, it would be good to not name it similar to the existing create method to avoid confusion when using autocomplete.

I suggest we do

import * as dom from './dom.js'

// ...

  public static readonly dom = dom

createElement could then be a named export.

And plugin authors could then use it like this:

const { createElement } = WaveSurfer.dom

WDYT?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No worries at all dude! I updated the code.

* only imports named exports from a file. So I needed to update dom.js to have named export createElement.

I think we'd need to leave the default export since others maybe importing the default from dom.js in their projects.


/** Create a new WaveSurfer instance */
public static create(options: WaveSurferOptions) {
return new WaveSurfer(options)
Expand Down