2020-08-17 21:07:16 +01:00
|
|
|
import React from 'react'
|
2020-08-17 21:59:29 +01:00
|
|
|
import PropTypes from 'prop-types'
|
2020-08-17 21:39:25 +01:00
|
|
|
import { connect } from 'react-redux'
|
2020-06-11 00:49:23 +01:00
|
|
|
import ImmutablePropTypes from 'react-immutable-proptypes'
|
|
|
|
import ImmutablePureComponent from 'react-immutable-pure-component'
|
2020-03-07 04:53:28 +00:00
|
|
|
import { defineMessages, injectIntl } from 'react-intl'
|
|
|
|
import { is } from 'immutable'
|
2020-03-06 15:38:22 +00:00
|
|
|
import { decode } from 'blurhash'
|
2020-12-17 06:34:00 +00:00
|
|
|
import videojs from 'video.js'
|
2020-03-27 22:57:03 +00:00
|
|
|
import { isPanoramic, isPortrait, minimumAspectRatio, maximumAspectRatio } from '../utils/media_aspect_ratio'
|
|
|
|
import { displayMedia } from '../initial_state'
|
|
|
|
import Button from './button'
|
2020-06-11 00:49:23 +01:00
|
|
|
import Icon from './icon'
|
2020-06-11 04:29:33 +01:00
|
|
|
import SensitiveMediaItem from './sensitive_media_item'
|
2020-03-27 22:57:03 +00:00
|
|
|
import Text from './text'
|
2020-03-06 15:38:22 +00:00
|
|
|
|
2020-12-17 06:34:00 +00:00
|
|
|
import '!style-loader!css-loader!video.js/dist/video-js.min.css'
|
|
|
|
|
|
|
|
const videoJsOptions = {
|
|
|
|
autoplay: false,
|
2020-12-17 21:06:45 +00:00
|
|
|
playbackRates: [0.5, 1, 1.5, 2],
|
2020-12-17 06:34:00 +00:00
|
|
|
controls: true,
|
2020-12-17 21:06:45 +00:00
|
|
|
sources: [{}],
|
|
|
|
}
|
2020-12-17 06:34:00 +00:00
|
|
|
|
2020-06-11 00:49:23 +01:00
|
|
|
class Video extends ImmutablePureComponent {
|
2019-08-07 06:02:36 +01:00
|
|
|
|
|
|
|
state = {
|
|
|
|
containerWidth: this.props.width,
|
|
|
|
revealed: this.props.visible !== undefined ? this.props.visible : (displayMedia !== 'hide_all' && !this.props.sensitive || displayMedia === 'show_all'),
|
2020-03-07 04:53:28 +00:00
|
|
|
}
|
|
|
|
|
2020-06-11 00:49:23 +01:00
|
|
|
componentDidMount() {
|
2020-12-22 20:47:02 +00:00
|
|
|
//
|
2020-06-11 00:49:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
2020-12-17 06:34:00 +00:00
|
|
|
if (this.videoPlayer) {
|
|
|
|
this.videoPlayer.dispose()
|
|
|
|
}
|
2020-06-11 00:49:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
componentWillReceiveProps(nextProps) {
|
|
|
|
if (!is(nextProps.visible, this.props.visible) && nextProps.visible !== undefined) {
|
|
|
|
this.setState({ revealed: nextProps.visible })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidUpdate(prevProps, prevState) {
|
|
|
|
if (prevState.revealed && !this.state.revealed && this.video) {
|
|
|
|
this.video.pause()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
setPlayerRef = (n) => {
|
|
|
|
this.player = n
|
2019-08-07 06:02:36 +01:00
|
|
|
|
2020-06-11 00:49:23 +01:00
|
|
|
if (n) {
|
2020-03-07 04:53:28 +00:00
|
|
|
if (this.props.cacheWidth) this.props.cacheWidth(this.player.offsetWidth)
|
2019-08-07 06:02:36 +01:00
|
|
|
this.setState({
|
2020-06-11 00:49:23 +01:00
|
|
|
containerWidth: n.offsetWidth,
|
2020-03-07 04:53:28 +00:00
|
|
|
})
|
2019-08-07 06:02:36 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-11 00:49:23 +01:00
|
|
|
setVideoRef = (n) => {
|
|
|
|
this.video = n
|
2020-12-22 20:47:02 +00:00
|
|
|
this.setupVideo()
|
|
|
|
}
|
|
|
|
|
|
|
|
setupVideo = () => {
|
|
|
|
if (!this.video) return null
|
|
|
|
|
|
|
|
videoJsOptions.sources = [
|
|
|
|
{
|
|
|
|
src: this.props.src,
|
|
|
|
type: this.props.fileContentType,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
src: this.props.sourceMp4,
|
|
|
|
type: 'video/mp4',
|
|
|
|
},
|
|
|
|
]
|
|
|
|
this.videoPlayer = videojs(this.video, videoJsOptions)
|
2020-06-11 00:49:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
handleClickRoot = (e) => e.stopPropagation()
|
2019-08-07 06:02:36 +01:00
|
|
|
|
|
|
|
toggleReveal = () => {
|
|
|
|
if (this.props.onToggleVisibility) {
|
2020-03-07 04:53:28 +00:00
|
|
|
this.props.onToggleVisibility()
|
2019-08-07 06:02:36 +01:00
|
|
|
} else {
|
2020-03-07 04:53:28 +00:00
|
|
|
this.setState({ revealed: !this.state.revealed })
|
2019-08-07 06:02:36 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
2020-03-06 15:38:22 +00:00
|
|
|
const {
|
|
|
|
preview,
|
|
|
|
src,
|
|
|
|
inline,
|
|
|
|
startTime,
|
|
|
|
intl,
|
|
|
|
alt,
|
|
|
|
detailed,
|
|
|
|
sensitive,
|
2020-06-11 04:29:33 +01:00
|
|
|
aspectRatio,
|
2020-03-06 15:38:22 +00:00
|
|
|
} = this.props
|
|
|
|
|
|
|
|
const {
|
|
|
|
containerWidth,
|
2020-03-07 04:53:28 +00:00
|
|
|
revealed,
|
2020-03-06 15:38:22 +00:00
|
|
|
} = this.state
|
|
|
|
|
2020-03-07 04:53:28 +00:00
|
|
|
const playerStyle = {}
|
2019-08-07 06:02:36 +01:00
|
|
|
|
2020-03-07 04:53:28 +00:00
|
|
|
let { width, height } = this.props
|
2020-03-06 15:38:22 +00:00
|
|
|
|
2019-08-07 06:02:36 +01:00
|
|
|
if (inline && containerWidth) {
|
2020-03-07 04:53:28 +00:00
|
|
|
width = containerWidth
|
|
|
|
const minSize = containerWidth / (16 / 9)
|
2020-01-28 16:29:37 +00:00
|
|
|
|
|
|
|
if (isPanoramic(aspectRatio)) {
|
2020-03-07 04:53:28 +00:00
|
|
|
height = Math.max(Math.floor(containerWidth / maximumAspectRatio), minSize)
|
2020-01-28 16:29:37 +00:00
|
|
|
} else if (isPortrait(aspectRatio)) {
|
2020-03-07 04:53:28 +00:00
|
|
|
height = Math.max(Math.floor(containerWidth / minimumAspectRatio), minSize)
|
2020-01-28 16:29:37 +00:00
|
|
|
} else {
|
2020-03-07 04:53:28 +00:00
|
|
|
height = Math.floor(containerWidth / aspectRatio)
|
2020-01-28 16:29:37 +00:00
|
|
|
}
|
2019-08-07 06:02:36 +01:00
|
|
|
|
2020-03-07 04:53:28 +00:00
|
|
|
playerStyle.height = height
|
2019-08-07 06:02:36 +01:00
|
|
|
}
|
|
|
|
|
2020-03-07 04:53:28 +00:00
|
|
|
let preload
|
2019-08-07 06:02:36 +01:00
|
|
|
|
2020-12-18 04:46:37 +00:00
|
|
|
if (startTime) {
|
2020-03-07 04:53:28 +00:00
|
|
|
preload = 'auto'
|
2019-08-07 06:02:36 +01:00
|
|
|
} else if (detailed) {
|
2020-03-07 04:53:28 +00:00
|
|
|
preload = 'metadata'
|
2020-01-28 16:29:37 +00:00
|
|
|
} else {
|
2020-03-07 04:53:28 +00:00
|
|
|
preload = 'none'
|
2020-01-28 16:29:37 +00:00
|
|
|
}
|
|
|
|
|
2020-06-11 04:29:33 +01:00
|
|
|
if (!revealed && sensitive) {
|
|
|
|
return <SensitiveMediaItem onClick={this.toggleReveal} />
|
|
|
|
}
|
|
|
|
|
2019-08-07 06:02:36 +01:00
|
|
|
return (
|
|
|
|
<div
|
2020-12-18 04:46:37 +00:00
|
|
|
className={[_s.d, _s.mt10, _s.outlineNone].join(' ')}
|
2019-08-07 06:02:36 +01:00
|
|
|
style={playerStyle}
|
|
|
|
ref={this.setPlayerRef}
|
|
|
|
onMouseEnter={this.handleMouseEnter}
|
|
|
|
onMouseLeave={this.handleMouseLeave}
|
|
|
|
onClick={this.handleClickRoot}
|
|
|
|
tabIndex={0}
|
|
|
|
>
|
2020-12-17 06:34:00 +00:00
|
|
|
<div data-vjs-player>
|
|
|
|
<video
|
|
|
|
className={[_s.d, _s.h100PC, _s.w100PC, _s.outlineNone, 'video-js'].join(' ')}
|
|
|
|
ref={this.setVideoRef}
|
2020-12-17 06:40:42 +00:00
|
|
|
playsInline
|
2020-12-18 04:46:37 +00:00
|
|
|
poster={preview}
|
|
|
|
preload={preload}
|
|
|
|
role='button'
|
|
|
|
tabIndex='0'
|
|
|
|
aria-label={alt}
|
|
|
|
title={alt}
|
|
|
|
width={width}
|
|
|
|
height={height}
|
2020-03-06 15:38:22 +00:00
|
|
|
/>
|
2019-08-07 06:02:36 +01:00
|
|
|
</div>
|
|
|
|
|
2020-06-11 04:29:33 +01:00
|
|
|
{
|
|
|
|
revealed && sensitive &&
|
|
|
|
<div className={[_s.posAbs, _s.z2, _s.top0, _s.right0, _s.mt10, _s.mr10].join(' ')}>
|
|
|
|
<Button
|
|
|
|
title={intl.formatMessage(messages.toggle_visible)}
|
|
|
|
icon='hidden'
|
|
|
|
backgroundColor='black'
|
|
|
|
className={[_s.px10, _s.bgBlackOpaque_onHover].join(' ')}
|
|
|
|
onClick={this.toggleReveal}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
}
|
2019-08-07 06:02:36 +01:00
|
|
|
</div>
|
2020-03-07 04:53:28 +00:00
|
|
|
)
|
2019-08-07 06:02:36 +01:00
|
|
|
}
|
|
|
|
}
|
2020-08-18 01:57:35 +01:00
|
|
|
|
|
|
|
const messages = defineMessages({
|
|
|
|
toggle_visible: { id: 'media_gallery.toggle_visible', defaultMessage: 'Hide media' },
|
|
|
|
})
|
|
|
|
|
|
|
|
Video.propTypes = {
|
|
|
|
preview: PropTypes.string,
|
|
|
|
src: PropTypes.string.isRequired,
|
2020-12-22 17:11:22 +00:00
|
|
|
sourceMp4: PropTypes.string,
|
2020-08-18 01:57:35 +01:00
|
|
|
alt: PropTypes.string,
|
|
|
|
width: PropTypes.number,
|
|
|
|
height: PropTypes.number,
|
|
|
|
sensitive: PropTypes.bool,
|
|
|
|
startTime: PropTypes.number,
|
|
|
|
detailed: PropTypes.bool,
|
|
|
|
inline: PropTypes.bool,
|
|
|
|
cacheWidth: PropTypes.func,
|
|
|
|
visible: PropTypes.bool,
|
|
|
|
onToggleVisibility: PropTypes.func,
|
|
|
|
intl: PropTypes.object.isRequired,
|
|
|
|
blurhash: PropTypes.string,
|
|
|
|
aspectRatio: PropTypes.number,
|
|
|
|
meta: ImmutablePropTypes.map,
|
2020-12-22 06:36:38 +00:00
|
|
|
fileContentType: PropTypes.string,
|
2020-08-18 01:57:35 +01:00
|
|
|
}
|
|
|
|
|
2020-12-18 04:46:37 +00:00
|
|
|
export default injectIntl(Video)
|