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

Add align support to the image block #55346

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 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
2 changes: 1 addition & 1 deletion docs/reference-guides/core-blocks.md
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ Insert an image to make a visual statement. ([Source](https://github.com/WordPre

- **Name:** core/image
- **Category:** media
- **Supports:** anchor, color (~~background~~, ~~text~~), filter (duotone)
- **Supports:** align (center, full, left, right, wide), anchor, color (~~background~~, ~~text~~), filter (duotone)
- **Attributes:** align, alt, aspectRatio, caption, height, href, id, lightbox, linkClass, linkDestination, linkTarget, rel, scale, sizeSlug, title, url, width

## Latest Comments
Expand Down
12 changes: 10 additions & 2 deletions packages/block-editor/src/hooks/align.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import classnames from 'classnames';
* WordPress dependencies
*/
import { createHigherOrderComponent } from '@wordpress/compose';
import { addFilter } from '@wordpress/hooks';
import { addFilter, applyFilters } from '@wordpress/hooks';
import {
getBlockSupport,
getBlockType,
Expand Down Expand Up @@ -138,7 +138,15 @@ function BlockEditAlignmentToolbarControls( {
nextAlign = '';
}
}
setAttributes( { align: nextAlign } );

const filteredAttributes = applyFilters(
'editor.hooks.updateAlignment',
nextAlign,
blockName,
attributes
);

setAttributes( filteredAttributes );
};

return (
Expand Down
4 changes: 3 additions & 1 deletion packages/block-library/src/image/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
"textdomain": "default",
"attributes": {
"align": {
"type": "string"
"type": "string",
"default": ""
},
"url": {
"type": "string",
Expand Down Expand Up @@ -95,6 +96,7 @@
}
},
"supports": {
"align": [ "left", "center", "right", "wide", "full" ],
"anchor": true,
"color": {
"text": false,
Expand Down
21 changes: 0 additions & 21 deletions packages/block-library/src/image/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ import { getBlobByURL, isBlobURL, revokeBlobURL } from '@wordpress/blob';
import { Placeholder } from '@wordpress/components';
import { useDispatch, useSelect } from '@wordpress/data';
import {
BlockAlignmentControl,
BlockControls,
BlockIcon,
MediaPlaceholder,
useBlockProps,
Expand Down Expand Up @@ -106,7 +104,6 @@ export function ImageEdit( {
url = '',
alt,
caption,
align,
id,
width,
height,
Expand Down Expand Up @@ -255,16 +252,6 @@ export function ImageEdit( {
}
}

function updateAlignment( nextAlign ) {
const extraUpdatedAttributes = [ 'wide', 'full' ].includes( nextAlign )
? { width: undefined, height: undefined }
: {};
setAttributes( {
...extraUpdatedAttributes,
align: nextAlign,
} );
}

let isTemp = isTemporaryImage( id, url );

// Upload a temporary image on mount.
Expand Down Expand Up @@ -375,14 +362,6 @@ export function ImageEdit( {
clientId={ clientId }
blockEditingMode={ blockEditingMode }
/>
{ ! url && blockEditingMode === 'default' && (
<BlockControls group="block">
<BlockAlignmentControl
value={ align }
onChange={ updateAlignment }
/>
</BlockControls>
) }
<MediaPlaceholder
icon={ <BlockIcon icon={ icon } /> }
onSelect={ onSelectImage }
Expand Down
22 changes: 0 additions & 22 deletions packages/block-library/src/image/image.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import {
MediaReplaceFlow,
store as blockEditorStore,
useSettings,
BlockAlignmentControl,
__experimentalImageEditor as ImageEditor,
__experimentalGetElementClassName,
__experimentalUseBorderProps as useBorderProps,
Expand Down Expand Up @@ -333,21 +332,6 @@ export default function Image( {
} );
}

function updateAlignment( nextAlign ) {
const extraUpdatedAttributes = [ 'wide', 'full' ].includes( nextAlign )
? {
width: undefined,
height: undefined,
aspectRatio: undefined,
scale: undefined,
}
: {};
Comment on lines -337 to -344
Copy link
Contributor

@talldan talldan Nov 2, 2023

Choose a reason for hiding this comment

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

As @fabiankaegy mentioned, resetting these values seems to be the one custom thing the image block did when changing alignments, and it should probably be retained.

An effect would be one way to handle it, the downside being modifying alignment and resetting the size become two separate changes on the undo stack.

Quietly whispered: Filtering alignment attribute changes could also work to avoid the undo issue.

Not sure if there are any other options, would be good to hear some ideas.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The "Quietly whispered" means maybe adding a new filter to where alignment is updated by the block support?

Copy link
Member

Choose a reason for hiding this comment

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

Would it work to still handle it in an effect but using the markNextChangeAsNonPersistent method to not create anything in the undo stack for the change?

Not sure whether the change would properly get reverted when someone does go back one step at that point. I just know we have something for that

setAttributes( {
...extraUpdatedAttributes,
align: nextAlign,
} );
}

useEffect( () => {
if ( ! isSelected ) {
setIsEditingImage( false );
Expand Down Expand Up @@ -435,12 +419,6 @@ export default function Image( {
const controls = (
<>
<BlockControls group="block">
{ hasNonContentControls && (
<BlockAlignmentControl
value={ align }
onChange={ updateAlignment }
/>
) }
{ hasNonContentControls && (
<ToolbarButton
onClick={ () => {
Expand Down
20 changes: 19 additions & 1 deletion packages/block-library/src/image/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import edit from './edit';
import metadata from './block.json';
import save from './save';
import transforms from './transforms';
import { addFilter } from '@wordpress/hooks';

const { name } = metadata;

Expand Down Expand Up @@ -56,4 +57,21 @@ export const settings = {
deprecated,
};

export const init = () => initBlock( { name, metadata, settings } );
export const init = () => {
addFilter(
'editor.hooks.updateAlignment',
'core/image/update-alignment',
( nextAlign, blockName ) =>
blockName === 'core/image' &&
[ 'wide', 'full' ].includes( nextAlign )
? {
width: undefined,
height: undefined,
aspectRatio: undefined,
scale: undefined,
align: nextAlign,
}
: { align: nextAlign }
draganescu marked this conversation as resolved.
Show resolved Hide resolved
);
initBlock( { name, metadata, settings } );
};
Loading