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

Update: Allow blocks with supports align to have a default alignment #9338

Merged
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
6 changes: 5 additions & 1 deletion packages/editor/src/hooks/align.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* External dependencies
*/
import classnames from 'classnames';
import { assign, includes } from 'lodash';
import { assign, get, includes } from 'lodash';

/**
* WordPress dependencies
Expand All @@ -23,6 +23,10 @@ import { BlockControls, BlockAlignmentToolbar } from '../components';
* @return {Object} Filtered block settings
*/
export function addAttribute( settings ) {
// allow blocks to specify their own attribute definition with default values if needed.
if ( get( settings.attributes, [ 'align', 'type' ] ) === 'string' ) {
Copy link
Member

Choose a reason for hiding this comment

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

Why do we specifically check the type? Would it be okay for us to override the attribute definition if it was a type other than string ? Would has( settings.attributes, [ 'align' ] ) not have been sufficient?

Copy link
Member Author

Choose a reason for hiding this comment

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

We are checking the type because when using the align hook the hook is the one setting the align value and the hook sets a string, so specifying an attribute with a different type would not work correctly.
When setting supports align the expectation is that the hook will work properly so in this case for the hook to work properly we need to change the align attribute, I think it is acceptable.

return settings;
}
if ( hasBlockSupport( settings, 'align' ) ) {
// Use Lodash's assign to gracefully handle if attributes are undefined
settings.attributes = assign( settings.attributes, {
Expand Down