Skip to content

Commit

Permalink
Block Editor: Don't memoize callbacks in 'BlockSettingsDropdown' (Wor…
Browse files Browse the repository at this point in the history
…dPress#59397)

* Block Editor: Don't memoize callbacks in 'BlockSettingsDropdown'
* Don't pass argument to 'updateSelectionAfterRemove'

Co-authored-by: Mamaduka <[email protected]>
Co-authored-by: tyxla <[email protected]>
  • Loading branch information
3 people authored and carstingaxion committed Mar 27, 2024
1 parent f226b54 commit 1ebd31f
Showing 1 changed file with 38 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
import { DropdownMenu, MenuGroup, MenuItem } from '@wordpress/components';
import { useDispatch, useSelect } from '@wordpress/data';
import { moreVertical } from '@wordpress/icons';
import { Children, cloneElement, useCallback } from '@wordpress/element';
import { Children, cloneElement } from '@wordpress/element';
import { __ } from '@wordpress/i18n';
import {
store as keyboardShortcutsStore,
Expand Down Expand Up @@ -127,41 +127,35 @@ export function BlockSettingsDropdown( {
const isMatch = __unstableUseShortcutEventMatch();
const hasSelectedBlocks = selectedBlockClientIds.length > 0;

const updateSelectionAfterDuplicate = useCallback(
async ( clientIdsPromise ) => {
if ( __experimentalSelectBlock ) {
const ids = await clientIdsPromise;
if ( ids && ids[ 0 ] ) {
__experimentalSelectBlock( ids[ 0 ], false );
}
}
},
[ __experimentalSelectBlock ]
);
async function updateSelectionAfterDuplicate( clientIdsPromise ) {
if ( ! __experimentalSelectBlock ) {
return;
}

const updateSelectionAfterRemove = useCallback( () => {
if ( __experimentalSelectBlock ) {
let blockToFocus = previousBlockClientId || firstParentClientId;
const ids = await clientIdsPromise;
if ( ids && ids[ 0 ] ) {
__experimentalSelectBlock( ids[ 0 ], false );
}
}

// Focus the first block if there's no previous block nor parent block.
if ( ! blockToFocus ) {
blockToFocus = getBlockOrder()[ 0 ];
}
function updateSelectionAfterRemove() {
if ( ! __experimentalSelectBlock ) {
return;
}

// Only update the selection if the original selection is removed.
const shouldUpdateSelection =
hasSelectedBlocks && getSelectedBlockClientIds().length === 0;
let blockToFocus = previousBlockClientId || firstParentClientId;

__experimentalSelectBlock( blockToFocus, shouldUpdateSelection );
// Focus the first block if there's no previous block nor parent block.
if ( ! blockToFocus ) {
blockToFocus = getBlockOrder()[ 0 ];
}
}, [
__experimentalSelectBlock,
previousBlockClientId,
firstParentClientId,
getBlockOrder,
hasSelectedBlocks,
getSelectedBlockClientIds,
] );

// Only update the selection if the original selection is removed.
const shouldUpdateSelection =
hasSelectedBlocks && getSelectedBlockClientIds().length === 0;

__experimentalSelectBlock( blockToFocus, shouldUpdateSelection );
}

// This can occur when the selected block (the parent)
// displays child blocks within a List View.
Expand All @@ -179,20 +173,17 @@ export function BlockSettingsDropdown( {
? undefined
: openedBlockSettingsMenu === currentClientId || false;

const onToggle = useCallback(
( localOpen ) => {
if ( localOpen && openedBlockSettingsMenu !== currentClientId ) {
setOpenedBlockSettingsMenu( currentClientId );
} else if (
! localOpen &&
openedBlockSettingsMenu &&
openedBlockSettingsMenu === currentClientId
) {
setOpenedBlockSettingsMenu( undefined );
}
},
[ currentClientId, openedBlockSettingsMenu, setOpenedBlockSettingsMenu ]
);
function onToggle( localOpen ) {
if ( localOpen && openedBlockSettingsMenu !== currentClientId ) {
setOpenedBlockSettingsMenu( currentClientId );
} else if (
! localOpen &&
openedBlockSettingsMenu &&
openedBlockSettingsMenu === currentClientId
) {
setOpenedBlockSettingsMenu( undefined );
}
}

return (
<BlockActions
Expand Down Expand Up @@ -233,7 +224,8 @@ export function BlockSettingsDropdown( {
canRemove
) {
event.preventDefault();
updateSelectionAfterRemove( onRemove() );
onRemove();
updateSelectionAfterRemove();
} else if (
isMatch(
'core/block-editor/duplicate',
Expand Down

0 comments on commit 1ebd31f

Please sign in to comment.