-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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 global styles related commands #51637
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @youknowriad 👋🏻 when does the focus loss occur? Is this always a probelm with the welcome guide? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was not able to track it properly but I think the focus loss is probably happening as a response to |
||
// 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, | ||
} ); | ||
Comment on lines
+158
to
+161
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why does Reset styles to default use a command loader? Couldn't There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's because of the |
||
|
||
useCommandLoader( { | ||
name: 'core/edit-site/open-styles-css', | ||
hook: useGlobalStylesOpenCssCommands, | ||
} ); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's change this to "Open style revisions".
Related: #51667 (comment)