Skip to content

Commit

Permalink
Created internal helper function to create description for speak func…
Browse files Browse the repository at this point in the history
…tion
  • Loading branch information
n2erjo00 committed Sep 27, 2024
1 parent dd47674 commit 372e6d0
Showing 1 changed file with 65 additions and 3 deletions.
68 changes: 65 additions & 3 deletions packages/block-editor/src/components/block-tools/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ import { isTextField } from '@wordpress/dom';
import { Popover } from '@wordpress/components';
import { __unstableUseShortcutEventMatch as useShortcutEventMatch } from '@wordpress/keyboard-shortcuts';
import { useRef } from '@wordpress/element';
import { switchToBlockType, store as blocksStore } from '@wordpress/blocks';
import {
switchToBlockType,
store as blocksStore,
getBlockType,
} from '@wordpress/blocks';
import { speak } from '@wordpress/a11y';
import { __ } from '@wordpress/i18n';

Expand All @@ -27,6 +31,7 @@ import ZoomOutModeInserters from './zoom-out-mode-inserters';
import { useShowBlockTools } from './use-show-block-tools';
import { unlock } from '../../lock-unlock';
import getEditorRegion from '../../utils/get-editor-region';
import { getBlockMoverDescription } from '../block-mover/mover-description';

function selector( select ) {
const {
Expand Down Expand Up @@ -74,6 +79,10 @@ export default function BlockTools( {
getSelectedBlockClientIds,
getBlockRootClientId,
isGroupable,
getBlock,
getBlockIndex,
getBlockOrder,
getBlockListSettings,
} = useSelect( blockEditorStore );
const { getGroupingBlockName } = useSelect( blocksStore );
const {
Expand Down Expand Up @@ -109,15 +118,31 @@ export default function BlockTools( {
event.preventDefault();
const rootClientId = getBlockRootClientId( clientIds[ 0 ] );
moveBlocksUp( clientIds, rootClientId );
speak( __( 'Block moved up.' ) );
moveSpeak(
clientIds,
'up',
getBlockRootClientId,
getBlock,
getBlockIndex,
getBlockOrder,
getBlockListSettings
);
}
} 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 );
speak( __( 'Block moved down.' ) );
moveSpeak(
clientIds,
'down',
getBlockRootClientId,
getBlock,
getBlockIndex,
getBlockOrder,
getBlockListSettings
);
}
} else if ( isMatch( 'core/block-editor/duplicate', event ) ) {
const clientIds = getSelectedBlockClientIds();
Expand Down Expand Up @@ -262,3 +287,40 @@ 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 372e6d0

Please sign in to comment.