Skip to content

Commit

Permalink
Override empty block insert behavior to replace empty block
Browse files Browse the repository at this point in the history
  • Loading branch information
aduth committed May 15, 2017
1 parent 7e0a52c commit 1592c27
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
7 changes: 7 additions & 0 deletions editor/inserter/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,5 +98,12 @@ export default connect(
block: wp.blocks.createBlock( slug ),
} );
},
} ),
// In `mergeProps`, we flip the order to enable a rendering component to
// override the default `onInsertBlock` behavior
( stateProps, dispatchProps, ownProps ) => ( {
...stateProps,
...dispatchProps,
...ownProps,
} )
)( clickOutside( Inserter ) );
13 changes: 12 additions & 1 deletion editor/modes/visual-editor/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { partial } from 'lodash';
/**
* WordPress dependencies
*/
import { createBlock } from 'blocks';
import Toolbar from 'components/toolbar';

/**
Expand Down Expand Up @@ -38,6 +39,7 @@ class VisualEditorBlock extends wp.element.Component {
this.maybeStartTyping = this.maybeStartTyping.bind( this );
this.removeOnBackspace = this.removeOnBackspace.bind( this );
this.mergeWithPrevious = this.mergeWithPrevious.bind( this );
this.replaceNewBlock = this.replaceNewBlock.bind( this );
this.previousOffset = null;
}

Expand Down Expand Up @@ -148,6 +150,13 @@ class VisualEditorBlock extends wp.element.Component {
);
}

replaceNewBlock( slug ) {
// When choosing block from inserter for a new empty block, override
// insert behavior to replace current block instead
const { uid, replaceBlocks } = this.props;
replaceBlocks( [ uid ], [ createBlock( slug ) ] );
}

componentDidUpdate( prevProps ) {
if ( this.previousOffset ) {
window.scrollTo(
Expand Down Expand Up @@ -215,7 +224,9 @@ class VisualEditorBlock extends wp.element.Component {
{ ...wrapperProps }
>
{ isNew && isSelected && (
<Inserter className="editor-visual-editor__empty-block-inserter" />
<Inserter
onInsertBlock={ this.replaceNewBlock }
className="editor-visual-editor__empty-block-inserter" />
) }
{ ! isNew && ( ( isSelected && ! isTyping ) || isHovered ) && (
<BlockMover uid={ block.uid } />
Expand Down

0 comments on commit 1592c27

Please sign in to comment.