Skip to content

Commit

Permalink
Do not navigate to the styles pages unless you're in a random listing…
Browse files Browse the repository at this point in the history
… page (#52728)
  • Loading branch information
youknowriad authored Jul 18, 2023
1 parent 69a1573 commit 5894190
Showing 1 changed file with 56 additions and 23 deletions.
79 changes: 56 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 @@ -11,15 +11,17 @@ import { privateApis as routerPrivateApis } from '@wordpress/router';
import { store as preferencesStore } from '@wordpress/preferences';
import { store as coreStore } from '@wordpress/core-data';
import { store as noticesStore } from '@wordpress/notices';
import { useViewportMatch } from '@wordpress/compose';

/**
* Internal dependencies
*/
import { unlock } from '../../lock-unlock';
import { store as editSiteStore } from '../../store';
import getIsListPage from '../../utils/get-is-list-page';

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

function useGlobalStylesResetCommands() {
const [ canReset, onReset ] = useGlobalStylesReset();
Expand Down Expand Up @@ -48,9 +50,12 @@ function useGlobalStylesResetCommands() {
}

function useGlobalStylesOpenCssCommands() {
const { openGeneralSidebar, setEditorCanvasContainerView } = unlock(
useDispatch( editSiteStore )
);
const { openGeneralSidebar, setEditorCanvasContainerView, setCanvasMode } =
unlock( useDispatch( editSiteStore ) );
const { params } = useLocation();
const isMobileViewport = useViewportMatch( 'medium', '<' );
const isListPage = getIsListPage( params, isMobileViewport );
const isEditorPage = ! isListPage;
const history = useHistory();
const { canEditCSS } = useSelect( ( select ) => {
const { getEntityRecord, __experimentalGetCurrentGlobalStylesId } =
Expand All @@ -66,6 +71,7 @@ function useGlobalStylesOpenCssCommands() {
!! globalStyles?._links?.[ 'wp:action-edit-css' ] ?? false,
};
}, [] );
const { getCanvasMode } = unlock( useSelect( editSiteStore ) );

const commands = useMemo( () => {
if ( ! canEditCSS ) {
Expand All @@ -79,10 +85,15 @@ function useGlobalStylesOpenCssCommands() {
icon: styles,
callback: ( { close } ) => {
close();
history.push( {
path: '/wp_global_styles',
canvas: 'edit',
} );
if ( ! isEditorPage ) {
history.push( {
path: '/wp_global_styles',
canvas: 'edit',
} );
}
if ( isEditorPage && getCanvasMode() !== 'edit' ) {
setCanvasMode( 'edit' );
}
openGeneralSidebar( 'edit-site/global-styles' );
setEditorCanvasContainerView( 'global-styles-css' );
},
Expand All @@ -93,6 +104,9 @@ function useGlobalStylesOpenCssCommands() {
openGeneralSidebar,
setEditorCanvasContainerView,
canEditCSS,
isEditorPage,
getCanvasMode,
setCanvasMode,
] );
return {
isLoading: false,
Expand All @@ -101,9 +115,13 @@ function useGlobalStylesOpenCssCommands() {
}

export function useCommonCommands() {
const { openGeneralSidebar, setEditorCanvasContainerView } = unlock(
useDispatch( editSiteStore )
);
const { openGeneralSidebar, setEditorCanvasContainerView, setCanvasMode } =
unlock( useDispatch( editSiteStore ) );
const { params } = useLocation();
const isMobileViewport = useViewportMatch( 'medium', '<' );
const isListPage = getIsListPage( params, isMobileViewport );
const isEditorPage = ! isListPage;
const { getCanvasMode } = unlock( useSelect( editSiteStore ) );
const { set } = useDispatch( preferencesStore );
const { createInfoNotice } = useDispatch( noticesStore );
const history = useHistory();
Expand All @@ -127,10 +145,15 @@ export function useCommonCommands() {
icon: backup,
callback: ( { close } ) => {
close();
history.push( {
path: '/wp_global_styles',
canvas: 'edit',
} );
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' );
},
Expand All @@ -141,10 +164,15 @@ export function useCommonCommands() {
label: __( 'Open styles' ),
callback: ( { close } ) => {
close();
history.push( {
path: '/wp_global_styles',
canvas: 'edit',
} );
if ( ! isEditorPage ) {
history.push( {
path: '/wp_global_styles',
canvas: 'edit',
} );
}
if ( isEditorPage && getCanvasMode() !== 'edit' ) {
setCanvasMode( 'edit' );
}
if ( isDistractionFree ) {
set( editSiteStore.name, 'distractionFree', false );
createInfoNotice( __( 'Distraction free mode turned off.' ), {
Expand All @@ -161,10 +189,15 @@ export function useCommonCommands() {
label: __( 'Learn about styles' ),
callback: ( { close } ) => {
close();
history.push( {
path: '/wp_global_styles',
canvas: 'edit',
} );
if ( ! isEditorPage ) {
history.push( {
path: '/wp_global_styles',
canvas: 'edit',
} );
}
if ( isEditorPage && getCanvasMode() !== 'edit' ) {
setCanvasMode( 'edit' );
}
openGeneralSidebar( 'edit-site/global-styles' );
set( 'core/edit-site', 'welcomeGuideStyles', true );
// sometimes there's a focus loss that happens after some time
Expand Down

0 comments on commit 5894190

Please sign in to comment.