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 Editor: Flatten, shortcut align hook rendering if unaligned #18963

Merged
merged 1 commit into from
Dec 6, 2019
Merged
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
63 changes: 28 additions & 35 deletions packages/block-editor/src/hooks/align.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import { assign, get, has, includes, without } from 'lodash';
/**
* WordPress dependencies
*/
import { compose, createHigherOrderComponent } from '@wordpress/compose';
import { createHigherOrderComponent } from '@wordpress/compose';
import { addFilter } from '@wordpress/hooks';
import { getBlockSupport, getBlockType, hasBlockSupport } from '@wordpress/blocks';
import { withSelect } from '@wordpress/data';
import { useSelect } from '@wordpress/data';

/**
* Internal dependencies
Expand Down Expand Up @@ -138,45 +138,38 @@ export const withToolbarControls = createHigherOrderComponent(
'withToolbarControls'
);

// Exported just for testing purposes, not exported outside the module.
export const insideSelectWithDataAlign = ( BlockListBlock ) => (
( props ) => {
const { name, attributes, hasWideEnabled } = props;
const { align } = attributes;
const validAlignments = getValidAlignments(
getBlockSupport( name, 'align' ),
hasBlockSupport( name, 'alignWide', true ),
hasWideEnabled
);

let wrapperProps = props.wrapperProps;
if ( includes( validAlignments, align ) ) {
wrapperProps = { ...wrapperProps, 'data-align': align };
}

return <BlockListBlock { ...props } wrapperProps={ wrapperProps } />;
}
);

/**
* Override the default block element to add alignment wrapper props.
*
* @param {Function} BlockListBlock Original component
* @return {Function} Wrapped component
*/
export const withDataAlign = createHigherOrderComponent(
compose( [
withSelect(
( select ) => {
const { getSettings } = select( 'core/block-editor' );
return {
hasWideEnabled: !! getSettings().alignWide,
};
}
),
insideSelectWithDataAlign,
] )
);
export const withDataAlign = createHigherOrderComponent( ( BlockListBlock ) => ( props ) => {
const { name, attributes } = props;
const { align } = attributes;
const hasWideEnabled = useSelect( ( select ) => (
!! select( 'core/block-editor' ).getSettings().alignWide
) );

// If an alignment is not assigned, there's no need to go through the
// effort to validate or assign its value.
if ( align === undefined ) {
return <BlockListBlock { ...props } />;
}

const validAlignments = getValidAlignments(
getBlockSupport( name, 'align' ),
hasBlockSupport( name, 'alignWide', true ),
hasWideEnabled
);

let wrapperProps = props.wrapperProps;
if ( includes( validAlignments, align ) ) {
wrapperProps = { ...wrapperProps, 'data-align': align };
}

return <BlockListBlock { ...props } wrapperProps={ wrapperProps } />;
} );

/**
* Override props assigned to save component to inject alignment class name if
Expand Down
82 changes: 48 additions & 34 deletions packages/block-editor/src/hooks/test/align.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* External dependencies
*/
import { noop } from 'lodash';
import renderer from 'react-test-renderer';
import renderer, { act } from 'react-test-renderer';

/**
* WordPress dependencies
Expand All @@ -17,10 +17,11 @@ import {
/**
* Internal dependencies
*/
import BlockEditorProvider from '../../components/provider';
import {
getValidAlignments,
withToolbarControls,
insideSelectWithDataAlign,
withDataAlign,
addAssignedAlign,
} from '../align';

Expand Down Expand Up @@ -169,19 +170,24 @@ describe( 'align', () => {
},
} );

const EnhancedComponent = insideSelectWithDataAlign( ( { wrapperProps } ) => (
const EnhancedComponent = withDataAlign( ( { wrapperProps } ) => (
<div { ...wrapperProps } />
) );

const wrapper = renderer.create(
<EnhancedComponent
attributes={ {
align: 'wide',
} }
name="core/foo"
/>
);
expect( wrapper.toTree().rendered.props.wrapperProps ).toEqual( {
let wrapper;
act( () => {
wrapper = renderer.create(
<BlockEditorProvider settings={ { alignWide: true } } value={ [] }>
<EnhancedComponent
attributes={ {
align: 'wide',
} }
name="core/foo"
/>
</BlockEditorProvider>
);
} );
expect( wrapper.root.findByType( 'div' ).props ).toEqual( {
'data-align': 'wide',
} );
} );
Expand All @@ -195,20 +201,24 @@ describe( 'align', () => {
},
} );

const EnhancedComponent = insideSelectWithDataAlign( ( { wrapperProps } ) => (
const EnhancedComponent = withDataAlign( ( { wrapperProps } ) => (
<div { ...wrapperProps } />
) );

const wrapper = renderer.create(
<EnhancedComponent
name="core/foo"
attributes={ {
align: 'wide',
} }
hasWideEnabled={ false }
/>
);
expect( wrapper.toTree().rendered.props.wrapperProps ).toEqual( undefined );
let wrapper;
act( () => {
wrapper = renderer.create(
<BlockEditorProvider settings={ { alignWide: false } } value={ [] }>
<EnhancedComponent
name="core/foo"
attributes={ {
align: 'wide',
} }
/>
</BlockEditorProvider>
);
} );
expect( wrapper.root.findByType( 'div' ).props ).toEqual( {} );
} );

it( 'should not render invalid align', () => {
Expand All @@ -220,20 +230,24 @@ describe( 'align', () => {
},
} );

const EnhancedComponent = insideSelectWithDataAlign( ( { wrapperProps } ) => (
const EnhancedComponent = withDataAlign( ( { wrapperProps } ) => (
<div { ...wrapperProps } />
) );

const wrapper = renderer.create(
<EnhancedComponent
name="core/foo"
attributes={ {
align: 'wide',
} }
/>
);

expect( wrapper.toTree().props.wrapperProps ).toBeUndefined();
let wrapper;
act( () => {
wrapper = renderer.create(
<BlockEditorProvider settings={ { alignWide: true } } value={ [] }>
<EnhancedComponent
name="core/foo"
attributes={ {
align: 'wide',
} }
/>
</BlockEditorProvider>
);
} );
expect( wrapper.root.findByType( 'div' ).props ).toEqual( {} );
} );
} );

Expand Down