Skip to content

Commit

Permalink
Fix dropping media from inserter into Cover block (WordPress#67056)
Browse files Browse the repository at this point in the history
Co-authored-by: tellthemachines <[email protected]>
Co-authored-by: ramonjd <[email protected]>
  • Loading branch information
3 people authored Nov 18, 2024
1 parent 5d0d155 commit 134e4b4
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 39 deletions.
60 changes: 32 additions & 28 deletions packages/block-editor/src/components/media-placeholder/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ export function MediaPlaceholder( {
( block.name === 'core/image' ||
block.name === 'core/audio' ||
block.name === 'core/video' ) &&
block.attributes.url
( block.attributes.url || block.attributes.src )
? [ block ]
: recursivelyFindMediaFromBlocks( block.innerBlocks )
);
Expand All @@ -252,33 +252,37 @@ export function MediaPlaceholder( {
}

const uploadedMediaList = await Promise.all(
mediaBlocks.map( ( block ) =>
block.attributes.id
? block.attributes
: new Promise( ( resolve, reject ) => {
window
.fetch( block.attributes.url )
.then( ( response ) => response.blob() )
.then( ( blob ) =>
mediaUpload( {
filesList: [ blob ],
additionalData: {
title: block.attributes.title,
alt_text: block.attributes.alt,
caption: block.attributes.caption,
},
onFileChange: ( [ media ] ) => {
if ( media.id ) {
resolve( media );
}
},
allowedTypes,
onError: reject,
} )
)
.catch( () => resolve( block.attributes.url ) );
} )
)
mediaBlocks.map( ( block ) => {
const blockType = block.name.split( '/' )[ 1 ];
if ( block.attributes.id ) {
block.attributes.type = blockType;
return block.attributes;
}
return new Promise( ( resolve, reject ) => {
window
.fetch( block.attributes.url )
.then( ( response ) => response.blob() )
.then( ( blob ) =>
mediaUpload( {
filesList: [ blob ],
additionalData: {
title: block.attributes.title,
alt_text: block.attributes.alt,
caption: block.attributes.caption,
type: blockType,
},
onFileChange: ( [ media ] ) => {
if ( media.id ) {
resolve( media );
}
},
allowedTypes,
onError: reject,
} )
)
.catch( () => resolve( block.attributes.url ) );
} );
} )
).catch( ( err ) => onError( err ) );

if ( multiple ) {
Expand Down
2 changes: 1 addition & 1 deletion packages/block-library/src/cover/edit/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ function CoverEdit( {
averageBackgroundColor
);

if ( backgroundType === IMAGE_BACKGROUND_TYPE && mediaAttributes.id ) {
if ( backgroundType === IMAGE_BACKGROUND_TYPE && mediaAttributes?.id ) {
const { imageDefaultSize } = getSettings();

// Try to use the previous selected image size if it's available
Expand Down
20 changes: 10 additions & 10 deletions packages/block-library/src/cover/shared.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export function dimRatioToClass( ratio ) {
}

export function attributesFromMedia( media ) {
if ( ! media || ! media.url ) {
if ( ! media || ( ! media.url && ! media.src ) ) {
return {
url: undefined,
id: undefined,
Expand All @@ -52,23 +52,23 @@ export function attributesFromMedia( media ) {
if ( media.media_type === IMAGE_BACKGROUND_TYPE ) {
mediaType = IMAGE_BACKGROUND_TYPE;
} else {
// only images and videos are accepted so if the media_type is not an image we can assume it is a video.
// Only images and videos are accepted so if the media_type is not an image we can assume it is a video.
// Videos contain the media type of 'file' in the object returned from the rest api.
mediaType = VIDEO_BACKGROUND_TYPE;
}
} else {
// For media selections originated from existing files in the media library.
if (
media.type !== IMAGE_BACKGROUND_TYPE &&
media.type !== VIDEO_BACKGROUND_TYPE
) {
return;
}
} else if (
media.type &&
( media.type === IMAGE_BACKGROUND_TYPE ||
media.type === VIDEO_BACKGROUND_TYPE )
) {
mediaType = media.type;
} else {
return;
}

return {
url: media.url,
url: media.url || media.src,
id: media.id,
alt: media?.alt,
backgroundType: mediaType,
Expand Down

0 comments on commit 134e4b4

Please sign in to comment.