You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

124 lines
3.4 KiB

  1. import ImmutablePropTypes from 'react-immutable-proptypes';
  2. import PureRenderMixin from 'react-addons-pure-render-mixin';
  3. import IconButton from './icon_button';
  4. import { defineMessages, injectIntl, FormattedMessage } from 'react-intl';
  5. const messages = defineMessages({
  6. toggle_sound: { id: 'video_player.toggle_sound', defaultMessage: 'Toggle sound' }
  7. });
  8. const videoStyle = {
  9. position: 'relative',
  10. zIndex: '1',
  11. width: '100%',
  12. height: '100%',
  13. objectFit: 'cover',
  14. top: '50%',
  15. transform: 'translateY(-50%)'
  16. };
  17. const muteStyle = {
  18. position: 'absolute',
  19. top: '10px',
  20. left: '10px',
  21. opacity: '0.8',
  22. zIndex: '5'
  23. };
  24. const spoilerStyle = {
  25. marginTop: '8px',
  26. background: '#000',
  27. color: '#fff',
  28. textAlign: 'center',
  29. height: '100%',
  30. cursor: 'pointer',
  31. display: 'flex',
  32. alignItems: 'center',
  33. justifyContent: 'center',
  34. flexDirection: 'column'
  35. };
  36. const spoilerSpanStyle = {
  37. display: 'block',
  38. fontSize: '14px'
  39. };
  40. const spoilerSubSpanStyle = {
  41. display: 'block',
  42. fontSize: '11px',
  43. fontWeight: '500'
  44. };
  45. const VideoPlayer = React.createClass({
  46. propTypes: {
  47. media: ImmutablePropTypes.map.isRequired,
  48. width: React.PropTypes.number,
  49. height: React.PropTypes.number,
  50. sensitive: React.PropTypes.bool
  51. },
  52. getDefaultProps () {
  53. return {
  54. width: 196,
  55. height: 110
  56. };
  57. },
  58. getInitialState () {
  59. return {
  60. visible: false,
  61. muted: true
  62. };
  63. },
  64. mixins: [PureRenderMixin],
  65. handleClick () {
  66. this.setState({ muted: !this.state.muted });
  67. },
  68. handleVideoClick (e) {
  69. e.stopPropagation();
  70. const node = ReactDOM.findDOMNode(this).querySelector('video');
  71. if (node.paused) {
  72. node.play();
  73. } else {
  74. node.pause();
  75. }
  76. },
  77. handleOpen () {
  78. this.setState({ visible: true });
  79. },
  80. render () {
  81. const { media, intl, width, height, sensitive } = this.props;
  82. if (sensitive && !this.state.visible) {
  83. return (
  84. <div style={{...spoilerStyle, width: `${width}px`, height: `${height}px` }} onClick={this.handleOpen}>
  85. <span style={spoilerSpanStyle}><FormattedMessage id='status.sensitive_warning' defaultMessage='Sensitive content' /></span>
  86. <span style={spoilerSubSpanStyle}><FormattedMessage id='status.sensitive_toggle' defaultMessage='Click to view' /></span>
  87. </div>
  88. );
  89. } else if (!sensitive && !this.state.visible) {
  90. return (
  91. <div style={{ cursor: 'pointer', position: 'relative', marginTop: '8px', width: `${width}px`, height: `${height}px`, background: `url(${media.get('preview_url')}) no-repeat center`, backgroundSize: 'cover' }} onClick={this.handleOpen}>
  92. <div style={{ position: 'absolute', top: '50%', left: '50%', fontSize: '36px', transform: 'translate(-50%, -50%)', padding: '5px', borderRadius: '100px', color: 'rgba(255, 255, 255, 0.8)' }}><i className='fa fa-play' /></div>
  93. </div>
  94. );
  95. }
  96. return (
  97. <div style={{ cursor: 'default', marginTop: '8px', overflow: 'hidden', width: `${width}px`, height: `${height}px`, boxSizing: 'border-box', background: '#000', position: 'relative' }}>
  98. <div style={muteStyle}><IconButton title={intl.formatMessage(messages.toggle_sound)} icon={this.state.muted ? 'volume-up' : 'volume-off'} onClick={this.handleClick} /></div>
  99. <video src={media.get('url')} autoPlay='true' loop={true} muted={this.state.muted} style={videoStyle} onClick={this.handleVideoClick} />
  100. </div>
  101. );
  102. }
  103. });
  104. export default injectIntl(VideoPlayer);