Skip to content

Commit

Permalink
Avoid passing down isFirst and isLast props (#15043)
Browse files Browse the repository at this point in the history
  • Loading branch information
youknowriad authored Apr 19, 2019
1 parent cdb3ccd commit a9671d6
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 31 deletions.
4 changes: 0 additions & 4 deletions packages/block-editor/src/components/block-list/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -363,8 +363,6 @@ export class BlockListBlock extends Component {
isFocusMode,
hasFixedToolbar,
isLocked,
isFirst,
isLast,
clientId,
rootClientId,
isSelected,
Expand Down Expand Up @@ -527,8 +525,6 @@ export class BlockListBlock extends Component {
<BlockMover
clientIds={ clientId }
blockElementId={ blockElementId }
isFirst={ isFirst }
isLast={ isLast }
isHidden={ ! ( isHovered || isSelected ) || hoverArea !== 'left' }
isDraggable={
isDraggable !== false &&
Expand Down
4 changes: 1 addition & 3 deletions packages/block-editor/src/components/block-list/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ class BlockList extends Component {

return (
<div className="editor-block-list__layout block-editor-block-list__layout">
{ map( blockClientIds, ( clientId, blockIndex ) => {
{ map( blockClientIds, ( clientId ) => {
const isBlockInSelection = hasMultiSelection ?
multiSelectedBlockClientIds.includes( clientId ) :
selectedBlockClientId === clientId;
Expand All @@ -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 }
/>
</AsyncModeProvider>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
/**
* External dependencies
*/
import { first, last } from 'lodash';

/**
* WordPress dependencies
*/
Expand All @@ -15,41 +10,28 @@ import BlockMover from '../block-mover';

function BlockListMultiControls( {
multiSelectedBlockClientIds,
clientId,
isSelecting,
isFirst,
isLast,
} ) {
if ( isSelecting ) {
return null;
}

return (
<BlockMover
key="mover"
clientId={ clientId }
clientIds={ multiSelectedBlockClientIds }
isFirst={ isFirst }
isLast={ isLast }
/>
);
}

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 );
16 changes: 11 additions & 5 deletions packages/block-editor/src/components/block-mover/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* External dependencies
*/
import { first, partial, castArray } from 'lodash';
import { first, last, partial, castArray } from 'lodash';
import classnames from 'classnames';

/**
Expand Down Expand Up @@ -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 } ) => {
Expand Down

0 comments on commit a9671d6

Please sign in to comment.