Skip to content

Commit

Permalink
templates context added
Browse files Browse the repository at this point in the history
  • Loading branch information
rinkalpagdar committed Nov 18, 2024
1 parent 7d21c9b commit a733e68
Showing 1 changed file with 53 additions and 20 deletions.
73 changes: 53 additions & 20 deletions packages/core-commands/src/site-editor-navigation-commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -361,26 +361,7 @@ const getSiteEditorBasicNavigationCommands = () =>
},
} );

result.push( {
name: 'core/edit-site/open-templates',
label: __( 'Templates' ),
icon: layout,
callback: ( { close } ) => {
const args = {
postType: 'wp_template',
};
const targetUrl = addQueryArgs(
'site-editor.php',
args
);
if ( isSiteEditor ) {
history.push( args );
} else {
document.location = targetUrl;
}
close();
},
} );

}

result.push( {
Expand Down Expand Up @@ -418,6 +399,53 @@ const getSiteEditorBasicNavigationCommands = () =>
};
};

const useOpenTemplatesCommandLoader = () => {
const history = useHistory();
const isSiteEditor = getPath(window.location.href)?.includes('site-editor.php');
const { isBlockBasedTheme, canCreateTemplate } = useSelect(
(select) => ({
isBlockBasedTheme: select(coreStore).getCurrentTheme()?.is_block_theme,
canCreateTemplate: select(coreStore).canUser('create', {
kind: 'postType',
name: 'wp_template',
}),
}),
[]
);

const commands = useMemo(() => {
if (!canCreateTemplate || !isBlockBasedTheme) {
return [];
}

return [
{
name: 'core/edit-site/open-templates',
label: __('Templates'),
icon: layout,
callback: ({ close }) => {
const args = {
postType: 'wp_template',
};
const targetUrl = addQueryArgs('site-editor.php', args);
if (isSiteEditor) {
history.push(args);
} else {
document.location = targetUrl;
}
close();
},
},
];
}, [canCreateTemplate, isBlockBasedTheme, history, isSiteEditor]);

return {
commands,
isLoading: false,
};
};


export function useSiteEditorNavigationCommands() {
useCommandLoader( {
name: 'core/edit-site/navigate-pages',
Expand All @@ -440,4 +468,9 @@ export function useSiteEditorNavigationCommands() {
hook: getSiteEditorBasicNavigationCommands(),
context: 'site-editor',
} );
useCommandLoader({
name: 'core/edit-site/open-templates',
hook: useOpenTemplatesCommandLoader,
context: 'entity-edit',
});
}

0 comments on commit a733e68

Please sign in to comment.