Skip to content

Commit f7e2524

Browse files
MariyaNizovtsovaMariya Nizovtsovacookpete
authored
Add Wistia player id to support multiple players (cookpete#905)
* Add unique wistia player id to have possibility to have multiple players with the same url on one page * Tidy up wistia playerID logic Co-authored-by: Mariya Nizovtsova <m.nizovtsova@itransition.com> Co-authored-by: Pete Cook <pete@cookpete.com>
1 parent 041393a commit f7e2524

3 files changed

Lines changed: 14 additions & 10 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ Key | Options
142142
`facebook` | `appId`: Your own [Facebook app ID](https://developers.facebook.com/docs/apps/register#app-id)<br />`version`: Facebook SDK version<br />`playerId`: Override player ID for consistent server-side rendering (use with [`react-uid`](https://github.com/thearnica/react-uid))
143143
`soundcloud` | `options`: Override the [default player options](https://developers.soundcloud.com/docs/api/html5-widget#params)
144144
`vimeo` | `playerOptions`: Override the [default params](https://developer.vimeo.com/player/sdk/embed)
145-
`wistia` | `options`: Override the [default player options](https://wistia.com/doc/embed-options#options_list)
145+
`wistia` | `options`: Override the [default player options](https://wistia.com/doc/embed-options#options_list)<br />`playerId`: Override player ID for consistent server-side rendering (use with [`react-uid`](https://github.com/thearnica/react-uid))
146146
`mixcloud` | `options`: Override the [default player options](https://www.mixcloud.com/developers/widget/#methods)
147147
`dailymotion` | `params`: Override the [default player vars](https://developer.dailymotion.com/player#player-parameters)
148148
`twitch` | `options`: Override the [default player options](https://dev.twitch.tv/docs/embed)<br />`playerId`: Override player ID for consistent server-side rendering (use with [`react-uid`](https://github.com/thearnica/react-uid))

src/players/Wistia.js

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,29 @@
11
import React, { Component } from 'react'
22

3-
import { callPlayer, getSDK } from '../utils'
3+
import { callPlayer, getSDK, randomString } from '../utils'
44
import { canPlay, MATCH_URL_WISTIA } from '../patterns'
55

66
const SDK_URL = 'https://fast.wistia.com/assets/external/E-v1.js'
77
const SDK_GLOBAL = 'Wistia'
8+
const PLAYER_ID_PREFIX = 'wistia-player-'
89

910
export default class Wistia extends Component {
1011
static displayName = 'Wistia'
1112
static canPlay = canPlay.wistia
1213
static loopOnEnded = true
1314
callPlayer = callPlayer
15+
playerID = this.props.config.playerId || `${PLAYER_ID_PREFIX}${randomString()}`
1416

1517
componentDidMount () {
1618
this.props.onMount && this.props.onMount(this)
1719
}
1820

19-
getID (url) {
20-
return url && url.match(MATCH_URL_WISTIA)[1]
21-
}
22-
2321
load (url) {
2422
const { playing, muted, controls, onReady, config, onError } = this.props
2523
getSDK(SDK_URL, SDK_GLOBAL).then(() => {
2624
window._wq = window._wq || []
2725
window._wq.push({
28-
id: this.getID(url),
26+
id: this.playerID,
2927
options: {
3028
autoPlay: playing,
3129
silentAutoPlay: 'allow',
@@ -105,14 +103,15 @@ export default class Wistia extends Component {
105103
}
106104

107105
render () {
108-
const id = this.getID(this.props.url)
109-
const className = `wistia_embed wistia_async_${id}`
106+
const { url } = this.props
107+
const videoID = url.match(MATCH_URL_WISTIA)[1]
108+
const className = `wistia_embed wistia_async_${videoID}`
110109
const style = {
111110
width: '100%',
112111
height: '100%'
113112
}
114113
return (
115-
<div key={id} className={className} style={style} />
114+
<div id={this.playerID} key={videoID} className={className} style={style} />
116115
)
117116
}
118117
}

test/players/Wistia.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ const TEST_CONFIG = {
1616
options: {}
1717
}
1818

19+
Wistia.prototype.componentWillMount = function () {
20+
this.playerID = 'mock-player-id'
21+
}
22+
1923
testPlayerMethods(Wistia, {
2024
play: 'play',
2125
pause: 'pause',
@@ -59,6 +63,7 @@ test('render()', t => {
5963
const style = { width: '100%', height: '100%' }
6064
t.true(wrapper.contains(
6165
<div
66+
id='mock-player-id'
6267
key='e4a27b971d'
6368
style={style}
6469
className='wistia_embed wistia_async_e4a27b971d'

0 commit comments

Comments
 (0)