Skip to content

Commit

Permalink
Add deprecation for moving border radius to inner image element
Browse files Browse the repository at this point in the history
New border block support styles and classes need to be on the inner img element. As a result it makes sense to move the border radius style with them. This requires a deprecation which this commit provides along with new fixtures.
  • Loading branch information
aaronrobertshaw committed Apr 30, 2021
1 parent ae4b948 commit 087c2b6
Show file tree
Hide file tree
Showing 5 changed files with 146 additions and 1 deletion.
99 changes: 98 additions & 1 deletion packages/block-library/src/image/deprecated.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@
* External dependencies
*/
import classnames from 'classnames';
import { isEmpty } from 'lodash';

/**
* WordPress dependencies
*/
import { RichText } from '@wordpress/block-editor';
import { RichText, useBlockProps } from '@wordpress/block-editor';

const blockAttributes = {
align: {
Expand All @@ -30,6 +31,12 @@ const blockAttributes = {
source: 'html',
selector: 'figcaption',
},
title: {
type: 'string',
source: 'attribute',
selector: 'img',
attribute: 'title',
},
href: {
type: 'string',
source: 'attribute',
Expand Down Expand Up @@ -57,6 +64,9 @@ const blockAttributes = {
height: {
type: 'number',
},
sizeSlug: {
type: 'string',
},
linkDestination: {
type: 'string',
},
Expand Down Expand Up @@ -189,6 +199,93 @@ const deprecated = [
);
},
},
{
attributes: blockAttributes,
supports: {
anchor: true,
color: {
__experimentalDuotone: 'img',
text: false,
background: false,
__experimentalBorder: {
radius: true,
},
},
},
save( { attributes } ) {
const {
url,
alt,
caption,
align,
href,
rel,
linkClass,
width,
height,
id,
linkTarget,
sizeSlug,
title,
} = attributes;

const newRel = isEmpty( rel ) ? undefined : rel;

const classes = classnames( {
[ `align${ align }` ]: align,
[ `size-${ sizeSlug }` ]: sizeSlug,
'is-resized': width || height,
} );

const image = (
<img
src={ url }
alt={ alt }
className={ id ? `wp-image-${ id }` : null }
width={ width }
height={ height }
title={ title }
/>
);

const figure = (
<>
{ href ? (
<a
className={ linkClass }
href={ href }
target={ linkTarget }
rel={ newRel }
>
{ image }
</a>
) : (
image
) }
{ ! RichText.isEmpty( caption ) && (
<RichText.Content
tagName="figcaption"
value={ caption }
/>
) }
</>
);

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

return (
<figure { ...useBlockProps.save( { className: classes } ) }>
{ figure }
</figure>
);
},
},
];

export default deprecated;
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<!-- wp:image {"id":260,"sizeSlug":"large","linkDestination":"none","style":{"border":{"radius":50}}} -->
<figure class="wp-block-image size-large" style="border-radius:50px"><img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAACklEQVR4nGMAAQAABQABDQottAAAAABJRU5ErkJggg==" alt="" class="wp-image-260"/></figure>
<!-- /wp:image -->
22 changes: 22 additions & 0 deletions packages/e2e-tests/fixtures/blocks/core__image__deprecated-4.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
[
{
"clientId": "_clientId_0",
"name": "core/image",
"isValid": true,
"attributes": {
"url": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAACklEQVR4nGMAAQAABQABDQottAAAAABJRU5ErkJggg==",
"alt": "",
"caption": "",
"id": 260,
"sizeSlug": "large",
"linkDestination": "none",
"style": {
"border": {
"radius": 50
}
}
},
"innerBlocks": [],
"originalContent": "<figure class=\"wp-block-image size-large\" style=\"border-radius:50px\"><img src=\"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAACklEQVR4nGMAAQAABQABDQottAAAAABJRU5ErkJggg==\" alt=\"\" class=\"wp-image-260\"/></figure>"
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
[
{
"blockName": "core/image",
"attrs": {
"id": 260,
"sizeSlug": "large",
"linkDestination": "none",
"style": {
"border": {
"radius": 50
}
}
},
"innerBlocks": [],
"innerHTML": "\n<figure class=\"wp-block-image size-large\" style=\"border-radius:50px\"><img src=\"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAACklEQVR4nGMAAQAABQABDQottAAAAABJRU5ErkJggg==\" alt=\"\" class=\"wp-image-260\"/></figure>\n",
"innerContent": [
"\n<figure class=\"wp-block-image size-large\" style=\"border-radius:50px\"><img src=\"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAACklEQVR4nGMAAQAABQABDQottAAAAABJRU5ErkJggg==\" alt=\"\" class=\"wp-image-260\"/></figure>\n"
]
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<!-- wp:image {"id":260,"sizeSlug":"large","linkDestination":"none","style":{"border":{"radius":50}}} -->
<figure class="wp-block-image size-large"><img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAACklEQVR4nGMAAQAABQABDQottAAAAABJRU5ErkJggg==" alt="" class="wp-image-260" style="border-radius:50px"/></figure>
<!-- /wp:image -->

0 comments on commit 087c2b6

Please sign in to comment.