Skip to content

Commit

Permalink
Add commands to access template, template parts and styles (#51501)
Browse files Browse the repository at this point in the history
* Add commands to access template, template parts and styles

* Add more navigation commands

* Fix icons
  • Loading branch information
youknowriad committed Jun 15, 2023
1 parent 66df1bc commit bb9ce52
Showing 1 changed file with 118 additions and 4 deletions.
122 changes: 118 additions & 4 deletions packages/core-commands/src/site-editor-navigation-commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,14 @@ import { __ } from '@wordpress/i18n';
import { useMemo } from '@wordpress/element';
import { useSelect } from '@wordpress/data';
import { store as coreStore } from '@wordpress/core-data';
import { post, page, layout, symbolFilled } from '@wordpress/icons';
import {
post,
page,
layout,
symbolFilled,
styles,
navigation,
} from '@wordpress/icons';
import { privateApis as routerPrivateApis } from '@wordpress/router';
import { getQueryArg, addQueryArgs, getPath } from '@wordpress/url';

Expand Down Expand Up @@ -55,9 +62,6 @@ const getNavigationCommandLoaderPerPostType = ( postType ) =>
'getEntityRecords',
[ 'postType', postType, query ]
),
// We're using the string literal to check whether we're in the site editor.
/* eslint-disable-next-line @wordpress/data-no-store-string-literals */
isSiteEditor: !! select( 'edit-site' ),
};
},
[ supportsSearch, search ]
Expand Down Expand Up @@ -114,6 +118,111 @@ const useTemplateNavigationCommandLoader =
const useTemplatePartNavigationCommandLoader =
getNavigationCommandLoaderPerPostType( 'wp_template_part' );

function useSiteEditorBasicNavigationCommands() {
const history = useHistory();
const isSiteEditor = getPath( window.location.href )?.includes(
'site-editor.php'
);
const commands = useMemo( () => {
const result = [];
result.push( {
name: 'core/edit-site/open-navigation',
label: __( 'Open navigation' ),
icon: navigation,
callback: ( { close } ) => {
const args = {
path: '/navigation',
};
const targetUrl = addQueryArgs( 'site-editor.php', args );
if ( isSiteEditor ) {
history.push( args );
} else {
document.location = targetUrl;
}
close();
},
} );

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

result.push( {
name: 'core/edit-site/open-styles',
label: __( 'Open style variations' ),
icon: styles,
callback: ( { close } ) => {
const args = {
path: '/wp_global_styles',
};
const targetUrl = addQueryArgs( 'site-editor.php', args );
if ( isSiteEditor ) {
history.push( args );
} else {
document.location = targetUrl;
}
close();
},
} );

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

result.push( {
name: 'core/edit-site/open-template-parts',
label: __( 'Open library' ),
icon: symbolFilled,
callback: ( { close } ) => {
const args = {
path: '/wp_template_part',
};
const targetUrl = addQueryArgs( 'site-editor.php', args );
if ( isSiteEditor ) {
history.push( args );
} else {
document.location = targetUrl;
}
close();
},
} );
return result;
}, [ history, isSiteEditor ] );

return {
commands,
isLoading: false,
};
}

export function useSiteEditorNavigationCommands() {
useCommandLoader( {
name: 'core/edit-site/navigate-pages',
Expand All @@ -131,4 +240,9 @@ export function useSiteEditorNavigationCommands() {
name: 'core/edit-site/navigate-template-parts',
hook: useTemplatePartNavigationCommandLoader,
} );
useCommandLoader( {
name: 'core/edit-site/basic-navigation',
hook: useSiteEditorBasicNavigationCommands,
context: 'site-editor',
} );
}

1 comment on commit bb9ce52

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Flaky tests detected in bb9ce52.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/5283257119
📝 Reported issues:

Please sign in to comment.