Skip to content

Commit

Permalink
Refactor away from helper function and simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
n2erjo00 committed Oct 2, 2024
1 parent 372e6d0 commit 9800c85
Showing 1 changed file with 37 additions and 62 deletions.
99 changes: 37 additions & 62 deletions packages/block-editor/src/components/block-tools/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,37 +112,49 @@ export default function BlockTools( {
return;
}

if ( isMatch( 'core/block-editor/move-up', event ) ) {
if (
isMatch( 'core/block-editor/move-up', event ) ||
isMatch( 'core/block-editor/move-down', event )
) {
const clientIds = getSelectedBlockClientIds();
if ( clientIds.length ) {
event.preventDefault();
const rootClientId = getBlockRootClientId( clientIds[ 0 ] );
moveBlocksUp( clientIds, rootClientId );
moveSpeak(
clientIds,
'up',
getBlockRootClientId,
getBlock,
getBlockIndex,
getBlockOrder,
getBlockListSettings
const direction = isMatch( 'core/block-editor/move-up', event )
? 'up'
: 'down';
if ( direction === 'up' ) {
moveBlocksUp( clientIds, rootClientId );
} else {
moveBlocksDown( clientIds, rootClientId );
}
const normalizedClientIds = Array.isArray( clientIds )
? clientIds
: [ clientIds ];
const blocksCount = normalizedClientIds.length;
const block = getBlock( normalizedClientIds[ 0 ] );
const blockType = block ? getBlockType( block.name ) : null;
const firstBlockIndex = getBlockIndex(
normalizedClientIds[ 0 ]
);
}
} else if ( isMatch( 'core/block-editor/move-down', event ) ) {
const clientIds = getSelectedBlockClientIds();
if ( clientIds.length ) {
event.preventDefault();
const rootClientId = getBlockRootClientId( clientIds[ 0 ] );
moveBlocksDown( clientIds, rootClientId );
moveSpeak(
clientIds,
'down',
getBlockRootClientId,
getBlock,
getBlockIndex,
getBlockOrder,
getBlockListSettings
const blockOrder = getBlockOrder( rootClientId );
const lastBlockIndex = getBlockIndex(
normalizedClientIds[ normalizedClientIds.length - 1 ]
);
const isFirstBlock = firstBlockIndex === 0;
const isLastBlock = lastBlockIndex === blockOrder.length - 1;
const blockSettings = getBlockListSettings( rootClientId );
const orientation = blockSettings?.orientation || 'vertical';
const description = getBlockMoverDescription(
blocksCount,
blockType?.title,
firstBlockIndex,
isFirstBlock,
isLastBlock,
direction === 'up' ? -1 : 1,
orientation
);
speak( description );
}
} else if ( isMatch( 'core/block-editor/duplicate', event ) ) {
const clientIds = getSelectedBlockClientIds();
Expand Down Expand Up @@ -287,40 +299,3 @@ export default function BlockTools( {
</div>
);
}

const moveSpeak = (
clientIds,
direction,
getBlockRootClientId,
getBlock,
getBlockIndex,
getBlockOrder,
getBlockListSettings
) => {
const rootClientId = getBlockRootClientId( clientIds[ 0 ] );
const normalizedClientIds = Array.isArray( clientIds )
? clientIds
: [ clientIds ];
const blocksCount = normalizedClientIds.length;
const block = getBlock( normalizedClientIds[ 0 ] );
const blockType = block ? getBlockType( block.name ) : null;
const firstBlockIndex = getBlockIndex( normalizedClientIds[ 0 ] );
const blockOrder = getBlockOrder( rootClientId );
const lastBlockIndex = getBlockIndex(
normalizedClientIds[ normalizedClientIds.length - 1 ]
);
const isFirstBlock = firstBlockIndex === 0;
const isLastBlock = lastBlockIndex === blockOrder.length - 1;
const blockSettings = getBlockListSettings( rootClientId );
const orientation = blockSettings?.orientation || 'vertical';
const description = getBlockMoverDescription(
blocksCount,
blockType?.title,
firstBlockIndex,
isFirstBlock,
isLastBlock,
direction === 'up' ? -1 : 1,
orientation
);
speak( description );
};

0 comments on commit 9800c85

Please sign in to comment.