-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add edit template part menu button (#34679)
* Add edit template part menu button * Fix isTemplatePart * Use template part name and move it above detach button * Use getSelectedBlock * Remove quotation marks * Directly get from template part id
- Loading branch information
1 parent
b815cb5
commit 86b2b8f
Showing
2 changed files
with
59 additions
and
0 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
57 changes: 57 additions & 0 deletions
57
packages/edit-site/src/components/edit-template-part-menu-button/index.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,57 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useSelect, useDispatch } from '@wordpress/data'; | ||
import { | ||
store as blockEditorStore, | ||
BlockSettingsMenuControls, | ||
} from '@wordpress/block-editor'; | ||
import { store as coreStore } from '@wordpress/core-data'; | ||
import { MenuItem } from '@wordpress/components'; | ||
import { isTemplatePart } from '@wordpress/blocks'; | ||
import { __, sprintf } from '@wordpress/i18n'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { store as editSiteStore } from '../../store'; | ||
|
||
export default function EditTemplatePartMenuButton() { | ||
const selectedTemplatePart = useSelect( ( select ) => { | ||
const block = select( blockEditorStore ).getSelectedBlock(); | ||
|
||
if ( block && isTemplatePart( block ) ) { | ||
const { theme, slug } = block.attributes; | ||
|
||
return select( coreStore ).getEntityRecord( | ||
'postType', | ||
'wp_template_part', | ||
// Ideally this should be an official public API. | ||
`${ theme }//${ slug }` | ||
); | ||
} | ||
}, [] ); | ||
const { setTemplatePart } = useDispatch( editSiteStore ); | ||
|
||
if ( ! selectedTemplatePart ) { | ||
return null; | ||
} | ||
|
||
return ( | ||
<BlockSettingsMenuControls> | ||
{ ( { onClose } ) => ( | ||
<MenuItem | ||
onClick={ () => { | ||
setTemplatePart( selectedTemplatePart.id ); | ||
onClose(); | ||
} } | ||
> | ||
{ | ||
/* translators: %s: template part title */ | ||
sprintf( __( 'Edit %s' ), selectedTemplatePart.slug ) | ||
} | ||
</MenuItem> | ||
) } | ||
</BlockSettingsMenuControls> | ||
); | ||
} |