Execute all matching key binding variations #8
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "ocavue-group-map"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
When users send two commands with the same key string to the
keymap()plugin, it's expected that only one will be executed:However, when two commands with different variations of the same key are provided,
prosemirror-keymapstill executes only one of them, which I found to be a bit surprising.Some examples of similar variations include:
Ctrl-SpacevsControl-Space: different variations of the same modifierCtrl-Shift-SpacevsShift-Ctrl-Space: different order of modifiersMod-avsmod-a: Undocumented modifier. Although the document never stated that lower casemod-is a valid modifier, it works find.This PR addresses the issue by allowing different variations of the same key.
An alternative approach would be for
prosemirror-keymapto exposenormalizeKeyNameas a public API, allowing me to accomplish the same task externally.Notice that two existing tests are failing on the master branch. The new added test is passed.
I don't think this is a useful enough thing to complicate the package's data structures for. It would be more sensible to raise an error when someone does this. Just use
chainCommandsto bind multiple commands to a single key.@marijnh Thanks for your reply! Would you be open to exporting
normalizeKeyNameas a public API? I can group multiple commands with the same key after normalization on my end.Are you aware that you can add multiple keymaps to a single editor? There's no need to combine them in code.
Yes, I understand that I can have multiple plugins returned by
keymap().The challenge is that sometimes I want to update the keybindings dynamically after the editor has been initialized. For example, I might want to change some keyboard shortcuts based on user settings.
I can use
view.setPropsto update the plugins list, but that would trigger a state update, which seems unnecessary since neitherstate.docnorstate.selectionchanges in this case.So, my current solution is to register a single plugin with the handleKeyDown prop. Inside that handler, I use event delegation and the keydownHandler to manage keybindings. Therefore, I want to pass all keybindings as one large keymap object. That's when I found that two keybindings with different key strings could override each other.
In this situation you can also loop over a list of keydown handlers. Which means there's still no need to combine keymap objects.
That totally makes sense! I guess calling several dozen keydown handlers for every key press shouldn't significantly impact performance. Thank you for your response!
Attached patch adds a check for this, so it doesn't cause accidental issues.
Pull request closed