Skip to content

Commit

Permalink
Fix dropping media from inserter into Cover block.
Browse files Browse the repository at this point in the history
  • Loading branch information
tellthemachines committed Nov 17, 2024
1 parent 2e4b8d8 commit 40b84f9
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 12 deletions.
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 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
26 changes: 16 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,29 @@ 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;
// If there's a url or src but no type, it's probably coming from the inserter so
// we still want to return what data we have.
} else if ( media.url ) {
mediaType = IMAGE_BACKGROUND_TYPE;
} else if ( media.src ) {
mediaType = VIDEO_BACKGROUND_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 40b84f9

Please sign in to comment.