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

Fix: Cover image tooltips for alignment are duplicated #11263

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
14 changes: 7 additions & 7 deletions packages/editor/src/components/alignment-toolbar/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,37 +17,37 @@ import { compose } from '@wordpress/compose';
*/
import { withBlockEditContext } from '../block-edit/context';

const ALIGNMENT_CONTROLS = [
const DEFAULT_ALIGNMENT_CONTROLS = [
{
icon: 'editor-alignleft',
title: __( 'Align left' ),
title: __( 'Align text left' ),
align: 'left',
},
{
icon: 'editor-aligncenter',
title: __( 'Align center' ),
title: __( 'Align text center' ),
align: 'center',
},
{
icon: 'editor-alignright',
title: __( 'Align right' ),
title: __( 'Align text right' ),
align: 'right',
},
];

export function AlignmentToolbar( { isCollapsed, value, onChange } ) {
export function AlignmentToolbar( { isCollapsed, value, onChange, alignmentControls = DEFAULT_ALIGNMENT_CONTROLS } ) {
Copy link
Contributor

Choose a reason for hiding this comment

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

This change is not needed anymore right? can we remove it?

Copy link
Member Author

Choose a reason for hiding this comment

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

It is not needed but I liked this change, it allows a block for example to only offer right or left alignment, or to change labels in a specific case, e.g: instead of text naming the actual field "Author name align left" that may be very useful for a11y.

function applyOrUnset( align ) {
return () => onChange( value === align ? undefined : align );
}

const activeAlignment = find( ALIGNMENT_CONTROLS, ( control ) => control.align === value );
const activeAlignment = find( alignmentControls, ( control ) => control.align === value );

return (
<Toolbar
isCollapsed={ isCollapsed }
icon={ activeAlignment ? activeAlignment.icon : 'editor-alignleft' }
label={ __( 'Change Text Alignment' ) }
controls={ ALIGNMENT_CONTROLS.map( ( control ) => {
controls={ alignmentControls.map( ( control ) => {
const { align } = control;
const isActive = ( value === align );

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,30 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`AlignmentToolbar should allow custom alignment controls to be specified 1`] = `
<Toolbar
controls={
Array [
Object {
"align": "custom-left",
"icon": "editor-alignleft",
"isActive": false,
"onClick": [Function],
"title": "My custom left",
},
Object {
"align": "custom-right",
"icon": "editor-aligncenter",
"isActive": true,
"onClick": [Function],
"title": "My custom right",
},
]
}
icon="editor-aligncenter"
label="Change Text Alignment"
/>
`;

exports[`AlignmentToolbar should match snapshot 1`] = `
<Toolbar
controls={
Expand All @@ -9,21 +34,21 @@ exports[`AlignmentToolbar should match snapshot 1`] = `
"icon": "editor-alignleft",
"isActive": true,
"onClick": [Function],
"title": "Align left",
"title": "Align text left",
},
Object {
"align": "center",
"icon": "editor-aligncenter",
"isActive": false,
"onClick": [Function],
"title": "Align center",
"title": "Align text center",
},
Object {
"align": "right",
"icon": "editor-alignright",
"isActive": false,
"onClick": [Function],
"title": "Align right",
"title": "Align text right",
},
]
}
Expand Down
43 changes: 43 additions & 0 deletions packages/editor/src/components/alignment-toolbar/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,47 @@ describe( 'AlignmentToolbar', () => {
expect( onChangeSpy ).toHaveBeenCalledTimes( 1 );
expect( onChangeSpy ).toHaveBeenCalledWith( 'center' );
} );

test( 'should allow custom alignment controls to be specified', () => {
const wrapperCustomControls = shallow(
<AlignmentToolbar
value={ 'custom-right' }
onChange={ onChangeSpy }
alignmentControls={ [
{
icon: 'editor-alignleft',
title: 'My custom left',
align: 'custom-left',
},
{
icon: 'editor-aligncenter',
title: 'My custom right',
align: 'custom-right',
},
] }
/>
);
expect( wrapperCustomControls ).toMatchSnapshot();
const customControls = wrapperCustomControls.props().controls;
expect( customControls ).toHaveLength( 2 );

// should correctly call on change when right alignment is pressed (active alignment)
const rightControl = customControls.find(
( { align } ) => align === 'custom-right'
);
expect( rightControl.title ).toBe( 'My custom right' );
rightControl.onClick();
expect( onChangeSpy ).toHaveBeenCalledTimes( 1 );
expect( onChangeSpy ).toHaveBeenCalledWith( undefined );
onChangeSpy.mockClear();

// should correctly call on change when right alignment is pressed (inactive alignment)
const leftControl = customControls.find(
( { align } ) => align === 'custom-left'
);
expect( leftControl.title ).toBe( 'My custom left' );
leftControl.onClick();
expect( onChangeSpy ).toHaveBeenCalledTimes( 1 );
expect( onChangeSpy ).toHaveBeenCalledWith( 'custom-left' );
} );
} );