-
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.
Revert "Cleanup: Remove the quick inserter code and state deps"
This reverts commit 7265186.
- Loading branch information
1 parent
f51ec08
commit 159c2c9
Showing
8 changed files
with
278 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { connect } from 'react-redux'; | ||
import classnames from 'classnames'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { __, sprintf } from '@wordpress/i18n'; | ||
import { IconButton } from '@wordpress/components'; | ||
import { Component } from '@wordpress/element'; | ||
import { createBlock, BlockIcon } from '@wordpress/blocks'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { Inserter } from '../../../components'; | ||
import { insertBlock } from '../../../actions'; | ||
import { getMostFrequentlyUsedBlocks, getBlockCount } from '../../../selectors'; | ||
|
||
export class VisualEditorInserter extends Component { | ||
constructor() { | ||
super( ...arguments ); | ||
|
||
this.showControls = this.toggleControls.bind( this, true ); | ||
this.hideControls = this.toggleControls.bind( this, false ); | ||
|
||
this.state = { | ||
isShowingControls: false, | ||
}; | ||
} | ||
|
||
toggleControls( isShowingControls ) { | ||
this.setState( { isShowingControls } ); | ||
} | ||
|
||
insertBlock( name ) { | ||
const { onInsertBlock } = this.props; | ||
onInsertBlock( createBlock( name ) ); | ||
} | ||
|
||
render() { | ||
const { blockCount } = this.props; | ||
const { isShowingControls } = this.state; | ||
const { mostFrequentlyUsedBlocks } = this.props; | ||
const classes = classnames( 'editor-visual-editor__inserter', { | ||
'is-showing-controls': isShowingControls, | ||
} ); | ||
|
||
return ( | ||
<div | ||
className={ classes } | ||
onFocus={ this.showControls } | ||
onBlur={ this.hideControls } | ||
> | ||
<Inserter | ||
insertIndex={ blockCount } | ||
position="top right" /> | ||
{ mostFrequentlyUsedBlocks && mostFrequentlyUsedBlocks.map( ( block ) => ( | ||
<IconButton | ||
key={ 'frequently_used_' + block.name } | ||
className="editor-inserter__block" | ||
onClick={ () => this.insertBlock( block.name ) } | ||
label={ sprintf( __( 'Insert %s' ), block.title ) } | ||
icon={ ( | ||
<span className="editor-visual-editor__inserter-block-icon"> | ||
<BlockIcon icon={ block.icon } /> | ||
</span> | ||
) } | ||
> | ||
{ block.title } | ||
</IconButton> | ||
) ) } | ||
</div> | ||
); | ||
} | ||
} | ||
|
||
export default connect( | ||
( state ) => { | ||
return { | ||
mostFrequentlyUsedBlocks: getMostFrequentlyUsedBlocks( state ), | ||
blockCount: getBlockCount( state ), | ||
}; | ||
}, | ||
{ onInsertBlock: insertBlock }, | ||
)( VisualEditorInserter ); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { shallow } from 'enzyme'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { getBlockType } from '@wordpress/blocks'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { VisualEditorInserter } from '../inserter'; | ||
|
||
describe( 'VisualEditorInserter', () => { | ||
it( 'should show controls when receiving focus', () => { | ||
const wrapper = shallow( <VisualEditorInserter /> ); | ||
|
||
wrapper.simulate( 'focus' ); | ||
|
||
expect( wrapper.state( 'isShowingControls' ) ).toBe( true ); | ||
} ); | ||
|
||
it( 'should hide controls when losing focus', () => { | ||
const wrapper = shallow( <VisualEditorInserter /> ); | ||
|
||
wrapper.simulate( 'focus' ); | ||
wrapper.simulate( 'blur' ); | ||
|
||
expect( wrapper.state( 'isShowingControls' ) ).toBe( false ); | ||
} ); | ||
|
||
it( 'should insert frequently used blocks', () => { | ||
const onInsertBlock = jest.fn(); | ||
const mostFrequentlyUsedBlocks = [ getBlockType( 'core/paragraph' ), getBlockType( 'core/image' ) ]; | ||
const wrapper = shallow( | ||
<VisualEditorInserter onInsertBlock={ onInsertBlock } mostFrequentlyUsedBlocks={ mostFrequentlyUsedBlocks } /> | ||
); | ||
wrapper.state.preferences = { | ||
blockUsage: { | ||
'core/paragraph': 42, | ||
'core/image': 34, | ||
}, | ||
}; | ||
|
||
wrapper | ||
.findWhere( ( node ) => node.prop( 'children' ) === 'Paragraph' ) | ||
.simulate( 'click' ); | ||
|
||
expect( onInsertBlock ).toHaveBeenCalled(); | ||
expect( onInsertBlock.mock.calls[ 0 ][ 0 ].name ).toBe( 'core/paragraph' ); | ||
} ); | ||
} ); |
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
Oops, something went wrong.