Skip to content

Commit

Permalink
ENTER twice should remove nesting
Browse files Browse the repository at this point in the history
  • Loading branch information
ellatrix committed Apr 11, 2018
1 parent 2900215 commit f69f171
Showing 1 changed file with 23 additions and 13 deletions.
36 changes: 23 additions & 13 deletions editor/components/block-list/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
getSaveElement,
isSharedBlock,
isUnmodifiedDefaultBlock,
getDefaultBlockName,
} from '@wordpress/blocks';
import { withFilters, withContext } from '@wordpress/components';
import { __, sprintf } from '@wordpress/i18n';
Expand Down Expand Up @@ -62,7 +63,6 @@ export class BlockListBlock extends Component {
this.maybeHover = this.maybeHover.bind( this );
this.hideHoverEffects = this.hideHoverEffects.bind( this );
this.mergeBlocks = this.mergeBlocks.bind( this );
this.insertBlocksAfter = this.insertBlocksAfter.bind( this );
this.onFocus = this.onFocus.bind( this );
this.preventDrag = this.preventDrag.bind( this );
this.onPointerDown = this.onPointerDown.bind( this );
Expand Down Expand Up @@ -267,10 +267,6 @@ export class BlockListBlock extends Component {
}
}

insertBlocksAfter( blocks ) {
this.props.onInsertBlocks( blocks, this.props.order + 1 );
}

/**
* Marks the block as selected when focused and not already selected. This
* specifically handles the case where block does not set focus on its own
Expand Down Expand Up @@ -348,9 +344,9 @@ export class BlockListBlock extends Component {
case ENTER:
// Insert default block after current block if enter and event
// not already handled by descendant.
this.props.onInsertBlocks( [
createBlock( 'core/paragraph' ),
], this.props.order + 1 );
this.props.onInsertBlocksAfter( [
createBlock( getDefaultBlockName() ),
] );
event.preventDefault();
break;

Expand Down Expand Up @@ -545,7 +541,7 @@ export class BlockListBlock extends Component {
isSelected={ isSelected }
attributes={ block.attributes }
setAttributes={ this.setAttributes }
insertBlocksAfter={ isLocked ? undefined : this.insertBlocksAfter }
insertBlocksAfter={ isLocked ? undefined : this.props.onInsertBlocksAfter }
onReplace={ isLocked ? undefined : onReplace }
mergeBlocks={ isLocked ? undefined : this.mergeBlocks }
id={ uid }
Expand Down Expand Up @@ -610,6 +606,7 @@ const applyWithSelect = withSelect( ( select, { uid, rootUID } ) => {
isSelectionEnabled,
getSelectedBlocksInitialCaretPosition,
getBlockSelectionEnd,
getBlockRootUID,
} = select( 'core/editor' );
const isSelected = isBlockSelected( uid );
return {
Expand All @@ -629,6 +626,8 @@ const applyWithSelect = withSelect( ( select, { uid, rootUID } ) => {
isSelectionEnabled: isSelectionEnabled(),
initialPosition: getSelectedBlocksInitialCaretPosition(),
isSelected,
rootUIDOfRoot: getBlockRootUID( rootUID ),
orderOfRoot: getBlockIndex( rootUID, getBlockRootUID( rootUID ) ),
};
} );

Expand All @@ -650,10 +649,21 @@ const applyWithDispatch = withDispatch( ( dispatch, ownProps ) => {
onSelect( uid = ownProps.uid, initialPosition ) {
selectBlock( uid, initialPosition );
},
onInsertBlocks( blocks, index ) {
const { rootUID, layout } = ownProps;
blocks = blocks.map( ( block ) => cloneBlock( block, { layout } ) );
insertBlocks( blocks, index, rootUID );
onInsertBlocksAfter( blocks ) {
const { block, order, isLast, rootUID, orderOfRoot, rootUIDOfRoot, layout } = ownProps;

blocks = blocks.map( ( oldBlock ) => cloneBlock( oldBlock, { layout } ) );

if (
rootUID && isLast && blocks.length === 1 &&
isUnmodifiedDefaultBlock( first( blocks ) ) &&
isUnmodifiedDefaultBlock( block )
) {
removeBlock( block.uid );
insertBlocks( blocks, orderOfRoot + 1, rootUIDOfRoot );
} else {
insertBlocks( blocks, order + 1, rootUID );
}
},
onRemove( uid ) {
removeBlock( uid );
Expand Down

0 comments on commit f69f171

Please sign in to comment.