Make keybindings.js tree-shakeable
What does this MR do?
Restructures app/assets/javascripts/behaviors/shortcuts/keybindings.js
to make the commands tree-shakeable.
Previously, all commands were exported in a single, large object; any bundle that included a keyboard shortcut would have to include all keyboard shortcut definitions.
Now, each command can be imported individually, allowing bundles to only include the definitions they actually use.
keysFor
API changes
This change slightly alters the API of the keysFor
function. Instead of accepting a command string:
keysFor('my.keyboard.shortcut.id', handler);
... it accepts a command object:
const command = {
id: 'my.keyboard.shortcut.id',
description: 'description here',
defaultKeys: ['mod+m']
};
keysFor(command, handler);
However, in reality, there will be no difference in usage; in either case, the command string/object is imported from keybindings.js
:
import { keysFor, MY_SHORTCUT } from './keybindings';
keysFor(MY_SHORTCUT, handler);
Other changes
In addition, keybindings.js
has been updated to avoid side-effects during import. The process that reads localStorage
for customization has been moved to a memoize'd function (getCustomizations()
), which makes it easier to test.
Context
These changes were recommended during review of !45308 (merged). I've pulled these changes into their own MR so that we have all the correct patterns in place before merging !45308 (merged).
Screenshots
This MR makes no user-facing changes.