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 styles revisions command conditionally #52945

Merged
merged 4 commits into from
Jul 26, 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
86 changes: 63 additions & 23 deletions packages/edit-site/src/hooks/commands/use-common-commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,13 +114,68 @@ function useGlobalStylesOpenCssCommands() {
};
}

export function useCommonCommands() {
function useGlobalStylesOpenRevisionsCommands() {
const { openGeneralSidebar, setEditorCanvasContainerView, setCanvasMode } =
unlock( useDispatch( editSiteStore ) );
const { getCanvasMode } = unlock( useSelect( editSiteStore ) );
const { params } = useLocation();
const isMobileViewport = useViewportMatch( 'medium', '<' );
const isListPage = getIsListPage( params, isMobileViewport );
const isEditorPage = ! isListPage;
const isEditorPage = ! getIsListPage( params, isMobileViewport );
const history = useHistory();
const hasRevisions = useSelect(
( select ) =>
select( coreStore ).getCurrentThemeGlobalStylesRevisions()?.length,
[]
);
const commands = useMemo( () => {
if ( ! hasRevisions ) {
return [];
}

return [
{
name: 'core/edit-site/open-global-styles-revisions',
label: __( 'Open styles revisions' ),
icon: backup,
callback: ( { close } ) => {
close();
if ( ! isEditorPage ) {
history.push( {
path: '/wp_global_styles',
canvas: 'edit',
} );
}
if ( isEditorPage && getCanvasMode() !== 'edit' ) {
setCanvasMode( 'edit' );
}
openGeneralSidebar( 'edit-site/global-styles' );
setEditorCanvasContainerView( 'global-styles-revisions' );
},
},
];
}, [
hasRevisions,
history,
openGeneralSidebar,
setEditorCanvasContainerView,
isEditorPage,
getCanvasMode,
setCanvasMode,
] );

return {
isLoading: false,
commands,
};
}

export function useCommonCommands() {
const { openGeneralSidebar, setCanvasMode } = unlock(
useDispatch( editSiteStore )
);
const { params } = useLocation();
const isMobileViewport = useViewportMatch( 'medium', '<' );
const isEditorPage = ! getIsListPage( params, isMobileViewport );
const { getCanvasMode } = unlock( useSelect( editSiteStore ) );
const { set } = useDispatch( preferencesStore );
const { createInfoNotice } = useDispatch( noticesStore );
Expand All @@ -139,26 +194,6 @@ export function useCommonCommands() {
};
}, [] );

useCommand( {
name: 'core/edit-site/open-global-styles-revisions',
label: __( 'Open styles revisions' ),
icon: backup,
callback: ( { close } ) => {
close();
if ( ! isEditorPage ) {
history.push( {
path: '/wp_global_styles',
canvas: 'edit',
} );
}
if ( isEditorPage && getCanvasMode() !== 'edit' ) {
setCanvasMode( 'edit' );
}
openGeneralSidebar( 'edit-site/global-styles' );
setEditorCanvasContainerView( 'global-styles-revisions' );
},
} );

useCommand( {
name: 'core/edit-site/open-styles',
label: __( 'Open styles' ),
Expand Down Expand Up @@ -228,4 +263,9 @@ export function useCommonCommands() {
name: 'core/edit-site/open-styles-css',
hook: useGlobalStylesOpenCssCommands,
} );

useCommandLoader( {
name: 'core/edit-site/open-styles-revisions',
hook: useGlobalStylesOpenRevisionsCommands,
} );
}
16 changes: 0 additions & 16 deletions test/e2e/specs/site-editor/user-global-styles-revisions.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,6 @@ test.describe( 'Global styles revisions', () => {
await admin.visitSiteEditor();
} );

test( 'should display no revisions message if landing via command center', async ( {
page,
} ) => {
await page
.getByRole( 'button', { name: 'Open command palette' } )
.focus();
await page.keyboard.press( 'Meta+k' );
await page.keyboard.type( 'styles revisions' );
await page
.getByRole( 'option', { name: 'Open styles revisions' } )
.click();
await expect(
page.getByTestId( 'global-styles-no-revisions' )
).toHaveText( 'No results found.' );
} );

test( 'should display revisions UI when there is more than 1 revision', async ( {
page,
editor,
Expand Down