Skip to content
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
60 changes: 60 additions & 0 deletions packages/core-commands/src/site-editor-navigation-commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
symbolFilled,
styles,
navigation,
brush,
} from '@wordpress/icons';
import { privateApis as routerPrivateApis } from '@wordpress/router';
import { addQueryArgs, getPath } from '@wordpress/url';
Expand Down Expand Up @@ -400,6 +401,61 @@ const getSiteEditorBasicNavigationCommands = () =>
};
};

const getGlobalStylesOpenCssCommands = () =>
function useGlobalStylesOpenCssCommands() {
const history = useHistory();
const isSiteEditor = getPath( window.location.href )?.includes(
'site-editor.php'
);
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' ],
};
}, [] );

const commands = useMemo( () => {
if ( ! canEditCSS ) {
return [];
}

return [
{
name: 'core/open-styles-css',
label: __( 'Open custom CSS' ),
icon: brush,
callback: ( { close } ) => {
close();

if ( isSiteEditor ) {
history.navigate( '/styles?section=/css' );
} else {
document.location = addQueryArgs(
'site-editor.php',
{
p: '/styles',
section: '/css',
}
);
}
},
},
];
}, [ history, canEditCSS, isSiteEditor ] );

return {
isLoading: false,
commands,
};
};

export function useSiteEditorNavigationCommands() {
useCommandLoader( {
name: 'core/edit-site/navigate-pages',
Expand All @@ -422,4 +478,8 @@ export function useSiteEditorNavigationCommands() {
hook: getSiteEditorBasicNavigationCommands(),
context: 'site-editor',
} );
useCommandLoader( {
name: 'core/edit-site/global-styles-css',
hook: getGlobalStylesOpenCssCommands(),
} );
}
65 changes: 1 addition & 64 deletions 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 { __, isRTL } from '@wordpress/i18n';
import { rotateLeft, rotateRight, help, brush, backup } from '@wordpress/icons';
import { rotateLeft, rotateRight, help, backup } from '@wordpress/icons';
import { useCommandLoader } from '@wordpress/commands';
import { privateApis as blockEditorPrivateApis } from '@wordpress/block-editor';
import { privateApis as routerPrivateApis } from '@wordpress/router';
Expand Down Expand Up @@ -94,64 +94,6 @@ const getGlobalStylesResetCommands = () =>
};
};

const getGlobalStylesOpenCssCommands = () =>
function useGlobalStylesOpenCssCommands() {
const { openGeneralSidebar, setEditorCanvasContainerView } = unlock(
useDispatch( editSiteStore )
);
const { params } = useLocation();
const { canvas = 'view' } = params;
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' ],
};
}, [] );

const commands = useMemo( () => {
if ( ! canEditCSS ) {
return [];
}

return [
{
name: 'core/edit-site/open-styles-css',
label: __( 'Open custom CSS' ),
icon: brush,
callback: ( { close } ) => {
close();
if ( canvas !== 'edit' ) {
history.navigate( '/styles?canvas=edit', {
transition: 'canvas-mode-edit-transition',
} );
}
openGeneralSidebar( 'edit-site/global-styles' );
setEditorCanvasContainerView( 'global-styles-css' );
},
},
];
}, [
history,
openGeneralSidebar,
setEditorCanvasContainerView,
canEditCSS,
canvas,
] );

return {
isLoading: false,
commands,
};
};

const getGlobalStylesOpenRevisionsCommands = () =>
function useGlobalStylesOpenRevisionsCommands() {
const { openGeneralSidebar, setEditorCanvasContainerView } = unlock(
Expand Down Expand Up @@ -220,11 +162,6 @@ export function useCommonCommands() {
hook: getGlobalStylesResetCommands(),
} );

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

useCommandLoader( {
name: 'core/edit-site/open-styles-revisions',
hook: getGlobalStylesOpenRevisionsCommands(),
Expand Down
Loading