Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add 'Open CSS' command to the command centre #51718

Merged
merged 2 commits into from
Jun 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Expand Up @@ -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' );
}

Copy link
Contributor

Choose a reason for hiding this comment

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

This is the first time I see this effect. It's a bit weird that this relies on goTo.

I had an idea that I needed elsewhere too to solve this more elegantly: Allow NavigatorProvider to work as a controlled component. I think that's something we'll be needing anyway in the site editor to avoid race conditions when synchronizing the URL to the state. ( cc @ciampo )

(This is not for this PR)

// 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.
Expand Down
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
Expand Up @@ -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
Expand Down Expand Up @@ -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 )
Expand Down Expand Up @@ -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,
} );
}