Skip to content

Commit

Permalink
Drag and Drop: When dragging a mix of video, audio, and image blocks,…
Browse files Browse the repository at this point in the history
… create individual blocks as appropriate (#65144)

* Drag and Drop: When dragging a mix of video, audio, and image blocks, create individual blocks as appropriate

* Consolidate into the fallback transform for the File block

Co-authored-by: andrewserong <andrewserong@git.wordpress.org>
Co-authored-by: ramonjd <ramonopoly@git.wordpress.org>
Co-authored-by: kevin940726 <kevin940726@git.wordpress.org>
Co-authored-by: swissspidy <swissspidy@git.wordpress.org>
  • Loading branch information
5 people authored Sep 11, 2024
1 parent 07c7821 commit 98c65ea
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 29 deletions.
32 changes: 26 additions & 6 deletions packages/block-library/src/file/transforms.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,32 @@ const transforms = {
const blobURL = createBlobURL( file );

// File will be uploaded in componentDidMount()
blocks.push(
createBlock( 'core/file', {
blob: blobURL,
fileName: file.name,
} )
);
if ( file.type.startsWith( 'video/' ) ) {
blocks.push(
createBlock( 'core/video', {
blob: createBlobURL( file ),
} )
);
} else if ( file.type.startsWith( 'image/' ) ) {
blocks.push(
createBlock( 'core/image', {
blob: createBlobURL( file ),
} )
);
} else if ( file.type.startsWith( 'audio/' ) ) {
blocks.push(
createBlock( 'core/audio', {
blob: createBlobURL( file ),
} )
);
} else {
blocks.push(
createBlock( 'core/file', {
blob: blobURL,
fileName: file.name,
} )
);
}
} );

return blocks;
Expand Down
23 changes: 0 additions & 23 deletions packages/block-library/src/image/transforms.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@
*/
import { createBlobURL, isBlobURL } from '@wordpress/blob';
import { createBlock, getBlockAttributes } from '@wordpress/blocks';
import { dispatch } from '@wordpress/data';
import { store as noticesStore } from '@wordpress/notices';
import { __ } from '@wordpress/i18n';

export function stripFirstImage( attributes, { shortcode } ) {
const { body } = document.implementation.createHTMLDocument( '' );
Expand Down Expand Up @@ -138,26 +135,6 @@ const transforms = {
// creating a new gallery.
type: 'files',
isMatch( files ) {
// The following check is intended to catch non-image files when dropped together with images.
if (
files.some(
( file ) => file.type.indexOf( 'image/' ) === 0
) &&
files.some(
( file ) => file.type.indexOf( 'image/' ) !== 0
)
) {
const { createErrorNotice } = dispatch( noticesStore );
createErrorNotice(
__(
'If uploading to a gallery all files need to be image formats'
),
{
id: 'gallery-transform-invalid-file',
type: 'snackbar',
}
);
}
return files.every(
( file ) => file.type.indexOf( 'image/' ) === 0
);
Expand Down

0 comments on commit 98c65ea

Please sign in to comment.