diff options
Diffstat (limited to 'client/src/assets/player/shared/control-bar/chapters-plugin.ts')
-rw-r--r-- | client/src/assets/player/shared/control-bar/chapters-plugin.ts | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/client/src/assets/player/shared/control-bar/chapters-plugin.ts b/client/src/assets/player/shared/control-bar/chapters-plugin.ts new file mode 100644 index 000000000..5be081694 --- /dev/null +++ b/client/src/assets/player/shared/control-bar/chapters-plugin.ts | |||
@@ -0,0 +1,64 @@ | |||
1 | import videojs from 'video.js' | ||
2 | import { ChaptersOptions } from '../../types' | ||
3 | import { VideoChapter } from '@peertube/peertube-models' | ||
4 | import { ProgressBarMarkerComponent } from './progress-bar-marker-component' | ||
5 | |||
6 | const Plugin = videojs.getPlugin('plugin') | ||
7 | |||
8 | class ChaptersPlugin extends Plugin { | ||
9 | private chapters: VideoChapter[] = [] | ||
10 | private markers: ProgressBarMarkerComponent[] = [] | ||
11 | |||
12 | constructor (player: videojs.Player, options: videojs.ComponentOptions & ChaptersOptions) { | ||
13 | super(player, options) | ||
14 | |||
15 | this.chapters = options.chapters | ||
16 | |||
17 | this.player.ready(() => { | ||
18 | player.addClass('vjs-chapters') | ||
19 | |||
20 | this.player.one('durationchange', () => { | ||
21 | for (const chapter of this.chapters) { | ||
22 | if (chapter.timecode === 0) continue | ||
23 | |||
24 | const marker = new ProgressBarMarkerComponent(player, { timecode: chapter.timecode }) | ||
25 | |||
26 | this.markers.push(marker) | ||
27 | this.getSeekBar().addChild(marker) | ||
28 | } | ||
29 | }) | ||
30 | }) | ||
31 | } | ||
32 | |||
33 | dispose () { | ||
34 | for (const marker of this.markers) { | ||
35 | this.getSeekBar().removeChild(marker) | ||
36 | } | ||
37 | } | ||
38 | |||
39 | getChapter (timecode: number) { | ||
40 | if (this.chapters.length !== 0) { | ||
41 | for (let i = this.chapters.length - 1; i >= 0; i--) { | ||
42 | const chapter = this.chapters[i] | ||
43 | |||
44 | if (chapter.timecode <= timecode) { | ||
45 | this.player.addClass('has-chapter') | ||
46 | |||
47 | return chapter.title | ||
48 | } | ||
49 | } | ||
50 | } | ||
51 | |||
52 | this.player.removeClass('has-chapter') | ||
53 | |||
54 | return '' | ||
55 | } | ||
56 | |||
57 | private getSeekBar () { | ||
58 | return this.player.getDescendant('ControlBar', 'ProgressControl', 'SeekBar') | ||
59 | } | ||
60 | } | ||
61 | |||
62 | videojs.registerPlugin('chapters', ChaptersPlugin) | ||
63 | |||
64 | export { ChaptersPlugin } | ||