Skip to content
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 save shortcut for navigation menu screen #21342

Merged
merged 3 commits into from Apr 3, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
44 changes: 44 additions & 0 deletions packages/edit-navigation/src/components/menu-editor/shortcuts.js
@@ -0,0 +1,44 @@
/**
* 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,
preventDefault: true,
talldan marked this conversation as resolved.
Show resolved Hide resolved
}
);

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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know we used this pattern in the other packages too, but I wonder if a hook is better than a component for the registration :) (not specific to this PR though)


export default MenuEditorShortcuts;