Skip to content

Commit

Permalink
Add command: Manage Template Parts
Browse files Browse the repository at this point in the history
  • Loading branch information
karthick-murugan committed Nov 15, 2024
1 parent 7300373 commit ca5020e
Showing 1 changed file with 55 additions and 0 deletions.
55 changes: 55 additions & 0 deletions packages/editor/src/components/commands/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
layout,
rotateRight,
rotateLeft,
category,
} from '@wordpress/icons';
import { useCommandLoader } from '@wordpress/commands';
import { store as preferencesStore } from '@wordpress/preferences';
Expand Down Expand Up @@ -278,6 +279,54 @@ const getEditorCommandLoader = () =>
};
};

const getSiteEditorTemplatePartCommands = () =>
function useSiteEditorCategoryCommands() {
if ( typeof window.goBack !== 'function' ) {
window.goBack = function () {
if ( window.history.length > 1 ) {
window.history.back();
} else {
// If no history is available, navigate to a default page
window.location.href = '/';
}
};
}

// Fetch the current template slug
const templateSlug = useSelect( ( select ) => {
const { getCurrentPost } = select( editorStore );
const currentTemplate = getCurrentPost();

// Check if we have the current post and it's of type 'wp_template_part'
return currentTemplate?.type === 'wp_template_part'
? currentTemplate.slug
: null;
}, [] );

const commands = [];

// Check if we are in the Site Editor and editing a category-related template
const isSiteEditor = window.location.href.includes( 'site-editor.php' );
if ( isSiteEditor && templateSlug ) {
commands.push( {
name: 'core/manage-categories',
label: __( 'Manage Template Parts' ),
icon: category,
callback: ( { close } ) => {
if ( typeof window.goBack === 'function' ) {
window.goBack();
} else {
window.history.back();
}
close();
},
isDefault: true,
} );
}

return { isLoading: false, commands };
};

const getEditedEntityContextualCommands = () =>
function useEditedEntityContextualCommands() {
const { postType } = useSelect( ( select ) => {
Expand Down Expand Up @@ -444,6 +493,12 @@ export default function useCommands() {
hook: getEditorCommandLoader(),
} );

useCommandLoader( {
name: 'core/editor/category-commands',
hook: getSiteEditorTemplatePartCommands(),
context: 'entity-edit',
} );

useCommandLoader( {
name: 'core/editor/contextual-commands',
hook: getEditedEntityContextualCommands(),
Expand Down

0 comments on commit ca5020e

Please sign in to comment.