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 global styles related commands #51637

Merged
merged 4 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
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ function useSiteEditorBasicNavigationCommands() {
} );

result.push( {
name: 'core/edit-site/open-styles',
name: 'core/edit-site/open-style-variations',
label: __( 'Open style variations' ),
icon: styles,
callback: ( { close } ) => {
Expand Down
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' );
}

// 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
2 changes: 2 additions & 0 deletions packages/edit-site/src/components/layout/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import { unlock } from '../../lock-unlock';
import SavePanel from '../save-panel';
import KeyboardShortcutsRegister from '../keyboard-shortcuts/register';
import KeyboardShortcutsGlobal from '../keyboard-shortcuts/global';
import { useCommonCommands } from '../../hooks/commands/use-common-commands';
import { useEditModeCommands } from '../../hooks/commands/use-edit-mode-commands';
import PageMain from '../page-main';
import { useIsSiteEditorLoading } from './hooks';
Expand All @@ -62,6 +63,7 @@ export default function Layout() {
useSyncCanvasModeWithURL();
useCommands();
useEditModeCommands();
useCommonCommands();

const hubRef = useRef();
const { params } = useLocation();
Expand Down
167 changes: 167 additions & 0 deletions packages/edit-site/src/hooks/commands/use-common-commands.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
/**
* WordPress dependencies
*/
import { useMemo } from '@wordpress/element';
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
*/
import { unlock } from '../../lock-unlock';
import { store as editSiteStore } from '../../store';

const { useGlobalStylesReset } = unlock( blockEditorPrivateApis );
const { useHistory } = unlock( routerPrivateApis );

function useGlobalStylesResetCommands() {
const [ canReset, onReset ] = useGlobalStylesReset();
const commands = useMemo( () => {
if ( ! canReset ) {
return [];
}

return [
{
name: 'core/edit-site/reset-global-styles',
label: __( 'Reset styles to defaults' ),
icon: trash,
callback: ( { close } ) => {
close();
onReset();
},
},
];
}, [ canReset, onReset ] );

return {
isLoading: false,
commands,
};
}

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 )
);
const { set } = useDispatch( preferencesStore );
const history = useHistory();

useCommand( {
name: 'core/edit-site/open-global-styles-revisions',
label: __( 'Open styles revisions' ),
Copy link
Member

Choose a reason for hiding this comment

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

Let's change this to "Open style revisions".

Related: #51667 (comment)

icon: backup,
callback: ( { close } ) => {
close();
history.push( {
path: '/wp_global_styles',
canvas: 'edit',
} );
openGeneralSidebar( 'edit-site/global-styles' );
setEditorCanvasContainerView( 'global-styles-revisions' );
},
} );

useCommand( {
name: 'core/edit-site/open-styles',
label: __( 'Open styles' ),
callback: ( { close } ) => {
close();
history.push( {
path: '/wp_global_styles',
canvas: 'edit',
} );
openGeneralSidebar( 'edit-site/global-styles' );
},
icon: styles,
} );

useCommand( {
name: 'core/edit-site/toggle-styles-welcome-guide',
label: __( 'Learn about styles' ),
callback: ( { close } ) => {
close();
history.push( {
path: '/wp_global_styles',
canvas: 'edit',
} );
openGeneralSidebar( 'edit-site/global-styles' );
set( 'core/edit-site', 'welcomeGuideStyles', true );
// sometimes there's a focus loss that happens after some time
Copy link
Contributor

Choose a reason for hiding this comment

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

@youknowriad 👋🏻 when does the focus loss occur? Is this always a probelm with the welcome guide?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I was not able to track it properly but I think the focus loss is probably happening as a response to openGeneralSidebar( 'edit-site/global-styles' ); which means the set( 'core/edit-site', 'welcomeGuideStyles', true ); we call after it is reverted to "false" due to the modal closing because of the focus loss.

// that closes the modal, we need to force reopening it.
setTimeout( () => {
set( 'core/edit-site', 'welcomeGuideStyles', true );
}, 500 );
},
icon: help,
} );

useCommandLoader( {
name: 'core/edit-site/reset-global-styles',
hook: useGlobalStylesResetCommands,
} );
Comment on lines +158 to +161
Copy link
Member

Choose a reason for hiding this comment

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

Why does Reset styles to default use a command loader? Couldn't useCommonCommands call useGlobalStylesReset directly and then call useCommand? useGlobalStylesReset doesn't appear to do any asynchronous data loading.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's because of the canReset check. Sometimes the command need to be "hidden". I did notice this pattern already and it made me wonder whether we should replace it with an isDisabled option in the useCommand hook to unregister the command once disabled or something like that.


useCommandLoader( {
name: 'core/edit-site/open-styles-css',
hook: useGlobalStylesOpenCssCommands,
} );
}
Loading