Skip to content

Commit

Permalink
Add 'Open CSS' command to the command centre (#51718)
Browse files Browse the repository at this point in the history
  • Loading branch information
dsas authored Jun 21, 2023
1 parent cabe3f7 commit 2de0919
Showing 2 changed files with 63 additions and 1 deletion.
3 changes: 3 additions & 0 deletions packages/edit-site/src/components/global-styles/ui.js
Original file line number Diff line number Diff line change
@@ -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.
61 changes: 60 additions & 1 deletion packages/edit-site/src/hooks/commands/use-common-commands.js
Original file line number Diff line number Diff line change
@@ -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,
} );
}

0 comments on commit 2de0919

Please sign in to comment.