Skip to content

Commit

Permalink
template parts context for site editor
Browse files Browse the repository at this point in the history
  • Loading branch information
rinkalpagdar committed Nov 14, 2024
1 parent fac072d commit b97d3f7
Showing 1 changed file with 52 additions and 27 deletions.
79 changes: 52 additions & 27 deletions packages/core-commands/src/site-editor-navigation-commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 ] );

Expand All @@ -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();
Expand Down Expand Up @@ -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(),
Expand Down

0 comments on commit b97d3f7

Please sign in to comment.