diff --git a/packages/edit-site/src/components/keyboard-shortcuts/global.js b/packages/edit-site/src/components/keyboard-shortcuts/global.js deleted file mode 100644 index 8ab9723e42ea6..0000000000000 --- a/packages/edit-site/src/components/keyboard-shortcuts/global.js +++ /dev/null @@ -1,51 +0,0 @@ -/** - * WordPress dependencies - */ -import { useShortcut } from '@wordpress/keyboard-shortcuts'; -import { useDispatch, useSelect } from '@wordpress/data'; -import { store as coreStore } from '@wordpress/core-data'; -import { store as editorStore } from '@wordpress/editor'; -import { privateApis as routerPrivateApis } from '@wordpress/router'; - -/** - * Internal dependencies - */ -import { store as editSiteStore } from '../../store'; -import { unlock } from '../../lock-unlock'; - -const { useHistory } = unlock( routerPrivateApis ); - -function KeyboardShortcutsGlobal() { - const { __experimentalGetDirtyEntityRecords, isSavingEntityRecord } = - useSelect( coreStore ); - const { hasNonPostEntityChanges } = useSelect( editorStore ); - const { setIsSaveViewOpened } = useDispatch( editSiteStore ); - const history = useHistory(); - - useShortcut( 'core/edit-site/save', ( event ) => { - event.preventDefault(); - - const dirtyEntityRecords = __experimentalGetDirtyEntityRecords(); - const hasDirtyEntities = !! dirtyEntityRecords.length; - const isSaving = dirtyEntityRecords.some( ( record ) => - isSavingEntityRecord( record.kind, record.name, record.key ) - ); - const _hasNonPostEntityChanges = hasNonPostEntityChanges(); - const isViewMode = - history.getLocationWithParams().params.canvas === 'view'; - if ( - ( ! hasDirtyEntities || ! _hasNonPostEntityChanges || isSaving ) && - ! isViewMode - ) { - return; - } - // At this point, we know that there are dirty entities, other than - // the edited post, and we're not in the process of saving, so open - // save view. - setIsSaveViewOpened( true ); - } ); - - return null; -} - -export default KeyboardShortcutsGlobal; diff --git a/packages/edit-site/src/components/keyboard-shortcuts/register.js b/packages/edit-site/src/components/keyboard-shortcuts/register.js deleted file mode 100644 index 8e491ab61c36f..0000000000000 --- a/packages/edit-site/src/components/keyboard-shortcuts/register.js +++ /dev/null @@ -1,27 +0,0 @@ -/** - * WordPress dependencies - */ -import { useEffect } from '@wordpress/element'; -import { store as keyboardShortcutsStore } from '@wordpress/keyboard-shortcuts'; -import { useDispatch } from '@wordpress/data'; -import { __ } from '@wordpress/i18n'; - -function KeyboardShortcutsRegister() { - // Registering the shortcuts. - const { registerShortcut } = useDispatch( keyboardShortcutsStore ); - useEffect( () => { - registerShortcut( { - name: 'core/edit-site/save', - category: 'global', - description: __( 'Save your changes.' ), - keyCombination: { - modifier: 'primary', - character: 's', - }, - } ); - }, [ registerShortcut ] ); - - return null; -} - -export default KeyboardShortcutsRegister; diff --git a/packages/edit-site/src/components/layout/index.js b/packages/edit-site/src/components/layout/index.js index 713b1f179b7ad..551d1448fde5c 100644 --- a/packages/edit-site/src/components/layout/index.js +++ b/packages/edit-site/src/components/layout/index.js @@ -35,8 +35,7 @@ import ErrorBoundary from '../error-boundary'; import { default as SiteHub, SiteHubMobile } from '../site-hub'; import ResizableFrame from '../resizable-frame'; import { unlock } from '../../lock-unlock'; -import KeyboardShortcutsRegister from '../keyboard-shortcuts/register'; -import KeyboardShortcutsGlobal from '../keyboard-shortcuts/global'; +import SaveKeyboardShortcut from '../save-keyboard-shortcut'; import { useIsSiteEditorLoading } from './hooks'; import useMovingAnimation from './animation'; import SidebarContent from '../sidebar'; @@ -80,8 +79,7 @@ export default function Layout( { route } ) { return ( <> - - + { canvas === 'view' && }
{ + registerShortcut( { + name: shortcutName, + category: 'global', + description: __( 'Save your changes.' ), + keyCombination: { + modifier: 'primary', + character: 's', + }, + } ); + return () => { + unregisterShortcut( shortcutName ); + }; + }, [ registerShortcut, unregisterShortcut ] ); + + useShortcut( 'core/edit-site/save', ( event ) => { + event.preventDefault(); + const dirtyEntityRecords = __experimentalGetDirtyEntityRecords(); + const hasDirtyEntities = !! dirtyEntityRecords.length; + const isSaving = dirtyEntityRecords.some( ( record ) => + isSavingEntityRecord( record.kind, record.name, record.key ) + ); + if ( ! hasDirtyEntities || isSaving ) { + return; + } + if ( hasNonPostEntityChanges() ) { + setIsSaveViewOpened( true ); + } else if ( ! isPostSavingLocked() ) { + savePost(); + } + } ); + + return null; +}