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: add skip forward/backward buttons #8147

Merged
merged 19 commits into from
Mar 6, 2023
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
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
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@
"mux.js": "^6.2.0",
"safe-json-parse": "4.0.0",
"videojs-contrib-quality-levels": "3.0.0",
"videojs-font": "4.0.0",
"videojs-font": "4.1.0",
"videojs-vtt.js": "0.15.4"
},
"devDependencies": {
Expand Down
3 changes: 0 additions & 3 deletions sandbox/icons.html.example
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,6 @@
<li><span class="vjs-icon-forward-5"></span> <code>.vjs-icon-forward-5</code></li>
<li><span class="vjs-icon-forward-10"></span> <code>.vjs-icon-forward-10</code></li>
<li><span class="vjs-icon-forward-30"></span> <code>.vjs-icon-forward-30</code></li>
<li><span class="vjs-icon-forward-30"></span> <code>.vjs-icon-forward-30</code></li>
<li><span class="vjs-icon-forward-30"></span> <code>.vjs-icon-forward-30</code></li>
<li><span class="vjs-icon-forward-30"></span> <code>.vjs-icon-forward-30</code></li>
<li><span class="vjs-icon-audio"></span> <code>.vjs-icon-audio</code></li>
<li><span class="vjs-icon-next-item"></span> <code>.vjs-next-item</code></li>
<li><span class="vjs-icon-previous-item"></span> <code>.vjs-icon-previous-item</code></li>
Expand Down
114 changes: 114 additions & 0 deletions sandbox/skip-buttons.html.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Video.js Sandbox</title>
<link href="../dist/video-js.css" rel="stylesheet" type="text/css" />
<script src="../dist/video.js"></script>
</head>
<body>
<div>
<h2>Forward: 5, Backward: 10</h2>
<video-js
id="vid1"
controls
preload="auto"
width="640"
height="264"
poster="https://vjs.zencdn.net/v/oceans.png"
>
<source src="https://vjs.zencdn.net/v/oceans.mp4" type="video/mp4" />
<source src="https://vjs.zencdn.net/v/oceans.webm" type="video/webm" />
<source src="https://vjs.zencdn.net/v/oceans.ogv" type="video/ogg" />
<track
kind="captions"
src="../docs/examples/shared/example-captions.vtt"
srclang="en"
label="English"
/>
<p class="vjs-no-js">
To view this video please enable JavaScript, and consider upgrading to
a web browser that
<a href="https://videojs.com/html5-video-support/" target="_blank"
>supports HTML5 video</a
>
</p>
</video-js>
</div>

<div>
<h2>Forward: 10, Backward: 30</h2>
<video-js
id="vid2"
controls
preload="auto"
width="640"
height="264"
poster="https://vjs.zencdn.net/v/oceans.png"
>
<source src="https://vjs.zencdn.net/v/oceans.mp4" type="video/mp4" />
<source src="https://vjs.zencdn.net/v/oceans.webm" type="video/webm" />
<source src="https://vjs.zencdn.net/v/oceans.ogv" type="video/ogg" />
<track
kind="captions"
src="../docs/examples/shared/example-captions.vtt"
srclang="en"
label="English"
/>
<p class="vjs-no-js">
To view this video please enable JavaScript, and consider upgrading to
a web browser that
<a href="https://videojs.com/html5-video-support/" target="_blank"
>supports HTML5 video</a
>
</p>
</video-js>
</div>

<div>
<h2>Forward: 10</h2>
<video-js
id="vid3"
controls
preload="auto"
width="640"
height="264"
poster="https://vjs.zencdn.net/v/oceans.png"
>
<source src="https://vjs.zencdn.net/v/oceans.mp4" type="video/mp4" />
<source src="https://vjs.zencdn.net/v/oceans.webm" type="video/webm" />
<source src="https://vjs.zencdn.net/v/oceans.ogv" type="video/ogg" />
<track
kind="captions"
src="../docs/examples/shared/example-captions.vtt"
srclang="en"
label="English"
/>
<p class="vjs-no-js">
To view this video please enable JavaScript, and consider upgrading to
a web browser that
<a href="https://videojs.com/html5-video-support/" target="_blank"
>supports HTML5 video</a
>
</p>
</video-js>
</div>

<script>
var vid1 = document.getElementById("vid1");
var options = {
muted: true,
controlBar: { skipButtons: { forward: 5, backward: 10 } },
};
videojs(vid1, options);

var vid2 = document.getElementById("vid2");
options.controlBar.skipButtons = { forward: 10, backward: 30 };
videojs(vid2, options);

var vid3 = document.getElementById("vid3");
options.controlBar.skipButtons = {forward: 10}
videojs(vid3, options);
</script>
</body>
</html>
40 changes: 40 additions & 0 deletions src/css/components/_skip-buttons.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
.video-js .vjs-skip-forward-5 {
cursor: pointer;
& .vjs-icon-placeholder {
@extend .vjs-icon-forward-5;
}
}

.video-js .vjs-skip-forward-10 {
cursor: pointer;
& .vjs-icon-placeholder {
@extend .vjs-icon-forward-10;
}
}
.video-js .vjs-skip-forward-30 {
cursor: pointer;
& .vjs-icon-placeholder {
@extend .vjs-icon-forward-30;
}
}

.video-js .vjs-skip-backward-5 {
cursor: pointer;
& .vjs-icon-placeholder {
@extend .vjs-icon-replay-5;
}
}

.video-js .vjs-skip-backward-10 {
cursor: pointer;
& .vjs-icon-placeholder {
@extend .vjs-icon-replay-10;
}
}

.video-js .vjs-skip-backward-30 {
cursor: pointer;
& .vjs-icon-placeholder {
@extend .vjs-icon-replay-30;
}
}
1 change: 1 addition & 0 deletions src/css/video-js.scss
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
@import "components/adaptive";
@import "components/captions-settings";
@import "components/title-bar";
@import "components/skip-buttons";

@import "print";

Expand Down
4 changes: 4 additions & 0 deletions src/js/control-bar/control-bar.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import './progress-control/progress-control.js';
import './picture-in-picture-toggle.js';
import './fullscreen-toggle.js';
import './volume-panel.js';
import './skip-buttons/skip-forward.js';
import './skip-buttons/skip-backward.js';
import './text-track-controls/chapters-button.js';
import './text-track-controls/descriptions-button.js';
import './text-track-controls/subtitles-button.js';
Expand Down Expand Up @@ -55,6 +57,8 @@ class ControlBar extends Component {
ControlBar.prototype.options_ = {
children: [
'playToggle',
'skipBackward',
'skipForward',
'volumePanel',
'currentTimeDisplay',
'timeDivider',
Expand Down
69 changes: 69 additions & 0 deletions src/js/control-bar/skip-buttons/skip-backward.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import Button from '../../button';
import Component from '../../component';

/**
* Button to skip backward a configurable amount of time
* through a video. Renders in the control bar.
*
* * e.g. options: {controlBar: {skipButtons: backward: 5}}
*
* @extends Button
*/
class SkipBackward extends Button {
constructor(player, options) {
super(player, options);

this.validOptions = [5, 10, 30];
this.skipTime = this.getSkipBackwardTime();

if (this.skipTime && this.validOptions.includes(this.skipTime)) {
this.controlText(`Skip backward ${this.skipTime} seconds`);
Copy link
Contributor

Choose a reason for hiding this comment

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

This way means the translation file need a translation for each allowed increment. It's relatively manageable with 3 permitted values, but it is possible to use a variable in these translations: this.localize('Skip backward {seconds} seconds', [this.skipTime])

Either way there should be at least an entry for each string in en.json, so there's a template to translate from.

Copy link
Member

@misteroneill misteroneill Feb 28, 2023

Choose a reason for hiding this comment

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

I agree.

It should be safe to pass a pre-localized string to controlText, like:

this.controlText(this.localize('Skip backward {seconds} seconds', [this.skipTime]));

The reason for this is that translators will want a tokenized static string. The {seconds} token tells them what sort of value to expect in that position.

Then the en.json should have a new entry:

"Skip backward {seconds} seconds": "Skip backward {seconds} seconds"

The reason to include these in en.json is that is used as a reference by the script that parses out missing translations.


EDIT: adding a bit more context here...

The controlText method will pass the string through localize again, but this will be a pass-thru because the replacement of {seconds} will have already happened. It's unlikely there would be a matching localization key for something like Skip backward 1 seconds.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh ok thank you for the explanation! How does translation normally work - am I only expected to add this to en.json and then translators can add the translations to the other json files?

this.show();
} else {
this.hide();
}
}

getSkipBackwardTime() {
const playerOptions = this.options_.playerOptions;

return playerOptions.controlBar && playerOptions.controlBar.skipButtons && playerOptions.controlBar.skipButtons.backward;
}

buildCSSClass() {
return `vjs-skip-backward-${this.getSkipBackwardTime()} ${super.buildCSSClass()}`;
}

/**
* On click, skips backward in the video by a configurable amount of seconds.
* If the current time in the video is less than the configured 'skip backward' time,
* skips to beginning of video or seekable range.
*
* Handle a click on a `SkipBackward` button
*
* @param {EventTarget~Event} event
* The `click` event that caused this function
* to be called
*/
handleClick(event) {
const currentVideoTime = this.player_.currentTime();
const liveTracker = this.player_.liveTracker;
const seekableStart = liveTracker && liveTracker.isLive() && liveTracker.seekableStart();
let newTime;

if (seekableStart && (currentVideoTime - this.skipTime <= seekableStart)) {
newTime = seekableStart;
} else if (currentVideoTime >= this.skipTime) {
newTime = currentVideoTime - this.skipTime;
} else {
newTime = 0;
}
this.player_.currentTime(newTime);
}
}

SkipBackward.prototype.controlText_ = 'Skip Backward';

Component.registerComponent('SkipBackward', SkipBackward);

export default SkipBackward;
66 changes: 66 additions & 0 deletions src/js/control-bar/skip-buttons/skip-forward.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import Button from '../../button';
import Component from '../../component';

/**
* Button to skip forward a configurable amount of time
* through a video. Renders in the control bar.
*
* e.g. options: {controlBar: {skipButtons: forward: 5}}
*
* @extends Button
*/
class SkipForward extends Button {
constructor(player, options) {
super(player, options);

this.validOptions = [5, 10, 30];
this.skipTime = this.getSkipForwardTime();

if (this.skipTime && this.validOptions.includes(this.skipTime)) {
this.controlText(`Skip forward ${this.skipTime} seconds`);
Copy link
Member

Choose a reason for hiding this comment

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

Same comment here about localization.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've now moved the control text strings from skip-forward and skip-backward to en.json for localization.

this.show();
} else {
this.hide();
}
}

getSkipForwardTime() {
const playerOptions = this.options_.playerOptions;

return playerOptions.controlBar && playerOptions.controlBar.skipButtons && playerOptions.controlBar.skipButtons.forward;
}

buildCSSClass() {
return `vjs-skip-forward-${this.getSkipForwardTime()} ${super.buildCSSClass()}`;
}

/**
* On click, skips forward in the duration/seekable range by a configurable amount of seconds.
* If the time left in the duration/seekable range is less than the configured 'skip forward' time,
* skips to end of duration/seekable range.
*
* Handle a click on a `SkipForward` button
*
* @param {EventTarget~Event} event
* The `click` event that caused this function
* to be called
*/
handleClick(event) {
const currentVideoTime = this.player_.currentTime();
const liveTracker = this.player_.liveTracker;
const duration = (liveTracker && liveTracker.isLive()) ? liveTracker.seekableEnd() : this.player_.duration();
let newTime;

if (currentVideoTime + this.skipTime <= duration) {
newTime = currentVideoTime + this.skipTime;
} else {
newTime = duration;
}

this.player_.currentTime(newTime);
}
}

Component.registerComponent('SkipForward', SkipForward);

export default SkipForward;
Loading