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

Gallery Block Refactor: Account for null image ids in gallery migrations #27855

Merged
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
65 changes: 25 additions & 40 deletions packages/block-library/src/gallery/deprecated.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,26 @@ export function getHrefAndDestination( image, destination ) {
return {};
}

/**
* Gets an Image block from gallery image data
*
* Used to migrate Galleries to nested Image InnerBlocks.
*
* @param {Object} image Image properties.
* @param {string} sizeSlug Gallery sizeSlug attribute.
* @param {string} linkTo Gallery linkTo attribute.
* @return {Object} Image block.
*/
export function getImageBlock( image, sizeSlug, linkTo ) {
return createBlock( 'core/image', {
...( image.id && { id: parseInt( image.id ) } ),
url: image.url,
alt: image.alt,
caption: image.caption,
sizeSlug,
...getHrefAndDestination( image, linkTo ),
} );
}
const v1 = {
attributes: {
images: {
Expand Down Expand Up @@ -173,14 +193,7 @@ const v1 = {
},
migrate( { images, imageCrop, linkTo, sizeSlug, columns, caption } ) {
const imageBlocks = images.map( ( image ) => {
return createBlock( 'core/image', {
id: parseInt( image.id ),
url: image.url,
alt: image.alt,
caption: image.caption,
sizeSlug,
...getHrefAndDestination( image, linkTo ),
} );
return getImageBlock( image, sizeSlug, linkTo );
} );
return [
{
Expand Down Expand Up @@ -456,14 +469,7 @@ const v3 = {
},
migrate( { images, imageCrop, linkTo, sizeSlug, columns, caption } ) {
const imageBlocks = images.map( ( image ) => {
return createBlock( 'core/image', {
id: parseInt( image.id ),
url: image.url,
alt: image.alt,
caption: image.caption,
sizeSlug,
...getHrefAndDestination( image, linkTo ),
} );
return getImageBlock( image, sizeSlug, linkTo );
} );

return [
Expand Down Expand Up @@ -552,14 +558,7 @@ const v4 = {
},
migrate( { images, imageCrop, linkTo, sizeSlug, columns, caption } ) {
const imageBlocks = images.map( ( image ) => {
return createBlock( 'core/image', {
id: parseInt( image.id ),
url: image.url,
alt: image.alt,
caption: image.caption,
sizeSlug,
...getHrefAndDestination( image, linkTo ),
} );
return getImageBlock( image, sizeSlug, linkTo );
} );

return [
Expand Down Expand Up @@ -743,14 +742,7 @@ const v5 = {
linkTo = 'none';
}
const imageBlocks = attributes.images.map( ( image ) => {
return createBlock( 'core/image', {
id: parseInt( image.id ),
url: image.url,
alt: image.alt,
caption: image.caption,
sizeSlug: attributes.sizeSlug,
...getHrefAndDestination( image, linkTo ),
} );
return getImageBlock( image, attributes.sizeSlug, linkTo );
} );
return [
{
Expand Down Expand Up @@ -1004,14 +996,7 @@ const v6 = {
linkTo = 'media';
}
const imageBlocks = images.map( ( image ) => {
return createBlock( 'core/image', {
id: parseInt( image.id ),
url: image.url,
alt: image.alt,
caption: image.caption,
sizeSlug,
...getHrefAndDestination( image, linkTo ),
} );
return getImageBlock( image, sizeSlug, linkTo );
} );
return [
{
Expand Down