UNPKG

react-bilibili-embed-renderer

Version:

It is better to use Bilibili's embed renderer component for React applications

80 lines (73 loc) 2.85 kB
/** * react-bilibili-embed-renderer v1.2.1 * 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 */ 'use strict'; var React = require('react'); function _interopNamespace(e) { if (e && e.__esModule) return e; var n = Object.create(null); if (e) { Object.keys(e).forEach(function (k) { if (k !== 'default') { var d = Object.getOwnPropertyDescriptor(e, k); Object.defineProperty(n, k, d.get ? d : { enumerable: true, get: function () { return e[k]; } }); } }); } n["default"] = e; return Object.freeze(n); } var React__namespace = /*#__PURE__*/_interopNamespace(React); 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 = props.aid; const page = props.page || 1; const isWide = props.isWide || true; const highQuality = props.highQuality || true; const hasDanmaku = props.hasDanmaku || false; const defaultAspectWidth = isBrowser ? 4 : 16; const defaultAspectHeight = isBrowser ? 3 : 9; const aspectWidth = props.aspectWidth || defaultAspectWidth; const aspectHeight = props.aspectHeight || defaultAspectHeight; const width = props.width || 480; const height = calcHeight(width, props.height, aspectWidth, aspectHeight); const iframeClassImp = props.iframeClass || ''; const highQualityValue = highQuality ? 1 : 0; const wideValue = isWide ? 1 : 0; const danmakuValue = hasDanmaku ? 1 : 0; const bilibiliUrl = '//player.bilibili.com/player.html'; const iframeSrc = `${bilibiliUrl}?aid=${aid}&page=${page}&high_quality=${highQualityValue}&as_wide=${wideValue}&danmaku=${danmakuValue}`; return (React__namespace.createElement(React__namespace.Fragment, null, React__namespace.createElement("iframe", { width: width, height: height, src: iframeSrc, allowFullScreen: true, className: iframeClassImp }))); }; module.exports = BilibiliEmbedRenderer; //# sourceMappingURL=index.jsx.map