Skip to content

Commit

Permalink
Gallery Block Refactor: Account for null image ids in gallery migrati…
Browse files Browse the repository at this point in the history
…ons (#27855)

Co-authored-by: Glen Davies <glen.davies@a8c.com>
  • Loading branch information
glendaviesnz and Glen Davies committed Apr 11, 2021
1 parent 0938b5e commit 94d9877
Showing 1 changed file with 25 additions and 40 deletions.
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

0 comments on commit 94d9877

Please sign in to comment.