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, Image: Move attributes to block boundary (JSON) #11212

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
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
91 changes: 54 additions & 37 deletions packages/block-library/src/gallery/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Copy link
Member

@jorgefilipecosta jorgefilipecosta Oct 29, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we remove all this query data specifying each property of the object? It looks like it is not used at all now (because the source is not a query). If we keep them it may give the idea that it is some "way" to validate the properties of the image object, but that is not the case. If the object contains other properties they are passed to the block anyway.

url: {
source: 'attribute',
Expand Down Expand Up @@ -64,6 +64,50 @@ 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',
},
};

function save( { attributes } ) {
const { images, columns = defaultColumnsNumber( attributes ), imageCrop, linkTo } = attributes;
return (
<ul className={ `columns-${ columns } ${ imageCrop ? 'is-cropped' : '' }` } >
{ images.map( ( image ) => {
let href;

switch ( linkTo ) {
case 'media':
href = image.url;
break;
case 'attachment':
href = image.link;
break;
}

const img = <img src={ image.url } alt={ image.alt } data-id={ image.id } data-link={ image.link } className={ image.id ? `wp-image-${ image.id }` : null } />;

return (
<li key={ image.id || image.url } className="blocks-gallery-item">
<figure>
{ href ? <a href={ href }>{ img }</a> : img }
{ image.caption && image.caption.length > 0 && (
<RichText.Content tagName="figcaption" value={ image.caption } />
) }
</figure>
</li>
);
} ) }
</ul>
);
}

export const name = 'core/gallery';

export const settings = {
Expand Down Expand Up @@ -164,42 +208,15 @@ export const settings = {

edit,

save( { attributes } ) {
const { images, columns = defaultColumnsNumber( attributes ), imageCrop, linkTo } = attributes;
return (
<ul className={ `columns-${ columns } ${ imageCrop ? 'is-cropped' : '' }` } >
{ images.map( ( image ) => {
let href;

switch ( linkTo ) {
case 'media':
href = image.url;
break;
case 'attachment':
href = image.link;
break;
}

const img = <img src={ image.url } alt={ image.alt } data-id={ image.id } data-link={ image.link } className={ image.id ? `wp-image-${ image.id }` : null } />;

return (
<li key={ image.id || image.url } className="blocks-gallery-item">
<figure>
{ href ? <a href={ href }>{ img }</a> : img }
{ image.caption && image.caption.length > 0 && (
<RichText.Content tagName="figcaption" value={ image.caption } />
) }
</figure>
</li>
);
} ) }
</ul>
);
},
save,

deprecated: [
{
attributes: blockAttributes,
attributes: deprecatedBlockAttributes,
save,
},
{
attributes: deprecatedBlockAttributes,
save( { attributes } ) {
const { images, columns = defaultColumnsNumber( attributes ), imageCrop, linkTo } = attributes;
return (
Expand Down Expand Up @@ -235,9 +252,9 @@ export const settings = {
},
{
attributes: {
...blockAttributes,
...deprecatedBlockAttributes,
images: {
...blockAttributes.images,
...deprecatedBlockAttributes.images,
selector: 'div.wp-block-gallery figure.blocks-gallery-image img',
},
align: {
Expand Down
137 changes: 82 additions & 55 deletions packages/block-library/src/image/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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',
Expand Down Expand Up @@ -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 = (
<img
src={ url }
alt={ alt }
className={ id ? `wp-image-${ id }` : null }
width={ width }
height={ height }
/>
);

const figure = (
<Fragment>
{ href ? <a href={ href } target={ linkTarget } rel={ linkTarget === '_blank' ? 'noreferrer noopener' : undefined }>{ image }</a> : image }
{ ! RichText.isEmpty( caption ) && <RichText.Content tagName="figcaption" value={ caption } /> }
</Fragment>
);

if ( 'left' === align || 'right' === align || 'center' === align ) {
return (
<div>
<figure className={ classes }>
{ figure }
</figure>
</div>
);
}

return (
<figure className={ classes }>
{ figure }
</figure>
);
}

export const settings = {
title: __( 'Image' ),

Expand Down Expand Up @@ -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 = (
<img
src={ url }
alt={ alt }
className={ id ? `wp-image-${ id }` : null }
width={ width }
height={ height }
/>
);

const figure = (
<Fragment>
{ href ? <a href={ href } target={ linkTarget } rel={ linkTarget === '_blank' ? 'noreferrer noopener' : undefined }>{ image }</a> : image }
{ ! RichText.isEmpty( caption ) && <RichText.Content tagName="figcaption" value={ caption } /> }
</Fragment>
);

if ( 'left' === align || 'right' === align || 'center' === align ) {
return (
<div>
<figure className={ classes }>
{ figure }
</figure>
</div>
);
}

return (
<figure className={ classes }>
{ figure }
</figure>
);
},
save,

deprecated: [
{
attributes: blockAttributes,
attributes: deprecatedBlockAttributes,
save,
},
{
attributes: deprecatedBlockAttributes,
save( { attributes } ) {
const { url, alt, caption, align, href, width, height, id } = attributes;

Expand Down Expand Up @@ -284,7 +311,7 @@ export const settings = {
},
},
{
attributes: blockAttributes,
attributes: deprecatedBlockAttributes,
save( { attributes } ) {
const { url, alt, caption, align, href, width, height, id } = attributes;

Expand All @@ -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 } : {};
Expand Down
2 changes: 1 addition & 1 deletion test/integration/full-content/fixtures/core__gallery.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<!-- wp:core/gallery -->
<!-- wp:core/gallery {"images":[{"url":"https://cldup.com/uuUqE_dXzy.jpg","alt":"title","caption":""},{"url":"http://google.com/hi.png","alt":"title","caption":""}]} -->
<ul class="wp-block-gallery columns-2 is-cropped">
<li class="blocks-gallery-item">
<figure>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[
{
"blockName": "core/gallery",
"attrs": {},
"attrs": {"images":[{"url":"https://cldup.com/uuUqE_dXzy.jpg","alt":"title","caption":""},{"url":"http://google.com/hi.png","alt":"title","caption":""}]},
"innerBlocks": [],
"innerHTML": "\n<ul class=\"wp-block-gallery columns-2 is-cropped\">\n\t<li class=\"blocks-gallery-item\">\n\t\t<figure>\n\t\t\t<img src=\"https://cldup.com/uuUqE_dXzy.jpg\" alt=\"title\" />\n\t\t</figure>\n\t</li>\n\t<li class=\"blocks-gallery-item\">\n\t\t<figure>\n\t\t\t<img src=\"http://google.com/hi.png\" alt=\"title\" />\n\t\t</figure>\n\t</li>\n</ul>\n"
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<!-- wp:gallery -->
<!-- wp:gallery {"images":[{"url":"https://cldup.com/uuUqE_dXzy.jpg","alt":"title","caption":""},{"url":"http://google.com/hi.png","alt":"title","caption":""}]} -->
<ul class="wp-block-gallery columns-2 is-cropped"><li class="blocks-gallery-item"><figure><img src="https://cldup.com/uuUqE_dXzy.jpg" alt="title"/></figure></li><li class="blocks-gallery-item"><figure><img src="http://google.com/hi.png" alt="title"/></figure></li></ul>
<!-- /wp:gallery -->
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<!-- wp:core/gallery {"columns":1} -->
<!-- wp:core/gallery {"images":[{"url":"https://cldup.com/uuUqE_dXzy.jpg","alt":"title","caption":""},{"url":"http://google.com/hi.png","alt":"title","caption":""}],"columns":1} -->
<ul class="wp-block-gallery columns-1 is-cropped">
<li class="blocks-gallery-item">
<figure>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
{
"blockName": "core/gallery",
"attrs": {
"images": [
{"url":"https://cldup.com/uuUqE_dXzy.jpg","alt":"title","caption":""},
{"url":"http://google.com/hi.png","alt":"title","caption":""}
],
"columns": 1
},
"innerBlocks": [],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<!-- wp:gallery {"columns":1} -->
<!-- wp:gallery {"images":[{"url":"https://cldup.com/uuUqE_dXzy.jpg","alt":"title","caption":""},{"url":"http://google.com/hi.png","alt":"title","caption":""}],"columns":1} -->
<ul class="wp-block-gallery columns-1 is-cropped"><li class="blocks-gallery-item"><figure><img src="https://cldup.com/uuUqE_dXzy.jpg" alt="title"/></figure></li><li class="blocks-gallery-item"><figure><img src="http://google.com/hi.png" alt="title"/></figure></li></ul>
<!-- /wp:gallery -->
2 changes: 1 addition & 1 deletion test/integration/full-content/fixtures/core__image.html
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<!-- wp:core/image -->
<!-- wp:core/image {"url":"https://cldup.com/uuUqE_dXzy.jpg","caption":""} -->
<figure class="wp-block-image"><img src="https://cldup.com/uuUqE_dXzy.jpg" alt="" /></figure>
<!-- /wp:core/image -->
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
[
{
"blockName": "core/image",
"attrs": {},
"attrs": {
"url": "https://cldup.com/uuUqE_dXzy.jpg",
"caption": ""
},
"innerBlocks": [],
"innerHTML": "\n<figure class=\"wp-block-image\"><img src=\"https://cldup.com/uuUqE_dXzy.jpg\" alt=\"\" /></figure>\n"
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<!-- wp:image -->
<!-- wp:image {"url":"https://cldup.com/uuUqE_dXzy.jpg","caption":""} -->
<figure class="wp-block-image"><img src="https://cldup.com/uuUqE_dXzy.jpg" alt=""/></figure>
<!-- /wp:image -->
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<!-- wp:core/image {"linkDestination":"attachment"} -->
<!-- wp:core/image {"url":"https://cldup.com/uuUqE_dXzy.jpg","caption":"","href":"http://localhost:8888/?attachment_id=7","linkDestination":"attachment"} -->
<figure class="wp-block-image"><a href="http://localhost:8888/?attachment_id=7"><img src="https://cldup.com/uuUqE_dXzy.jpg" alt="" /></a></figure>
<!-- /wp:core/image -->
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
{
"blockName": "core/image",
"attrs": {
"url":"https://cldup.com/uuUqE_dXzy.jpg",
"caption":"",
"href":"http://localhost:8888/?attachment_id=7",
"linkDestination": "attachment"
},
"innerBlocks": [],
Expand Down
Loading