-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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 a new keyboard shortcut package #19100
Conversation
It's a huge PR and I won't have time to review but I applaud the efforts. Great idea 💯 |
packages/block-editor/src/components/keyboard-shortcuts/index.js
Outdated
Show resolved
Hide resolved
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.
I found some misspellings of the word "shortcut" throughout the files and suggested corrections so they don't look weird.
packages/block-editor/src/components/keyboard-shortcuts/index.js
Outdated
Show resolved
Hide resolved
packages/block-editor/src/components/keyboard-shortcuts/index.js
Outdated
Show resolved
Hide resolved
d810e97
to
6708224
Compare
I updated the Modal accordingly. |
packages/edit-post/src/components/keyboard-shortcut-help-modal/shortcut.js
Outdated
Show resolved
Hide resolved
packages/edit-post/src/components/keyboard-shortcut-help-modal/shortcut.js
Outdated
Show resolved
Hide resolved
|
||
shortcutKeys.forEach( ( shortcut ) => { | ||
const keys = shortcut.split( '+' ); | ||
const modifiers = new Set( keys.filter( ( value ) => value.length > 1 ) ); |
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.
What's the single-character modifier we're filtering here? Comment or externalized/named function could help clarify.
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.
I can't tell. This is ported from KeyboardShortcuts.
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.
It looks like it determines whether a key is a modifier by the length of the string. E.g. if I add a pass a shortcut Shift+Cmd+M
, it'll determine that the modifiers are Shift and Cmd because they're not a single character.
That does seem a little bit unusual. Might be safer to check against an array of known modifiers.
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.
It looks like it determines whether a key is a modifier by the length of the string. E.g. if I add a pass a shortcut
Shift+Cmd+M
, it'll determine that the modifiers are Shift and Cmd because they're not a single character.
That sounds about right.
Might be safer to check against an array of known modifiers.
I'd have been content if the original implementation had simply added an inline comment, to save ourselves the struggle in trying to decipher its meaning 😅
packages/block-editor/src/components/keyboard-shortcuts/index.js
Outdated
Show resolved
Hide resolved
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.
I really like the direction this is taking. 👍
I spotted a couple of bugs to be fixed - shortcuts not working and the unregister action has the wrong type.
packages/block-editor/src/components/keyboard-shortcuts/index.js
Outdated
Show resolved
Hide resolved
packages/block-editor/src/components/keyboard-shortcuts/index.js
Outdated
Show resolved
Hide resolved
// Registering the shortcuts | ||
const { registerShortcut } = useDispatch( 'core/keyboard-shortcuts' ); | ||
useEffect( () => { | ||
registerShortcut( |
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.
In terms of displaying a list of shortcuts in the help modal, it seems great that this can be done dynamically using the values from the store, and it might be good to move towards this more.
The main issue I see with shortcuts being registered in this way is that it could result in a situation where a component with shortcuts hasn't yet been rendered, meaning the shortcut isn't registered and so the description won't appear in the help modal.
I wondered if an api more like registerBlock
in the blocks api might be preferable to get around that issue and then have these registered outside of the component scope.
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.
I think registration belongs to the initialization of a package. In this case, the block-editor package does both at the same time but I agree that we should be careful about that.
I do think it should always be tied to a component though and registerBlock
is the wrong approach here (can't change now) to avoid globals. See #8981 for the problems of the globals approach.
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.
I might be wrong, but I think a similar issue exists today with the KeyboardShortcuts
component anyway. If I remember correctly that's why there was a separate BlockActions
component in the first place—the shortcuts have to be rendered in a different part of the render tree. So it might be a moot point, if the component isn't rendered the shortcuts won't work.
There's one case where this is still a little problematic, which is if you load the editor in Code Editor mode and then open the help modal, Code Editor elminates a huge part of the render tree, including the shortcut registration. Most of the Block shortcuts are then not visible. 😄
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.
There's one case where this is still a little problematic, which is if you load the editor in Code Editor mode and then open the help modal, Code Editor elminates a huge part of the render tree, including the shortcut registration. Most of the Block shortcuts are then not visible.
That's a good point, I'll fix.
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.
I split the registration and the handling into two components.
packages/block-editor/src/components/keyboard-shortcuts/index.js
Outdated
Show resolved
Hide resolved
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.
@youknowriad I took if for a one last spin and it's all working well. Code looks good to me too. 🎉
Only thing that might be nice is tests for those new selectors and reducers, but the bulk of the code looks great and I'm about to go away until 2020. 😄
c450eb9
to
6aa4d62
Compare
keyCombination: '/', | ||
description: __( 'Change the block type after adding a new paragraph.' ), | ||
/* translators: The forward-slash character. e.g. '/'. */ | ||
ariaLabel: __( 'Forward-slash' ), |
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.
Should this be an option in the shortcut registration? I see there's a few more usages of this in globalShortcuts
as well.
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.
Potentially yes, we'll probably find a few other options once we refactor all existing shortcuts.
|
||
shortcutKeys.forEach( ( shortcut ) => { | ||
const keys = shortcut.split( '+' ); | ||
const modifiers = new Set( keys.filter( ( value ) => value.length > 1 ) ); |
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.
It looks like it determines whether a key is a modifier by the length of the string. E.g. if I add a pass a shortcut
Shift+Cmd+M
, it'll determine that the modifiers are Shift and Cmd because they're not a single character.
That sounds about right.
Might be safer to check against an array of known modifiers.
I'd have been content if the original implementation had simply added an inline comment, to save ourselves the struggle in trying to decipher its meaning 😅
Co-Authored-By: Andrew Duthie <andrew@andrewduthie.com>
Co-Authored-By: Andrew Duthie <andrew@andrewduthie.com>
Co-Authored-By: Andrew Duthie <andrew@andrewduthie.com>
Thanks for this change. Read also the dev-note post and looking forward to the next steps to offer "a centralized UI to modify the keyboard shortcuts per user" so that #3218 gets addressed 🎉 A couple quick questions /Cc @youknowriad 1 2
Does this mean plugin |
1- I believe right now, both callbacks are triggered but we can enhance the package to do some conflicts detection. 2- Yes, that's what it means but that's the case for blocks, for style variations, for any filter. Basically any WordPress API. So I'm not sure that's really a concern. |
@@ -0,0 +1,32 @@ | |||
# Keycodes |
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.
I assume this was meant to be "Keyboard Shortcuts"? 😃 (Guessing it may have been missed for revision in a copy paste)
Related: https://github.com/WordPress/gutenberg/blob/master/packages/keycodes/README.md
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.
Copy/paste? me? never
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.
Fixed in 8c6916c
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.
Thanks
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.
The commit doesn't seem right though :) copy/paste issue?
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.
The commit doesn't seem right though :) copy/paste issue?
🤦♂ ac818ae
There's a few ideas in this PR:
useShortcut
hook as a replacement to the KeyboardShorcuts package but there's no need to pass the actual combination since it's retrieved from the store.Once all the keyboard shortcuts refactored to use this package, the whole modal could be generated without any hard-coded code.