Skip to content

Commit

Permalink
Add save shortcut for navigation menu screen (#21342)
Browse files Browse the repository at this point in the history
* Add save shortcut for navigation menu screen

* Remove option that is not an option

* Update package lock to reflect new keyboard shortcuts dependency
  • Loading branch information
talldan committed Apr 3, 2020
1 parent c720301 commit 052c835
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 0 deletions.
1 change: 1 addition & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/edit-navigation/package.json
Expand Up @@ -33,6 +33,7 @@
"@wordpress/element": "file:../element",
"@wordpress/hooks": "file:../hooks",
"@wordpress/i18n": "file:../i18n",
"@wordpress/keyboard-shortcuts": "file:../keyboard-shortcuts",
"@wordpress/media-utils": "file:../media-utils",
"@wordpress/notices": "file:../notices",
"lodash": "^4.17.15",
Expand Down
3 changes: 3 additions & 0 deletions packages/edit-navigation/src/components/menu-editor/index.js
Expand Up @@ -16,13 +16,15 @@ import { Panel, PanelBody, Button } from '@wordpress/components';
* Internal dependencies
*/
import useNavigationBlocks from './use-navigation-blocks';
import MenuEditorShortcuts from './shortcuts';

export default function MenuEditor( { menuId, blockEditorSettings } ) {
const [ blocks, setBlocks, saveBlocks ] = useNavigationBlocks( menuId );

return (
<div className="edit-navigation-menu-editor">
<BlockEditorKeyboardShortcuts.Register />
<MenuEditorShortcuts.Register />

<BlockEditorProvider
value={ blocks }
Expand All @@ -34,6 +36,7 @@ export default function MenuEditor( { menuId, blockEditorSettings } ) {
} }
>
<BlockEditorKeyboardShortcuts />
<MenuEditorShortcuts saveBlocks={ saveBlocks } />
<Panel className="edit-navigation-menu-editor__panel">
<PanelBody title={ __( 'Navigation structure' ) }>
{ !! blocks.length && (
Expand Down
43 changes: 43 additions & 0 deletions packages/edit-navigation/src/components/menu-editor/shortcuts.js
@@ -0,0 +1,43 @@
/**
* WordPress dependencies
*/
import { useEffect, useCallback } from '@wordpress/element';
import { useDispatch } from '@wordpress/data';
import { useShortcut } from '@wordpress/keyboard-shortcuts';
import { __ } from '@wordpress/i18n';

function MenuEditorShortcuts( { saveBlocks } ) {
useShortcut(
'core/edit-navigation/save-menu',
useCallback( ( event ) => {
event.preventDefault();
saveBlocks();
} ),
{
bindGlobal: true,
}
);

return null;
}

function RegisterMenuEditorShortcuts() {
const { registerShortcut } = useDispatch( 'core/keyboard-shortcuts' );
useEffect( () => {
registerShortcut( {
name: 'core/edit-navigation/save-menu',
category: 'global',
description: __( 'Save the menu currently being edited.' ),
keyCombination: {
modifier: 'primary',
character: 's',
},
} );
}, [ registerShortcut ] );

return null;
}

MenuEditorShortcuts.Register = RegisterMenuEditorShortcuts;

export default MenuEditorShortcuts;

0 comments on commit 052c835

Please sign in to comment.