Video.tsx 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /*
  2. * <<
  3. * Davinci
  4. * ==
  5. * Copyright (C) 2016 - 2017 EDP
  6. * ==
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. * >>
  19. */
  20. import React, { useContext } from 'react'
  21. import { LayerContext } from '../../util'
  22. import { VIDEO_REG, IFRAME_REG, MEDIA_SRC_REG } from './constants'
  23. const Video: React.FC = () => {
  24. const {
  25. layer: { params }
  26. } = useContext(LayerContext)
  27. const { src, controlSetting, start, end } = params
  28. const setting = controlSetting.reduce((acc, key) => {
  29. let dataKey = key
  30. if (key === 'autoplay') {
  31. // support revealjs data-autoplay
  32. dataKey = 'data-autoplay'
  33. }
  34. return {
  35. ...acc,
  36. [dataKey]: true
  37. }
  38. }, {})
  39. let srcWithParams = src
  40. const videoRegExp = src && VIDEO_REG.test(src)
  41. const iframeRegExp = src && IFRAME_REG.test(src)
  42. if(src && videoRegExp){
  43. const startParams = `#t=${start ? start : 0}`
  44. const endParams = end ? `,${end}` : ''
  45. srcWithParams = start ? `${srcWithParams}${startParams}` : `${srcWithParams}${endParams}`
  46. }
  47. if(src && iframeRegExp){
  48. const iframeSrc = src.match(MEDIA_SRC_REG)
  49. srcWithParams = iframeRegExp ? iframeSrc[0] + '&autoplay=true' : src
  50. }
  51. const mediaType = videoRegExp ? 'video' : 'iframe'
  52. switch (mediaType) {
  53. case 'video':
  54. return (
  55. <video src={srcWithParams} preload="auto" {...setting}>
  56. 你的浏览器不支持 <code>video</code> 标签.
  57. </video>
  58. )
  59. default:
  60. return <iframe src={srcWithParams} frameBorder="0" allow="autoplay" />
  61. }
  62. }
  63. export default Video