Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Editor: Handle block merge in withDispatch callback #12820

Merged
merged 1 commit into from
Dec 21, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 19 additions & 35 deletions packages/editor/src/components/block-list/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ export class BlockListBlock extends Component {
this.maybeHover = this.maybeHover.bind( this );
this.forceFocusedContextualToolbar = this.forceFocusedContextualToolbar.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 );
Expand Down Expand Up @@ -235,30 +234,6 @@ export class BlockListBlock extends Component {
}
}

mergeBlocks( forward = false ) {
const {
clientId,
getPreviousBlockClientId,
getNextBlockClientId,
onMerge,
} = this.props;
const previousBlockClientId = getPreviousBlockClientId( clientId );
const nextBlockClientId = getNextBlockClientId( clientId );
// Do nothing when it's the first block.
if (
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice refactoring of this logic, the new version is equivalent but simpler.

( ! forward && ! previousBlockClientId ) ||
( forward && ! nextBlockClientId )
) {
return;
}

if ( forward ) {
onMerge( clientId, nextBlockClientId );
} else {
onMerge( previousBlockClientId, clientId );
}
}

insertBlocksAfter( blocks ) {
this.props.onInsertBlocks( blocks, this.props.order + 1 );
}
Expand Down Expand Up @@ -504,7 +479,7 @@ export class BlockListBlock extends Component {
setAttributes={ this.setAttributes }
insertBlocksAfter={ isLocked ? undefined : this.insertBlocksAfter }
onReplace={ isLocked ? undefined : onReplace }
mergeBlocks={ isLocked ? undefined : this.mergeBlocks }
mergeBlocks={ isLocked ? undefined : this.props.onMerge }
clientId={ clientId }
isSelectionEnabled={ this.props.isSelectionEnabled }
toggleSelection={ this.props.toggleSelection }
Expand Down Expand Up @@ -670,8 +645,6 @@ const applyWithSelect = withSelect(
getEditorSettings,
hasSelectedInnerBlock,
getTemplateLock,
getPreviousBlockClientId,
getNextBlockClientId,
__unstableGetBlockWithoutInnerBlocks,
} = select( 'core/editor' );
const block = __unstableGetBlockWithoutInnerBlocks( clientId );
Expand Down Expand Up @@ -716,11 +689,6 @@ const applyWithSelect = withSelect(
isValid,
isSelected,
isParentOfSelectedBlock,

// We only care about these selectors when events are triggered.
// We call them dynamically in the event handlers to avoid unnecessary re-renders.
getPreviousBlockClientId,
getNextBlockClientId,
};
}
);
Expand Down Expand Up @@ -758,8 +726,24 @@ const applyWithDispatch = withDispatch( ( dispatch, ownProps, { select } ) => {
onRemove( clientId ) {
removeBlock( clientId );
},
onMerge( ...args ) {
mergeBlocks( ...args );
onMerge( forward ) {
const { clientId } = ownProps;
const {
getPreviousBlockClientId,
getNextBlockClientId,
} = select( 'core/editor' );

if ( forward ) {
const nextBlockClientId = getNextBlockClientId( clientId );
if ( nextBlockClientId ) {
mergeBlocks( clientId, nextBlockClientId );
}
} else {
const previousBlockClientId = getPreviousBlockClientId( clientId );
if ( previousBlockClientId ) {
mergeBlocks( previousBlockClientId, clientId );
}
}
},
onReplace( blocks ) {
replaceBlocks( [ ownProps.clientId ], blocks );
Expand Down