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 warning: add ellipsis menu and convert to classic block #7741

Merged
merged 7 commits into from
Aug 28, 2018
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
13 changes: 11 additions & 2 deletions packages/editor/src/components/block-list/block-invalid-warning.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ import { withDispatch } from '@wordpress/data';
*/
import Warning from '../warning';

function BlockInvalidWarning( { convertToHTML, convertToBlocks } ) {
function BlockInvalidWarning( { convertToHTML, convertToBlocks, convertToClassic } ) {
const hasHTMLBlock = !! getBlockType( 'core/html' );

return (
<Warning
actions={ [
primaryActions={ [
<Button key="convert" onClick={ convertToBlocks } isLarge isPrimary={ ! hasHTMLBlock }>
{ __( 'Convert to Blocks' ) }
</Button>,
Expand All @@ -30,6 +30,10 @@ function BlockInvalidWarning( { convertToHTML, convertToBlocks } ) {
</Button>
),
] }
secondaryActions={ [
{ title: __( 'Convert to Blocks' ), onClick: convertToBlocks },
{ title: __( 'Convert to Classic Block' ), onClick: convertToClassic },
] }
>
{ __( 'This block has been modified externally.' ) }
</Warning>
Expand All @@ -39,6 +43,11 @@ function BlockInvalidWarning( { convertToHTML, convertToBlocks } ) {
export default withDispatch( ( dispatch, { block } ) => {
const { replaceBlock } = dispatch( 'core/editor' );
return {
convertToClassic() {
replaceBlock( block.uid, createBlock( 'core/freeform', {
content: block.originalContent,
} ) );
},
convertToHTML() {
replaceBlock( block.clientId, createBlock( 'core/html', {
content: block.originalContent,
Expand Down
32 changes: 29 additions & 3 deletions packages/editor/src/components/warning/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,49 @@ import classnames from 'classnames';
* WordPress dependencies
*/
import { Children } from '@wordpress/element';
import { Dropdown, IconButton, MenuGroup, MenuItem } from '@wordpress/components';
import { __ } from '@wordpress/i18n';

function Warning( { className, actions, children } ) {
function Warning( { className, primaryActions, children, secondaryActions } ) {
return (
<div className={ classnames( className, 'editor-warning' ) }>
<div className="editor-warning__contents">
<p className="editor-warning__message">{ children }</p>

{ Children.count( actions ) > 0 && (
{ Children.count( primaryActions ) > 0 && (
<div className="editor-warning__actions">
{ Children.map( actions, ( action, i ) => (
{ Children.map( primaryActions, ( action, i ) => (
<span key={ i } className="editor-warning__action">
{ action }
</span>
) ) }
</div>
) }
</div>

{ secondaryActions && (
<Dropdown
className="editor-warning__secondary"
position="bottom left"
renderToggle={ ( { isOpen, onToggle } ) => (
<IconButton
icon="ellipsis"
label={ __( 'More options' ) }
onClick={ onToggle }
aria-expanded={ isOpen }
/>
) }
renderContent={ () => (
<MenuGroup label={ __( 'More options' ) }>
{ secondaryActions.map( ( item, pos ) =>
<MenuItem onClick={ item.onClick } key={ pos }>
{ item.title }
</MenuItem>
) }
</MenuGroup>
) }
/>
) }
</div>
);
}
Expand Down
22 changes: 22 additions & 0 deletions packages/editor/src/components/warning/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,25 @@
margin-left: 0;
}
}

.editor-warning__secondary {
margin: 3px 0 0 -4px;

// the padding and margin of the more menu is intentionally non-standard
.components-icon-button {
width: auto;
padding: 8px 2px;
}

@include break-small() {
margin-left: 4px;

.components-icon-button {
padding: 8px 4px;
}
}

.components-button svg {
transform: rotate(90deg);
}
}
13 changes: 11 additions & 2 deletions packages/editor/src/components/warning/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,16 @@ describe( 'Warning', () => {
expect( wrapper ).toMatchSnapshot();
} );

it( 'should has valid class', () => {
it( 'should have valid class', () => {
const wrapper = shallow( <Warning /> );

expect( wrapper.hasClass( 'editor-warning' ) ).toBe( true );
expect( wrapper.find( '.editor-warning__actions' ) ).toHaveLength( 0 );
expect( wrapper.find( '.editor-warning__hidden' ) ).toHaveLength( 0 );
} );

it( 'should show child error message element', () => {
const wrapper = shallow( <Warning actions={ <button /> }>Message</Warning> );
const wrapper = shallow( <Warning primaryActions={ <button /> }>Message</Warning> );

const actions = wrapper.find( '.editor-warning__actions' );
const action = actions.childAt( 0 );
Expand All @@ -32,4 +33,12 @@ describe( 'Warning', () => {
expect( action.hasClass( 'editor-warning__action' ) ).toBe( true );
expect( action.childAt( 0 ).type() ).toBe( 'button' );
} );

it( 'should show hidden actions', () => {
const wrapper = shallow( <Warning secondaryActions={ [ { title: 'test', onClick: null } ] }>Message</Warning> );

const actions = wrapper.find( '.editor-warning__secondary' );

expect( actions ).toHaveLength( 1 );
} );
} );