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

Block Library: Add caption alignment to the image block. #20015

Closed
wants to merge 1 commit 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
3 changes: 3 additions & 0 deletions packages/block-library/src/image/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
"align": {
"type": "string"
},
"captionAlign": {
"type": "string"
},
"url": {
"type": "string",
"source": "attribute",
Expand Down
18 changes: 18 additions & 0 deletions packages/block-library/src/image/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { compose } from '@wordpress/compose';
import { withSelect, withDispatch } from '@wordpress/data';
import {
BlockAlignmentToolbar,
AlignmentToolbar,
BlockControls,
BlockIcon,
InspectorControls,
Expand Down Expand Up @@ -90,6 +91,7 @@ export class ImageEdit extends Component {
super( ...arguments );
this.updateAlt = this.updateAlt.bind( this );
this.updateAlignment = this.updateAlignment.bind( this );
this.updateCaptionAlignment = this.updateCaptionAlignment.bind( this );
this.onFocusCaption = this.onFocusCaption.bind( this );
this.onImageClick = this.onImageClick.bind( this );
this.onSelectImage = this.onSelectImage.bind( this );
Expand Down Expand Up @@ -280,6 +282,12 @@ export class ImageEdit extends Component {
} );
}

updateCaptionAlignment( nextAlign ) {
this.props.setAttributes( {
captionAlign: nextAlign,
} );
}

updateImage( sizeSlug ) {
const { image } = this.props;

Expand Down Expand Up @@ -336,6 +344,7 @@ export class ImageEdit extends Component {
alt,
caption,
align,
captionAlign,
id,
href,
rel,
Expand All @@ -355,6 +364,10 @@ export class ImageEdit extends Component {
value={ align }
onChange={ this.updateAlignment }
/>
<AlignmentToolbar
value={ align }
onChange={ this.updateCaptionAlignment }
/>
{ url && (
<MediaReplaceFlow
mediaURL={ url }
Expand Down Expand Up @@ -671,6 +684,11 @@ export class ImageEdit extends Component {
}
isSelected={ this.state.captionFocused }
inlineToolbar
style={
captionAlign
? { textAlign: captionAlign }
: undefined
}
/>
) }
</figure>
Expand Down
9 changes: 8 additions & 1 deletion packages/block-library/src/image/save.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export default function save( { attributes } ) {
alt,
caption,
align,
captionAlign,
href,
rel,
linkClass,
Expand Down Expand Up @@ -60,7 +61,13 @@ export default function save( { attributes } ) {
image
) }
{ ! RichText.isEmpty( caption ) && (
<RichText.Content tagName="figcaption" value={ caption } />
<RichText.Content
tagName="figcaption"
value={ caption }
className={ classnames( {
[ `has-text-align-${ captionAlign }` ]: captionAlign,
} ) }
/>
) }
</>
);
Expand Down