Skip to content

Commit f20c453

Browse files
Fix: Cover image tooltips for alignment are duplicated (#11263)
## Description Fixes: #6013 The same tooltip as used for Block Alignment and text Alignment. This PR makes sure we use different tooltips for the text alignment buttons. https://user-images.githubusercontent.com/253067/38373204-92586b58-38e8-11e8-8ec2-09e73368cacb.png ## How has this been tested? I added a cover block and I checked that the tooltips for text alignment are in the format "Text align left/center/right" and block alignment tooltips are in the format "Text left/center/right" ## Screenshots <!-- if applicable --> Before: ![align](https://user-images.githubusercontent.com/253067/38373204-92586b58-38e8-11e8-8ec2-09e73368cacb.png) ![align2](https://user-images.githubusercontent.com/253067/38373205-928a870a-38e8-11e8-8391-ce5135c7593a.png) After: ![image](https://user-images.githubusercontent.com/11271197/47747203-b62a8f80-dc7f-11e8-86fd-970f642296ae.png)
1 parent 53209f4 commit f20c453

File tree

3 files changed

+78
-10
lines changed

3 files changed

+78
-10
lines changed

packages/editor/src/components/alignment-toolbar/index.js

+7-7
Original file line numberDiff line numberDiff line change
@@ -17,37 +17,37 @@ import { compose } from '@wordpress/compose';
1717
*/
1818
import { withBlockEditContext } from '../block-edit/context';
1919

20-
const ALIGNMENT_CONTROLS = [
20+
const DEFAULT_ALIGNMENT_CONTROLS = [
2121
{
2222
icon: 'editor-alignleft',
23-
title: __( 'Align left' ),
23+
title: __( 'Align text left' ),
2424
align: 'left',
2525
},
2626
{
2727
icon: 'editor-aligncenter',
28-
title: __( 'Align center' ),
28+
title: __( 'Align text center' ),
2929
align: 'center',
3030
},
3131
{
3232
icon: 'editor-alignright',
33-
title: __( 'Align right' ),
33+
title: __( 'Align text right' ),
3434
align: 'right',
3535
},
3636
];
3737

38-
export function AlignmentToolbar( { isCollapsed, value, onChange } ) {
38+
export function AlignmentToolbar( { isCollapsed, value, onChange, alignmentControls = DEFAULT_ALIGNMENT_CONTROLS } ) {
3939
function applyOrUnset( align ) {
4040
return () => onChange( value === align ? undefined : align );
4141
}
4242

43-
const activeAlignment = find( ALIGNMENT_CONTROLS, ( control ) => control.align === value );
43+
const activeAlignment = find( alignmentControls, ( control ) => control.align === value );
4444

4545
return (
4646
<Toolbar
4747
isCollapsed={ isCollapsed }
4848
icon={ activeAlignment ? activeAlignment.icon : 'editor-alignleft' }
4949
label={ __( 'Change Text Alignment' ) }
50-
controls={ ALIGNMENT_CONTROLS.map( ( control ) => {
50+
controls={ alignmentControls.map( ( control ) => {
5151
const { align } = control;
5252
const isActive = ( value === align );
5353

packages/editor/src/components/alignment-toolbar/test/__snapshots__/index.js.snap

+28-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,30 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3+
exports[`AlignmentToolbar should allow custom alignment controls to be specified 1`] = `
4+
<Toolbar
5+
controls={
6+
Array [
7+
Object {
8+
"align": "custom-left",
9+
"icon": "editor-alignleft",
10+
"isActive": false,
11+
"onClick": [Function],
12+
"title": "My custom left",
13+
},
14+
Object {
15+
"align": "custom-right",
16+
"icon": "editor-aligncenter",
17+
"isActive": true,
18+
"onClick": [Function],
19+
"title": "My custom right",
20+
},
21+
]
22+
}
23+
icon="editor-aligncenter"
24+
label="Change Text Alignment"
25+
/>
26+
`;
27+
328
exports[`AlignmentToolbar should match snapshot 1`] = `
429
<Toolbar
530
controls={
@@ -9,21 +34,21 @@ exports[`AlignmentToolbar should match snapshot 1`] = `
934
"icon": "editor-alignleft",
1035
"isActive": true,
1136
"onClick": [Function],
12-
"title": "Align left",
37+
"title": "Align text left",
1338
},
1439
Object {
1540
"align": "center",
1641
"icon": "editor-aligncenter",
1742
"isActive": false,
1843
"onClick": [Function],
19-
"title": "Align center",
44+
"title": "Align text center",
2045
},
2146
Object {
2247
"align": "right",
2348
"icon": "editor-alignright",
2449
"isActive": false,
2550
"onClick": [Function],
26-
"title": "Align right",
51+
"title": "Align text right",
2752
},
2853
]
2954
}

packages/editor/src/components/alignment-toolbar/test/index.js

+43
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,47 @@ describe( 'AlignmentToolbar', () => {
4141
expect( onChangeSpy ).toHaveBeenCalledTimes( 1 );
4242
expect( onChangeSpy ).toHaveBeenCalledWith( 'center' );
4343
} );
44+
45+
test( 'should allow custom alignment controls to be specified', () => {
46+
const wrapperCustomControls = shallow(
47+
<AlignmentToolbar
48+
value={ 'custom-right' }
49+
onChange={ onChangeSpy }
50+
alignmentControls={ [
51+
{
52+
icon: 'editor-alignleft',
53+
title: 'My custom left',
54+
align: 'custom-left',
55+
},
56+
{
57+
icon: 'editor-aligncenter',
58+
title: 'My custom right',
59+
align: 'custom-right',
60+
},
61+
] }
62+
/>
63+
);
64+
expect( wrapperCustomControls ).toMatchSnapshot();
65+
const customControls = wrapperCustomControls.props().controls;
66+
expect( customControls ).toHaveLength( 2 );
67+
68+
// should correctly call on change when right alignment is pressed (active alignment)
69+
const rightControl = customControls.find(
70+
( { align } ) => align === 'custom-right'
71+
);
72+
expect( rightControl.title ).toBe( 'My custom right' );
73+
rightControl.onClick();
74+
expect( onChangeSpy ).toHaveBeenCalledTimes( 1 );
75+
expect( onChangeSpy ).toHaveBeenCalledWith( undefined );
76+
onChangeSpy.mockClear();
77+
78+
// should correctly call on change when right alignment is pressed (inactive alignment)
79+
const leftControl = customControls.find(
80+
( { align } ) => align === 'custom-left'
81+
);
82+
expect( leftControl.title ).toBe( 'My custom left' );
83+
leftControl.onClick();
84+
expect( onChangeSpy ).toHaveBeenCalledTimes( 1 );
85+
expect( onChangeSpy ).toHaveBeenCalledWith( 'custom-left' );
86+
} );
4487
} );

0 commit comments

Comments
 (0)