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

File block: Do not persist blob urls and fix undo #63282

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 @@ -273,7 +273,7 @@ Add a link to a downloadable file. ([Source](https://github.com/WordPress/gutenb
- **Name:** core/file
- **Category:** media
- **Supports:** align, anchor, color (background, gradients, link, ~~text~~), interactivity, spacing (margin, padding)
- **Attributes:** displayPreview, downloadButtonText, fileId, fileName, href, id, previewHeight, showDownloadButton, textLinkHref, textLinkTarget
- **Attributes:** blob, displayPreview, downloadButtonText, fileId, fileName, href, id, previewHeight, showDownloadButton, textLinkHref, textLinkTarget

## Footnotes

Expand Down
4 changes: 4 additions & 0 deletions packages/block-library/src/file/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
"id": {
"type": "number"
},
"blob": {
"type": "string",
"__experimentalRole": "local"
},
"href": {
"type": "string"
},
Expand Down
25 changes: 19 additions & 6 deletions packages/block-library/src/file/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import {
store as blockEditorStore,
__experimentalGetElementClassName,
} from '@wordpress/block-editor';
import { useEffect } from '@wordpress/element';
import { useEffect, useState } from '@wordpress/element';
import { useCopyToClipboard } from '@wordpress/compose';
import { __, _x } from '@wordpress/i18n';
import { file as icon } from '@wordpress/icons';
Expand Down Expand Up @@ -73,6 +73,7 @@ function FileEdit( { attributes, isSelected, setAttributes, clientId } ) {
displayPreview,
previewHeight,
} = attributes;
const [ temporaryURL, setTemporaryURL ] = useState( attributes.blob );
const { media } = useSelect(
( select ) => ( {
media:
Expand All @@ -88,7 +89,7 @@ function FileEdit( { attributes, isSelected, setAttributes, clientId } ) {
useDispatch( blockEditorStore );

useUploadMediaFromBlobURL( {
url: href,
url: temporaryURL,
onChange: onSelectFile,
onError: onUploadError,
} );
Expand All @@ -108,6 +109,12 @@ function FileEdit( { attributes, isSelected, setAttributes, clientId } ) {

function onSelectFile( newMedia ) {
if ( ! newMedia || ! newMedia.url ) {
setTemporaryURL();
return;
}

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

Expand All @@ -120,7 +127,9 @@ function FileEdit( { attributes, isSelected, setAttributes, clientId } ) {
displayPreview: isPdf ? true : undefined,
previewHeight: isPdf ? 600 : undefined,
fileId: `wp-block-file--media-${ clientId }`,
blob: undefined,
} );
setTemporaryURL();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if it might make sense to have a helper util for this

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there's a lot of duplication between how these media blocks are setup, a lot could be share but there are also some things that are to explain: some use "url" others "href" and others "src" for the uploaded files

}

function onUploadError( message ) {
Expand Down Expand Up @@ -166,16 +175,16 @@ function FileEdit( { attributes, isSelected, setAttributes, clientId } ) {

const blockProps = useBlockProps( {
className: clsx(
isBlobURL( href ) && getAnimateClassName( { type: 'loading' } ),
!! temporaryURL && getAnimateClassName( { type: 'loading' } ),
{
'is-transient': isBlobURL( href ),
'is-transient': !! temporaryURL,
}
),
} );

const displayPreviewInEditor = browserSupportsPdfs() && displayPreview;

if ( ! href ) {
if ( ! href && ! temporaryURL ) {
return (
<div { ...blockProps }>
<MediaPlaceholder
Expand All @@ -197,7 +206,11 @@ function FileEdit( { attributes, isSelected, setAttributes, clientId } ) {
return (
<>
<FileBlockInspector
hrefs={ { href, textLinkHref, attachmentPage } }
hrefs={ {
href: href || temporaryURL,
textLinkHref,
attachmentPage,
} }
{ ...{
openInNewWindow: !! textLinkTarget,
showDownloadButton,
Expand Down
3 changes: 1 addition & 2 deletions packages/block-library/src/file/transforms.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,8 @@ const transforms = {
// File will be uploaded in componentDidMount()
blocks.push(
createBlock( 'core/file', {
href: blobURL,
blob: blobURL,
fileName: file.name,
textLinkHref: blobURL,
} )
);
} );
Expand Down
Loading