This repository has been archived by the owner on Feb 29, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 113
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(content): show inline video players for youtube and vimeo
- Loading branch information
Showing
14 changed files
with
299 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
const React = require("react"); | ||
const classNames = require("classnames"); | ||
|
||
const MediaPreview = React.createClass({ | ||
getInitialState() { | ||
return { | ||
showPlayer: false | ||
}; | ||
}, | ||
|
||
getDefaultProps() { | ||
return { | ||
previewInfo: {}, | ||
}; | ||
}, | ||
|
||
onPreviewClick(evt) { | ||
evt.preventDefault(); | ||
this.setState({showPlayer: true}); | ||
}, | ||
|
||
render() { | ||
const previewInfo = this.props.previewInfo; | ||
|
||
let player; | ||
if (this.state.showPlayer && previewInfo.previewURL) { | ||
player = (<iframe className="video-preview-player" | ||
src={previewInfo.previewURL} | ||
width="367" | ||
height="206" | ||
type="text/html" | ||
ref="previewPlayer" | ||
frameBorder="0" | ||
allowFullScreen></iframe>); | ||
} | ||
|
||
const style = previewInfo.thumbnail ? {backgroundImage: `url(${previewInfo.thumbnail.url})`} : null; | ||
return (<div className={classNames("video-preview", {isPlaying: this.state.showPlayer})} | ||
style={style} onClick={this.onPreviewClick}> | ||
{player} | ||
</div>); | ||
} | ||
}); | ||
|
||
MediaPreview.propTypes = { | ||
previewInfo: React.PropTypes.shape({ | ||
previewURL: React.PropTypes.string, | ||
thumbnail: React.PropTypes.object, | ||
type: React.PropTypes.string, | ||
}) | ||
}; | ||
|
||
module.exports = MediaPreview; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
.video-preview { | ||
display: flex; | ||
align-items: center; | ||
justify-content: center; | ||
background: $black no-repeat center center; | ||
width: 367px; | ||
height: 206px; | ||
border-radius: 6px; | ||
transition: 0.2s opacity; | ||
overflow: hidden; | ||
margin-top: 20px; | ||
|
||
&::after { | ||
content: ''; | ||
width: 65px; | ||
height: 40px; | ||
background: url('img/playButton@2x.png') no-repeat center center; | ||
background-size: 65px 40px; | ||
display: block; | ||
} | ||
|
||
&:hover::after { | ||
background-image: url('img/playButton-hover@2x.png'); | ||
} | ||
|
||
&.isPlaying::after { | ||
display: none; | ||
} | ||
} | ||
|
||
.video-preview-player { | ||
width: 100%; | ||
height: 100%; | ||
display: block; | ||
border: 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
const getYouTubeID = require("get-youtube-id"); | ||
|
||
module.exports = function getVideoPreview(url) { | ||
if (!url) { | ||
return null; | ||
} | ||
return getVideoURL.youtube(url) || | ||
getVideoURL.vimeo(url) || | ||
null; | ||
}; | ||
|
||
const getVideoURL = { | ||
youtube: function(url) { | ||
let videoId = getYouTubeID(url, {fuzzy: false}); | ||
if (!videoId) { | ||
return null; | ||
} | ||
|
||
return `https://www.youtube.com/embed/${videoId}?autoplay=1`; | ||
}, | ||
|
||
vimeo: function(url) { | ||
const vimeoRegex = /(http|https)?:\/\/(www\.)?vimeo.com\/(?:channels\/(?:\w+\/)?|groups\/([^\/]*)\/videos\/|)(\d+)(?:|\/\?)/; | ||
let idMatches = url.match(vimeoRegex); | ||
if (!idMatches) { | ||
return null; | ||
} | ||
let videoId = idMatches[idMatches.length - 1]; | ||
|
||
return `https://player.vimeo.com/video/${videoId}?autoplay=1`; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
const {assert} = require("chai"); | ||
const TestUtils = require("react-addons-test-utils"); | ||
const React = require("react"); | ||
const ReactDOM = require("react-dom"); | ||
const MediaPreview = require("components/MediaPreview/MediaPreview"); | ||
|
||
const fakeProps = { | ||
previewInfo: { | ||
previewURL: "https://www.youtube.com/embed/lDv68xYHFXM", | ||
thumbnail: { | ||
"url": "https://i.ytimg.com/vi/lDv68xYHFXM/hqdefault.jpg", | ||
"height": 360, | ||
"width": 480, | ||
}, | ||
type: "video", | ||
} | ||
}; | ||
|
||
describe("MediaPreview", () => { | ||
let instance; | ||
let el; | ||
|
||
function setup(customPreviewProps = {}) { | ||
const previewInfo = Object.assign({}, fakeProps.previewInfo, customPreviewProps); | ||
instance = TestUtils.renderIntoDocument(<MediaPreview {...{previewInfo}} />); | ||
el = ReactDOM.findDOMNode(instance); | ||
} | ||
|
||
beforeEach(() => setup()); | ||
|
||
it("should not throw if missing props", () => { | ||
assert.doesNotThrow(() => { | ||
TestUtils.renderIntoDocument(<MediaPreview />); | ||
}); | ||
}); | ||
|
||
it("should create a MediaPreview instance", () => { | ||
assert.instanceOf(instance, MediaPreview); | ||
}); | ||
|
||
it("should have a background thumbnail", () => { | ||
assert.equal(el.style.backgroundImage, `url("${fakeProps.previewInfo.thumbnail.url}")`); | ||
}); | ||
|
||
it("should show the video iframe when the preview is clicked", () => { | ||
TestUtils.Simulate.click(el); | ||
const previewPlayer = instance.refs.previewPlayer; | ||
assert.equal(previewPlayer.src, fakeProps.previewInfo.previewURL); | ||
}); | ||
|
||
it("should not show the iframe when clicked and previewURL is falsey", () => { | ||
setup({previewURL: null}); | ||
TestUtils.Simulate.click(el); | ||
const previewPlayer = instance.refs.previewPlayer; | ||
assert.equal(typeof previewPlayer, "undefined"); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
const {assert} = require("chai"); | ||
|
||
const getVideoPreview = require("lib/getVideoPreview"); | ||
|
||
describe("getVideoPreview", () => { | ||
it("should return null if url is falsey", () => { | ||
assert.equal(getVideoPreview(), null); | ||
assert.equal(getVideoPreview(null), null); | ||
}); | ||
it("should return null if url is not an accepted video service url", () => { | ||
assert.equal(getVideoPreview("http://foo.com"), null); | ||
}); | ||
it("should return null for a youtube URL without a valid video id", () => { | ||
assert.equal(getVideoPreview("https://www.youtube.com/feed/trending"), null); | ||
}); | ||
it("should return null for a vimeo URL without a valid video id", () => { | ||
assert.equal(getVideoPreview("https://vimeo.com/channels/staffpicks"), null); | ||
}); | ||
it("should return an embed url for a valid youtube url", () => { | ||
const videoId = "lDv68xYHFXM"; | ||
assert.equal(getVideoPreview(`https://www.youtube.com/watch?v=${videoId}`), `https://www.youtube.com/embed/${videoId}?autoplay=1`); | ||
}); | ||
it("should return an embed url for a valid vimeo url", () => { | ||
const videoId = "1202674"; | ||
assert.equal(getVideoPreview(`https://vimeo.com/${videoId}`), `https://player.vimeo.com/video/${videoId}?autoplay=1`); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.