Skip to content

Commit

Permalink
Video block: Add poster image (#9335)
Browse files Browse the repository at this point in the history
* Video: Add poster

* Add i18n to upload title

* remove duplicate css style

* Responsive CSS

* Update player reload

* Blocks: Adjust poster rendering in Video block
  • Loading branch information
Soean authored and youknowriad committed Aug 30, 2018
1 parent bf2ce55 commit 208c0fa
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 4 deletions.
62 changes: 59 additions & 3 deletions packages/block-library/src/video/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
*/
import { __ } from '@wordpress/i18n';
import {
BaseControl,
Button,
Disabled,
IconButton,
PanelBody,
Expand All @@ -11,11 +13,12 @@ import {
ToggleControl,
withNotices,
} from '@wordpress/components';
import { Component, Fragment } from '@wordpress/element';
import { Component, Fragment, createRef } from '@wordpress/element';
import {
BlockControls,
InspectorControls,
MediaPlaceholder,
MediaUpload,
RichText,
editorMediaUpload,
} from '@wordpress/editor';
Expand All @@ -30,8 +33,11 @@ class VideoEdit extends Component {
editing: ! this.props.attributes.src,
};

this.videoPlayer = createRef();
this.toggleAttribute = this.toggleAttribute.bind( this );
this.onSelectURL = this.onSelectURL.bind( this );
this.onSelectPoster = this.onSelectPoster.bind( this );
this.onRemovePoster = this.onRemovePoster.bind( this );
}

componentDidMount() {
Expand All @@ -55,6 +61,12 @@ class VideoEdit extends Component {
}
}

componentDidUpdate( prevProps ) {
if ( this.props.attributes.poster !== prevProps.attributes.poster ) {
this.videoPlayer.current.load();
}
}

toggleAttribute( attribute ) {
return ( newValue ) => {
this.props.setAttributes( { [ attribute ]: newValue } );
Expand All @@ -74,8 +86,27 @@ class VideoEdit extends Component {
this.setState( { editing: false } );
}

onSelectPoster( image ) {
const { setAttributes } = this.props;
setAttributes( { poster: image.url } );
}

onRemovePoster() {
const { setAttributes } = this.props;
setAttributes( { poster: '' } );
}

render() {
const { autoplay, caption, controls, loop, muted, preload, src } = this.props.attributes;
const {
autoplay,
caption,
controls,
loop,
muted,
poster,
preload,
src,
} = this.props.attributes;
const { setAttributes, isSelected, className, noticeOperations, noticeUI } = this.props;
const { editing } = this.state;
const switchToEditing = () => {
Expand Down Expand Up @@ -160,6 +191,26 @@ class VideoEdit extends Component {
{ value: 'none', label: __( 'None' ) },
] }
/>
<BaseControl
className="editor-video-poster-control"
label={ __( 'Poster Image' ) }
>
<MediaUpload
title={ __( 'Select Poster Image' ) }
onSelect={ this.onSelectPoster }
type="image"
render={ ( { open } ) => (
<Button isDefault onClick={ open }>
{ ! this.props.attributes.poster ? __( 'Select Poster Image' ) : __( 'Replace image' ) }
</Button>
) }
/>
{ !! this.props.attributes.poster &&
<Button onClick={ this.onRemovePoster } isLink isDestructive>
{ __( 'Remove Poster Image' ) }
</Button>
}
</BaseControl>
</PanelBody>
</InspectorControls>
<figure className={ className }>
Expand All @@ -168,7 +219,12 @@ class VideoEdit extends Component {
video when the controls are enabled.
*/ }
<Disabled>
<video controls={ controls } src={ src } />
<video
controls={ controls }
poster={ poster }
src={ src }
ref={ this.videoPlayer }
/>
</Disabled>
{ ( ( caption && caption.length ) || !! isSelected ) && (
<RichText
Expand Down
8 changes: 8 additions & 0 deletions packages/block-library/src/video/editor.scss
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
.editor-block-list__block[data-align="center"] {
text-align: center;
}

.editor-video-poster-control .components-button {
margin-right: 8px;
}

.editor-video-poster-control .components-button + .components-button {
margin-top: 1em;
}
9 changes: 8 additions & 1 deletion packages/block-library/src/video/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ export const settings = {
selector: 'video',
attribute: 'muted',
},
poster: {
type: 'string',
source: 'attribute',
selector: 'video',
attribute: 'poster',
},
preload: {
type: 'string',
source: 'attribute',
Expand Down Expand Up @@ -101,7 +107,7 @@ export const settings = {
edit,

save( { attributes } ) {
const { autoplay, caption, controls, loop, muted, preload, src } = attributes;
const { autoplay, caption, controls, loop, muted, poster, preload, src } = attributes;
return (
<figure>
{ src && (
Expand All @@ -110,6 +116,7 @@ export const settings = {
controls={ controls }
loop={ loop }
muted={ muted }
poster={ poster }
preload={ preload !== 'metadata' ? preload : undefined }
src={ src }
/>
Expand Down
6 changes: 6 additions & 0 deletions packages/block-library/src/video/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
max-width: 100%;
}

@supports (position: sticky) {
[poster] {
object-fit: cover;
}
}

&.aligncenter {
text-align: center;
}
Expand Down

0 comments on commit 208c0fa

Please sign in to comment.