Skip to content

Commit

Permalink
Use block meta to store file block's image blob
Browse files Browse the repository at this point in the history
  • Loading branch information
noisysocks committed Mar 28, 2023
1 parent 80aca27 commit 61ad707
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 21 deletions.
48 changes: 31 additions & 17 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 @@ -59,7 +59,7 @@ function ClipboardToolbarButton( { text, disabled } ) {
);
}

function FileEdit( { attributes, isSelected, setAttributes, clientId } ) {
function FileEdit( { attributes, isSelected, setAttributes, clientId, meta } ) {
const {
id,
fileId,
Expand All @@ -72,6 +72,9 @@ function FileEdit( { attributes, isSelected, setAttributes, clientId } ) {
displayPreview,
previewHeight,
} = attributes;

const { blobURL } = meta;

const { media, mediaUpload } = useSelect(
( select ) => ( {
media:
Expand All @@ -87,22 +90,27 @@ function FileEdit( { attributes, isSelected, setAttributes, clientId } ) {
const { toggleSelection, __unstableMarkNextChangeAsNotPersistent } =
useDispatch( blockEditorStore );

const [ isUploadingBlob, setIsUploadingBlob ] = useState( false );

useEffect( () => {
// Upload a file drag-and-dropped into the editor.
if ( isBlobURL( href ) ) {
const file = getBlobByURL( href );
const file = getBlobByURL( blobURL );
if ( file ) {
setIsUploadingBlob( true );

mediaUpload( {
filesList: [ file ],
onFileChange: ( [ newMedia ] ) => onSelectFile( newMedia ),
onError: onUploadError,
onFileChange: ( [ newMedia ] ) => {
onSelectFile( newMedia, { isPersistent: false } );
setIsUploadingBlob( false );
},
onError: ( message ) => {
onUploadError( message, { isPersistent: false } );
setIsUploadingBlob( false );
},
} );

revokeBlobURL( href );
}

if ( downloadButtonText === undefined ) {
changeDownloadButtonText( _x( 'Download', 'button label' ) );
revokeBlobURL( blobURL );
}
}, [] );

Expand All @@ -114,9 +122,12 @@ function FileEdit( { attributes, isSelected, setAttributes, clientId } ) {
}
}, [ href, fileId, clientId ] );

function onSelectFile( newMedia ) {
if ( newMedia && newMedia.url ) {
function onSelectFile( newMedia, { isPersistent = true } = {} ) {
if ( newMedia && newMedia.url && ! isBlobURL( newMedia.url ) ) {
const isPdf = newMedia.url.endsWith( '.pdf' );
if ( ! isPersistent ) {
__unstableMarkNextChangeAsNotPersistent();
}
setAttributes( {
href: newMedia.url,
fileName: newMedia.title,
Expand Down Expand Up @@ -178,9 +189,9 @@ function FileEdit( { attributes, isSelected, setAttributes, clientId } ) {

const blockProps = useBlockProps( {
className: classnames(
isBlobURL( href ) && getAnimateClassName( { type: 'loading' } ),
isUploadingBlob && getAnimateClassName( { type: 'loading' } ),
{
'is-transient': isBlobURL( href ),
'is-transient': isUploadingBlob,
}
),
} );
Expand Down Expand Up @@ -232,7 +243,7 @@ function FileEdit( { attributes, isSelected, setAttributes, clientId } ) {
/>
<ClipboardToolbarButton
text={ href }
disabled={ isBlobURL( href ) }
disabled={ isUploadingBlob }
/>
</BlockControls>
<div { ...blockProps }>
Expand Down Expand Up @@ -297,7 +308,10 @@ function FileEdit( { attributes, isSelected, setAttributes, clientId } ) {
'button'
)
) }
value={ downloadButtonText }
value={
downloadButtonText ??
_x( 'Download', 'button label' )
}
withoutInteractiveFormatting
placeholder={ __( 'Add text…' ) }
onChange={ ( text ) =>
Expand Down
6 changes: 2 additions & 4 deletions packages/block-library/src/file/transforms.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,8 @@ const transforms = {

// File will be uploaded in componentDidMount()
blocks.push(
createBlock( 'core/file', {
href: blobURL,
fileName: file.name,
textLinkHref: blobURL,
createBlock( 'core/file', {}, [], {
blobURL,
} )
);
} );
Expand Down

0 comments on commit 61ad707

Please sign in to comment.