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

Command center: Add another batch of commands to the site editor #51832

Merged
merged 3 commits into from
Jun 25, 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
2 changes: 1 addition & 1 deletion packages/commands/src/hooks/use-command.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export default function useCommand( command ) {
label: command.label,
searchLabel: command.searchLabel,
icon: command.icon,
callback: currentCallback.current,
callback: ( ...args ) => currentCallback.current( ...args ),
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is an actual bug that made commands run outdated callbacks.

Copy link
Contributor

Choose a reason for hiding this comment

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

Can you give an example of this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

With this the homeUrl would get the wrong value (initial value) in the "view site" command.

} );
return () => {
unregisterCommand( command.name );
Expand Down
21 changes: 20 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 @@ -4,7 +4,7 @@
import { useMemo } from '@wordpress/element';
import { useDispatch, useSelect } from '@wordpress/data';
import { __ } from '@wordpress/i18n';
import { trash, backup, help, styles } from '@wordpress/icons';
import { trash, backup, help, styles, external } from '@wordpress/icons';
import { useCommandLoader, useCommand } from '@wordpress/commands';
import { privateApis as blockEditorPrivateApis } from '@wordpress/block-editor';
import { privateApis as routerPrivateApis } from '@wordpress/router';
Expand Down Expand Up @@ -105,6 +105,15 @@ export function useCommonCommands() {
);
const { set } = useDispatch( preferencesStore );
const history = useHistory();
const { homeUrl } = useSelect( ( select ) => {
const {
getUnstableBase, // Site index.
} = select( coreStore );

return {
homeUrl: getUnstableBase()?.home,
};
}, [] );

useCommand( {
name: 'core/edit-site/open-global-styles-revisions',
Expand Down Expand Up @@ -155,6 +164,16 @@ export function useCommonCommands() {
icon: help,
} );

useCommand( {
name: 'core/edit-site/view-site',
label: __( 'View site' ),
callback: ( { close } ) => {
close();
window.open( homeUrl, '_blank' );
},
icon: external,
} );

useCommandLoader( {
name: 'core/edit-site/reset-global-styles',
hook: useGlobalStylesResetCommands,
Expand Down
108 changes: 106 additions & 2 deletions packages/edit-site/src/hooks/commands/use-edit-mode-commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,22 @@
* WordPress dependencies
*/
import { useSelect, useDispatch } from '@wordpress/data';
import { __ } from '@wordpress/i18n';
import { trash, backup, layout, page } from '@wordpress/icons';
import { __, isRTL } from '@wordpress/i18n';
import {
code,
cog,
trash,
backup,
layout,
page,
drawerLeft,
drawerRight,
blockDefault,
} from '@wordpress/icons';
import { useCommandLoader } from '@wordpress/commands';
import { privateApis as routerPrivateApis } from '@wordpress/router';
import { store as preferencesStore } from '@wordpress/preferences';
import { store as interfaceStore } from '@wordpress/interface';

/**
* Internal dependencies
Expand Down Expand Up @@ -106,10 +118,102 @@ function useEditModeCommandLoader() {
};
}

function useEditUICommandLoader() {
const { openGeneralSidebar, closeGeneralSidebar, switchEditorMode } =
useDispatch( editSiteStore );
const { canvasMode, editorMode, activeSidebar } = useSelect(
( select ) => ( {
isPage: select( editSiteStore ).isPage(),
hasPageContentFocus: select( editSiteStore ).hasPageContentFocus(),
canvasMode: unlock( select( editSiteStore ) ).getCanvasMode(),
editorMode: select( editSiteStore ).getEditorMode(),
activeSidebar: select( interfaceStore ).getActiveComplementaryArea(
editSiteStore.name
),
} ),
[]
);
const { toggle } = useDispatch( preferencesStore );

if ( canvasMode !== 'edit' ) {
return { isLoading: false, commands: [] };
}

const commands = [];

commands.push( {
Copy link
Contributor

@ntsekouras ntsekouras Jun 23, 2023

Choose a reason for hiding this comment

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

I think it's better to getActiveComplementaryArea and add some of these commands conditionally.

--edit

Can we make these "toggles" instead? When the sidebar is already open, the command does nothing and can be confusing.

I had the tab opened for a while and missed @jasmussen similar comment 😆

name: 'core/open-settings-sidebar',
label: __( 'Toggle settings sidebar' ),
icon: isRTL() ? drawerLeft : drawerRight,
callback: ( { close } ) => {
close();
if ( activeSidebar === 'edit-site/template' ) {
closeGeneralSidebar();
} else {
openGeneralSidebar( 'edit-site/template' );
}
},
} );

commands.push( {
name: 'core/open-block-inspector',
label: __( 'Toggle block inspector' ),
icon: blockDefault,
callback: ( { close } ) => {
close();
if ( activeSidebar === 'edit-site/block-inspector' ) {
closeGeneralSidebar();
} else {
openGeneralSidebar( 'edit-site/block-inspector' );
}
},
} );

commands.push( {
name: 'core/toggle-distraction-free-mode',
label: __( 'Toggle spotlight mode' ),
icon: cog,
callback: ( { close } ) => {
toggle( 'core/edit-site', 'focusMode' );
close();
},
} );

commands.push( {
name: 'core/toggle-top-toolbar',
label: __( 'Toggle top toolbar' ),
icon: cog,
callback: ( { close } ) => {
toggle( 'core/edit-site', 'fixedToolbar' );
close();
},
} );

commands.push( {
name: 'core/toggle-code-editor',
label: __( 'Toggle code editor' ),
icon: code,
callback: ( { close } ) => {
switchEditorMode( editorMode === 'visual' ? 'text' : 'visual' );
close();
},
} );

return {
isLoading: false,
commands,
};
}

export function useEditModeCommands() {
useCommandLoader( {
name: 'core/edit-site/manipulate-document',
hook: useEditModeCommandLoader,
context: 'site-editor-edit',
} );

useCommandLoader( {
name: 'core/edit-site/edit-ui',
hook: useEditUICommandLoader,
} );
Comment on lines +215 to +218
Copy link
Member

Choose a reason for hiding this comment

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

Why are we using a command loader here instead of regular useCommand calls? None of the selectors in useEditUICommandLoader appear to do any data fetching.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Replied in the other PR :)

}