-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Refactor the visual editor shortcuts to use the keyboard-shortcuts package #19318
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
09b58de
Refactor the visual editor shortcuts to use the keyboard-shortcuts pa…
youknowriad ce22673
Remove custom undo/redo handlers
youknowriad 763e64f
Fix block editor shortcuts
youknowriad c56bd9b
little tweak
youknowriad 122cdce
Fix unit tests
youknowriad 55e696b
Add missing explicit store dependency
youknowriad File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: There's inconsistency here in that the file is named as "register shortcuts" but the component as "shortcuts register". |
||
// 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do we have inconsistency that from the
block-editor
package, the component which manages shortcut registration is defined asBlockEditorKeyboardShortcuts.Register
(a property ofBlockEditorKeyboardShortcuts
), but then the same sort of component fromeditor
package has its own standalone exported memberEditorKeyboardShortcutsRegister
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
because the Editor one registers both the Text and Visual shortcuts that can't be applied at the same time, another option is to split the registration into two components as well but I feel a better pattern is that each package would have a single register component.