diff --git a/packages/edit-site/src/components/global-styles/ui.js b/packages/edit-site/src/components/global-styles/ui.js index 218665c682def4..97c6324ffa78b6 100644 --- a/packages/edit-site/src/components/global-styles/ui.js +++ b/packages/edit-site/src/components/global-styles/ui.js @@ -260,7 +260,10 @@ function GlobalStylesEditorCanvasContainerLink() { // Switching to any container other than revisions should // redirect from the revisions screen to the root global styles screen. goTo( '/' ); + } else if ( editorCanvasContainerView === 'global-styles-css' ) { + goTo( '/css' ); } + // location?.path is not a dependency because we don't want to track it. // Doing so will cause an infinite loop. We could abstract logic to avoid // having to disable the check later. diff --git a/packages/edit-site/src/hooks/commands/use-common-commands.js b/packages/edit-site/src/hooks/commands/use-common-commands.js index 1d30eff4bf6877..34fef28c277fea 100644 --- a/packages/edit-site/src/hooks/commands/use-common-commands.js +++ b/packages/edit-site/src/hooks/commands/use-common-commands.js @@ -2,13 +2,14 @@ * WordPress dependencies */ import { useMemo } from '@wordpress/element'; -import { useDispatch } from '@wordpress/data'; +import { useDispatch, useSelect } from '@wordpress/data'; import { __ } from '@wordpress/i18n'; import { trash, backup, help, styles } from '@wordpress/icons'; import { useCommandLoader, useCommand } from '@wordpress/commands'; import { privateApis as blockEditorPrivateApis } from '@wordpress/block-editor'; import { privateApis as routerPrivateApis } from '@wordpress/router'; import { store as preferencesStore } from '@wordpress/preferences'; +import { store as coreStore } from '@wordpress/core-data'; /** * Internal dependencies @@ -45,6 +46,59 @@ function useGlobalStylesResetCommands() { }; } +function useGlobalStylesOpenCssCommands() { + const { openGeneralSidebar, setEditorCanvasContainerView } = unlock( + useDispatch( editSiteStore ) + ); + const history = useHistory(); + const { canEditCSS } = useSelect( ( select ) => { + const { getEntityRecord, __experimentalGetCurrentGlobalStylesId } = + select( coreStore ); + + const globalStylesId = __experimentalGetCurrentGlobalStylesId(); + const globalStyles = globalStylesId + ? getEntityRecord( 'root', 'globalStyles', globalStylesId ) + : undefined; + + return { + canEditCSS: + !! globalStyles?._links?.[ 'wp:action-edit-css' ] ?? false, + }; + }, [] ); + + const commands = useMemo( () => { + if ( ! canEditCSS ) { + return []; + } + + return [ + { + name: 'core/edit-site/open-styles-css', + label: __( 'Open CSS' ), + icon: styles, + callback: ( { close } ) => { + close(); + history.push( { + path: '/wp_global_styles', + canvas: 'edit', + } ); + openGeneralSidebar( 'edit-site/global-styles' ); + setEditorCanvasContainerView( 'global-styles-css' ); + }, + }, + ]; + }, [ + history, + openGeneralSidebar, + setEditorCanvasContainerView, + canEditCSS, + ] ); + return { + isLoading: false, + commands, + }; +} + export function useCommonCommands() { const { openGeneralSidebar, setEditorCanvasContainerView } = unlock( useDispatch( editSiteStore ) @@ -105,4 +159,9 @@ export function useCommonCommands() { name: 'core/edit-site/reset-global-styles', hook: useGlobalStylesResetCommands, } ); + + useCommandLoader( { + name: 'core/edit-site/open-styles-css', + hook: useGlobalStylesOpenCssCommands, + } ); }