diff --git a/packages/block-library/src/gallery/block.json b/packages/block-library/src/gallery/block.json index 699a5fa1d14be5..0879d4af75573c 100644 --- a/packages/block-library/src/gallery/block.json +++ b/packages/block-library/src/gallery/block.json @@ -4,7 +4,7 @@ "attributes": { "images": { "type": "array", - "default": [ ], + "default": [], "source": "query", "selector": ".blocks-gallery-item", "query": { @@ -43,7 +43,10 @@ }, "ids": { "type": "array", - "default": [ ] + "items": { + "type": "number" + }, + "default": [] }, "columns": { "type": "number", diff --git a/packages/block-library/src/gallery/deprecated.js b/packages/block-library/src/gallery/deprecated.js index 74af7e3f50b6e7..7b56cf931f5bd1 100644 --- a/packages/block-library/src/gallery/deprecated.js +++ b/packages/block-library/src/gallery/deprecated.js @@ -15,6 +15,126 @@ import { RichText } from '@wordpress/block-editor'; import { defaultColumnsNumber } from './shared'; const deprecated = [ + { + attributes: { + images: { + type: 'array', + default: [], + source: 'query', + selector: '.blocks-gallery-item', + query: { + url: { + source: 'attribute', + selector: 'img', + attribute: 'src', + }, + fullUrl: { + source: 'attribute', + selector: 'img', + attribute: 'data-full-url', + }, + link: { + source: 'attribute', + selector: 'img', + attribute: 'data-link', + }, + alt: { + source: 'attribute', + selector: 'img', + attribute: 'alt', + default: '', + }, + id: { + source: 'attribute', + selector: 'img', + attribute: 'data-id', + }, + caption: { + type: 'string', + source: 'html', + selector: '.blocks-gallery-item__caption', + }, + }, + }, + ids: { + type: 'array', + default: [], + }, + columns: { + type: 'number', + }, + caption: { + type: 'string', + source: 'html', + selector: '.blocks-gallery-caption', + }, + imageCrop: { + type: 'boolean', + default: true, + }, + linkTo: { + type: 'string', + default: 'none', + }, + }, + isEligible( { ids } ) { + return ids && ids.some( ( id ) => typeof id === 'string' ); + }, + migrate( attributes ) { + return { + ...attributes, + ids: map( attributes.ids, ( id ) => { + const parsedId = parseInt( id, 10 ); + return Number.isInteger( parsedId ) ? parsedId : null; + } ), + }; + }, + save( { attributes } ) { + const { images, columns = defaultColumnsNumber( attributes ), imageCrop, caption, linkTo } = attributes; + + return ( +
+ + { ! RichText.isEmpty( caption ) && } +
+ ); + }, + }, { attributes: { images: { diff --git a/packages/e2e-tests/fixtures/blocks/core__gallery__deprecated-4.html b/packages/e2e-tests/fixtures/blocks/core__gallery__deprecated-4.html new file mode 100644 index 00000000000000..b9dd47cb63e5d3 --- /dev/null +++ b/packages/e2e-tests/fixtures/blocks/core__gallery__deprecated-4.html @@ -0,0 +1,21 @@ + + + \ No newline at end of file diff --git a/packages/e2e-tests/fixtures/blocks/core__gallery__deprecated-4.json b/packages/e2e-tests/fixtures/blocks/core__gallery__deprecated-4.json new file mode 100644 index 00000000000000..4270b4d419c2d5 --- /dev/null +++ b/packages/e2e-tests/fixtures/blocks/core__gallery__deprecated-4.json @@ -0,0 +1,39 @@ +[ + { + "clientId": "_clientId_0", + "name": "core/gallery", + "isValid": true, + "attributes": { + "images": [ + { + "url": "https://sergioestevaofolio.files.wordpress.com/2016/09/cropped-img_9054-1.jpg?w=190", + "alt": "", + "id": "1421", + "caption": "" + }, + { + "url": "https://sergioestevaofolio.files.wordpress.com/2017/09/cropped-l1001498-1.jpg?w=580", + "alt": "", + "id": "1440", + "caption": "" + }, + { + "url": "https://sergioestevaofolio.files.wordpress.com/2017/05/cropped-l1005945-2-2.jpg?w=580", + "alt": "", + "id": "1362", + "caption": "" + } + ], + "ids": [ + 1421, + 1440, + 1362 + ], + "caption": "", + "imageCrop": true, + "linkTo": "none" + }, + "innerBlocks": [], + "originalContent": "" + } +] diff --git a/packages/e2e-tests/fixtures/blocks/core__gallery__deprecated-4.parsed.json b/packages/e2e-tests/fixtures/blocks/core__gallery__deprecated-4.parsed.json new file mode 100644 index 00000000000000..abed1ba09ec5ad --- /dev/null +++ b/packages/e2e-tests/fixtures/blocks/core__gallery__deprecated-4.parsed.json @@ -0,0 +1,17 @@ +[ + { + "blockName": "core/gallery", + "attrs": { + "ids": [ + "1421", + "1440", + "1362" + ] + }, + "innerBlocks": [], + "innerHTML": "\n\n", + "innerContent": [ + "\n\n" + ] + } +] diff --git a/packages/e2e-tests/fixtures/blocks/core__gallery__deprecated-4.serialized.html b/packages/e2e-tests/fixtures/blocks/core__gallery__deprecated-4.serialized.html new file mode 100644 index 00000000000000..b97fe94748b244 --- /dev/null +++ b/packages/e2e-tests/fixtures/blocks/core__gallery__deprecated-4.serialized.html @@ -0,0 +1,3 @@ + + +