From 8ae9a7bb187118bfbb11ff54b4c6af6070f261df Mon Sep 17 00:00:00 2001 From: Kai Hao Date: Fri, 14 May 2021 14:36:50 +0800 Subject: [PATCH] Add missing keyboard shortcuts --- package-lock.json | 1 + packages/customize-widgets/package.json | 1 + .../components/keyboard-shortcuts/index.js | 90 +++++++++++++++++++ .../components/sidebar-block-editor/index.js | 7 ++ .../sidebar-block-editor/sidebar-adapter.js | 5 ++ 5 files changed, 104 insertions(+) create mode 100644 packages/customize-widgets/src/components/keyboard-shortcuts/index.js diff --git a/package-lock.json b/package-lock.json index ad1f0e05b165f..fd62a8f04b710 100644 --- a/package-lock.json +++ b/package-lock.json @@ -13491,6 +13491,7 @@ "@wordpress/i18n": "file:packages/i18n", "@wordpress/icons": "file:packages/icons", "@wordpress/is-shallow-equal": "file:packages/is-shallow-equal", + "@wordpress/keyboard-shortcuts": "file:packages/keyboard-shortcuts", "@wordpress/keycodes": "file:packages/keycodes", "@wordpress/media-utils": "file:packages/media-utils", "@wordpress/widgets": "file:packages/widgets", diff --git a/packages/customize-widgets/package.json b/packages/customize-widgets/package.json index 3324ab6c99210..63b977fd8006c 100644 --- a/packages/customize-widgets/package.json +++ b/packages/customize-widgets/package.json @@ -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", diff --git a/packages/customize-widgets/src/components/keyboard-shortcuts/index.js b/packages/customize-widgets/src/components/keyboard-shortcuts/index.js new file mode 100644 index 0000000000000..fe7349fdae04a --- /dev/null +++ b/packages/customize-widgets/src/components/keyboard-shortcuts/index.js @@ -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; diff --git a/packages/customize-widgets/src/components/sidebar-block-editor/index.js b/packages/customize-widgets/src/components/sidebar-block-editor/index.js index cf127e6123f2b..37c8d04d54522 100644 --- a/packages/customize-widgets/src/components/sidebar-block-editor/index.js +++ b/packages/customize-widgets/src/components/sidebar-block-editor/index.js @@ -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, @@ -63,9 +64,15 @@ export default function SidebarBlockEditor( { return ( <> + +