Skip to content

Commit

Permalink
Gallery block refactor: make invalid file type errors consistent (#30396
Browse files Browse the repository at this point in the history
)


Co-authored-by: Glen Davies <[email protected]>
Co-authored-by: Aaron Robertshaw <[email protected]>
  • Loading branch information
3 people committed Apr 11, 2021
1 parent eaa0c7a commit 20477de
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 11 deletions.
22 changes: 18 additions & 4 deletions packages/block-library/src/gallery/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,12 @@ function GalleryEdit( props ) {
};
}

function isValidFileType( file ) {
return ALLOWED_MEDIA_TYPES.some(
( mediaType ) => file.type?.indexOf( mediaType ) === 0
);
}

function updateImages( selectedImages ) {
const newFileUploads =
Object.prototype.toString.call( selectedImages ) ===
Expand All @@ -217,10 +223,18 @@ function GalleryEdit( props ) {
} )
: selectedImages;

if ( ! imageArray.every( isValidFileType ) ) {
noticeOperations.removeAllNotices();
noticeOperations.createErrorNotice(
__(
'If uploading to a gallery all files need to be image formats'
),
{ id: 'gallery-upload-invalid-file' }
);
}

const processedImages = imageArray
.filter(
( file ) => file.url || file.type?.indexOf( 'image/' ) === 0
)
.filter( ( file ) => file.url || isValidFileType( file ) )
.map( ( file ) => {
if ( ! file.url ) {
return pickRelevantMediaFiles( {
Expand All @@ -232,7 +246,7 @@ function GalleryEdit( props ) {
} );

// Because we are reusing existing innerImage blocks any reordering
// done in the media libary will be lost so we need to reapply that ordering
// done in the media library will be lost so we need to reapply that ordering
// once the new image blocks are merged in with existing.
const newOrderMap = processedImages.reduce(
( result, image, index ) => (
Expand Down
11 changes: 4 additions & 7 deletions packages/block-library/src/image/transforms.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ const transforms = {
},
},
{
// Note: when dragging and dropping multiple files onto a gallery this overrrides the
// Note: when dragging and dropping multiple files onto a gallery this overrides the
// gallery transform in order to add new images to the gallery instead of
// creating a new gallery.
type: 'files',
Expand All @@ -141,15 +141,12 @@ const transforms = {
( file ) => file.type.indexOf( 'image/' ) !== 0
)
) {
const { createWarningNotice } = dispatch( noticesStore );
createWarningNotice(
const { createErrorNotice } = dispatch( noticesStore );
createErrorNotice(
__(
'If uploading to a gallery all files need to be image formats'
),
{
id: 'image-upload-format-warning',
type: 'snackbar',
}
{ id: 'gallery-transform-invalid-file' }
);
}
return every(
Expand Down

0 comments on commit 20477de

Please sign in to comment.