-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4139 from WordPress/add/reusable-block-deletion
Add Reusable Block deletion
- Loading branch information
Showing
19 changed files
with
541 additions
and
128 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
editor/components/block-settings-menu/reusable-block-settings.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { connect } from 'react-redux'; | ||
import { noop } from 'lodash'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { Fragment } from '@wordpress/element'; | ||
import { IconButton } from '@wordpress/components'; | ||
import { __ } from '@wordpress/i18n'; | ||
import { isReusableBlock } from '@wordpress/blocks'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { getBlock, getReusableBlock } from '../../store/selectors'; | ||
import { convertBlockToStatic, convertBlockToReusable, deleteReusableBlock } from '../../store/actions'; | ||
|
||
export function ReusableBlockSettings( { reusableBlock, onConvertToStatic, onConvertToReusable, onDelete } ) { | ||
return ( | ||
<Fragment> | ||
{ ! reusableBlock && ( | ||
<IconButton | ||
className="editor-block-settings-menu__control" | ||
icon="controls-repeat" | ||
onClick={ onConvertToReusable } | ||
> | ||
{ __( 'Convert to Reusable Block' ) } | ||
</IconButton> | ||
) } | ||
{ reusableBlock && ( | ||
<div className="editor-block-settings-menu__section"> | ||
<IconButton | ||
className="editor-block-settings-menu__control" | ||
icon="controls-repeat" | ||
onClick={ onConvertToStatic } | ||
> | ||
{ __( 'Detach from Reusable Block' ) } | ||
</IconButton> | ||
<IconButton | ||
className="editor-block-settings-menu__control" | ||
icon="no" | ||
disabled={ reusableBlock.isTemporary } | ||
onClick={ () => onDelete( reusableBlock.id ) } | ||
> | ||
{ __( 'Delete Reusable Block' ) } | ||
</IconButton> | ||
</div> | ||
) } | ||
</Fragment> | ||
); | ||
} | ||
|
||
export default connect( | ||
( state, { uid } ) => { | ||
const block = getBlock( state, uid ); | ||
return { | ||
reusableBlock: isReusableBlock( block ) ? getReusableBlock( state, block.attributes.ref ) : null, | ||
}; | ||
}, | ||
( dispatch, { uid, onToggle = noop } ) => ( { | ||
onConvertToStatic() { | ||
dispatch( convertBlockToStatic( uid ) ); | ||
onToggle(); | ||
}, | ||
onConvertToReusable() { | ||
dispatch( convertBlockToReusable( uid ) ); | ||
onToggle(); | ||
}, | ||
onDelete( id ) { | ||
// TODO: Make this a <Confirm /> component or similar | ||
// eslint-disable-next-line no-alert | ||
const hasConfirmed = window.confirm( __( | ||
'Are you sure you want to delete this Reusable Block?\n\n' + | ||
'It will be permanently removed from all posts and pages that use it.' | ||
) ); | ||
|
||
if ( hasConfirmed ) { | ||
dispatch( deleteReusableBlock( id ) ); | ||
onToggle(); | ||
} | ||
}, | ||
} ) | ||
)( ReusableBlockSettings ); |
49 changes: 0 additions & 49 deletions
49
editor/components/block-settings-menu/reusable-block-toggle.js
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
editor/components/block-settings-menu/test/reusable-block-settings.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { shallow } from 'enzyme'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { ReusableBlockSettings } from '../reusable-block-settings'; | ||
|
||
describe( 'ReusableBlockSettings', () => { | ||
it( 'should allow converting a static block to reusable', () => { | ||
const onConvert = jest.fn(); | ||
const wrapper = shallow( | ||
<ReusableBlockSettings | ||
reusableBlock={ null } | ||
onConvertToReusable={ onConvert } | ||
/> | ||
); | ||
|
||
const text = wrapper.find( 'IconButton' ).children().text(); | ||
expect( text ).toEqual( 'Convert to Reusable Block' ); | ||
|
||
wrapper.find( 'IconButton' ).simulate( 'click' ); | ||
expect( onConvert ).toHaveBeenCalled(); | ||
} ); | ||
|
||
it( 'should allow converting a reusable block to static', () => { | ||
const onConvert = jest.fn(); | ||
const wrapper = shallow( | ||
<ReusableBlockSettings | ||
reusableBlock={ {} } | ||
onConvertToStatic={ onConvert } | ||
/> | ||
); | ||
|
||
const text = wrapper.find( 'IconButton' ).first().children().text(); | ||
expect( text ).toEqual( 'Detach from Reusable Block' ); | ||
|
||
wrapper.find( 'IconButton' ).first().simulate( 'click' ); | ||
expect( onConvert ).toHaveBeenCalled(); | ||
} ); | ||
|
||
it( 'should allow deleting a reusable block', () => { | ||
const onDelete = jest.fn(); | ||
const wrapper = shallow( | ||
<ReusableBlockSettings | ||
reusableBlock={ { id: 123 } } | ||
onDelete={ onDelete } | ||
/> | ||
); | ||
|
||
const text = wrapper.find( 'IconButton' ).last().children().text(); | ||
expect( text ).toEqual( 'Delete Reusable Block' ); | ||
|
||
wrapper.find( 'IconButton' ).last().simulate( 'click' ); | ||
expect( onDelete ).toHaveBeenCalledWith( 123 ); | ||
} ); | ||
} ); |
Oops, something went wrong.