Merge pull request #317 from akinwale/issue311

Issue #311 playback input improvements
This commit is contained in:
Jeremy Kauffman 2017-07-04 14:13:13 -04:00 committed by GitHub
commit 658ce9115f
2 changed files with 56 additions and 5 deletions

View file

@ -4,12 +4,31 @@ import Link from "component/link";
import Modal from "component/modal"; import Modal from "component/modal";
class VideoPlayButton extends React.PureComponent { class VideoPlayButton extends React.PureComponent {
componentDidMount() {
this.keyDownListener = this.onKeyDown.bind(this);
document.addEventListener("keydown", this.keyDownListener);
}
componentWillUnmount() {
document.removeEventListener("keydown", this.keyDownListener);
}
onPurchaseConfirmed() { onPurchaseConfirmed() {
this.props.closeModal(); this.props.closeModal();
this.props.startPlaying(); this.props.startPlaying();
this.props.loadVideo(this.props.uri); this.props.loadVideo(this.props.uri);
} }
onKeyDown(event) {
if (
"input" !== event.target.tagName.toLowerCase() &&
"Space" === event.code
) {
event.preventDefault();
this.onWatchClick();
}
}
onWatchClick() { onWatchClick() {
this.props.purchaseUri(this.props.uri).then(() => { this.props.purchaseUri(this.props.uri).then(() => {
if (!this.props.modal) { if (!this.props.modal) {
@ -73,9 +92,11 @@ class VideoPlayButton extends React.PureComponent {
onConfirmed={this.onPurchaseConfirmed.bind(this)} onConfirmed={this.onPurchaseConfirmed.bind(this)}
onAborted={closeModal} onAborted={closeModal}
> >
{__("This will purchase")} <strong>{title}</strong> {__("for")} {__("This will purchase")} <strong>{title}</strong> {__("for")}{" "}
{" "}<strong><FilePrice uri={uri} look="plain" /></strong> <strong>
{" "}{__("credits")}. <FilePrice uri={uri} look="plain" />
</strong>{" "}
{__("credits")}.
</Modal> </Modal>
<Modal <Modal
isOpen={modal == "timedOut"} isOpen={modal == "timedOut"}

View file

@ -15,6 +15,8 @@ class VideoPlayer extends React.PureComponent {
startedPlaying: false, startedPlaying: false,
unplayable: false, unplayable: false,
}; };
this.togglePlayListener = this.togglePlay.bind(this);
} }
componentDidMount() { componentDidMount() {
@ -40,12 +42,14 @@ class VideoPlayer extends React.PureComponent {
player.append( player.append(
this.file(), this.file(),
container, container,
{ autoplay: false, controls: true }, { autoplay: false, controls: true, enableKeyboard: true },
renderMediaCallback.bind(this) renderMediaCallback.bind(this)
); );
document.addEventListener("keydown", this.togglePlayListener);
const mediaElement = this.refs.media.children[0]; const mediaElement = this.refs.media.children[0];
if (mediaElement) { if (mediaElement) {
mediaElement.addEventListener("click", this.togglePlayListener);
mediaElement.addEventListener( mediaElement.addEventListener(
"loadedmetadata", "loadedmetadata",
loadedMetadata.bind(this), loadedMetadata.bind(this),
@ -53,7 +57,6 @@ class VideoPlayer extends React.PureComponent {
once: true, once: true,
} }
); );
mediaElement.addEventListener( mediaElement.addEventListener(
"webkitfullscreenchange", "webkitfullscreenchange",
win32FullScreenChange.bind(this) win32FullScreenChange.bind(this)
@ -65,6 +68,33 @@ class VideoPlayer extends React.PureComponent {
} }
} }
componentWillUnmount() {
document.removeEventListener("keydown", this.togglePlayListener);
const mediaElement = this.refs.media.children[0];
if (mediaElement) {
mediaElement.removeEventListener("click", this.togglePlayListener);
}
}
togglePlay(event) {
// ignore all events except click and spacebar keydown, or input events in a form control
if (
"keydown" === event.type &&
("Space" !== event.code || "input" === event.target.tagName.toLowerCase())
) {
return;
}
event.preventDefault();
const mediaElement = this.refs.media.children[0];
if (mediaElement) {
if (!mediaElement.paused) {
mediaElement.pause();
} else {
mediaElement.play();
}
}
}
getPreferredVolume() { getPreferredVolume() {
const volumePreference = parseFloat(getSession("prefs_volume")); const volumePreference = parseFloat(getSession("prefs_volume"));
return isNaN(volumePreference) ? 1 : volumePreference; return isNaN(volumePreference) ? 1 : volumePreference;