diff --git a/projects/plugins/jetpack/changelog/fix-tiled-gallery-crash b/projects/plugins/jetpack/changelog/fix-tiled-gallery-crash new file mode 100644 index 000000000000..d96772ef47c6 --- /dev/null +++ b/projects/plugins/jetpack/changelog/fix-tiled-gallery-crash @@ -0,0 +1,4 @@ +Significance: patch +Type: bugfix + +Tiled Gallery: prevent editor crash when transforming a core gallery block. diff --git a/projects/plugins/jetpack/extensions/blocks/tiled-gallery/editor.js b/projects/plugins/jetpack/extensions/blocks/tiled-gallery/editor.js index f05ed8021509..4bc8a8d6a662 100644 --- a/projects/plugins/jetpack/extensions/blocks/tiled-gallery/editor.js +++ b/projects/plugins/jetpack/extensions/blocks/tiled-gallery/editor.js @@ -69,7 +69,7 @@ const exampleAttributes = { * @param {Array} images - Array of image objects * @return {Array} Array of image objects which have id and url */ -function getValidImages( images ) { +function getValidImages( images = [] ) { return images.filter( ( { id, url } ) => id && url ); } registerJetpackBlockFromMetadata( metadata, { @@ -98,7 +98,17 @@ registerJetpackBlockFromMetadata( metadata, { { type: 'block', blocks: [ 'core/gallery', 'jetpack/slideshow' ], - transform: ( { images } ) => { + transform: ( { images = [] }, innerBlocks ) => { + if ( ! images.length && innerBlocks?.length ) { + images = innerBlocks + .filter( b => b.name === 'core/image' && b.attributes?.url ) + .map( ( { attributes } ) => ( { + id: attributes.id, + url: attributes.url, + link: attributes.href, + alt: attributes.alt, + } ) ); + } const validImages = getValidImages( images ); if ( validImages.length > 0 ) { return createBlock( metadata.name, {