From b97d3f7dcc5321d7e60685cf4f13d087e877409e Mon Sep 17 00:00:00 2001 From: Rinkal Pagdar <92097119+rinkalpagdar@users.noreply.github.com> Date: Thu, 14 Nov 2024 10:01:41 +0530 Subject: [PATCH] template parts context for site editor --- .../src/site-editor-navigation-commands.js | 79 ++++++++++++------- 1 file changed, 52 insertions(+), 27 deletions(-) diff --git a/packages/core-commands/src/site-editor-navigation-commands.js b/packages/core-commands/src/site-editor-navigation-commands.js index 2785d809d41e0..5efbd8b34f2a5 100644 --- a/packages/core-commands/src/site-editor-navigation-commands.js +++ b/packages/core-commands/src/site-editor-navigation-commands.js @@ -240,32 +240,6 @@ const getNavigationCommandLoaderPerTemplate = ( templateType ) => } ) ); - if ( - orderedRecords?.length > 0 && - templateType === 'wp_template_part' - ) { - result.push( { - name: 'core/edit-site/open-template-parts', - label: __( 'Template parts' ), - icon: symbolFilled, - callback: ( { close } ) => { - const args = { - postType: 'wp_template_part', - categoryId: 'all-parts', - }; - const targetUrl = addQueryArgs( - 'site-editor.php', - args - ); - if ( isSiteEditor ) { - history.push( args ); - } else { - document.location = targetUrl; - } - close(); - }, - } ); - } return result; }, [ canCreateTemplate, isBlockBasedTheme, orderedRecords, history ] ); @@ -274,7 +248,53 @@ const getNavigationCommandLoaderPerTemplate = ( templateType ) => isLoading, }; }; - + const getTemplatePartsCommandLoader = () => { + function useTemplatePartsCommandLoader() { + const history = useHistory(); + const isSiteEditor = getPath( window.location.href )?.includes( 'site-editor.php' ); + const { canCreateTemplate } = useSelect( + ( select ) => ({ + canCreateTemplate: select( coreStore ).canUser( 'create', { + kind: 'postType', + name: 'wp_template_part', + } ), + }), + [] + ); + + const commands = useMemo(() => { + if (!canCreateTemplate) return []; + + return [ + { + name: 'core/edit-site/open-template-parts', + label: __( 'Template parts' ), + icon: symbolFilled, + callback: ({ close }) => { + const args = { + postType: 'wp_template_part', + categoryId: 'all-parts', + }; + const targetUrl = addQueryArgs( 'site-editor.php', args ); + if (isSiteEditor) { + history.push( args ); + } else { + document.location = targetUrl; + } + close(); + }, + }, + ]; + }, [ canCreateTemplate, history, isSiteEditor ]); + + return { + commands, + isLoading: false, + }; + } + + return useTemplatePartsCommandLoader; + }; const getSiteEditorBasicNavigationCommands = () => function useSiteEditorBasicNavigationCommands() { const history = useHistory(); @@ -435,6 +455,11 @@ export function useSiteEditorNavigationCommands() { name: 'core/edit-site/navigate-template-parts', hook: getNavigationCommandLoaderPerTemplate( 'wp_template_part' ), } ); + useCommandLoader( { + name: 'core/edit-site/navigate-template-parts', + hook: getTemplatePartsCommandLoader(), + context: 'entity-edit', + } ); useCommandLoader( { name: 'core/edit-site/basic-navigation', hook: getSiteEditorBasicNavigationCommands(),