forked from WordPress/gutenberg
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Site editor: Fix save shortcut (WordPress#66423)
Co-authored-by: ntsekouras <[email protected]> Co-authored-by: mcsf <[email protected]> Co-authored-by: youknowriad <[email protected]>
- Loading branch information
1 parent
ba17e59
commit f760da0
Showing
4 changed files
with
71 additions
and
82 deletions.
There are no files selected for viewing
51 changes: 0 additions & 51 deletions
51
packages/edit-site/src/components/keyboard-shortcuts/global.js
This file was deleted.
Oops, something went wrong.
27 changes: 0 additions & 27 deletions
27
packages/edit-site/src/components/keyboard-shortcuts/register.js
This file was deleted.
Oops, something went wrong.
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
69 changes: 69 additions & 0 deletions
69
packages/edit-site/src/components/save-keyboard-shortcut/index.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,69 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useEffect } from '@wordpress/element'; | ||
import { | ||
useShortcut, | ||
store as keyboardShortcutsStore, | ||
} from '@wordpress/keyboard-shortcuts'; | ||
import { __ } from '@wordpress/i18n'; | ||
import { useDispatch, useSelect } from '@wordpress/data'; | ||
import { store as coreStore } from '@wordpress/core-data'; | ||
import { store as editorStore } from '@wordpress/editor'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { store as editSiteStore } from '../../store'; | ||
|
||
const shortcutName = 'core/edit-site/save'; | ||
|
||
/** | ||
* Register the save keyboard shortcut in view mode. | ||
* | ||
* @return {null} Returns null. | ||
*/ | ||
export default function SaveKeyboardShortcut() { | ||
const { __experimentalGetDirtyEntityRecords, isSavingEntityRecord } = | ||
useSelect( coreStore ); | ||
const { hasNonPostEntityChanges, isPostSavingLocked } = | ||
useSelect( editorStore ); | ||
const { savePost } = useDispatch( editorStore ); | ||
const { setIsSaveViewOpened } = useDispatch( editSiteStore ); | ||
const { registerShortcut, unregisterShortcut } = useDispatch( | ||
keyboardShortcutsStore | ||
); | ||
useEffect( () => { | ||
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; | ||
} |