Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Navigation: Improve selector performance #40700

Merged
merged 2 commits into from
Apr 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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(
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cleanup: The component wasn't using this.

clientId,
false
),
hasDescendants: !! descendants,
selectedBlockHasDescendants: !! getClientIdsOfDescendants( [
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same for selectedBlockHasDescendants.

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