Skip to content

Commit

Permalink
Audio Block: Do not persist blob urls and fix undo (WordPress#63257)
Browse files Browse the repository at this point in the history
Co-authored-by: youknowriad <youknowriad@git.wordpress.org>
Co-authored-by: Mamaduka <mamaduka@git.wordpress.org>
  • Loading branch information
3 people authored and huubl committed Jul 10, 2024
1 parent 32b4004 commit b902a3d
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 9 deletions.
2 changes: 1 addition & 1 deletion docs/reference-guides/core-blocks.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Embed a simple audio player. ([Source](https://github.com/WordPress/gutenberg/tr
- **Name:** core/audio
- **Category:** media
- **Supports:** align, anchor, interactivity (clientNavigation), spacing (margin, padding)
- **Attributes:** autoplay, caption, id, loop, preload, src
- **Attributes:** autoplay, blob, caption, id, loop, preload, src

## Avatar

Expand Down
4 changes: 4 additions & 0 deletions packages/block-library/src/audio/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
"keywords": [ "music", "sound", "podcast", "recording" ],
"textdomain": "default",
"attributes": {
"blob": {
"type": "string",
"__experimentalRole": "local"
},
"src": {
"type": "string",
"source": "attribute",
Expand Down
26 changes: 19 additions & 7 deletions packages/block-library/src/audio/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import { __, _x } from '@wordpress/i18n';
import { useDispatch } from '@wordpress/data';
import { audio as icon } from '@wordpress/icons';
import { store as noticesStore } from '@wordpress/notices';
import { useState } from '@wordpress/element';

/**
* Internal dependencies
Expand All @@ -45,10 +46,10 @@ function AudioEdit( {
insertBlocksAfter,
} ) {
const { id, autoplay, loop, preload, src } = attributes;
const isTemporaryAudio = ! id && isBlobURL( src );
const [ temporaryURL, setTemporaryURL ] = useState( attributes.blob );

useUploadMediaFromBlobURL( {
url: src,
url: temporaryURL,
allowedTypes: ALLOWED_MEDIA_TYPES,
onChange: onSelectAudio,
onError: onUploadError,
Expand All @@ -72,7 +73,8 @@ function AudioEdit( {
onReplace( embedBlock );
return;
}
setAttributes( { src: newSrc, id: undefined } );
setAttributes( { src: newSrc, id: undefined, blob: undefined } );
setTemporaryURL();
}
}

Expand All @@ -95,27 +97,37 @@ function AudioEdit( {
src: undefined,
id: undefined,
caption: undefined,
blob: undefined,
} );
setTemporaryURL();
return;
}

if ( isBlobURL( media.url ) ) {
setTemporaryURL( media.url );
return;
}

// Sets the block's attribute and updates the edit component from the
// selected media, then switches off the editing UI.
setAttributes( {
blob: undefined,
src: media.url,
id: media.id,
caption: media.caption,
} );
setTemporaryURL();
}

const classes = clsx( className, {
'is-transient': isTemporaryAudio,
'is-transient': !! temporaryURL,
} );

const blockProps = useBlockProps( {
className: classes,
} );

if ( ! src ) {
if ( ! src && ! temporaryURL ) {
return (
<div { ...blockProps }>
<MediaPlaceholder
Expand Down Expand Up @@ -190,9 +202,9 @@ function AudioEdit( {
file or change the position slider when the controls are enabled.
*/ }
<Disabled isDisabled={ ! isSingleSelected }>
<audio controls="controls" src={ src } />
<audio controls="controls" src={ src ?? temporaryURL } />
</Disabled>
{ isTemporaryAudio && <Spinner /> }
{ !! temporaryURL && <Spinner /> }
<Caption
attributes={ attributes }
setAttributes={ setAttributes }
Expand Down
2 changes: 1 addition & 1 deletion packages/block-library/src/audio/transforms.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const transforms = {
// It's already done as part of the `componentDidMount`
// in the audio block.
const block = createBlock( 'core/audio', {
src: createBlobURL( file ),
blob: createBlobURL( file ),
} );

return block;
Expand Down

0 comments on commit b902a3d

Please sign in to comment.