-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add global styles related commands (#51637)
Co-authored-by: Dean Sas <dean.sas@automattic.com>
- Loading branch information
1 parent
71d2dc5
commit 6dc6a04
Showing
4 changed files
with
173 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
167 changes: 167 additions & 0 deletions
167
packages/edit-site/src/hooks/commands/use-common-commands.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,167 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useMemo } from '@wordpress/element'; | ||
import { useDispatch, useSelect } from '@wordpress/data'; | ||
import { __ } from '@wordpress/i18n'; | ||
import { trash, backup, help, styles } from '@wordpress/icons'; | ||
import { useCommandLoader, useCommand } from '@wordpress/commands'; | ||
import { privateApis as blockEditorPrivateApis } from '@wordpress/block-editor'; | ||
import { privateApis as routerPrivateApis } from '@wordpress/router'; | ||
import { store as preferencesStore } from '@wordpress/preferences'; | ||
import { store as coreStore } from '@wordpress/core-data'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { unlock } from '../../lock-unlock'; | ||
import { store as editSiteStore } from '../../store'; | ||
|
||
const { useGlobalStylesReset } = unlock( blockEditorPrivateApis ); | ||
const { useHistory } = unlock( routerPrivateApis ); | ||
|
||
function useGlobalStylesResetCommands() { | ||
const [ canReset, onReset ] = useGlobalStylesReset(); | ||
const commands = useMemo( () => { | ||
if ( ! canReset ) { | ||
return []; | ||
} | ||
|
||
return [ | ||
{ | ||
name: 'core/edit-site/reset-global-styles', | ||
label: __( 'Reset styles to defaults' ), | ||
icon: trash, | ||
callback: ( { close } ) => { | ||
close(); | ||
onReset(); | ||
}, | ||
}, | ||
]; | ||
}, [ canReset, onReset ] ); | ||
|
||
return { | ||
isLoading: false, | ||
commands, | ||
}; | ||
} | ||
|
||
function useGlobalStylesOpenCssCommands() { | ||
const { openGeneralSidebar, setEditorCanvasContainerView } = unlock( | ||
useDispatch( editSiteStore ) | ||
); | ||
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' ] ?? false, | ||
}; | ||
}, [] ); | ||
|
||
const commands = useMemo( () => { | ||
if ( ! canEditCSS ) { | ||
return []; | ||
} | ||
|
||
return [ | ||
{ | ||
name: 'core/edit-site/open-styles-css', | ||
label: __( 'Open CSS' ), | ||
icon: styles, | ||
callback: ( { close } ) => { | ||
close(); | ||
history.push( { | ||
path: '/wp_global_styles', | ||
canvas: 'edit', | ||
} ); | ||
openGeneralSidebar( 'edit-site/global-styles' ); | ||
setEditorCanvasContainerView( 'global-styles-css' ); | ||
}, | ||
}, | ||
]; | ||
}, [ | ||
history, | ||
openGeneralSidebar, | ||
setEditorCanvasContainerView, | ||
canEditCSS, | ||
] ); | ||
return { | ||
isLoading: false, | ||
commands, | ||
}; | ||
} | ||
|
||
export function useCommonCommands() { | ||
const { openGeneralSidebar, setEditorCanvasContainerView } = unlock( | ||
useDispatch( editSiteStore ) | ||
); | ||
const { set } = useDispatch( preferencesStore ); | ||
const history = useHistory(); | ||
|
||
useCommand( { | ||
name: 'core/edit-site/open-global-styles-revisions', | ||
label: __( 'Open styles revisions' ), | ||
icon: backup, | ||
callback: ( { close } ) => { | ||
close(); | ||
history.push( { | ||
path: '/wp_global_styles', | ||
canvas: 'edit', | ||
} ); | ||
openGeneralSidebar( 'edit-site/global-styles' ); | ||
setEditorCanvasContainerView( 'global-styles-revisions' ); | ||
}, | ||
} ); | ||
|
||
useCommand( { | ||
name: 'core/edit-site/open-styles', | ||
label: __( 'Open styles' ), | ||
callback: ( { close } ) => { | ||
close(); | ||
history.push( { | ||
path: '/wp_global_styles', | ||
canvas: 'edit', | ||
} ); | ||
openGeneralSidebar( 'edit-site/global-styles' ); | ||
}, | ||
icon: styles, | ||
} ); | ||
|
||
useCommand( { | ||
name: 'core/edit-site/toggle-styles-welcome-guide', | ||
label: __( 'Learn about styles' ), | ||
callback: ( { close } ) => { | ||
close(); | ||
history.push( { | ||
path: '/wp_global_styles', | ||
canvas: 'edit', | ||
} ); | ||
openGeneralSidebar( 'edit-site/global-styles' ); | ||
set( 'core/edit-site', 'welcomeGuideStyles', true ); | ||
// sometimes there's a focus loss that happens after some time | ||
// that closes the modal, we need to force reopening it. | ||
setTimeout( () => { | ||
set( 'core/edit-site', 'welcomeGuideStyles', true ); | ||
}, 500 ); | ||
}, | ||
icon: help, | ||
} ); | ||
|
||
useCommandLoader( { | ||
name: 'core/edit-site/reset-global-styles', | ||
hook: useGlobalStylesResetCommands, | ||
} ); | ||
|
||
useCommandLoader( { | ||
name: 'core/edit-site/open-styles-css', | ||
hook: useGlobalStylesOpenCssCommands, | ||
} ); | ||
} |