lbry-desktop/ui/component/viewers/videoViewer/internal/effects/use-autoplay-next.js

87 lines
2.8 KiB
JavaScript
Raw Normal View History

2022-05-17 08:57:14 +02:00
/**
* Videojs "Autoplay Next" button.
*
* --- How to use ---
* Apply `useAutoplayNext` in your React component. It registers an effect that
* listens to the given Redux state, and returns a callback for you to mount the
* custom videojs component.
*
* --- Notes ---
* Usually, custom videojs components can just listen to videojs events, query
* states from `player` (e.g. player.paused()) and update accordingly. But since
* the state comes from Redux, there will be a need to listen and pass the info
* to videojs somehow.
*
* Instead of going through an 'effect->css->videojs' trip, we'll just listen to
* the Redux state through a normal effect to update the component.
*
* This file aims to encapsulate both the React and Videojs side of things
* through a single `useAutoplayNext` call.
*/
// @flow
import React from 'react';
import videojs from 'video.js';
import type { Player } from '../videojs';
// ****************************************************************************
// AutoplayNextButton
// ****************************************************************************
class AutoplayNextButton extends videojs.getComponent('Button') {
constructor(player, options = {}, autoplayNext) {
super(player, options, autoplayNext);
const title = __(autoplayNext ? 'Autoplay Next On' : 'Autoplay Next Off');
this.controlText(title);
this.addClass('vjs-button--autoplay-next');
this.setAttribute('aria-label', title);
this.setAttribute('aria-checked', autoplayNext);
}
}
function addAutoplayNextButton(player: Player, toggleAutoplayNext: () => void, autoplayNext: boolean) {
Reuse videojs instance between video reload, return mobile UI plugin for iOS (#1512) * add mobile plugin back on ios * further touchups and fix ios * finish mobile functionality * dont show big play button on mobile * remove logs * proof of concept * dont go full screen on rotate * add back functionality * replace dispose event with navigate away * bugfix * turn off show if you liked button and nag only on homepage * add back old functionality * ending event not working * test here * working but needs cleanup * more player touchups * bugfix * add settings button on mobile * more touchups * more cleanups * touchup loading functionality * fix hover thumbnails * touchup and eslint fix * fix repopulation bug * change recsys event name * bugfix events * change the way buttons are removed and added * finish chapters button * refactor to use videojs methods * refactor to fix autoplay next * ux touchups * seems to be behaving properly * control bar behaving how it should * fix control bar on ios * working on flow and eslint errors * bugfix and flow fixes * bring back nudge * fix playlist button bug * remove chapter markers properly * show big play button * bugfix recsys closed event * fix analytics bug * fix embeds * bugfix * possible bugfix for kp * bugfix playlist buttons * fix issue with mobile ui plugin * fix firefox autoplay issue * fix bug for play on floating player closed * bugfix volume control for ios * instantiate new player if switching between claim types * fix flow and lint errors * fix control bar not showing up when switching sources * dispose old player if recreating * bugfix save position * reset recsys data between videos * fix audio upload posters * clear claimSrcVhs on reload * bugfix errant image previews showing up * reset player value of having already switched quality * fix watch position not being used * bugfix switching between sources not perserving position * fix save position bug * fix playlist buttons * bugfix * code cleanup and add back 5 second feature
2022-06-10 18:18:58 +02:00
const controlBar = player.controlBar;
2022-05-17 08:57:14 +02:00
const autoplayButton = new AutoplayNextButton(
player,
{
name: 'AutoplayNextButton',
text: 'Autoplay Next',
clickHandler: () => {
toggleAutoplayNext();
},
},
autoplayNext
);
Reuse videojs instance between video reload, return mobile UI plugin for iOS (#1512) * add mobile plugin back on ios * further touchups and fix ios * finish mobile functionality * dont show big play button on mobile * remove logs * proof of concept * dont go full screen on rotate * add back functionality * replace dispose event with navigate away * bugfix * turn off show if you liked button and nag only on homepage * add back old functionality * ending event not working * test here * working but needs cleanup * more player touchups * bugfix * add settings button on mobile * more touchups * more cleanups * touchup loading functionality * fix hover thumbnails * touchup and eslint fix * fix repopulation bug * change recsys event name * bugfix events * change the way buttons are removed and added * finish chapters button * refactor to use videojs methods * refactor to fix autoplay next * ux touchups * seems to be behaving properly * control bar behaving how it should * fix control bar on ios * working on flow and eslint errors * bugfix and flow fixes * bring back nudge * fix playlist button bug * remove chapter markers properly * show big play button * bugfix recsys closed event * fix analytics bug * fix embeds * bugfix * possible bugfix for kp * bugfix playlist buttons * fix issue with mobile ui plugin * fix firefox autoplay issue * fix bug for play on floating player closed * bugfix volume control for ios * instantiate new player if switching between claim types * fix flow and lint errors * fix control bar not showing up when switching sources * dispose old player if recreating * bugfix save position * reset recsys data between videos * fix audio upload posters * clear claimSrcVhs on reload * bugfix errant image previews showing up * reset player value of having already switched quality * fix watch position not being used * bugfix switching between sources not perserving position * fix save position bug * fix playlist buttons * bugfix * code cleanup and add back 5 second feature
2022-06-10 18:18:58 +02:00
if (controlBar) {
controlBar.addChild(autoplayButton);
}
2022-05-17 08:57:14 +02:00
}
// ****************************************************************************
// useAutoplayNext
// ****************************************************************************
export default function useAutoplayNext(playerRef: any, autoplayNext: boolean) {
React.useEffect(() => {
const player = playerRef.current;
if (player) {
const touchOverlay = player.getChild('TouchOverlay');
const controlBar = player.getChild('controlBar') || touchOverlay.getChild('controlBar');
const autoplayButton = controlBar.getChild('AutoplayNextButton');
if (autoplayButton) {
const title = autoplayNext ? __('Autoplay Next On') : __('Autoplay Next Off');
autoplayButton.controlText(title);
autoplayButton.setAttribute('aria-label', title);
autoplayButton.setAttribute('aria-checked', autoplayNext);
}
}
}, [autoplayNext]);
return addAutoplayNextButton;
}