react-player
Version:
A React component for playing a variety of URLs, including file paths, YouTube, Facebook, Twitch, SoundCloud, Streamable, Vimeo, Wistia and DailyMotion
175 lines (174 loc) • 6.03 kB
JavaScript
var __defProp = Object.defineProperty;
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
var __publicField = (obj, key, value) => {
__defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
return value;
};
import React, { Component, Suspense } from "react";
import merge from "deepmerge";
import memoize from "memoize-one";
import isEqual from "react-fast-compare";
import { propTypes, defaultProps } from "./props.js";
import { omit, lazy } from "./utils.js";
import Player from "./Player.js";
const Preview = lazy(() => import(
/* webpackChunkName: 'reactPlayerPreview' */
"./Preview.js"
));
const SUPPORTED_PROPS = Object.keys(propTypes);
const customPlayers = [];
const createReactPlayer = (players, fallback) => {
var _a;
return _a = class extends Component {
constructor() {
super(...arguments);
__publicField(this, "state", {
showPreview: !!this.props.light
});
// Use references, as refs is used by React
__publicField(this, "references", {
wrapper: (wrapper) => {
this.wrapper = wrapper;
},
player: (player) => {
this.player = player;
}
});
__publicField(this, "handleClickPreview", (e) => {
this.setState({ showPreview: false });
this.props.onClickPreview(e);
});
__publicField(this, "showPreview", () => {
this.setState({ showPreview: true });
});
__publicField(this, "getDuration", () => {
if (!this.player)
return null;
return this.player.getDuration();
});
__publicField(this, "getCurrentTime", () => {
if (!this.player)
return null;
return this.player.getCurrentTime();
});
__publicField(this, "getSecondsLoaded", () => {
if (!this.player)
return null;
return this.player.getSecondsLoaded();
});
__publicField(this, "getInternalPlayer", (key = "player") => {
if (!this.player)
return null;
return this.player.getInternalPlayer(key);
});
__publicField(this, "seekTo", (fraction, type, keepPlaying) => {
if (!this.player)
return null;
this.player.seekTo(fraction, type, keepPlaying);
});
__publicField(this, "handleReady", () => {
this.props.onReady(this);
});
__publicField(this, "getActivePlayer", memoize((url) => {
for (const player of [...customPlayers, ...players]) {
if (player.canPlay(url)) {
return player;
}
}
if (fallback) {
return fallback;
}
return null;
}));
__publicField(this, "getConfig", memoize((url, key) => {
const { config } = this.props;
return merge.all([
defaultProps.config,
defaultProps.config[key] || {},
config,
config[key] || {}
]);
}));
__publicField(this, "getAttributes", memoize((url) => {
return omit(this.props, SUPPORTED_PROPS);
}));
__publicField(this, "renderActivePlayer", (url) => {
if (!url)
return null;
const player = this.getActivePlayer(url);
if (!player)
return null;
const config = this.getConfig(url, player.key);
return /* @__PURE__ */ React.createElement(
Player,
{
...this.props,
key: player.key,
ref: this.references.player,
config,
activePlayer: player.lazyPlayer || player,
onReady: this.handleReady
}
);
});
}
shouldComponentUpdate(nextProps, nextState) {
return !isEqual(this.props, nextProps) || !isEqual(this.state, nextState);
}
componentDidUpdate(prevProps) {
const { light } = this.props;
if (!prevProps.light && light) {
this.setState({ showPreview: true });
}
if (prevProps.light && !light) {
this.setState({ showPreview: false });
}
}
renderPreview(url) {
if (!url)
return null;
const { light, playIcon, previewTabIndex, oEmbedUrl, previewAriaLabel } = this.props;
return /* @__PURE__ */ React.createElement(
Preview,
{
url,
light,
playIcon,
previewTabIndex,
previewAriaLabel,
oEmbedUrl,
onClick: this.handleClickPreview
}
);
}
render() {
const { url, style, width, height, fallback: fallback2, wrapper: Wrapper } = this.props;
const { showPreview } = this.state;
const attributes = this.getAttributes(url);
const wrapperRef = typeof Wrapper === "string" ? this.references.wrapper : void 0;
const UniversalSuspense = fallback2 === false ? ({ children }) => children : Suspense;
return /* @__PURE__ */ React.createElement(Wrapper, { ref: wrapperRef, style: { ...style, width, height }, ...attributes }, /* @__PURE__ */ React.createElement(UniversalSuspense, { fallback: fallback2 }, showPreview ? this.renderPreview(url) : this.renderActivePlayer(url)));
}
}, __publicField(_a, "displayName", "ReactPlayer"), __publicField(_a, "propTypes", propTypes), __publicField(_a, "defaultProps", defaultProps), __publicField(_a, "addCustomPlayer", (player) => {
customPlayers.push(player);
}), __publicField(_a, "removeCustomPlayers", () => {
customPlayers.length = 0;
}), __publicField(_a, "canPlay", (url) => {
for (const Player2 of [...customPlayers, ...players]) {
if (Player2.canPlay(url)) {
return true;
}
}
return false;
}), __publicField(_a, "canEnablePIP", (url) => {
for (const Player2 of [...customPlayers, ...players]) {
if (Player2.canEnablePIP && Player2.canEnablePIP(url)) {
return true;
}
}
return false;
}), _a;
};
export {
createReactPlayer
};