UNPKG

react-bilibili-embed-renderer

Version:

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

58 lines (54 loc) 2.31 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 */ import * as React from '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.createElement(React.Fragment, null, React.createElement("iframe", { width: width, height: height, src: iframeSrc, allowFullScreen: true, className: iframeClassImp }))); }; export { BilibiliEmbedRenderer as default }; //# sourceMappingURL=index.es.jsx.map