From 6df2cd375b11ff546159898133232208fd660007 Mon Sep 17 00:00:00 2001 From: Riad Benguella Date: Thu, 18 Apr 2019 13:55:58 +0100 Subject: [PATCH] Avoid passing down isFirst and isLast props --- .../src/components/block-list/block.js | 4 ---- .../src/components/block-list/index.js | 4 +--- .../components/block-list/multi-controls.js | 20 +------------------ .../src/components/block-mover/index.js | 16 ++++++++++----- 4 files changed, 13 insertions(+), 31 deletions(-) diff --git a/packages/block-editor/src/components/block-list/block.js b/packages/block-editor/src/components/block-list/block.js index c5abcdc5deee36..b3c743d4a397c0 100644 --- a/packages/block-editor/src/components/block-list/block.js +++ b/packages/block-editor/src/components/block-list/block.js @@ -363,8 +363,6 @@ export class BlockListBlock extends Component { isFocusMode, hasFixedToolbar, isLocked, - isFirst, - isLast, clientId, rootClientId, isSelected, @@ -527,8 +525,6 @@ export class BlockListBlock extends Component { - { map( blockClientIds, ( clientId, blockIndex ) => { + { map( blockClientIds, ( clientId ) => { const isBlockInSelection = hasMultiSelection ? multiSelectedBlockClientIds.includes( clientId ) : selectedBlockClientId === clientId; @@ -216,8 +216,6 @@ class BlockList extends Component { blockRef={ this.setBlockRef } onSelectionStart={ this.onSelectionStart } rootClientId={ rootClientId } - isFirst={ blockIndex === 0 } - isLast={ blockIndex === blockClientIds.length - 1 } isDraggable={ isDraggable } /> diff --git a/packages/block-editor/src/components/block-list/multi-controls.js b/packages/block-editor/src/components/block-list/multi-controls.js index 5fb34625389ee5..e4dd88aeda030f 100644 --- a/packages/block-editor/src/components/block-list/multi-controls.js +++ b/packages/block-editor/src/components/block-list/multi-controls.js @@ -1,8 +1,3 @@ -/** - * External dependencies - */ -import { first, last } from 'lodash'; - /** * WordPress dependencies */ @@ -15,10 +10,7 @@ import BlockMover from '../block-mover'; function BlockListMultiControls( { multiSelectedBlockClientIds, - clientId, isSelecting, - isFirst, - isLast, } ) { if ( isSelecting ) { return null; @@ -26,30 +18,20 @@ function BlockListMultiControls( { return ( ); } -export default withSelect( ( select, { clientId } ) => { +export default withSelect( ( select ) => { const { getMultiSelectedBlockClientIds, isMultiSelecting, - getBlockIndex, - getBlockCount, } = select( 'core/block-editor' ); const clientIds = getMultiSelectedBlockClientIds(); - const firstIndex = getBlockIndex( first( clientIds ), clientId ); - const lastIndex = getBlockIndex( last( clientIds ), clientId ); return { multiSelectedBlockClientIds: clientIds, isSelecting: isMultiSelecting(), - isFirst: firstIndex === 0, - isLast: lastIndex + 1 === getBlockCount(), }; } )( BlockListMultiControls ); diff --git a/packages/block-editor/src/components/block-mover/index.js b/packages/block-editor/src/components/block-mover/index.js index 6220f440edcbcd..10fc40ba0e09d2 100644 --- a/packages/block-editor/src/components/block-mover/index.js +++ b/packages/block-editor/src/components/block-mover/index.js @@ -1,7 +1,7 @@ /** * External dependencies */ -import { first, partial, castArray } from 'lodash'; +import { first, last, partial, castArray } from 'lodash'; import classnames from 'classnames'; /** @@ -117,16 +117,22 @@ export class BlockMover extends Component { export default compose( withSelect( ( select, { clientIds } ) => { - const { getBlock, getBlockIndex, getTemplateLock, getBlockRootClientId } = select( 'core/block-editor' ); - const firstClientId = first( castArray( clientIds ) ); + const { getBlock, getBlockIndex, getTemplateLock, getBlockRootClientId, getBlockOrder } = select( 'core/block-editor' ); + const normalizedClientIds = castArray( clientIds ); + const firstClientId = first( normalizedClientIds ); const block = getBlock( firstClientId ); - const rootClientId = getBlockRootClientId( first( castArray( clientIds ) ) ); + const rootClientId = getBlockRootClientId( first( normalizedClientIds ) ); + const blockOrder = getBlockOrder( rootClientId ); + const firstIndex = getBlockIndex( firstClientId, rootClientId ); + const lastIndex = getBlockIndex( last( normalizedClientIds ), rootClientId ); return { - firstIndex: getBlockIndex( firstClientId, rootClientId ), blockType: block ? getBlockType( block.name ) : null, isLocked: getTemplateLock( rootClientId ) === 'all', rootClientId, + firstIndex, + isFirst: firstIndex === 0, + isLast: lastIndex === blockOrder.length - 1, }; } ), withDispatch( ( dispatch, { clientIds, rootClientId } ) => {