-
Notifications
You must be signed in to change notification settings - Fork 4.1k
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
Pattern Overrides: Memoize controls component #63189
Draft
Mamaduka
wants to merge
1
commit into
trunk
Choose a base branch
from
optimize/pattern-overrides-hook
base: trunk
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+85
−75
Draft
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ import { privateApis as patternsPrivateApis } from '@wordpress/patterns'; | |
import { createHigherOrderComponent } from '@wordpress/compose'; | ||
import { useBlockEditingMode } from '@wordpress/block-editor'; | ||
import { useSelect } from '@wordpress/data'; | ||
import { memo } from '@wordpress/element'; | ||
import { store as blocksStore } from '@wordpress/blocks'; | ||
|
||
/** | ||
|
@@ -25,6 +26,68 @@ const { | |
PATTERN_SYNC_TYPES, | ||
} = unlock( patternsPrivateApis ); | ||
|
||
// Split into a separate component to avoid a store subscription | ||
// on every block. | ||
const MemoizedControlsWithStoreSubscription = memo( | ||
function ControlsWithStoreSubscription( props ) { | ||
const blockEditingMode = useBlockEditingMode(); | ||
const { hasPatternOverridesSource, isEditingSyncedPattern } = useSelect( | ||
( select ) => { | ||
const { getBlockBindingsSource } = unlock( | ||
select( blocksStore ) | ||
); | ||
const { getCurrentPostType, getEditedPostAttribute } = | ||
select( editorStore ); | ||
|
||
return { | ||
// For editing link to the site editor if the theme and user permissions support it. | ||
hasPatternOverridesSource: !! getBlockBindingsSource( | ||
'core/pattern-overrides' | ||
), | ||
isEditingSyncedPattern: | ||
getCurrentPostType() === PATTERN_TYPES.user && | ||
getEditedPostAttribute( 'meta' ) | ||
?.wp_pattern_sync_status !== | ||
PATTERN_SYNC_TYPES.unsynced && | ||
getEditedPostAttribute( 'wp_pattern_sync_status' ) !== | ||
PATTERN_SYNC_TYPES.unsynced, | ||
}; | ||
}, | ||
[] | ||
); | ||
|
||
const bindings = props.metadata?.bindings; | ||
const hasPatternBindings = | ||
!! bindings && | ||
Object.values( bindings ).some( | ||
( binding ) => binding.source === 'core/pattern-overrides' | ||
); | ||
|
||
const shouldShowPatternOverridesControls = | ||
isEditingSyncedPattern && blockEditingMode === 'default'; | ||
const shouldShowResetOverridesControl = | ||
! isEditingSyncedPattern && | ||
!! props.metadata?.name && | ||
blockEditingMode !== 'disabled' && | ||
hasPatternBindings; | ||
|
||
if ( ! hasPatternOverridesSource ) { | ||
return null; | ||
} | ||
|
||
return ( | ||
<> | ||
{ shouldShowPatternOverridesControls && ( | ||
<PatternOverridesControls { ...props } /> | ||
) } | ||
{ shouldShowResetOverridesControl && ( | ||
<ResetOverridesControl { ...props } /> | ||
) } | ||
</> | ||
); | ||
} | ||
); | ||
|
||
/** | ||
* Override the default edit UI to include a new block inspector control for | ||
* assigning a partial syncing controls to supported blocks in the pattern editor. | ||
|
@@ -41,9 +104,19 @@ const withPatternOverrideControls = createHigherOrderComponent( | |
|
||
return ( | ||
<> | ||
<BlockEdit { ...props } /> | ||
<BlockEdit key="edit" { ...props } /> | ||
{ props.isSelected && isSupportedBlock && ( | ||
<ControlsWithStoreSubscription { ...props } /> | ||
<MemoizedControlsWithStoreSubscription | ||
clientId={ props.clientId } | ||
name={ props.name } | ||
metadata={ props.attributes.metadata } | ||
setAttributes={ props.setAttributes } | ||
hasUnsupportedAttributes={ | ||
props.name === 'core/image' && | ||
( !! props.attributes.caption?.length || | ||
!! props.attributes.href?.length ) | ||
} | ||
Comment on lines
+114
to
+118
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. I had to lift this condition to avoid passing the whole attribute object. |
||
/> | ||
) } | ||
{ isSupportedBlock && <PatternOverridesBlockControls /> } | ||
</> | ||
|
@@ -52,63 +125,6 @@ const withPatternOverrideControls = createHigherOrderComponent( | |
'withPatternOverrideControls' | ||
); | ||
|
||
// Split into a separate component to avoid a store subscription | ||
// on every block. | ||
function ControlsWithStoreSubscription( props ) { | ||
const blockEditingMode = useBlockEditingMode(); | ||
const { hasPatternOverridesSource, isEditingSyncedPattern } = useSelect( | ||
( select ) => { | ||
const { getBlockBindingsSource } = unlock( select( blocksStore ) ); | ||
const { getCurrentPostType, getEditedPostAttribute } = | ||
select( editorStore ); | ||
|
||
return { | ||
// For editing link to the site editor if the theme and user permissions support it. | ||
hasPatternOverridesSource: !! getBlockBindingsSource( | ||
'core/pattern-overrides' | ||
), | ||
isEditingSyncedPattern: | ||
getCurrentPostType() === PATTERN_TYPES.user && | ||
getEditedPostAttribute( 'meta' )?.wp_pattern_sync_status !== | ||
PATTERN_SYNC_TYPES.unsynced && | ||
getEditedPostAttribute( 'wp_pattern_sync_status' ) !== | ||
PATTERN_SYNC_TYPES.unsynced, | ||
}; | ||
}, | ||
[] | ||
); | ||
|
||
const bindings = props.attributes.metadata?.bindings; | ||
const hasPatternBindings = | ||
!! bindings && | ||
Object.values( bindings ).some( | ||
( binding ) => binding.source === 'core/pattern-overrides' | ||
); | ||
|
||
const shouldShowPatternOverridesControls = | ||
isEditingSyncedPattern && blockEditingMode === 'default'; | ||
const shouldShowResetOverridesControl = | ||
! isEditingSyncedPattern && | ||
!! props.attributes.metadata?.name && | ||
blockEditingMode !== 'disabled' && | ||
hasPatternBindings; | ||
|
||
if ( ! hasPatternOverridesSource ) { | ||
return null; | ||
} | ||
|
||
return ( | ||
<> | ||
{ shouldShowPatternOverridesControls && ( | ||
<PatternOverridesControls { ...props } /> | ||
) } | ||
{ shouldShowResetOverridesControl && ( | ||
<ResetOverridesControl { ...props } /> | ||
) } | ||
</> | ||
); | ||
} | ||
|
||
addFilter( | ||
'editor.BlockEdit', | ||
'core/editor/with-pattern-override-controls', | ||
|
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
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.
Can't we use
createBlockEditFilter
like the other hooks?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.
This package is different, but I can copy the same logic here. This is still draft PR :)
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.
Oh, maybe we could add a private API for now?
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.
This is only the
editor.BlockEdit
filter in the editor package. Does it make sense to abstract it now?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.
I think it does at some point anyway, we need to start thinking about the block supports API.
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.
In my case, to actually start building it 😅 Hopefully, I can share something before 6.7 betas.
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.
Block bindings panels should use it too, in case it helps.
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.
@cbravobernal, which component exactly are we talking about?
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.
I'm still working on it, and will move it from
block-editor
toeditor
package. But is this one:#62880