/
hover.ts
139 lines (117 loc) · 4.17 KB
/
hover.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/**
* The Hover plugin follows the mouse and shows a timestamp
*/
import BasePlugin, { type BasePluginEvents } from '../base-plugin.js'
import createElement from '../dom.js'
export type HoverPluginOptions = {
lineColor?: string
lineWidth?: string | number
labelColor?: string
labelSize?: string | number
labelBackground?: string
/* Allows to customize time label */
formatTimeCallback?: (seconds: number) => string
}
const defaultOptions = {
lineWidth: 1,
labelSize: 11,
formatTimeCallback(seconds: number) {
const minutes = Math.floor(seconds / 60)
const secondsRemainder = Math.floor(seconds) % 60
const paddedSeconds = `0${secondsRemainder}`.slice(-2)
return `${minutes}:${paddedSeconds}`
},
}
export type HoverPluginEvents = BasePluginEvents & {
hover: [relX: number]
}
class HoverPlugin extends BasePlugin<HoverPluginEvents, HoverPluginOptions> {
protected options: HoverPluginOptions & typeof defaultOptions
private wrapper: HTMLElement
private label: HTMLElement
private unsubscribe: () => void = () => undefined
constructor(options?: HoverPluginOptions) {
super(options || {})
this.options = Object.assign({}, defaultOptions, options)
// Create the plugin elements
this.wrapper = createElement('div', { part: 'hover' })
this.label = createElement('span', { part: 'hover-label' }, this.wrapper)
}
public static create(options?: HoverPluginOptions) {
return new HoverPlugin(options)
}
private addUnits(value: string | number): string {
const units = typeof value === 'number' ? 'px' : ''
return `${value}${units}`
}
/** Called by wavesurfer, don't call manually */
onInit() {
if (!this.wavesurfer) {
throw Error('WaveSurfer is not initialized')
}
const wsOptions = this.wavesurfer.options
const lineColor = this.options.lineColor || wsOptions.cursorColor || wsOptions.progressColor
// Vertical line
Object.assign(this.wrapper.style, {
position: 'absolute',
zIndex: 10,
left: 0,
top: 0,
height: '100%',
pointerEvents: 'none',
borderLeft: `${this.addUnits(this.options.lineWidth)} solid ${lineColor}`,
opacity: '0',
transition: 'opacity .1s ease-in',
})
// Timestamp label
Object.assign(this.label.style, {
display: 'block',
backgroundColor: this.options.labelBackground,
color: this.options.labelColor,
fontSize: `${this.addUnits(this.options.labelSize)}`,
transition: 'transform .1s ease-in',
padding: '2px 3px',
})
// Append the wrapper
const container = this.wavesurfer.getWrapper()
container.appendChild(this.wrapper)
// Attach pointer events
container.addEventListener('pointermove', this.onPointerMove)
container.addEventListener('pointerleave', this.onPointerLeave)
container.addEventListener('wheel', this.onPointerMove)
this.unsubscribe = () => {
container.removeEventListener('pointermove', this.onPointerMove)
container.removeEventListener('pointerleave', this.onPointerLeave)
container.removeEventListener('wheel', this.onPointerLeave)
}
}
private onPointerMove = (e: PointerEvent | WheelEvent) => {
if (!this.wavesurfer) return
// Position
const bbox = this.wavesurfer.getWrapper().getBoundingClientRect()
const { width } = bbox
const offsetX = e.clientX - bbox.left
const relX = Math.min(1, Math.max(0, offsetX / width))
const posX = Math.min(width - this.options.lineWidth - 1, offsetX)
this.wrapper.style.transform = `translateX(${posX}px)`
this.wrapper.style.opacity = '1'
// Timestamp
const duration = this.wavesurfer.getDuration() || 0
this.label.textContent = this.options.formatTimeCallback(duration * relX)
const labelWidth = this.label.offsetWidth
this.label.style.transform =
posX + labelWidth > width ? `translateX(-${labelWidth + this.options.lineWidth}px)` : ''
// Emit a hover event with the relative X position
this.emit('hover', relX)
}
private onPointerLeave = () => {
this.wrapper.style.opacity = '0'
}
/** Unmount */
public destroy() {
super.destroy()
this.unsubscribe()
this.wrapper.remove()
}
}
export default HoverPlugin