Skip to content
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

Add video block attributes: Autoplay, Controls, Loop, Muted #7672

Merged
merged 5 commits into from
Jul 12, 2018
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions core-blocks/test/fixtures/core__video.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@
"name": "core/video",
"isValid": true,
"attributes": {
"src": "https://awesome-fake.video/file.mp4",
"caption": []
"autoplay": false,
"caption": [],
"controls": true,
"loop": false,
"muted": false,
"src": "https://awesome-fake.video/file.mp4"
},
"innerBlocks": [],
"originalContent": "<figure class=\"wp-block-video\"><video src=\"https://awesome-fake.video/file.mp4\" controls=\"\"></video></figure>"
Expand Down
45 changes: 42 additions & 3 deletions core-blocks/video/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,19 @@
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import { IconButton, Toolbar, withNotices } from '@wordpress/components';
import {
IconButton,
PanelBody,
Toolbar,
ToggleControl,
withNotices,
} from '@wordpress/components';
import { Component, Fragment } from '@wordpress/element';
import {
BlockControls,
InspectorControls,
MediaPlaceholder,
RichText,
BlockControls,
} from '@wordpress/editor';

/**
Expand All @@ -23,10 +30,18 @@ class VideoEdit extends Component {
this.state = {
editing: ! this.props.attributes.src,
};

this.toggleAttribute = this.toggleAttribute.bind( this );
}

toggleAttribute( attribute ) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like this would make a handy higher-order-component components could use as a sort of mixin, given it's the second place we've used this pattern (after Audio).

If you could either create a HOC for this and Audio (and probably other) components to use that'd be neat… or if that's too much of a pain could you at least file an issue with the "code quality" label for it and link to this comment? 😄

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your comment. I am not a React expert (yet). So I created an issue for this: #7717

return ( newValue ) => {
this.props.setAttributes( { [ attribute ]: newValue } );
};
}

render() {
const { caption, src } = this.props.attributes;
const { autoplay, caption, controls, loop, muted, src } = this.props.attributes;
const { setAttributes, isSelected, className, noticeOperations, noticeUI } = this.props;
const { editing } = this.state;
const switchToEditing = () => {
Expand Down Expand Up @@ -86,6 +101,30 @@ class VideoEdit extends Component {
/>
</Toolbar>
</BlockControls>
<InspectorControls>
<PanelBody title={ __( 'Playback Controls' ) }>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know this was already changed, but I actually think "Playback controls" for audio is maybe something I should have caught before and isn't the right fit.

Calling this "playback controls" when "autoplay" and "loop" aren't "controls" per-se is weird, and it also is redundant as you have "Playback Controls > Controls".

Realistically this should just be "Video Options" and the option underneath labelled "Controls" should arguably be "Playback Controls".

<ToggleControl
label={ __( 'Autoplay' ) }
onChange={ this.toggleAttribute( 'autoplay' ) }
checked={ autoplay }
/>
<ToggleControl
label={ __( 'Controls' ) }
onChange={ this.toggleAttribute( 'controls' ) }
checked={ controls }
/>
<ToggleControl
label={ __( 'Loop' ) }
onChange={ this.toggleAttribute( 'loop' ) }
checked={ loop }
/>
<ToggleControl
label={ __( 'Muted' ) }
onChange={ this.toggleAttribute( 'muted' ) }
checked={ muted }
/>
</PanelBody>
</InspectorControls>
<figure className={ className }>
<video controls src={ src } />
{ ( ( caption && caption.length ) || !! isSelected ) && (
Expand Down
51 changes: 43 additions & 8 deletions core-blocks/video/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,45 @@ export const settings = {
category: 'common',

attributes: {
autoplay: {
type: 'boolean',
source: 'attribute',
selector: 'video',
attribute: 'autoplay',
},
caption: {
type: 'array',
source: 'children',
selector: 'figcaption',
},
controls: {
type: 'boolean',
source: 'attribute',
selector: 'video',
attribute: 'controls',
default: true,
},
id: {
type: 'number',
},
loop: {
type: 'boolean',
source: 'attribute',
selector: 'video',
attribute: 'loop',
},
muted: {
type: 'boolean',
source: 'attribute',
selector: 'video',
attribute: 'muted',
},
src: {
type: 'string',
source: 'attribute',
selector: 'video',
attribute: 'src',
},
caption: {
type: 'array',
source: 'children',
selector: 'figcaption',
},
},

supports: {
Expand All @@ -49,12 +74,22 @@ export const settings = {
edit,

save( { attributes } ) {
const { src, caption } = attributes;
const { autoplay, caption, controls, loop, muted, src } = attributes;
return (

<figure>
{ src && <video controls src={ src } /> }
{ caption && caption.length > 0 && <RichText.Content tagName="figcaption" value={ caption } /> }
{ src && (
<video
autoPlay={ autoplay }
controls={ controls }
src={ src }
loop={ loop }
muted={ muted }
/>
) }
{ caption && caption.length && (
<RichText.Content tagName="figcaption" value={ caption } />
) }
</figure>
);
},
Expand Down