-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor the visual editor shortcuts to use the keyboard-shortcuts pa…
…ckage (#19318)
- Loading branch information
1 parent
ffcd657
commit 3757147
Showing
12 changed files
with
134 additions
and
182 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
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
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
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
47 changes: 47 additions & 0 deletions
47
packages/editor/src/components/global-keyboard-shortcuts/register-shortcuts.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,47 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useEffect } from '@wordpress/element'; | ||
import { useDispatch } from '@wordpress/data'; | ||
import { __ } from '@wordpress/i18n'; | ||
import { BlockEditorKeyboardShortcuts } from '@wordpress/block-editor'; | ||
|
||
function EditorKeyboardShortcutsRegister() { | ||
// Registering the shortcuts | ||
const { registerShortcut } = useDispatch( 'core/keyboard-shortcuts' ); | ||
useEffect( () => { | ||
registerShortcut( { | ||
name: 'core/editor/save', | ||
category: 'global', | ||
description: __( 'Save your changes.' ), | ||
keyCombination: { | ||
modifier: 'primary', | ||
character: 's', | ||
}, | ||
} ); | ||
|
||
registerShortcut( { | ||
name: 'core/editor/undo', | ||
category: 'global', | ||
description: __( 'Undo your last changes.' ), | ||
keyCombination: { | ||
modifier: 'primary', | ||
character: 'z', | ||
}, | ||
} ); | ||
|
||
registerShortcut( { | ||
name: 'core/editor/redo', | ||
category: 'global', | ||
description: __( 'Redo your last undo.' ), | ||
keyCombination: { | ||
modifier: 'primaryShift', | ||
character: 'z', | ||
}, | ||
} ); | ||
}, [ registerShortcut ] ); | ||
|
||
return <BlockEditorKeyboardShortcuts.Register />; | ||
} | ||
|
||
export default EditorKeyboardShortcutsRegister; |
63 changes: 21 additions & 42 deletions
63
packages/editor/src/components/global-keyboard-shortcuts/save-shortcut.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 |
---|---|---|
@@ -1,50 +1,29 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { KeyboardShortcuts } from '@wordpress/components'; | ||
import { rawShortcut } from '@wordpress/keycodes'; | ||
import { compose } from '@wordpress/compose'; | ||
import { withSelect, withDispatch } from '@wordpress/data'; | ||
import { useShortcut } from '@wordpress/keyboard-shortcuts'; | ||
import { useDispatch, useSelect } from '@wordpress/data'; | ||
|
||
export function SaveShortcut( { onSave } ) { | ||
return ( | ||
<KeyboardShortcuts | ||
bindGlobal | ||
shortcuts={ { | ||
[ rawShortcut.primary( 's' ) ]: ( event ) => { | ||
event.preventDefault(); | ||
onSave(); | ||
}, | ||
} } | ||
/> | ||
); | ||
} | ||
function SaveShortcut() { | ||
const { savePost } = useDispatch( 'core/editor' ); | ||
const isEditedPostDirty = useSelect( ( select ) => select( 'core/editor' ).isEditedPostDirty, [] ); | ||
|
||
useShortcut( 'core/editor/save', ( event ) => { | ||
event.preventDefault(); | ||
|
||
export default compose( [ | ||
withSelect( ( select ) => { | ||
const { isEditedPostDirty } = select( 'core/editor' ); | ||
// TODO: This should be handled in the `savePost` effect in | ||
// considering `isSaveable`. See note on `isEditedPostSaveable` | ||
// selector about dirtiness and meta-boxes. | ||
// | ||
// See: `isEditedPostSaveable` | ||
if ( ! isEditedPostDirty() ) { | ||
return; | ||
} | ||
|
||
return { | ||
isDirty: isEditedPostDirty(), | ||
}; | ||
} ), | ||
withDispatch( ( dispatch, ownProps, { select } ) => { | ||
const { savePost } = dispatch( 'core/editor' ); | ||
savePost(); | ||
}, { bindGlobal: true } ); | ||
|
||
return { | ||
onSave() { | ||
// TODO: This should be handled in the `savePost` effect in | ||
// considering `isSaveable`. See note on `isEditedPostSaveable` | ||
// selector about dirtiness and meta-boxes. | ||
// | ||
// See: `isEditedPostSaveable` | ||
const { isEditedPostDirty } = select( 'core/editor' ); | ||
if ( ! isEditedPostDirty() ) { | ||
return; | ||
} | ||
return null; | ||
} | ||
|
||
savePost(); | ||
}, | ||
}; | ||
} ), | ||
] )( SaveShortcut ); | ||
export default SaveShortcut; |
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
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
Oops, something went wrong.