Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix dropping media from inserter into Cover block #67056

Merged
merged 2 commits into from
Nov 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 ) ) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we give the media and text block the same treat while we're at it?

if ( ! media || ! media.url ) {

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, that's a good point! Yeah it would be nice. Media & Text seems to be slightly differently broken though 😅 (trying to drag an image into it actually removes the existing image instead of doing nothing) so I'd rather tackle that as a separate PR.

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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❤️

// 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
Loading