From 745507f56fd4ccd34336a7ad61b063ad831bfe8d Mon Sep 17 00:00:00 2001 From: Miguel Fonseca Date: Mon, 29 Oct 2018 16:07:51 +0000 Subject: [PATCH 1/4] Gallery: don't source `images` attribute in HTML --- packages/block-library/src/gallery/index.js | 56 +++++++++++++++++++-- 1 file changed, 51 insertions(+), 5 deletions(-) diff --git a/packages/block-library/src/gallery/index.js b/packages/block-library/src/gallery/index.js index 137090eaf339b..b06bafdc8fa84 100644 --- a/packages/block-library/src/gallery/index.js +++ b/packages/block-library/src/gallery/index.js @@ -17,12 +17,12 @@ import { G, Path, SVG } from '@wordpress/components'; */ import { default as edit, defaultColumnsNumber, pickRelevantMediaFiles } from './edit'; +// Attributes are to be saved as JSON in the block boundary for easier +// server-side access. const blockAttributes = { images: { type: 'array', default: [], - source: 'query', - selector: 'ul.wp-block-gallery .blocks-gallery-item', query: { url: { source: 'attribute', @@ -64,6 +64,17 @@ const blockAttributes = { }, }; +// Identical schema, but wherein `images` are not saved in the block boundary, +// and instead are sourced in the block's inner HTML. +const deprecatedBlockAttributes = { + ...blockAttributes, + images: { + ...blockAttributes.images, + source: 'query', + selector: 'ul.wp-block-gallery .blocks-gallery-item', + }, +}; + export const name = 'core/gallery'; export const settings = { @@ -199,7 +210,42 @@ export const settings = { deprecated: [ { - attributes: blockAttributes, + attributes: deprecatedBlockAttributes, + save( { attributes } ) { + const { images, columns = defaultColumnsNumber( attributes ), imageCrop, linkTo } = attributes; + return ( + + ); + }, + }, + { + attributes: deprecatedBlockAttributes, save( { attributes } ) { const { images, columns = defaultColumnsNumber( attributes ), imageCrop, linkTo } = attributes; return ( @@ -235,9 +281,9 @@ export const settings = { }, { attributes: { - ...blockAttributes, + ...deprecatedBlockAttributes, images: { - ...blockAttributes.images, + ...deprecatedBlockAttributes.images, selector: 'div.wp-block-gallery figure.blocks-gallery-image img', }, align: { From a27cc2f36ef5d29877fdf7f4341d120609c207c4 Mon Sep 17 00:00:00 2001 From: Miguel Fonseca Date: Mon, 29 Oct 2018 16:12:27 +0000 Subject: [PATCH 2/4] Factor out `save` method --- packages/block-library/src/gallery/index.js | 99 ++++++++------------- 1 file changed, 35 insertions(+), 64 deletions(-) diff --git a/packages/block-library/src/gallery/index.js b/packages/block-library/src/gallery/index.js index b06bafdc8fa84..b7fe92407cbec 100644 --- a/packages/block-library/src/gallery/index.js +++ b/packages/block-library/src/gallery/index.js @@ -75,6 +75,39 @@ const deprecatedBlockAttributes = { }, }; +function save( { attributes } ) { + const { images, columns = defaultColumnsNumber( attributes ), imageCrop, linkTo } = attributes; + return ( + + ); +} + export const name = 'core/gallery'; export const settings = { @@ -175,74 +208,12 @@ export const settings = { edit, - save( { attributes } ) { - const { images, columns = defaultColumnsNumber( attributes ), imageCrop, linkTo } = attributes; - return ( - - ); - }, + save, deprecated: [ { attributes: deprecatedBlockAttributes, - save( { attributes } ) { - const { images, columns = defaultColumnsNumber( attributes ), imageCrop, linkTo } = attributes; - return ( - - ); - }, + save, }, { attributes: deprecatedBlockAttributes, From 29ad70046ba0b171382782aa420da72e61d0f521 Mon Sep 17 00:00:00 2001 From: Miguel Fonseca Date: Mon, 29 Oct 2018 16:38:41 +0000 Subject: [PATCH 3/4] Image: don't source attributes in HTML --- packages/block-library/src/image/index.js | 137 +++++++++++++--------- 1 file changed, 82 insertions(+), 55 deletions(-) diff --git a/packages/block-library/src/image/index.js b/packages/block-library/src/image/index.js index f7f6e17f6b66c..7e5a9c918d198 100644 --- a/packages/block-library/src/image/index.js +++ b/packages/block-library/src/image/index.js @@ -31,26 +31,16 @@ export const name = 'core/image'; const blockAttributes = { url: { type: 'string', - source: 'attribute', - selector: 'img', - attribute: 'src', }, alt: { type: 'string', - source: 'attribute', - selector: 'img', - attribute: 'alt', default: '', }, caption: { - source: 'html', - selector: 'figcaption', + type: 'string', }, href: { type: 'string', - source: 'attribute', - selector: 'figure > a', - attribute: 'href', }, id: { type: 'number', @@ -70,6 +60,37 @@ const blockAttributes = { }, linkTarget: { type: 'string', + }, +}; + +const deprecatedBlockAttributes = { + ...blockAttributes, + url: { + ...blockAttributes.url, + source: 'attribute', + selector: 'img', + attribute: 'src', + }, + alt: { + ...blockAttributes.alt, + source: 'attribute', + selector: 'img', + attribute: 'alt', + }, + caption: { + ...blockAttributes.caption, + type: undefined, + source: 'html', + selector: 'figcaption', + }, + href: { + ...blockAttributes.href, + source: 'attribute', + selector: 'figure > a', + attribute: 'href', + }, + linkTarget: { + ...blockAttributes.linkTarget, source: 'attribute', selector: 'figure > a', attribute: 'target', @@ -99,6 +120,48 @@ const schema = { }, }; +function save( { attributes } ) { + const { url, alt, caption, align, href, width, height, id, linkTarget } = attributes; + + const classes = classnames( { + [ `align${ align }` ]: align, + 'is-resized': width || height, + } ); + + const image = ( + { + ); + + const figure = ( + + { href ? { image } : image } + { ! RichText.isEmpty( caption ) && } + + ); + + if ( 'left' === align || 'right' === align || 'center' === align ) { + return ( +
+
+ { figure } +
+
+ ); + } + + return ( +
+ { figure } +
+ ); +} + export const settings = { title: __( 'Image' ), @@ -212,51 +275,15 @@ export const settings = { edit, - save( { attributes } ) { - const { url, alt, caption, align, href, width, height, id, linkTarget } = attributes; - - const classes = classnames( { - [ `align${ align }` ]: align, - 'is-resized': width || height, - } ); - - const image = ( - { - ); - - const figure = ( - - { href ? { image } : image } - { ! RichText.isEmpty( caption ) && } - - ); - - if ( 'left' === align || 'right' === align || 'center' === align ) { - return ( -
-
- { figure } -
-
- ); - } - - return ( -
- { figure } -
- ); - }, + save, deprecated: [ { - attributes: blockAttributes, + attributes: deprecatedBlockAttributes, + save, + }, + { + attributes: deprecatedBlockAttributes, save( { attributes } ) { const { url, alt, caption, align, href, width, height, id } = attributes; @@ -284,7 +311,7 @@ export const settings = { }, }, { - attributes: blockAttributes, + attributes: deprecatedBlockAttributes, save( { attributes } ) { const { url, alt, caption, align, href, width, height, id } = attributes; @@ -307,7 +334,7 @@ export const settings = { }, }, { - attributes: blockAttributes, + attributes: deprecatedBlockAttributes, save( { attributes } ) { const { url, alt, caption, align, href, width, height } = attributes; const extraImageProps = width || height ? { width, height } : {}; From 6dc6d88b7d2b4320adb31a88f4bad630306436a3 Mon Sep 17 00:00:00 2001 From: Miguel Fonseca Date: Mon, 29 Oct 2018 17:40:23 +0000 Subject: [PATCH 4/4] Update integration tests for Gallery, Image --- test/integration/full-content/fixtures/core__gallery.html | 2 +- .../full-content/fixtures/core__gallery.parsed.json | 2 +- .../full-content/fixtures/core__gallery.serialized.html | 2 +- .../full-content/fixtures/core__gallery__columns.html | 2 +- .../full-content/fixtures/core__gallery__columns.parsed.json | 4 ++++ .../fixtures/core__gallery__columns.serialized.html | 2 +- test/integration/full-content/fixtures/core__image.html | 2 +- .../full-content/fixtures/core__image.parsed.json | 5 ++++- .../full-content/fixtures/core__image.serialized.html | 2 +- .../full-content/fixtures/core__image__attachment-link.html | 2 +- .../fixtures/core__image__attachment-link.parsed.json | 3 +++ .../fixtures/core__image__attachment-link.serialized.html | 2 +- .../full-content/fixtures/core__image__center-caption.html | 2 +- .../fixtures/core__image__center-caption.parsed.json | 2 ++ .../fixtures/core__image__center-caption.serialized.html | 2 +- .../full-content/fixtures/core__image__custom-link.html | 2 +- .../fixtures/core__image__custom-link.parsed.json | 3 +++ .../fixtures/core__image__custom-link.serialized.html | 2 +- .../full-content/fixtures/core__image__media-link.html | 2 +- .../fixtures/core__image__media-link.parsed.json | 3 +++ .../fixtures/core__image__media-link.serialized.html | 2 +- 21 files changed, 34 insertions(+), 16 deletions(-) diff --git a/test/integration/full-content/fixtures/core__gallery.html b/test/integration/full-content/fixtures/core__gallery.html index 5e48c7e66351f..8670368046b38 100644 --- a/test/integration/full-content/fixtures/core__gallery.html +++ b/test/integration/full-content/fixtures/core__gallery.html @@ -1,4 +1,4 @@ - +