Skip to content

Commit

Permalink
Merge pull request #2299 from WordPress/add/audio-block
Browse files Browse the repository at this point in the history
Add Audio Block
  • Loading branch information
timmyc authored Aug 11, 2017
2 parents eb31727 + 3ce1560 commit 2b18b14
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 0 deletions.
76 changes: 76 additions & 0 deletions blocks/library/audio/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/**
* External dependencies
*/

/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import { Placeholder } from '@wordpress/components';

/**
* Internal dependencies
*/
import './style.scss';
import { registerBlockType, source } from '../../api';
import MediaUploadButton from '../../media-upload-button';

const { attr } = source;

registerBlockType( 'core/audio', {
title: __( 'Audio' ),

icon: 'media-audio',

category: 'common',

attributes: {
src: {
type: 'string',
source: attr( 'audio', 'src' ),
},
},

edit( { attributes, setAttributes, className } ) {
const { src } = attributes;
const onSelectAudio = ( media ) => {
if ( media && media.url ) {
setAttributes( { src: media.url } );
}
};

if ( ! src ) {
return [
<Placeholder
key="placeholder"
icon="media-audio"
label={ __( 'Audio' ) }
instructions={ __( 'Select an audio file from your library, or upload a new one:' ) }
className={ className }>
<MediaUploadButton
buttonProps={ { isLarge: true } }
onSelect={ onSelectAudio }
type="audio"
>
{ __( 'Insert from Media Library' ) }
</MediaUploadButton>
</Placeholder>,
];
}

/* eslint-disable jsx-a11y/no-static-element-interactions, jsx-a11y/onclick-has-role, jsx-a11y/click-events-have-key-events */
return [
<div key="audio">
<audio controls="controls" src={ src } />
</div>,
];
/* eslint-enable jsx-a11y/no-static-element-interactions, jsx-a11y/onclick-has-role, jsx-a11y/click-events-have-key-events */
},

save( { attributes } ) {
const { src } = attributes;
return (
<audio controls="controls" src={ src } />
);
},
} );
3 changes: 3 additions & 0 deletions blocks/library/audio/style.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.wp-block-audio .components-placeholder__input {
margin-top: 1em;
}
1 change: 1 addition & 0 deletions blocks/library/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ import './shortcode';
import './text-columns';
import './verse';
import './video';
import './audio';
3 changes: 3 additions & 0 deletions blocks/test/fixtures/core__audio.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<!-- wp:core/audio -->
<audio controls="" src="https://media.simplecast.com/episodes/audio/80564/draft-podcast-51-livePublish2.mp3" class="wp-block-audio"></audio>
<!-- /wp:core/audio -->
10 changes: 10 additions & 0 deletions blocks/test/fixtures/core__audio.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[
{
"uid": "_uid_0",
"name": "core/audio",
"isValid": true,
"attributes": {
"src": "https://media.simplecast.com/episodes/audio/80564/draft-podcast-51-livePublish2.mp3"
}
}
]
11 changes: 11 additions & 0 deletions blocks/test/fixtures/core__audio.parsed.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[
{
"blockName": "core/audio",
"attrs": null,
"rawContent": "\n<audio controls=\"\" src=\"https://media.simplecast.com/episodes/audio/80564/draft-podcast-51-livePublish2.mp3\" class=\"wp-block-audio\"></audio>\n"
},
{
"attrs": {},
"rawContent": "\n"
}
]
3 changes: 3 additions & 0 deletions blocks/test/fixtures/core__audio.serialized.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<!-- wp:core/audio -->
<audio controls="" src="https://media.simplecast.com/episodes/audio/80564/draft-podcast-51-livePublish2.mp3" class="wp-block-audio"></audio>
<!-- /wp:core/audio -->

0 comments on commit 2b18b14

Please sign in to comment.