Skip to content

Commit

Permalink
Add missing keyboard shortcuts
Browse files Browse the repository at this point in the history
  • Loading branch information
kevin940726 committed May 14, 2021
1 parent 4156968 commit 8ae9a7b
Show file tree
Hide file tree
Showing 5 changed files with 104 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/customize-widgets/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"@wordpress/i18n": "file:../i18n",
"@wordpress/icons": "file:../icons",
"@wordpress/is-shallow-equal": "file:../is-shallow-equal",
"@wordpress/keyboard-shortcuts": "file:../keyboard-shortcuts",
"@wordpress/keycodes": "file:../keycodes",
"@wordpress/media-utils": "file:../media-utils",
"@wordpress/widgets": "file:../widgets",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/**
* WordPress dependencies
*/
import { useEffect } from '@wordpress/element';
import {
useShortcut,
store as keyboardShortcutsStore,
} from '@wordpress/keyboard-shortcuts';
import { useDispatch } from '@wordpress/data';
import { __ } from '@wordpress/i18n';

function KeyboardShortcuts( { undo, redo, save } ) {
useShortcut(
'core/customize-widgets/undo',
( event ) => {
undo();
event.preventDefault();
},
{ bindGlobal: true }
);

useShortcut(
'core/customize-widgets/redo',
( event ) => {
redo();
event.preventDefault();
},
{ bindGlobal: true }
);

useShortcut(
'core/customize-widgets/save',
( event ) => {
event.preventDefault();
save();
},
{ bindGlobal: true }
);

return null;
}

function KeyboardShortcutsRegister() {
const { registerShortcut, unregisterShortcut } = useDispatch(
keyboardShortcutsStore
);

useEffect( () => {
registerShortcut( {
name: 'core/customize-widgets/undo',
category: 'global',
description: __( 'Undo your last changes.' ),
keyCombination: {
modifier: 'primary',
character: 'z',
},
} );

registerShortcut( {
name: 'core/customize-widgets/redo',
category: 'global',
description: __( 'Redo your last undo.' ),
keyCombination: {
modifier: 'primaryShift',
character: 'z',
},
} );

registerShortcut( {
name: 'core/customize-widgets/save',
category: 'global',
description: __( 'Save your changes.' ),
keyCombination: {
modifier: 'primary',
character: 's',
},
} );

return () => {
unregisterShortcut( 'core/customize-widgets/undo' );
unregisterShortcut( 'core/customize-widgets/redo' );
unregisterShortcut( 'core/customize-widgets/save' );
};
}, [ registerShortcut ] );

return null;
}

KeyboardShortcuts.Register = KeyboardShortcutsRegister;
export default KeyboardShortcuts;
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import BlockInspectorButton from '../block-inspector-button';
import Header from '../header';
import useInserter from '../inserter/use-inserter';
import SidebarEditorProvider from './sidebar-editor-provider';
import KeyboardShortcuts from '../keyboard-shortcuts';

export default function SidebarBlockEditor( {
blockEditorSettings,
Expand Down Expand Up @@ -63,9 +64,15 @@ export default function SidebarBlockEditor( {
return (
<>
<BlockEditorKeyboardShortcuts.Register />
<KeyboardShortcuts.Register />

<SidebarEditorProvider sidebar={ sidebar } settings={ settings }>
<BlockEditorKeyboardShortcuts />
<KeyboardShortcuts
undo={ sidebar.undo }
redo={ sidebar.redo }
save={ sidebar.save }
/>

<Header
sidebar={ sidebar }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ export default class SidebarAdapter {

this.undo = this.undo.bind( this );
this.redo = this.redo.bind( this );
this.save = this.save.bind( this );
}

subscribe( callback ) {
Expand Down Expand Up @@ -342,4 +343,8 @@ export default class SidebarAdapter {
this.historySubscribers.delete( listener );
};
}

save() {
this.api.previewer.save();
}
}

0 comments on commit 8ae9a7b

Please sign in to comment.