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 1 commit
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
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
15 changes: 0 additions & 15 deletions packages/block-library/src/image/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,6 @@ export function ImageEdit( {
url = '',
alt,
caption,
align,
id,
width,
height,
Expand Down Expand Up @@ -278,20 +277,6 @@ export function ImageEdit( {
}
}, [] );

useEffect( () => {
const extraUpdatedAttributes = [ 'wide', 'full' ].includes( align )
? {
width: undefined,
height: undefined,
aspectRatio: undefined,
scale: undefined,
}
: {};
setAttributes( {
...extraUpdatedAttributes,
} );
}, [ align, setAttributes ] );

// If an image is temporary, revoke the Blob url when it is uploaded (and is
// no longer temporary).
useEffect( () => {
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 } );
};