-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a control per block to reset pattern overrides (#57907)
* Add a control per block to reset pattern overrides * Reword the control * Update code to check for new block condensed bindings format * Fix the name field * Add e2e test * Fix nested block --------- Co-authored-by: kevin940726 <[email protected]> Co-authored-by: talldan <[email protected]> Co-authored-by: glendaviesnz <[email protected]>
- Loading branch information
1 parent
c9568e2
commit 0d2f5e9
Showing
5 changed files
with
209 additions
and
6 deletions.
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
78 changes: 78 additions & 0 deletions
78
packages/patterns/src/components/reset-overrides-control.js
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 |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { | ||
store as blockEditorStore, | ||
BlockControls, | ||
} from '@wordpress/block-editor'; | ||
import { ToolbarButton, ToolbarGroup } from '@wordpress/components'; | ||
import { useSelect, useRegistry } from '@wordpress/data'; | ||
import { store as coreStore } from '@wordpress/core-data'; | ||
import { parse } from '@wordpress/blocks'; | ||
import { __ } from '@wordpress/i18n'; | ||
|
||
function recursivelyFindBlockWithId( blocks, id ) { | ||
for ( const block of blocks ) { | ||
if ( block.attributes.metadata?.id === id ) { | ||
return block; | ||
} | ||
|
||
const found = recursivelyFindBlockWithId( block.innerBlocks, id ); | ||
if ( found ) { | ||
return found; | ||
} | ||
} | ||
} | ||
|
||
export default function ResetOverridesControl( props ) { | ||
const registry = useRegistry(); | ||
const id = props.attributes.metadata?.id; | ||
const patternWithOverrides = useSelect( | ||
( select ) => { | ||
if ( ! id ) { | ||
return undefined; | ||
} | ||
|
||
const { getBlockParentsByBlockName, getBlocksByClientId } = | ||
select( blockEditorStore ); | ||
const patternBlock = getBlocksByClientId( | ||
getBlockParentsByBlockName( props.clientId, 'core/block' ) | ||
)[ 0 ]; | ||
|
||
if ( ! patternBlock?.attributes.content?.[ id ] ) { | ||
return undefined; | ||
} | ||
|
||
return patternBlock; | ||
}, | ||
[ props.clientId, id ] | ||
); | ||
|
||
const resetOverrides = async () => { | ||
const editedRecord = await registry | ||
.resolveSelect( coreStore ) | ||
.getEditedEntityRecord( | ||
'postType', | ||
'wp_block', | ||
patternWithOverrides.attributes.ref | ||
); | ||
const blocks = editedRecord.blocks ?? parse( editedRecord.content ); | ||
const block = recursivelyFindBlockWithId( blocks, id ); | ||
|
||
props.setAttributes( block.attributes ); | ||
}; | ||
|
||
return ( | ||
<BlockControls group="other"> | ||
<ToolbarGroup> | ||
<ToolbarButton | ||
onClick={ resetOverrides } | ||
disabled={ ! patternWithOverrides } | ||
__experimentalIsFocusable | ||
> | ||
{ __( 'Reset' ) } | ||
</ToolbarButton> | ||
</ToolbarGroup> | ||
</BlockControls> | ||
); | ||
} |
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
0d2f5e9
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.
Flaky tests detected in 0d2f5e9.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.
🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/7810212394
📝 Reported issues:
/test/e2e/specs/interactivity/directive-bind.spec.ts