Skip to content

Commit

Permalink
Navigation: Improve selector performance (#40700)
Browse files Browse the repository at this point in the history
* Update variable names and cleanup
  • Loading branch information
Mamaduka authored Apr 29, 2022
1 parent d6aafa2 commit 351c3a6
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 42 deletions.
23 changes: 5 additions & 18 deletions packages/block-library/src/navigation-link/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -452,26 +452,20 @@ export default function NavigationLinkEdit( {
isAtMaxNesting,
isTopLevelLink,
isParentOfSelectedBlock,
hasDescendants,
hasChildren,
userCanCreatePages,
userCanCreatePosts,
} = useSelect(
( select ) => {
const {
getBlocks,
getBlockCount,
getBlockName,
getBlockRootClientId,
getClientIdsOfDescendants,
hasSelectedInnerBlock,
getSelectedBlockClientId,
getBlockParentsByBlockName,
} = select( blockEditorStore );

const selectedBlockId = getSelectedBlockClientId();

const descendants = getClientIdsOfDescendants( [ clientId ] )
.length;

return {
innerBlocks: getBlocks( clientId ),
isAtMaxNesting:
Expand All @@ -486,14 +480,7 @@ export default function NavigationLinkEdit( {
clientId,
true
),
isImmediateParentOfSelectedBlock: hasSelectedInnerBlock(
clientId,
false
),
hasDescendants: !! descendants,
selectedBlockHasDescendants: !! getClientIdsOfDescendants( [
selectedBlockId,
] )?.length,
hasChildren: !! getBlockCount( clientId ),
userCanCreatePages: select( coreStore ).canUser(
'create',
'pages'
Expand Down Expand Up @@ -537,7 +524,7 @@ export default function NavigationLinkEdit( {
setIsLinkOpen( true );
}
// If block has inner blocks, transform to Submenu.
if ( hasDescendants ) {
if ( hasChildren ) {
transformToSubmenu();
}
}, [] );
Expand Down Expand Up @@ -647,7 +634,7 @@ export default function NavigationLinkEdit( {
'is-editing': isSelected || isParentOfSelectedBlock,
'is-dragging-within': isDraggingWithin,
'has-link': !! url,
'has-child': hasDescendants,
'has-child': hasChildren,
'has-text-color': !! textColor || !! customTextColor,
[ getColorClassName( 'color', textColor ) ]: !! textColor,
'has-background': !! backgroundColor || customBackgroundColor,
Expand Down
30 changes: 13 additions & 17 deletions packages/block-library/src/navigation-submenu/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -309,37 +309,33 @@ export default function NavigationSubmenuEdit( {
isTopLevelItem,
isParentOfSelectedBlock,
isImmediateParentOfSelectedBlock,
hasDescendants,
selectedBlockHasDescendants,
hasChildren,
selectedBlockHasChildren,
userCanCreatePages,
userCanCreatePosts,
onlyDescendantIsEmptyLink,
} = useSelect(
( select ) => {
const {
getClientIdsOfDescendants,
hasSelectedInnerBlock,
getSelectedBlockClientId,
getBlockParentsByBlockName,
getBlock,
getBlockCount,
getBlockOrder,
} = select( blockEditorStore );

let _onlyDescendantIsEmptyLink;

const selectedBlockId = getSelectedBlockClientId();

const descendants = getClientIdsOfDescendants( [ clientId ] )
.length;

const selectedBlockDescendants = getClientIdsOfDescendants( [
selectedBlockId,
] );
const selectedBlockChildren = getBlockOrder( selectedBlockId );

// Check for a single descendant in the submenu. If that block
// is a link block in a "placeholder" state with no label then
// we can consider as an "empty" link.
if ( selectedBlockDescendants?.length === 1 ) {
const singleBlock = getBlock( selectedBlockDescendants[ 0 ] );
if ( selectedBlockChildren?.length === 1 ) {
const singleBlock = getBlock( selectedBlockChildren[ 0 ] );

_onlyDescendantIsEmptyLink =
singleBlock?.name === 'core/navigation-link' &&
Expand All @@ -360,8 +356,8 @@ export default function NavigationSubmenuEdit( {
clientId,
false
),
hasDescendants: !! descendants,
selectedBlockHasDescendants: !! selectedBlockDescendants?.length,
hasChildren: !! getBlockCount( clientId ),
selectedBlockHasChildren: !! selectedBlockChildren?.length,
userCanCreatePages: select( coreStore ).canUser(
'create',
'pages'
Expand Down Expand Up @@ -481,7 +477,7 @@ export default function NavigationSubmenuEdit( {
'is-editing': isSelected || isParentOfSelectedBlock,
'is-dragging-within': isDraggingWithin,
'has-link': !! url,
'has-child': hasDescendants,
'has-child': hasChildren,
'has-text-color': !! textColor || !! customTextColor,
[ getColorClassName( 'color', textColor ) ]: !! textColor,
'has-background': !! backgroundColor || customBackgroundColor,
Expand Down Expand Up @@ -538,9 +534,9 @@ export default function NavigationSubmenuEdit( {
renderAppender:
isSelected ||
( isImmediateParentOfSelectedBlock &&
! selectedBlockHasDescendants ) ||
! selectedBlockHasChildren ) ||
// Show the appender while dragging to allow inserting element between item and the appender.
hasDescendants
hasChildren
? InnerBlocks.ButtonBlockAppender
: false,
}
Expand All @@ -554,7 +550,7 @@ export default function NavigationSubmenuEdit( {
}

const canConvertToLink =
! selectedBlockHasDescendants || onlyDescendantIsEmptyLink;
! selectedBlockHasChildren || onlyDescendantIsEmptyLink;

return (
<Fragment>
Expand Down
12 changes: 5 additions & 7 deletions packages/block-library/src/navigation/edit/inner-blocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,12 @@ export default function NavigationInnerBlocks( {
} ) {
const {
isImmediateParentOfSelectedBlock,
selectedBlockHasDescendants,
selectedBlockHasChildren,
isSelected,
} = useSelect(
( select ) => {
const {
getClientIdsOfDescendants,
getBlockCount,
hasSelectedInnerBlock,
getSelectedBlockClientId,
} = select( blockEditorStore );
Expand All @@ -60,9 +60,7 @@ export default function NavigationInnerBlocks( {
clientId,
false
),
selectedBlockHasDescendants: !! getClientIdsOfDescendants( [
selectedBlockId,
] )?.length,
selectedBlockHasChildren: !! getBlockCount( selectedBlockId ),

// This prop is already available but computing it here ensures it's
// fresh compared to isImmediateParentOfSelectedBlock.
Expand Down Expand Up @@ -93,7 +91,7 @@ export default function NavigationInnerBlocks( {
// appender.
const parentOrChildHasSelection =
isSelected ||
( isImmediateParentOfSelectedBlock && ! selectedBlockHasDescendants );
( isImmediateParentOfSelectedBlock && ! selectedBlockHasChildren );

const placeholder = useMemo( () => <PlaceholderPreview />, [] );

Expand Down Expand Up @@ -127,7 +125,7 @@ export default function NavigationInnerBlocks( {
renderAppender:
isSelected ||
( isImmediateParentOfSelectedBlock &&
! selectedBlockHasDescendants ) ||
! selectedBlockHasChildren ) ||
// Show the appender while dragging to allow inserting element between item and the appender.
parentOrChildHasSelection
? InnerBlocks.ButtonBlockAppender
Expand Down

0 comments on commit 351c3a6

Please sign in to comment.