-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Block Editor: Optimize the 'useBlockDisplayTitle' hook #58250
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,15 +3,14 @@ | |
*/ | ||
import { useSelect } from '@wordpress/data'; | ||
import { | ||
getBlockType, | ||
__experimentalGetBlockLabel as getBlockLabel, | ||
isReusableBlock, | ||
__experimentalGetBlockLabel as getBlockLabel, | ||
store as blocksStore, | ||
} from '@wordpress/blocks'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import useBlockDisplayInformation from '../use-block-display-information'; | ||
import { store as blockEditorStore } from '../../store'; | ||
|
||
/** | ||
|
@@ -35,49 +34,52 @@ export default function useBlockDisplayTitle( { | |
maximumLength, | ||
context, | ||
} ) { | ||
const { attributes, name, reusableBlockTitle } = useSelect( | ||
const blockTitle = useSelect( | ||
( select ) => { | ||
if ( ! clientId ) { | ||
return {}; | ||
return null; | ||
} | ||
|
||
const { | ||
getBlockName, | ||
getBlockAttributes, | ||
__experimentalGetReusableBlockTitle, | ||
} = select( blockEditorStore ); | ||
const { getBlockType, getActiveBlockVariation } = | ||
select( blocksStore ); | ||
|
||
const blockName = getBlockName( clientId ); | ||
if ( ! blockName ) { | ||
return {}; | ||
const blockType = getBlockType( blockName ); | ||
if ( ! blockType ) { | ||
return null; | ||
} | ||
const isReusable = isReusableBlock( getBlockType( blockName ) ); | ||
return { | ||
attributes: getBlockAttributes( clientId ), | ||
name: blockName, | ||
reusableBlockTitle: | ||
isReusable && | ||
__experimentalGetReusableBlockTitle( | ||
getBlockAttributes( clientId ).ref | ||
), | ||
}; | ||
|
||
const attributes = getBlockAttributes( clientId ); | ||
const isReusable = isReusableBlock( blockType ); | ||
const reusableBlockTitle = isReusable | ||
? __experimentalGetReusableBlockTitle( attributes.ref ) | ||
: null; | ||
|
||
if ( reusableBlockTitle ) { | ||
return reusableBlockTitle; | ||
} | ||
|
||
const label = getBlockLabel( blockType, attributes, context ); | ||
// If the label is defined we prioritize it over a possible block variation title match. | ||
if ( label !== blockType.title ) { | ||
return label; | ||
} | ||
|
||
const match = getActiveBlockVariation( blockName, attributes ); | ||
// Label will fallback to the title if no label is defined for the current label context. | ||
return match?.title || blockType.title; | ||
}, | ||
[ clientId ] | ||
[ clientId, context ] | ||
); | ||
|
||
const blockInformation = useBlockDisplayInformation( clientId ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is such a weird selector There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In the sense that you might be getting a bunch of info that you don't need |
||
if ( ! name || ! blockInformation ) { | ||
if ( ! blockTitle ) { | ||
return null; | ||
} | ||
const blockType = getBlockType( name ); | ||
const blockLabel = blockType | ||
? getBlockLabel( blockType, attributes, context ) | ||
: null; | ||
|
||
const label = reusableBlockTitle || blockLabel; | ||
// Label will fallback to the title if no label is defined for the current | ||
// label context. If the label is defined we prioritize it over a | ||
// possible block variation title match. | ||
const blockTitle = | ||
label && label !== blockType.title ? label : blockInformation.title; | ||
|
||
if ( | ||
maximumLength && | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should probably deprecate this selector and handle the pattern block title via
__experimentalLabel
.cc @talldan, @kevin940726
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note that I'm adjusting this selector here Avoid fetching ALL reusable blocks (user patterns) on post/site editor load
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here's the correct PR link - #58239.