UNPKG

react-youtube

Version:

React.js powered YouTube player component

294 lines (293 loc) 9.96 kB
var __defProp = Object.defineProperty; var __defProps = Object.defineProperties; var __getOwnPropDescs = Object.getOwnPropertyDescriptors; var __getOwnPropSymbols = Object.getOwnPropertySymbols; var __hasOwnProp = Object.prototype.hasOwnProperty; var __propIsEnum = Object.prototype.propertyIsEnumerable; var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value; var __spreadValues = (a, b) => { for (var prop in b || (b = {})) if (__hasOwnProp.call(b, prop)) __defNormalProp(a, prop, b[prop]); if (__getOwnPropSymbols) for (var prop of __getOwnPropSymbols(b)) { if (__propIsEnum.call(b, prop)) __defNormalProp(a, prop, b[prop]); } return a; }; var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b)); var __async = (__this, __arguments, generator) => { return new Promise((resolve, reject) => { var fulfilled = (value) => { try { step(generator.next(value)); } catch (e) { reject(e); } }; var rejected = (value) => { try { step(generator.throw(value)); } catch (e) { reject(e); } }; var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected); step((generator = generator.apply(__this, __arguments)).next()); }); }; // src/YouTube.tsx import PropTypes from "prop-types"; import React from "react"; import isEqual from "fast-deep-equal"; import youTubePlayer from "youtube-player"; function shouldUpdateVideo(prevProps, props) { var _a, _b; if (prevProps.videoId !== props.videoId) { return true; } const prevVars = ((_a = prevProps.opts) == null ? void 0 : _a.playerVars) || {}; const vars = ((_b = props.opts) == null ? void 0 : _b.playerVars) || {}; return prevVars.start !== vars.start || prevVars.end !== vars.end; } function filterResetOptions(opts = {}) { return __spreadProps(__spreadValues({}, opts), { height: 0, width: 0, playerVars: __spreadProps(__spreadValues({}, opts.playerVars), { autoplay: 0, start: 0, end: 0 }) }); } function shouldResetPlayer(prevProps, props) { return prevProps.videoId !== props.videoId || !isEqual(filterResetOptions(prevProps.opts), filterResetOptions(props.opts)); } function shouldUpdatePlayer(prevProps, props) { var _a, _b, _c, _d; return prevProps.id !== props.id || prevProps.className !== props.className || ((_a = prevProps.opts) == null ? void 0 : _a.width) !== ((_b = props.opts) == null ? void 0 : _b.width) || ((_c = prevProps.opts) == null ? void 0 : _c.height) !== ((_d = props.opts) == null ? void 0 : _d.height) || prevProps.iframeClassName !== props.iframeClassName || prevProps.title !== props.title; } var defaultProps = { videoId: "", id: "", className: "", iframeClassName: "", style: {}, title: "", loading: void 0, opts: {}, onReady: () => { }, onError: () => { }, onPlay: () => { }, onPause: () => { }, onEnd: () => { }, onStateChange: () => { }, onPlaybackRateChange: () => { }, onPlaybackQualityChange: () => { } }; var propTypes = { videoId: PropTypes.string, id: PropTypes.string, className: PropTypes.string, iframeClassName: PropTypes.string, style: PropTypes.object, title: PropTypes.string, loading: PropTypes.oneOf(["lazy", "eager"]), opts: PropTypes.objectOf(PropTypes.any), onReady: PropTypes.func, onError: PropTypes.func, onPlay: PropTypes.func, onPause: PropTypes.func, onEnd: PropTypes.func, onStateChange: PropTypes.func, onPlaybackRateChange: PropTypes.func, onPlaybackQualityChange: PropTypes.func }; var _YouTube = class extends React.Component { constructor(props) { super(props); this.destroyPlayerPromise = void 0; this.onPlayerReady = (event) => { var _a, _b; return (_b = (_a = this.props).onReady) == null ? void 0 : _b.call(_a, event); }; this.onPlayerError = (event) => { var _a, _b; return (_b = (_a = this.props).onError) == null ? void 0 : _b.call(_a, event); }; this.onPlayerStateChange = (event) => { var _a, _b, _c, _d, _e, _f, _g, _h; (_b = (_a = this.props).onStateChange) == null ? void 0 : _b.call(_a, event); switch (event.data) { case _YouTube.PlayerState.ENDED: (_d = (_c = this.props).onEnd) == null ? void 0 : _d.call(_c, event); break; case _YouTube.PlayerState.PLAYING: (_f = (_e = this.props).onPlay) == null ? void 0 : _f.call(_e, event); break; case _YouTube.PlayerState.PAUSED: (_h = (_g = this.props).onPause) == null ? void 0 : _h.call(_g, event); break; default: } }; this.onPlayerPlaybackRateChange = (event) => { var _a, _b; return (_b = (_a = this.props).onPlaybackRateChange) == null ? void 0 : _b.call(_a, event); }; this.onPlayerPlaybackQualityChange = (event) => { var _a, _b; return (_b = (_a = this.props).onPlaybackQualityChange) == null ? void 0 : _b.call(_a, event); }; this.destroyPlayer = () => { if (this.internalPlayer) { this.destroyPlayerPromise = this.internalPlayer.destroy().then(() => this.destroyPlayerPromise = void 0); return this.destroyPlayerPromise; } return Promise.resolve(); }; this.createPlayer = () => { if (typeof document === "undefined") return; if (this.destroyPlayerPromise) { this.destroyPlayerPromise.then(this.createPlayer); return; } const playerOpts = __spreadProps(__spreadValues({}, this.props.opts), { videoId: this.props.videoId }); this.internalPlayer = youTubePlayer(this.container, playerOpts); this.internalPlayer.on("ready", this.onPlayerReady); this.internalPlayer.on("error", this.onPlayerError); this.internalPlayer.on("stateChange", this.onPlayerStateChange); this.internalPlayer.on("playbackRateChange", this.onPlayerPlaybackRateChange); this.internalPlayer.on("playbackQualityChange", this.onPlayerPlaybackQualityChange); if (this.props.title || this.props.loading) { this.internalPlayer.getIframe().then((iframe) => { if (this.props.title) iframe.setAttribute("title", this.props.title); if (this.props.loading) iframe.setAttribute("loading", this.props.loading); }); } }; this.resetPlayer = () => this.destroyPlayer().then(this.createPlayer); this.updatePlayer = () => { var _a; (_a = this.internalPlayer) == null ? void 0 : _a.getIframe().then((iframe) => { if (this.props.id) iframe.setAttribute("id", this.props.id); else iframe.removeAttribute("id"); if (this.props.iframeClassName) iframe.setAttribute("class", this.props.iframeClassName); else iframe.removeAttribute("class"); if (this.props.opts && this.props.opts.width) iframe.setAttribute("width", this.props.opts.width.toString()); else iframe.removeAttribute("width"); if (this.props.opts && this.props.opts.height) iframe.setAttribute("height", this.props.opts.height.toString()); else iframe.removeAttribute("height"); if (this.props.title) iframe.setAttribute("title", this.props.title); else iframe.setAttribute("title", "YouTube video player"); if (this.props.loading) iframe.setAttribute("loading", this.props.loading); else iframe.removeAttribute("loading"); }); }; this.getInternalPlayer = () => { return this.internalPlayer; }; this.updateVideo = () => { var _a, _b, _c, _d; if (typeof this.props.videoId === "undefined" || this.props.videoId === null) { (_a = this.internalPlayer) == null ? void 0 : _a.stopVideo(); return; } let autoplay = false; const opts = { videoId: this.props.videoId }; if ((_b = this.props.opts) == null ? void 0 : _b.playerVars) { autoplay = this.props.opts.playerVars.autoplay === 1; if ("start" in this.props.opts.playerVars) { opts.startSeconds = this.props.opts.playerVars.start; } if ("end" in this.props.opts.playerVars) { opts.endSeconds = this.props.opts.playerVars.end; } } if (autoplay) { (_c = this.internalPlayer) == null ? void 0 : _c.loadVideoById(opts); return; } (_d = this.internalPlayer) == null ? void 0 : _d.cueVideoById(opts); }; this.refContainer = (container) => { this.container = container; }; this.container = null; this.internalPlayer = null; } componentDidMount() { this.createPlayer(); } componentDidUpdate(prevProps) { return __async(this, null, function* () { if (shouldUpdatePlayer(prevProps, this.props)) { this.updatePlayer(); } if (shouldResetPlayer(prevProps, this.props)) { yield this.resetPlayer(); } if (shouldUpdateVideo(prevProps, this.props)) { this.updateVideo(); } }); } componentWillUnmount() { this.destroyPlayer(); } render() { return /* @__PURE__ */ React.createElement("div", { className: this.props.className, style: this.props.style }, /* @__PURE__ */ React.createElement("div", { id: this.props.id, className: this.props.iframeClassName, ref: this.refContainer })); } }; var YouTube = _YouTube; YouTube.propTypes = propTypes; YouTube.defaultProps = defaultProps; YouTube.PlayerState = { UNSTARTED: -1, ENDED: 0, PLAYING: 1, PAUSED: 2, BUFFERING: 3, CUED: 5 }; var YouTube_default = YouTube; export { YouTube_default as default }; //# sourceMappingURL=YouTube.esm.js.map