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 default block style if missing #12519

Merged
merged 2 commits into from Jan 29, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 16 additions & 0 deletions packages/editor/src/components/block-styles/index.js
Expand Up @@ -11,6 +11,8 @@ import { compose } from '@wordpress/compose';
import { withSelect, withDispatch } from '@wordpress/data';
import TokenList from '@wordpress/token-list';
import { ENTER, SPACE } from '@wordpress/keycodes';
import { _x } from '@wordpress/i18n';
import { getBlockType } from '@wordpress/blocks';

/**
* Internal dependencies
Expand Down Expand Up @@ -68,13 +70,25 @@ function BlockStyles( {
onChangeClassName,
name,
attributes,
type,
onSwitch = noop,
onHoverClassName = noop,
} ) {
if ( ! styles || styles.length === 0 ) {
return null;
}

if ( ! type.styles && ! find( styles, 'isDefault' ) ) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this ! type.styles really necessary?

Copy link
Member Author

Choose a reason for hiding this comment

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

This condition checks whether the block type originally contains styles, whereas styles contains all styles (original styles + ones added through registerBlockStyle).

The default style only needs to be added when the block type didn't already contain styles in the beginning.

So yeah, it's necessary :-)

Copy link
Contributor

Choose a reason for hiding this comment

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

The default style only needs to be added when the block type didn't already contain styles in the beginning.

I'm not certain I understand why? Why can't we always add a default style anytime we're missing one?

Copy link
Member Author

Choose a reason for hiding this comment

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

Right. Yeah I guess I did some wrong thinking there 🤔

styles = [
{
name: 'default',
label: _x( 'Default', 'block style' ),
isDefault: true,
},
...styles,
];
}

const activeStyle = getActiveStyle( styles, className );
function updateClassName( style ) {
const updatedClassName = replaceActiveStyle( className, activeStyle, style );
Expand Down Expand Up @@ -131,12 +145,14 @@ export default compose( [
const { getBlock } = select( 'core/editor' );
const { getBlockStyles } = select( 'core/blocks' );
const block = getBlock( clientId );
const blockType = getBlockType( block.name );

return {
name: block.name,
attributes: block.attributes,
className: block.attributes.className || '',
styles: getBlockStyles( block.name ),
type: blockType,
};
} ),
withDispatch( ( dispatch, { clientId } ) => {
Expand Down