Skip to content
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

Audio Block: Do not persist blob urls and fix undo #63257

Merged
merged 1 commit into from
Jul 9, 2024
Merged
Show file tree
Hide file tree
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
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
Loading