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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
|
import videojs from 'video.js'
type KeyHandler = { accept: (event: KeyboardEvent) => boolean, cb: (e: KeyboardEvent) => void }
const Plugin = videojs.getPlugin('plugin')
class PeerTubeHotkeysPlugin extends Plugin {
private static readonly VOLUME_STEP = 0.1
private static readonly SEEK_STEP = 5
private readonly handleKeyFunction: (event: KeyboardEvent) => void
private readonly handlers: KeyHandler[]
constructor (player: videojs.Player, options: videojs.PlayerOptions) {
super(player, options)
this.handlers = this.buildHandlers()
this.handleKeyFunction = (event: KeyboardEvent) => this.onKeyDown(event)
document.addEventListener('keydown', this.handleKeyFunction)
}
dispose () {
document.removeEventListener('keydown', this.handleKeyFunction)
}
private onKeyDown (event: KeyboardEvent) {
if (!this.isValidKeyTarget(event.target as HTMLElement)) return
for (const handler of this.handlers) {
if (handler.accept(event)) {
handler.cb(event)
return
}
}
}
private buildHandlers () {
const handlers: KeyHandler[] = [
// Play
{
accept: e => (e.key === ' ' || e.key === 'MediaPlayPause'),
cb: e => {
e.preventDefault()
e.stopPropagation()
if (this.player.paused()) this.player.play()
else this.player.pause()
}
},
// Increase volume
{
accept: e => this.isNaked(e, 'ArrowUp'),
cb: e => {
e.preventDefault()
this.player.volume(this.player.volume() + PeerTubeHotkeysPlugin.VOLUME_STEP)
}
},
// Decrease volume
{
accept: e => this.isNaked(e, 'ArrowDown'),
cb: e => {
e.preventDefault()
this.player.volume(this.player.volume() - PeerTubeHotkeysPlugin.VOLUME_STEP)
}
},
// Rewind
{
accept: e => this.isNaked(e, 'ArrowLeft') || this.isNaked(e, 'MediaRewind'),
cb: e => {
e.preventDefault()
const target = Math.max(0, this.player.currentTime() - PeerTubeHotkeysPlugin.SEEK_STEP)
this.player.currentTime(target)
}
},
// Forward
{
accept: e => this.isNaked(e, 'ArrowRight') || this.isNaked(e, 'MediaForward'),
cb: e => {
e.preventDefault()
const target = Math.min(this.player.duration(), this.player.currentTime() + PeerTubeHotkeysPlugin.SEEK_STEP)
this.player.currentTime(target)
}
},
// Fullscreen
{
// f key or Ctrl + Enter
accept: e => this.isNaked(e, 'f') || (!e.altKey && e.ctrlKey && e.key === 'Enter'),
cb: e => {
e.preventDefault()
if (this.player.isFullscreen()) this.player.exitFullscreen()
else this.player.requestFullscreen()
}
},
// Mute
{
accept: e => this.isNaked(e, 'm'),
cb: e => {
e.preventDefault()
this.player.muted(!this.player.muted())
}
},
// Increase playback rate
{
accept: e => e.key === '>',
cb: () => {
const target = Math.min(this.player.playbackRate() + 0.1, 5)
this.player.playbackRate(parseFloat(target.toFixed(2)))
}
},
// Decrease playback rate
{
accept: e => e.key === '<',
cb: () => {
const target = Math.max(this.player.playbackRate() - 0.1, 0.10)
this.player.playbackRate(parseFloat(target.toFixed(2)))
}
},
// Previous frame
{
accept: e => e.key === ',',
cb: () => {
this.player.pause()
// Calculate movement distance (assuming 30 fps)
const dist = 1 / 30
this.player.currentTime(this.player.currentTime() - dist)
}
},
// Next frame
{
accept: e => e.key === '.',
cb: () => {
this.player.pause()
// Calculate movement distance (assuming 30 fps)
const dist = 1 / 30
this.player.currentTime(this.player.currentTime() + dist)
}
}
]
// 0-9 key handlers
for (let i = 0; i < 10; i++) {
handlers.push({
accept: e => this.isNakedOrShift(e, i + ''),
cb: e => {
e.preventDefault()
this.player.currentTime(this.player.duration() * i * 0.1)
}
})
}
return handlers
}
private isValidKeyTarget (eventEl: HTMLElement) {
const playerEl = this.player.el()
const activeEl = document.activeElement
const currentElTagName = eventEl.tagName.toLowerCase()
return (
activeEl === playerEl ||
activeEl === playerEl.querySelector('.vjs-tech') ||
activeEl === playerEl.querySelector('.vjs-control-bar') ||
eventEl.id === 'content' ||
currentElTagName === 'body' ||
currentElTagName === 'video'
)
}
private isNaked (event: KeyboardEvent, key: string) {
return (!event.ctrlKey && !event.altKey && !event.metaKey && !event.shiftKey && event.key === key)
}
private isNakedOrShift (event: KeyboardEvent, key: string) {
return (!event.ctrlKey && !event.altKey && !event.metaKey && event.key === key)
}
}
videojs.registerPlugin('peerTubeHotkeysPlugin', PeerTubeHotkeysPlugin)
export { PeerTubeHotkeysPlugin }
|