Skip to content

Commit

Permalink
add image width and height via css inline style, update width and hei…
Browse files Browse the repository at this point in the history
…ght attrs to be string
  • Loading branch information
draganescu committed Jul 5, 2023
1 parent 09bf154 commit 20b9223
Show file tree
Hide file tree
Showing 3 changed files with 181 additions and 6 deletions.
4 changes: 2 additions & 2 deletions packages/block-library/src/image/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,10 @@
"__experimentalRole": "content"
},
"width": {
"type": "number"
"type": "string"
},
"height": {
"type": "number"
"type": "string"
},
"aspectRatio": {
"type": "string"
Expand Down
179 changes: 177 additions & 2 deletions packages/block-library/src/image/deprecated.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ import classnames from 'classnames';
/**
* WordPress dependencies
*/
import { RichText, useBlockProps } from '@wordpress/block-editor';
import {
RichText,
useBlockProps,
__experimentalGetElementClassName as getBorderClassesAndStyles,
} from '@wordpress/block-editor';

/**
* Deprecation for adding the `wp-image-${id}` class to the image block for
Expand Down Expand Up @@ -539,4 +543,175 @@ const v5 = {
},
};

export default [ v5, v4, v3, v2, v1 ];
/**
* Deprecation for adding width and height as style rules on the inner img.
* It also updates the widht and height attributes to be strings instead of numbers.
*
* @see https://github.com/WordPress/gutenberg/pull/31366
*/
const v6 = {
attributes: {
align: {
type: 'string',
},
url: {
type: 'string',
source: 'attribute',
selector: 'img',
attribute: 'src',
__experimentalRole: 'content',
},
alt: {
type: 'string',
source: 'attribute',
selector: 'img',
attribute: 'alt',
default: '',
__experimentalRole: 'content',
},
caption: {
type: 'string',
source: 'html',
selector: 'figcaption',
__experimentalRole: 'content',
},
title: {
type: 'string',
source: 'attribute',
selector: 'img',
attribute: 'title',
__experimentalRole: 'content',
},
href: {
type: 'string',
source: 'attribute',
selector: 'figure > a',
attribute: 'href',
__experimentalRole: 'content',
},
rel: {
type: 'string',
source: 'attribute',
selector: 'figure > a',
attribute: 'rel',
},
linkClass: {
type: 'string',
source: 'attribute',
selector: 'figure > a',
attribute: 'class',
},
id: {
type: 'number',
__experimentalRole: 'content',
},
width: {
type: 'number',
},
height: {
type: 'number',
},
aspectRatio: {
type: 'string',
},
scale: {
type: 'string',
},
sizeSlug: {
type: 'string',
},
linkDestination: {
type: 'string',
},
linkTarget: {
type: 'string',
source: 'attribute',
selector: 'figure > a',
attribute: 'target',
},
},
save( { attributes } ) {
const {
url,
alt,
caption,
align,
href,
rel,
linkClass,
width,
height,
aspectRatio,
scale,
id,
linkTarget,
sizeSlug,
title,
} = attributes;

const newRel = ! rel ? undefined : rel;
const borderProps = getBorderClassesAndStyles( attributes );

const classes = classnames( {
[ `align${ align }` ]: align,
[ `size-${ sizeSlug }` ]: sizeSlug,
'is-resized': width || height,
'has-custom-border':
!! borderProps.className ||
( borderProps.style &&
Object.keys( borderProps.style ).length > 0 ),
} );

const imageClasses = classnames( borderProps.className, {
[ `wp-image-${ id }` ]: !! id,
} );

const image = (
<img
src={ url }
alt={ alt }
className={ imageClasses || undefined }
style={ {
...borderProps.style,
aspectRatio,
objectFit: scale,
} }
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
className={ getBorderClassesAndStyles( 'caption' ) }
tagName="figcaption"
value={ caption }
/>
) }
</>
);

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

export default [ v6, v5, v4, v3, v2, v1 ];
4 changes: 2 additions & 2 deletions packages/block-library/src/image/save.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ export default function save( { attributes } ) {
...borderProps.style,
aspectRatio,
objectFit: scale,
width,
height,
} }
width={ width }
height={ height }
title={ title }
/>
);
Expand Down

0 comments on commit 20b9223

Please sign in to comment.