Skip to content

Commit

Permalink
Support 'preload' attribute for Video Block (#7929)
Browse files Browse the repository at this point in the history
* Add preload for the video block

* Set metadata as default

* Add preload to tests

* feat: Disable editor interaction with video/audio blocks

* fix: allow preload=none

* fix validation issues
  • Loading branch information
Soean authored and tofumatt committed Aug 21, 2018
1 parent 4f8ffab commit 8755ebf
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 5 deletions.
9 changes: 8 additions & 1 deletion packages/block-library/src/audio/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*/
import { __ } from '@wordpress/i18n';
import {
Disabled,
IconButton,
PanelBody,
SelectControl,
Expand Down Expand Up @@ -129,7 +130,13 @@ class AudioEdit extends Component {
</PanelBody>
</InspectorControls>
<figure className={ className }>
<audio controls="controls" src={ src } />
{ /*
Disable the audio tag so the user clicking on it won't play the
file or change the position slider when the controls are enabled.
*/ }
<Disabled>
<audio controls="controls" src={ src } />
</Disabled>
{ ( ( caption && caption.length ) || !! isSelected ) && (
<RichText
tagName="figcaption"
Expand Down
22 changes: 20 additions & 2 deletions packages/block-library/src/video/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
*/
import { __ } from '@wordpress/i18n';
import {
Disabled,
IconButton,
PanelBody,
SelectControl,
Toolbar,
ToggleControl,
withNotices,
Expand Down Expand Up @@ -73,7 +75,7 @@ class VideoEdit extends Component {
}

render() {
const { autoplay, caption, controls, loop, muted, src } = this.props.attributes;
const { autoplay, caption, controls, loop, muted, preload, src } = this.props.attributes;
const { setAttributes, isSelected, className, noticeOperations, noticeUI } = this.props;
const { editing } = this.state;
const switchToEditing = () => {
Expand Down Expand Up @@ -148,10 +150,26 @@ class VideoEdit extends Component {
onChange={ this.toggleAttribute( 'controls' ) }
checked={ controls }
/>
<SelectControl
label={ __( 'Preload' ) }
value={ preload }
onChange={ ( value ) => setAttributes( { preload: value } ) }
options={ [
{ value: 'auto', label: __( 'Auto' ) },
{ value: 'metadata', label: __( 'Metadata' ) },
{ value: 'none', label: __( 'None' ) },
] }
/>
</PanelBody>
</InspectorControls>
<figure className={ className }>
<video controls src={ src } />
{ /*
Disable the video tag so the user clicking on it won't play the
video when the controls are enabled.
*/ }
<Disabled>
<video controls={ controls } src={ src } />
</Disabled>
{ ( ( caption && caption.length ) || !! isSelected ) && (
<RichText
tagName="figcaption"
Expand Down
12 changes: 10 additions & 2 deletions packages/block-library/src/video/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,13 @@ export const settings = {
selector: 'video',
attribute: 'muted',
},
preload: {
type: 'string',
source: 'attribute',
selector: 'video',
attribute: 'preload',
default: 'metadata',
},
src: {
type: 'string',
source: 'attribute',
Expand Down Expand Up @@ -94,16 +101,17 @@ export const settings = {
edit,

save( { attributes } ) {
const { autoplay, caption, controls, loop, muted, src } = attributes;
const { autoplay, caption, controls, loop, muted, preload, src } = attributes;
return (
<figure>
{ src && (
<video
autoPlay={ autoplay }
controls={ controls }
src={ src }
loop={ loop }
muted={ muted }
preload={ preload !== 'metadata' ? preload : undefined }
src={ src }
/>
) }
{ caption && caption.length > 0 && (
Expand Down
1 change: 1 addition & 0 deletions test/integration/full-content/fixtures/core__video.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"controls": true,
"loop": false,
"muted": false,
"preload": "metadata",
"src": "https://awesome-fake.video/file.mp4"
},
"innerBlocks": [],
Expand Down

0 comments on commit 8755ebf

Please sign in to comment.