-
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
- Loading branch information
1 parent
2881d64
commit d8e58d6
Showing
4 changed files
with
134 additions
and
2 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
75 changes: 75 additions & 0 deletions
75
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,75 @@ | ||
/** | ||
* 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 ) { | ||
return blocks.find( ( block ) => { | ||
if ( block.attributes.metadata?.id === id ) { | ||
return block; | ||
} | ||
|
||
return recursivelyFindBlockWithId( block.innerBlocks, id ); | ||
} ); | ||
} | ||
|
||
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.overrides?.[ 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> | ||
<ToolbarGroup> | ||
<ToolbarButton | ||
onClick={ resetOverrides } | ||
disabled={ ! patternWithOverrides } | ||
__experimentalIsFocusable | ||
> | ||
{ __( 'Reset to original' ) } | ||
</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