+import {Component} from "./component";
+
function reverseMap(map) {
const reversed = {};
for (const [key, value] of Object.entries(map)) {
return reversed;
}
-/**
- * @extends {Component}
- */
-class Shortcuts {
+
+export class Shortcuts extends Component {
setup() {
this.container = this.$el;
return;
}
- const shortcutId = this.mapByShortcut[event.key];
- if (shortcutId) {
- const wasHandled = this.runShortcut(shortcutId);
- if (wasHandled) {
- event.preventDefault();
- }
- }
+ this.handleShortcutPress(event);
});
window.addEventListener('keydown', event => {
});
}
+ /**
+ * @param {KeyboardEvent} event
+ */
+ handleShortcutPress(event) {
+
+ const keys = [
+ event.ctrlKey ? 'Ctrl' : '',
+ event.metaKey ? 'Cmd' : '',
+ event.key,
+ ];
+
+ const combo = keys.filter(s => Boolean(s)).join(' + ');
+
+ const shortcutId = this.mapByShortcut[combo];
+ if (shortcutId) {
+ const wasHandled = this.runShortcut(shortcutId);
+ if (wasHandled) {
+ event.preventDefault();
+ }
+ }
+ }
+
/**
* Run the given shortcut, and return a boolean to indicate if the event
* was successfully handled by a shortcut action.
*/
runShortcut(id) {
const el = this.container.querySelector(`[data-shortcut="${id}"]`);
- console.info('Shortcut run', el);
if (!el) {
return false;
}
this.hintsShowing = false;
}
-}
-
-export default Shortcuts;
\ No newline at end of file
+}
\ No newline at end of file