Skip to content

Commit

Permalink
Use a Map to store file block's image blob
Browse files Browse the repository at this point in the history
  • Loading branch information
noisysocks committed Mar 29, 2023
1 parent d05504b commit 37d6855
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 20 deletions.
1 change: 1 addition & 0 deletions packages/block-library/src/file/blob-urls.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const blobURLs = new Map();
40 changes: 29 additions & 11 deletions packages/block-library/src/file/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import classnames from 'classnames';
/**
* WordPress dependencies
*/
import { getBlobByURL, isBlobURL, revokeBlobURL } from '@wordpress/blob';
import { getBlobByURL, revokeBlobURL } from '@wordpress/blob';
import {
__unstableGetAnimateClassName as getAnimateClassName,
ResizableBox,
Expand All @@ -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 All @@ -35,6 +35,7 @@ import { store as noticesStore } from '@wordpress/notices';
*/
import FileBlockInspector from './inspector';
import { browserSupportsPdfs } from './utils';
import { blobURLs } from './blob-urls';

export const MIN_PREVIEW_HEIGHT = 200;
export const MAX_PREVIEW_HEIGHT = 2000;
Expand Down Expand Up @@ -72,6 +73,7 @@ function FileEdit( { attributes, isSelected, setAttributes, clientId } ) {
displayPreview,
previewHeight,
} = attributes;

const { media, mediaUpload } = useSelect(
( select ) => ( {
media:
Expand All @@ -87,21 +89,34 @@ 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 );
if ( blobURLs.has( clientId ) ) {
const blobURL = blobURLs.get( clientId );
const file = getBlobByURL( blobURL );

setIsUploadingBlob( true );

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

revokeBlobURL( href );
revokeBlobURL( blobURL );
blobURLs.delete( clientId );
}

if ( downloadButtonText === undefined ) {
__unstableMarkNextChangeAsNotPersistent();
changeDownloadButtonText( _x( 'Download', 'button label' ) );
}
}, [] );
Expand All @@ -114,9 +129,12 @@ function FileEdit( { attributes, isSelected, setAttributes, clientId } ) {
}
}, [ href, fileId, clientId ] );

function onSelectFile( newMedia ) {
function onSelectFile( newMedia, { isPersistent = true } = {} ) {
if ( newMedia && newMedia.url ) {
const isPdf = newMedia.url.endsWith( '.pdf' );
if ( ! isPersistent ) {
__unstableMarkNextChangeAsNotPersistent();
}
setAttributes( {
href: newMedia.url,
fileName: newMedia.title,
Expand Down Expand Up @@ -178,9 +196,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 +250,7 @@ function FileEdit( { attributes, isSelected, setAttributes, clientId } ) {
/>
<ClipboardToolbarButton
text={ href }
disabled={ isBlobURL( href ) }
disabled={ isUploadingBlob }
/>
</BlockControls>
<div { ...blockProps }>
Expand Down
17 changes: 8 additions & 9 deletions packages/block-library/src/file/transforms.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ import { select } from '@wordpress/data';
import { store as coreStore } from '@wordpress/core-data';
import { getFilename } from '@wordpress/url';

/**
* Internal dependencies
*/
import { blobURLs } from './blob-urls';

const transforms = {
from: [
{
Expand All @@ -22,15 +27,9 @@ const transforms = {

files.forEach( ( file ) => {
const blobURL = createBlobURL( file );

// File will be uploaded in componentDidMount()
blocks.push(
createBlock( 'core/file', {
href: blobURL,
fileName: file.name,
textLinkHref: blobURL,
} )
);
const block = createBlock( 'core/file' );
blobURLs.set( block.clientId, blobURL );
blocks.push( block );
} );

return blocks;
Expand Down

0 comments on commit 37d6855

Please sign in to comment.