12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455 |
- const shortcuts = {};
- let _shortcuts = [];
- const lib = {
- handleKeyDown(event, keyCode, shift, ctrl, alt) {
- _shortcuts.forEach(shortcut => {
- if (
- shortcut.keyCode === keyCode &&
- shortcut.shift === shift &&
- shortcut.ctrl === ctrl &&
- shortcut.alt === alt
- ) {
- console.log(
- `Executing shortcut ${shortcut.name}. Prevent default: ${shortcut.preventDefault}`
- );
- if (shortcut.preventDefault === true) event.preventDefault();
- shortcut.handler(event);
- }
- });
- },
- registerShortcut(name, shortcut) {
- shortcuts[name] = shortcut;
- shortcuts[name].name = name;
- shortcuts[name].alt = shortcuts[name].alt ? shortcuts[name].alt : false;
- shortcuts[name].ctrl = shortcuts[name].ctrl
- ? shortcuts[name].ctrl
- : false;
- shortcuts[name].shift = shortcuts[name].shift
- ? shortcuts[name].shift
- : false;
- lofig.get("shortcutOverrides").then(overrides => {
- if (overrides && overrides[name])
- shortcuts[name] = Object.assign(
- shortcuts[name],
- overrides[name]
- );
- });
- lib.remakeShortcutsArray();
- },
- unregisterShortcut: name => {
- delete shortcuts[name];
- lib.remakeShortcutsArray();
- },
- remakeShortcutsArray: () => {
- _shortcuts = Object.keys(shortcuts).map(key => shortcuts[key]);
- }
- };
- export default lib;
|