-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Support 'Autoplay' and 'Loop' in Audio Block 'Playback Controls' #7322
Changes from 7 commits
3ad7a80
f9c8655
683df3c
dbfb283
bec018f
a7d03bc
9cec9f3
d679123
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -105,6 +105,45 @@ describe( 'block parser', () => { | |
); | ||
expect( value ).toBe( 'chicken' ); | ||
} ); | ||
|
||
it( 'should return the matcher\'s string attribute value', () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Seeing this has motivated me to work on #7366 after reviewing this 😆 |
||
const value = parseWithAttributeSchema( | ||
'<audio src="#" loop>', | ||
{ | ||
type: 'string', | ||
source: 'attribute', | ||
selector: 'audio', | ||
attribute: 'src', | ||
}, | ||
); | ||
expect( value ).toBe( '#' ); | ||
} ); | ||
|
||
it( 'should return the matcher\'s true boolean attribute value', () => { | ||
const value = parseWithAttributeSchema( | ||
'<audio src="#" loop>', | ||
{ | ||
type: 'boolean', | ||
source: 'attribute', | ||
selector: 'audio', | ||
attribute: 'loop', | ||
}, | ||
); | ||
expect( value ).toBe( true ); | ||
} ); | ||
|
||
it( 'should return the matcher\'s false boolean attribute value', () => { | ||
const value = parseWithAttributeSchema( | ||
'<audio src="#" autoplay>', | ||
{ | ||
type: 'boolean', | ||
source: 'attribute', | ||
selector: 'audio', | ||
attribute: 'loop', | ||
}, | ||
); | ||
expect( value ).toBe( false ); | ||
} ); | ||
} ); | ||
|
||
describe( 'getBlockAttribute', () => { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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'; | ||
|
||
/** | ||
|
@@ -23,10 +30,18 @@ class AudioEdit extends Component { | |
this.state = { | ||
editing: ! this.props.attributes.src, | ||
}; | ||
|
||
this.toggleAttribute = this.toggleAttribute.bind( this ); | ||
} | ||
|
||
toggleAttribute( attribute ) { | ||
return ( newValue ) => { | ||
this.props.setAttributes( { [ attribute ]: newValue } ); | ||
}; | ||
} | ||
|
||
render() { | ||
const { caption, src } = this.props.attributes; | ||
const { autoplay, caption, loop, src } = this.props.attributes; | ||
const { setAttributes, isSelected, className, noticeOperations, noticeUI } = this.props; | ||
const { editing } = this.state; | ||
const switchToEditing = () => { | ||
|
@@ -86,14 +101,28 @@ class AudioEdit extends Component { | |
/> | ||
</Toolbar> | ||
</BlockControls> | ||
<InspectorControls> | ||
<PanelBody title={ __( 'Playback Controls' ) }> | ||
<ToggleControl | ||
label={ __( 'Autoplay' ) } | ||
onChange={ this.toggleAttribute( 'autoplay' ) } | ||
checked={ autoplay } | ||
/> | ||
<ToggleControl | ||
label={ __( 'Loop' ) } | ||
onChange={ this.toggleAttribute( 'loop' ) } | ||
checked={ loop } | ||
/> | ||
</PanelBody> | ||
</InspectorControls> | ||
<figure className={ className }> | ||
<audio controls="controls" src={ src } /> | ||
{ ( ( caption && caption.length ) || !! isSelected ) && ( | ||
<RichText | ||
tagName="figcaption" | ||
placeholder={ __( 'Write caption…' ) } | ||
value={ caption } | ||
onChange={ ( value ) => setAttributes( { caption: value } ) } | ||
onChange={ this.toggleAttribute( 'caption' ) } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice! 👍 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh, wait, it just occurs to me that this isn't a toggle. I hastily read through this part of the diff. I don't think this should be using the method If you want to keep it a single function I'd choose a more representative function name like I'd just make a new function that explicitly sets the caption here… I think it's good to move away from the inline function, but I don't think it's good to use this name for this event handler. |
||
inlineToolbar | ||
/> | ||
) } | ||
|
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.
I know this is the WordPress style but I see both variable first and constant first in Gutenberg. While my preference is variable first, I just wish we were consistent.
That's not specific to this commit though really. I was just working on a file earlier that had the same issue 😆. We just need some better rules is all.
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, I get why this is, but because the function's docstring says we return a value here and this is the only behaviour where we don't rely on
hpqParse
, can we document what's happening here?🤷♂️
It will just make the next person reading this not have to wonder, for a few seconds as I did, what this does 😄
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.
Added a note in db1103158cba6b4fb6f6f8800a48b78ab9ae968b