Skip to content
Merged
Changes from all 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
148 changes: 85 additions & 63 deletions packages/block-library/src/audio/edit.native.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
/**
* External dependencies
*/
import { Text } from 'react-native';
import { Text, TouchableWithoutFeedback } from 'react-native';
import { isEmpty } from 'lodash';

/**
* WordPress dependencies
Expand All @@ -16,17 +17,17 @@ import {
ToolbarGroup,
} from '@wordpress/components';
import {
BlockCaption,
BlockControls,
BlockIcon,
InspectorControls,
MediaPlaceholder,
MediaUpload,
MediaUploadProgress,
RichText,
} from '@wordpress/block-editor';
import { __ } from '@wordpress/i18n';
import { __, sprintf } from '@wordpress/i18n';
import { audio as icon, replace } from '@wordpress/icons';
import { createBlock } from '@wordpress/blocks';
import { useState } from '@wordpress/element';

const ALLOWED_MEDIA_TYPES = [ 'audio' ];

Expand All @@ -38,8 +39,12 @@ function AudioEdit( {
noticeUI,
insertBlocksAfter,
onFocus,
onBlur,
clientId,
} ) {
const { id, autoplay, caption, loop, preload, src } = attributes;
const { id, autoplay, loop, preload, src } = attributes;

const [ isCaptionSelected, setIsCaptionSelected ] = useState( false );

const onFileChange = ( { mediaId, mediaUrl } ) => {
setAttributes( { id: mediaId, src: mediaUrl } );
Expand Down Expand Up @@ -78,6 +83,16 @@ function AudioEdit( {
setAttributes( { src: media.url, id: media.id } );
}

function onAudioPress() {
setIsCaptionSelected( false );
}

function onFocusCaption() {
if ( ! isCaptionSelected ) {
setIsCaptionSelected( true );
}
}

if ( ! src ) {
return (
<View>
Expand Down Expand Up @@ -121,30 +136,13 @@ function AudioEdit( {
renderContent={ ( { isUploadInProgress, isUploadFailed } ) => {
return (
<View>
{ getBlockControls( open ) }
{ ! isCaptionSelected && getBlockControls( open ) }
{ getMediaOptions() }
<Text>
⏯ Audio Player goes here.{ ' ' }
{ isUploadInProgress && 'Uploading...' }
{ isUploadFailed && 'ERROR' }
</Text>
{ ( ! RichText.isEmpty( caption ) ||
isSelected ) && (
<RichText
tagName="figcaption"
placeholder={ __( 'Write caption…' ) }
value={ caption }
onChange={ ( value ) =>
setAttributes( { caption: value } )
}
inlineToolbar
__unstableOnSplitAtEnd={ () =>
insertBlocksAfter(
createBlock( 'core/paragraph' )
)
}
/>
) }
</View>
);
} }
Expand All @@ -153,46 +151,70 @@ function AudioEdit( {
}

return (
<>
Comment thread
jd-alexander marked this conversation as resolved.
<InspectorControls>
<PanelBody title={ __( 'Audio settings' ) }>
<ToggleControl
label={ __( 'Autoplay' ) }
onChange={ toggleAttribute( 'autoplay' ) }
checked={ autoplay }
/>
<ToggleControl
label={ __( 'Loop' ) }
onChange={ toggleAttribute( 'loop' ) }
checked={ loop }
/>
<SelectControl
label={ __( 'Preload' ) }
value={ preload || '' }
// `undefined` is required for the preload attribute to be unset.
onChange={ ( value ) =>
setAttributes( {
preload: value || undefined,
} )
}
options={ [
{ value: '', label: __( 'Browser default' ) },
{ value: 'auto', label: __( 'Auto' ) },
{ value: 'metadata', label: __( 'Metadata' ) },
{ value: 'none', label: __( 'None' ) },
] }
/>
</PanelBody>
</InspectorControls>
<MediaUpload
allowedTypes={ ALLOWED_MEDIA_TYPES }
isReplacingMedia={ true }
onSelect={ onSelectAudio }
render={ ( { open, getMediaOptions } ) => {
return getBlockUI( open, getMediaOptions );
} }
/>
</>
<TouchableWithoutFeedback
accessible={ ! isSelected }
onPress={ onAudioPress }
disabled={ ! isSelected }
>
<View>
<InspectorControls>
<PanelBody title={ __( 'Audio settings' ) }>
<ToggleControl
label={ __( 'Autoplay' ) }
onChange={ toggleAttribute( 'autoplay' ) }
checked={ autoplay }
/>
<ToggleControl
label={ __( 'Loop' ) }
onChange={ toggleAttribute( 'loop' ) }
checked={ loop }
/>
<SelectControl
label={ __( 'Preload' ) }
value={ preload || '' }
// `undefined` is required for the preload attribute to be unset.
onChange={ ( value ) =>
setAttributes( {
preload: value || undefined,
} )
}
options={ [
{ value: '', label: __( 'Browser default' ) },
{ value: 'auto', label: __( 'Auto' ) },
{ value: 'metadata', label: __( 'Metadata' ) },
{ value: 'none', label: __( 'None' ) },
] }
/>
</PanelBody>
</InspectorControls>
<MediaUpload
allowedTypes={ ALLOWED_MEDIA_TYPES }
isReplacingMedia={ true }
onSelect={ onSelectAudio }
render={ ( { open, getMediaOptions } ) => {
return getBlockUI( open, getMediaOptions );
} }
/>
<BlockCaption
accessible={ true }
accessibilityLabelCreator={ ( caption ) =>
isEmpty( caption )
? /* translators: accessibility text. Empty Audio caption. */
__( 'Audio caption. Empty' )
: sprintf(
/* translators: accessibility text. %s: Audio caption. */
__( 'Audio caption. %s' ),
caption
)
}
clientId={ clientId }
isSelected={ isCaptionSelected }
onFocus={ onFocusCaption }
onBlur={ onBlur }
insertBlocksAfter={ insertBlocksAfter }
/>
</View>
</TouchableWithoutFeedback>
);
}
export default withNotices( AudioEdit );