-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add caption button to player control bar
- Loading branch information
1 parent
7b86b9b
commit c97b8fd
Showing
8 changed files
with
124 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
client/src/assets/player/shared/control-bar/caption-toggle-button.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import videojs from 'video.js' | ||
import { TheaterButtonOptions } from '../../types' | ||
import { getStoredPreferredSubtitle } from '../../peertube-player-local-storage' | ||
|
||
const Button = videojs.getComponent('Button') | ||
class CaptionToggleButton extends Button { | ||
|
||
constructor (player: videojs.Player, options: TheaterButtonOptions & videojs.ComponentOptions) { | ||
super(player, options) | ||
|
||
player.on('texttrackchange', () => this.update()) | ||
} | ||
|
||
buildCSSClass () { | ||
// Inherits vjs-captions-button for the icon | ||
return `vjs-caption-toggle-control vjs-captions-button ${super.buildCSSClass()}` | ||
} | ||
|
||
handleClick (event: any) { | ||
super.handleClick(event) | ||
|
||
const toEnable = this.getShowing() | ||
? undefined | ||
: this.getCaptionToEnable()?.id | ||
|
||
for (const track of this.player_.textTracks().tracks_) { | ||
if (toEnable && track.id === toEnable) track.mode = 'showing' | ||
else track.mode = 'disabled' | ||
} | ||
} | ||
|
||
private update () { | ||
if (this.getShowing()) { | ||
this.controlText('Disable subtitles') | ||
this.addClass('enabled') | ||
return | ||
} | ||
|
||
this.controlText(this.player_.localize('Enable {1} subtitle', [ this.getCaptionToEnable()?.label ])) | ||
this.removeClass('enabled') | ||
} | ||
|
||
private getShowing () { | ||
return this.listCaptions().find(t => t.mode === 'showing') | ||
} | ||
|
||
private getCaptionToEnable () { | ||
const captionToEnable = getStoredPreferredSubtitle() || this.listCaptions()[0]?.id | ||
const captions = this.listCaptions() | ||
|
||
return captions.find(t => t.id === captionToEnable) || captions[0] | ||
} | ||
|
||
private listCaptions () { | ||
return this.player_.textTracks().tracks_.filter(t => t.kind === 'captions') | ||
} | ||
} | ||
|
||
videojs.registerComponent('CaptionToggleButton', CaptionToggleButton) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters