-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
ReactPlayer.js
179 lines (153 loc) · 4.6 KB
/
ReactPlayer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import React, { Component, Suspense, lazy } from 'react'
import merge from 'deepmerge'
import memoize from 'memoize-one'
import isEqual from 'react-fast-compare'
import { propTypes, defaultProps } from './props'
import { omit } from './utils'
import Player from './Player'
const Preview = lazy(() => import(/* webpackChunkName: 'reactPlayerPreview' */'./Preview'))
const IS_BROWSER = typeof window !== 'undefined' && window.document
const SUPPORTED_PROPS = Object.keys(propTypes)
// Return null when rendering on the server
// as Suspense is not supported yet
const UniversalSuspense = IS_BROWSER ? Suspense : () => null
const customPlayers = []
export const createReactPlayer = (players, fallback) => {
return class ReactPlayer extends Component {
static displayName = 'ReactPlayer'
static propTypes = propTypes
static defaultProps = defaultProps
static addCustomPlayer = player => { customPlayers.push(player) }
static removeCustomPlayers = () => { customPlayers.length = 0 }
static canPlay = url => {
for (const Player of [...customPlayers, ...players]) {
if (Player.canPlay(url)) {
return true
}
}
return false
}
static canEnablePIP = url => {
for (const Player of [...customPlayers, ...players]) {
if (Player.canEnablePIP && Player.canEnablePIP(url)) {
return true
}
}
return false
}
state = {
showPreview: !!this.props.light
}
// Use references, as refs is used by React
references = {
wrapper: wrapper => { this.wrapper = wrapper },
player: player => { this.player = player }
}
shouldComponentUpdate (nextProps, nextState) {
return !isEqual(this.props, nextProps) || !isEqual(this.state, nextState)
}
componentDidUpdate (prevProps) {
const { light } = this.props
if (!prevProps.light && light) {
this.setState({ showPreview: true })
}
if (prevProps.light && !light) {
this.setState({ showPreview: false })
}
}
handleClickPreview = () => {
this.setState({ showPreview: false })
}
showPreview = () => {
this.setState({ showPreview: true })
}
getDuration = () => {
if (!this.player) return null
return this.player.getDuration()
}
getCurrentTime = () => {
if (!this.player) return null
return this.player.getCurrentTime()
}
getSecondsLoaded = () => {
if (!this.player) return null
return this.player.getSecondsLoaded()
}
getInternalPlayer = (key = 'player') => {
if (!this.player) return null
return this.player.getInternalPlayer(key)
}
seekTo = (fraction, type) => {
if (!this.player) return null
this.player.seekTo(fraction, type)
}
handleReady = () => {
this.props.onReady(this)
}
getActivePlayer = memoize(url => {
for (const player of [...customPlayers, ...players]) {
if (player.canPlay(url)) {
return player
}
}
if (fallback) {
return fallback
}
return null
})
getConfig = memoize((url, key) => {
const { config } = this.props
return merge.all([
defaultProps.config,
defaultProps.config[key] || {},
config,
config[key] || {}
])
})
getAttributes = memoize(url => {
return omit(this.props, SUPPORTED_PROPS)
})
renderPreview (url) {
if (!url) return null
const { light, playIcon } = this.props
return (
<Preview
url={url}
light={light}
playIcon={playIcon}
onClick={this.handleClickPreview}
/>
)
}
renderActivePlayer = url => {
if (!url) return null
const player = this.getActivePlayer(url)
if (!player) return null
const config = this.getConfig(url, player.key)
return (
<Player
{...this.props}
key={player.key}
ref={this.references.player}
config={config}
activePlayer={player.lazyPlayer || player}
onReady={this.handleReady}
/>
)
}
render () {
const { url, style, width, height, wrapper: Wrapper } = this.props
const { showPreview } = this.state
const attributes = this.getAttributes(url)
return (
<Wrapper ref={this.references.wrapper} style={{ ...style, width, height }} {...attributes}>
<UniversalSuspense fallback={null}>
{showPreview
? this.renderPreview(url)
: this.renderActivePlayer(url)}
</UniversalSuspense>
</Wrapper>
)
}
}
}