Skip to content

Commit

Permalink
When inserting block from title, replace block if appropriate (#16574)
Browse files Browse the repository at this point in the history
  • Loading branch information
mchowning authored Jul 19, 2019
1 parent b1b9c11 commit b0884b7
Showing 1 changed file with 17 additions and 8 deletions.
25 changes: 17 additions & 8 deletions packages/block-editor/src/components/block-list/index.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,19 @@ export class BlockList extends Component {
// create an empty block of the selected type
const newBlock = createBlock( itemValue );

this.finishBlockAppendingOrReplacing( newBlock );
this.finishInsertingOrReplacingBlock( newBlock );
}

finishBlockAppendingOrReplacing( newBlock ) {
// now determine whether we need to replace the currently selected block (if it's empty)
// or just add a new block as usual
finishInsertingOrReplacingBlock( newBlock ) {
if ( this.isReplaceable( this.props.selectedBlock ) ) {
// do replace here
// replace selected block
this.props.replaceBlock( this.props.selectedBlockClientId, newBlock );
} else if ( this.props.isPostTitleSelected && this.isReplaceable( this.props.firstBlock ) ) {
// replace first block in post: there is no selected block when the post title is selected,
// so replaceBlock does not select the new block and we need to manually select the new block
const { clientId: firstBlockId } = this.props.firstBlock;
this.props.replaceBlock( firstBlockId, newBlock );
this.props.selectBlock( newBlock.clientId );
} else {
this.props.insertBlock( newBlock, this.getNewBlockInsertionIndex() );
}
Expand Down Expand Up @@ -110,7 +114,7 @@ export class BlockList extends Component {
newMediaBlock.attributes.id = payload.mediaId;

// finally append or replace as appropriate
this.finishBlockAppendingOrReplacing( newMediaBlock );
this.finishInsertingOrReplacingBlock( newMediaBlock );
} );
}

Expand Down Expand Up @@ -260,7 +264,7 @@ export class BlockList extends Component {
const paragraphBlock = createBlock( 'core/paragraph' );
return (
<TouchableWithoutFeedback onPress={ () => {
this.finishBlockAppendingOrReplacing( paragraphBlock );
this.finishInsertingOrReplacingBlock( paragraphBlock );
} } >
<View style={ styles.blockListFooter } />
</TouchableWithoutFeedback>
Expand All @@ -277,6 +281,7 @@ export class BlockList extends Component {
export default compose( [
withSelect( ( select, { rootClientId } ) => {
const {
getBlock,
getBlockCount,
getBlockName,
getBlockIndex,
Expand All @@ -287,13 +292,15 @@ export default compose( [
} = select( 'core/block-editor' );

const selectedBlockClientId = getSelectedBlockClientId();
const blockClientIds = getBlockOrder( rootClientId );

return {
blockClientIds: getBlockOrder( rootClientId ),
blockClientIds,
blockCount: getBlockCount( rootClientId ),
getBlockName,
isBlockSelected,
selectedBlock: getSelectedBlock(),
firstBlock: getBlock( blockClientIds[ 0 ] ),
selectedBlockClientId,
selectedBlockOrder: getBlockIndex( selectedBlockClientId ),
};
Expand All @@ -303,12 +310,14 @@ export default compose( [
insertBlock,
replaceBlock,
clearSelectedBlock,
selectBlock,
} = dispatch( 'core/block-editor' );

return {
clearSelectedBlock,
insertBlock,
replaceBlock,
selectBlock,
};
} ),
] )( BlockList );
Expand Down

0 comments on commit b0884b7

Please sign in to comment.