react-bilibili-embed-renderer
Version:
It is better to use Bilibili's embed renderer component for React applications
99 lines (90 loc) • 4.34 kB
JSX
/**
* react-bilibili-embed-renderer v1.3.0
* https://github.com/zeffon/bilibili-embed-renderer.git
*
* Copyright (c) ZeffonWu < zeffonwu@gmail.com > and project contributors.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* Author site: https://zeffon.cn
*/
import * as React from 'react';
/******************************************************************************
Copyright (c) Microsoft Corporation.
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
***************************************************************************** */
function __rest(s, e) {
var t = {};
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function")
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
t[p[i]] = s[p[i]];
}
return t;
}
typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
var e = new Error(message);
return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
};
const calcHeight = (width, height, aspectWidth, aspectHeight) => {
if (height) {
return height;
}
if (typeof width === 'number') {
return (width * aspectHeight) / aspectWidth;
}
else if (typeof width === 'string' && width.search('px') !== -1) {
const widthNum = Number(width.replace('px', ''));
return (widthNum * aspectHeight) / aspectWidth;
}
else if (typeof width === 'string' && width === '100%') {
return '100%';
}
return 360;
};
const isBrowser = !!(typeof window !== 'undefined' &&
window.document &&
window.document.createElement);
const BilibiliEmbedRenderer = (props) => {
const { aid, bvid, page = 1, isWide = true, highQuality = true, hasDanmaku = false, aspectWidth, aspectHeight, width = 480, height, iframeClass = '', className } = props, rest = __rest(props
// Validate that either aid or bvid is provided
, ["aid", "bvid", "page", "isWide", "highQuality", "hasDanmaku", "aspectWidth", "aspectHeight", "width", "height", "iframeClass", "className"]);
// Validate that either aid or bvid is provided
if (!aid && !bvid) {
throw new Error('Either aid or bvid must be provided');
}
const defaultAspectWidth = isBrowser ? 4 : 16;
const defaultAspectHeight = isBrowser ? 3 : 9;
const finalAspectWidth = aspectWidth || defaultAspectWidth;
const finalAspectHeight = aspectHeight || defaultAspectHeight;
const finalHeight = calcHeight(width, height, finalAspectWidth, finalAspectHeight);
const highQualityValue = highQuality ? 1 : 0;
const wideValue = isWide ? 1 : 0;
const danmakuValue = hasDanmaku ? 1 : 0;
const bilibiliUrl = '//player.bilibili.com/player.html';
// Build iframe src with either aid or bvid (bvid takes precedence)
let iframeSrc = `${bilibiliUrl}?`;
if (bvid) {
iframeSrc += `bvid=${bvid}`;
}
else {
iframeSrc += `aid=${aid}`;
}
iframeSrc += `&page=${page}&high_quality=${highQualityValue}&as_wide=${wideValue}&danmaku=${danmakuValue}`;
// Combine iframeClass with className
const finalClassName = [iframeClass, className].filter(Boolean).join(' ');
return (React.createElement("iframe", Object.assign({ width: width, height: finalHeight, src: iframeSrc, allowFullScreen: true, className: finalClassName || undefined }, rest)));
};
export { BilibiliEmbedRenderer as default };
//# sourceMappingURL=index.es.jsx.map