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
Changes from 1 commit
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
83 changes: 60 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 @@ -19,6 +19,7 @@ import { useViewportMatch } from '@wordpress/compose';
import { unlock } from '../../lock-unlock';
import { store as editSiteStore } from '../../store';
import getIsListPage from '../../utils/get-is-list-page';
import useGlobalStylesRevisions from '../../components/global-styles/screen-revisions/use-global-styles-revisions';

const { useGlobalStylesReset } = unlock( blockEditorPrivateApis );
const { useHistory, useLocation } = unlock( routerPrivateApis );
Expand Down Expand Up @@ -114,13 +115,64 @@ 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 { revisions } = useGlobalStylesRevisions();
ntsekouras marked this conversation as resolved.
Show resolved Hide resolved
const commands = useMemo( () => {
if ( ! revisions?.length ) {
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' );
},
},
];
}, [
revisions,
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 +191,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 +260,9 @@ export function useCommonCommands() {
name: 'core/edit-site/open-styles-css',
hook: useGlobalStylesOpenCssCommands,
} );

useCommandLoader( {
name: 'core/edit-site/open-styles-revisions',
hook: useGlobalStylesOpenRevisionsCommands,
} );
}