Skip to content

Commit

Permalink
File block: Do not persist blob urls and fix undo (#63282)
Browse files Browse the repository at this point in the history
Co-authored-by: youknowriad <youknowriad@git.wordpress.org>
Co-authored-by: ellatrix <ellatrix@git.wordpress.org>
  • Loading branch information
3 people authored Jul 9, 2024
1 parent d3b3749 commit 3b3f820
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 @@ -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();
}

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

1 comment on commit 3b3f820

@github-actions
Copy link

Choose a reason for hiding this comment

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

Flaky tests detected in 3b3f820.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/9853547823
📝 Reported issues:

Please sign in to comment.