Skip to content

Commit

Permalink
Move template part Edit button from @wordpress/edit-site to @wordpres…
Browse files Browse the repository at this point in the history
…s/editor
  • Loading branch information
noisysocks committed Mar 21, 2024
1 parent ebfdca1 commit 34e9348
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 32 deletions.
1 change: 0 additions & 1 deletion packages/edit-site/src/hooks/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,3 @@
*/
import './components';
import './push-changes-to-global-styles';
import './template-part-edit';
1 change: 1 addition & 0 deletions packages/editor/src/hooks/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
import './custom-sources-backwards-compatibility';
import './default-autocompleters';
import './pattern-overrides';
import './template-part-edit';
Original file line number Diff line number Diff line change
Expand Up @@ -8,54 +8,43 @@ import { store as coreStore } from '@wordpress/core-data';
import { ToolbarButton } from '@wordpress/components';
import { addFilter } from '@wordpress/hooks';
import { createHigherOrderComponent } from '@wordpress/compose';
import { privateApis as routerPrivateApis } from '@wordpress/router';

/**
* Internal dependencies
*/
import { useLink } from '../components/routes/link';
import { unlock } from '../lock-unlock';
import { TEMPLATE_PART_POST_TYPE } from '../utils/constants';

const { useLocation } = unlock( routerPrivateApis );
import { store as editorStore } from '../store';

function EditTemplatePartMenuItem( { attributes } ) {
const { theme, slug } = attributes;
const { params } = useLocation();
const templatePart = useSelect(
const { templatePart, onNavigateToEntityRecord } = useSelect(
( select ) => {
const { getCurrentTheme, getEntityRecord } = select( coreStore );

return getEntityRecord(
'postType',
TEMPLATE_PART_POST_TYPE,
// Ideally this should be an official public API.
`${ theme || getCurrentTheme()?.stylesheet }//${ slug }`
);
const { getEditorSettings } = select( editorStore );
return {
templatePart: getEntityRecord(
'postType',
'wp_template_part',
// Ideally this should be an official public API.
`${ theme || getCurrentTheme()?.stylesheet }//${ slug }`
),
onNavigateToEntityRecord:
getEditorSettings().onNavigateToEntityRecord,
};
},
[ theme, slug ]
);

const linkProps = useLink(
{
postId: templatePart?.id,
postType: templatePart?.type,
canvas: 'edit',
},
{
fromTemplateId: params.postId || templatePart?.id,
}
);

if ( ! templatePart ) {
if ( ! templatePart || ! onNavigateToEntityRecord ) {
return null;
}

return (
<ToolbarButton
{ ...linkProps }
onClick={ ( event ) => {
linkProps.onClick( event );
onClick={ () => {
onNavigateToEntityRecord( {
postId: templatePart.id,
postType: templatePart.type,
} );
} }
>
{ __( 'Edit' ) }
Expand Down Expand Up @@ -84,6 +73,6 @@ export const withEditBlockControls = createHigherOrderComponent(

addFilter(
'editor.BlockEdit',
'core/edit-site/template-part-edit-button',
'core/editor/template-part-edit-button',
withEditBlockControls
);

0 comments on commit 34e9348

Please sign in to comment.