Skip to content

Commit

Permalink
Add a new empty block when Enter.key is pressed in the title block
Browse files Browse the repository at this point in the history
  • Loading branch information
daniloercoli committed Jan 24, 2019
1 parent 523121c commit 9291516
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/block-management/block-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ type PropsType = {
blockCount: number,
focusBlock: ( clientId: string ) => void,
insertBlock: ( block: BlockType, position: number ) => void,
insertBlocks: ( blocks: Array<Object>, index: number ) => void,
replaceBlock: ( string, BlockType ) => mixed,
selectedBlock: ?BlockType,
selectedBlockClientId: string,
Expand Down Expand Up @@ -71,6 +72,7 @@ export class BlockManager extends React.Component<PropsType, StateType> {
( this: any ).keyboardDidHide = this.keyboardDidHide.bind( this );
( this: any ).onCaretVerticalPositionChange = this.onCaretVerticalPositionChange.bind( this );
( this: any ).scrollViewInnerRef = this.scrollViewInnerRef.bind( this );
( this: any ).insertBlocksAfterTitle = this.insertBlocksAfterTitle.bind( this );

this.state = {
blockTypePickerVisible: false,
Expand All @@ -87,6 +89,15 @@ export class BlockManager extends React.Component<PropsType, StateType> {
this.setState( { blockTypePickerVisible: show } );
}

insertBlocksAfterTitle = ( blocks: Array<Object> ) => {
this.props.insertBlocks( blocks, 0 );
// now set the focus
if ( blocks[ 0 ] ) {
// focus on the first block inserted
this.props.focusBlock( blocks[ 0 ].clientId );
}
}

onBlockTypeSelected( itemValue: string ) {
this.setState( { blockTypePickerVisible: false } );

Expand Down Expand Up @@ -167,6 +178,7 @@ export class BlockManager extends React.Component<PropsType, StateType> {
style={ styles.title }
title={ this.props.title }
onUpdate={ this.props.setTitleAction }
insertBlocksAfter={ this.insertBlocksAfterTitle }
placeholder={ 'Pick a title...' } />
<View
style={ styles.titleSeparator } />
Expand Down Expand Up @@ -294,12 +306,14 @@ export default compose( [
const {
clearSelectedBlock,
insertBlock,
insertBlocks,
replaceBlock,
selectBlock,
} = dispatch( 'core/editor' );

return {
insertBlock,
insertBlocks,
focusBlock: ( clientId ) => {
clearSelectedBlock();
selectBlock( clientId );
Expand Down

0 comments on commit 9291516

Please sign in to comment.