-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Video Editor: Add new video-editor block to enable selecting a custom video poster #11648
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
# Video Editor | ||
|
||
`VideoEditor` is a block component which enables the selection of a frame or the uploading of a custom image | ||
to serve as the video poster. | ||
|
||
It requires a `media` object to work properly. | ||
|
||
## Props | ||
|
||
### `className` | ||
|
||
<table> | ||
<tr><th>Type</th><td>string</td></tr> | ||
<tr><th>Required</th><td>No</td></tr> | ||
</table> | ||
|
||
String of classes (class names) appended to the video editor wrapper (`.video-editor [className]`). | ||
|
||
### `media` | ||
|
||
<table> | ||
<tr><th>Type</th><td>Object</td></tr> | ||
<tr><th>Required</th><td>Yes</td></tr> | ||
</table> | ||
|
||
This object needs to contain the following property: | ||
|
||
`media.videopress_guid` `{string}`: The unique identifier of the video (e.g. 'kUJmAcSf'). | ||
|
||
It can also contain these optional properties (with defaults set by VideoPress): | ||
- `media.width` `{number}`: The width of the video. Defaults to `854`. | ||
- `media.height` `{number}`: The height of the video. Defaults to `480`. | ||
|
||
### `onCancel` | ||
|
||
<table> | ||
<tr><th>Type</th><td>Function</td></tr> | ||
<tr><th>Required</th><td>No</td></tr> | ||
</table> | ||
|
||
A function which will get called on clicking the "Cancel" video editor button. If this prop is omitted, the "Cancel" | ||
button won't be rendered. | ||
|
||
### `onUpdatePoster` | ||
|
||
<table> | ||
<tr><th>Type</th><td>Function</td></tr> | ||
<tr><th>Required</th><td>No</td></tr> | ||
</table> | ||
|
||
A function which will get called after selecting a frame or uploading a custom image. | ||
It receives one argument: | ||
- the props of the video editor which include the poster URL and the ID of the media item. | ||
|
||
## Example | ||
|
||
```js | ||
import VideoEditor from 'blocks/video-editor'; | ||
|
||
render() { | ||
return ( | ||
<VideoEditor | ||
media={ { videopress_guid: 'kUJmAcSf' } } | ||
/> | ||
); | ||
} | ||
`` | ||
|
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,56 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import React, { Component } from 'react'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import VideoEditor from '../'; | ||
|
||
class VideoEditorExample extends Component { | ||
constructor() { | ||
super(); | ||
|
||
this.state = { | ||
media: { | ||
videopress_guid: 'vLAM7Mk6' | ||
} | ||
}; | ||
} | ||
|
||
handleUpdatePoster = ( { posterUrl } ) => { | ||
this.setState( { posterUrl } ); | ||
} | ||
|
||
render() { | ||
return ( | ||
<div> | ||
<div style={ { height: '50vh' } }> | ||
<VideoEditor | ||
media={ this.state.media } | ||
onUpdatePoster={ this.handleUpdatePoster } | ||
/> | ||
<div style={ { fontSize: '10px', textAlign: 'center' } }> | ||
Free B-Roll provided by <a href="http://Videezy.com">Videezy.com</a> | ||
</div> | ||
</div> | ||
|
||
<div style={ { | ||
textAlign: 'center', | ||
margin: '20px auto 0', | ||
width: '50%' | ||
} }> | ||
<h4>Changes to the poster above are shown below</h4> | ||
{ this.state.posterUrl && | ||
<img src={ this.state.posterUrl } /> | ||
} | ||
</div> | ||
</div> | ||
); | ||
} | ||
} | ||
|
||
VideoEditorExample.displayName = 'VideoEditor'; | ||
|
||
export default VideoEditorExample; |
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,226 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import React, { Component, PropTypes } from 'react'; | ||
import { connect } from 'react-redux'; | ||
import { get, noop } from 'lodash'; | ||
import { localize } from 'i18n-calypso'; | ||
import classNames from 'classnames'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import ProgressBar from 'components/progress-bar'; | ||
import Notice from 'components/notice'; | ||
import DetailPreviewVideo from 'post-editor/media-modal/detail/detail-preview-video'; | ||
import VideoEditorControls from './video-editor-controls'; | ||
import { updatePoster } from 'state/ui/editor/video-editor/actions'; | ||
import { | ||
getPosterUploadProgress, | ||
getPosterUrl, | ||
shouldShowVideoEditorError, | ||
} from 'state/selectors'; | ||
|
||
class VideoEditor extends Component { | ||
static propTypes = { | ||
className: PropTypes.string, | ||
media: PropTypes.object.isRequired, | ||
onCancel: PropTypes.func, | ||
onUpdatePoster: PropTypes.func, | ||
|
||
// Connected props | ||
posterUrl: PropTypes.string, | ||
shouldShowError: PropTypes.bool, | ||
uploadProgress: PropTypes.number, | ||
}; | ||
|
||
static defaultProps = { | ||
onCancel: noop, | ||
onUpdatePoster: noop, | ||
}; | ||
|
||
state = { | ||
error: false, | ||
isLoading: true, | ||
isSelectingFrame: false, | ||
pauseVideo: false, | ||
}; | ||
|
||
componentWillReceiveProps( nextProps ) { | ||
if ( nextProps.shouldShowError && ! this.props.shouldShowError ) { | ||
this.setState( { | ||
error: true, | ||
pauseVideo: false | ||
} ); | ||
|
||
return; | ||
} | ||
|
||
if ( this.props.posterUrl !== nextProps.posterUrl ) { | ||
this.props.onUpdatePoster( this.getVideoEditorProps( nextProps.posterUrl ) ); | ||
} | ||
} | ||
|
||
selectFrame = () => { | ||
const { isLoading } = this.state; | ||
|
||
if ( isLoading ) { | ||
this.setState( { error: true } ); | ||
return; | ||
} | ||
|
||
this.setState( { | ||
error: false, | ||
isSelectingFrame: true, | ||
pauseVideo: true, | ||
} ); | ||
} | ||
|
||
/** | ||
* Updates the poster by selecting a particular frame of the video. | ||
* @param {number} currentTime - Time at which to capture the frame | ||
*/ | ||
updatePoster = ( currentTime ) => { | ||
if ( ! this.state.isSelectingFrame ) { | ||
return; | ||
} | ||
|
||
const { media } = this.props; | ||
const guid = get( media, 'videopress_guid', null ); | ||
|
||
if ( guid ) { | ||
this.props.updatePoster( guid, { atTime: currentTime } ); | ||
} | ||
} | ||
|
||
setError = () => { | ||
this.setState( { error: true } ); | ||
} | ||
|
||
setIsLoading = () => { | ||
this.setState( { isLoading: false } ); | ||
} | ||
|
||
pauseVideo = () => { | ||
this.setState( { | ||
error: false, | ||
isSelectingFrame: false, | ||
pauseVideo: true, | ||
} ); | ||
} | ||
|
||
/** | ||
* Uploads an image to use as the poster for the video. | ||
* @param {object} file - Uploaded image | ||
*/ | ||
uploadImage = ( file ) => { | ||
if ( ! file ) { | ||
return; | ||
} | ||
|
||
const { media } = this.props; | ||
const guid = get( media, 'videopress_guid', null ); | ||
|
||
if ( guid ) { | ||
this.props.updatePoster( guid, { file } ); | ||
} | ||
} | ||
|
||
getVideoEditorProps( posterUrl ) { | ||
const { media } = this.props; | ||
const videoProperties = { posterUrl }; | ||
|
||
if ( media && media.ID ) { | ||
videoProperties.ID = media.ID; | ||
} | ||
|
||
return videoProperties; | ||
} | ||
|
||
renderError() { | ||
const { | ||
onCancel, | ||
translate, | ||
} = this.props; | ||
|
||
return ( | ||
<Notice | ||
status="is-error" | ||
showDismiss={ true } | ||
text={ translate( 'We are unable to edit this video.' ) } | ||
isCompact={ false } | ||
onDismissClick={ onCancel } /> | ||
); | ||
} | ||
|
||
render() { | ||
const { | ||
className, | ||
media, | ||
onCancel, | ||
uploadProgress, | ||
translate, | ||
} = this.props; | ||
const { | ||
error, | ||
isLoading, | ||
isSelectingFrame, | ||
pauseVideo, | ||
} = this.state; | ||
|
||
const classes = classNames( | ||
'video-editor', | ||
className, | ||
); | ||
|
||
return ( | ||
<div className={ classes }> | ||
{ error && this.renderError() } | ||
|
||
<figure> | ||
<div className="video-editor__content"> | ||
<div className="video-editor__preview-wrapper"> | ||
<DetailPreviewVideo | ||
className="video-editor__preview" | ||
isPlaying={ ! pauseVideo } | ||
item={ media } | ||
onPause={ this.updatePoster } | ||
onScriptLoadError={ this.setError } | ||
onVideoLoaded={ this.setIsLoading } | ||
/> | ||
</div> | ||
{ uploadProgress && ! error && ! isSelectingFrame && | ||
<ProgressBar | ||
className="video-editor__progress-bar" | ||
isPulsing={ true } | ||
total={ 100 } | ||
value={ uploadProgress } /> | ||
} | ||
<span className="video-editor__text"> | ||
{ translate( 'Select a frame to use as the thumbnail image or upload your own.' ) } | ||
</span> | ||
<VideoEditorControls | ||
isPosterUpdating={ uploadProgress && ! error } | ||
isVideoLoading={ isLoading } | ||
onCancel={ onCancel } | ||
onSelectFrame={ this.selectFrame } | ||
onUploadImage={ this.uploadImage } | ||
onUploadImageClick={ this.pauseVideo } | ||
/> | ||
</div> | ||
</figure> | ||
</div> | ||
); | ||
} | ||
} | ||
|
||
export default connect( | ||
( state ) => { | ||
return { | ||
posterUrl: getPosterUrl( state ), | ||
shouldShowError: shouldShowVideoEditorError( state ), | ||
uploadProgress: getPosterUploadProgress( state ), | ||
}; | ||
}, | ||
{ updatePoster } | ||
)( localize( VideoEditor ) ); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also optional, but the component may be easier to use if we can handle media not being available initially.