1 import {Component} from './component';
3 function reverseMap(map) {
5 for (const [key, value] of Object.entries(map)) {
11 export class Shortcuts extends Component {
14 this.container = this.$el;
15 this.mapById = JSON.parse(this.$opts.keyMap);
16 this.mapByShortcut = reverseMap(this.mapById);
18 this.hintsShowing = false;
20 this.hideHints = this.hideHints.bind(this);
21 this.hintAbortController = null;
23 this.setupListeners();
27 window.addEventListener('keydown', event => {
28 if (event.target.closest('input, select, textarea')) {
32 this.handleShortcutPress(event);
35 window.addEventListener('keydown', event => {
36 if (event.key === '?') {
37 if (this.hintsShowing) {
47 * @param {KeyboardEvent} event
49 handleShortcutPress(event) {
51 event.ctrlKey ? 'Ctrl' : '',
52 event.metaKey ? 'Cmd' : '',
56 const combo = keys.filter(s => Boolean(s)).join(' + ');
58 const shortcutId = this.mapByShortcut[combo];
60 const wasHandled = this.runShortcut(shortcutId);
62 event.preventDefault();
68 * Run the given shortcut, and return a boolean to indicate if the event
69 * was successfully handled by a shortcut action.
74 const el = this.container.querySelector(`[data-shortcut="${id}"]`);
79 if (el.matches('input, textarea, select')) {
84 if (el.matches('a, button')) {
89 if (el.matches('div[tabindex]')) {
95 console.error('Shortcut attempted to be ran for element type that does not have handling setup', el);
101 const wrapper = document.createElement('div');
102 wrapper.classList.add('shortcut-container');
103 this.container.append(wrapper);
105 const shortcutEls = this.container.querySelectorAll('[data-shortcut]');
106 const displayedIds = new Set();
107 for (const shortcutEl of shortcutEls) {
108 const id = shortcutEl.getAttribute('data-shortcut');
109 if (displayedIds.has(id)) {
113 const key = this.mapById[id];
114 this.showHintLabel(shortcutEl, key, wrapper);
115 displayedIds.add(id);
118 this.hintAbortController = new AbortController();
119 const signal = this.hintAbortController.signal;
120 window.addEventListener('scroll', this.hideHints, {signal});
121 window.addEventListener('focus', this.hideHints, {signal});
122 window.addEventListener('blur', this.hideHints, {signal});
123 window.addEventListener('click', this.hideHints, {signal});
125 this.hintsShowing = true;
129 * @param {Element} targetEl
130 * @param {String} key
131 * @param {Element} wrapper
133 showHintLabel(targetEl, key, wrapper) {
134 const targetBounds = targetEl.getBoundingClientRect();
136 const label = document.createElement('div');
137 label.classList.add('shortcut-hint');
138 label.textContent = key;
140 const linkage = document.createElement('div');
141 linkage.classList.add('shortcut-linkage');
142 linkage.style.left = `${targetBounds.x}px`;
143 linkage.style.top = `${targetBounds.y}px`;
144 linkage.style.width = `${targetBounds.width}px`;
145 linkage.style.height = `${targetBounds.height}px`;
147 wrapper.append(label, linkage);
149 const labelBounds = label.getBoundingClientRect();
151 label.style.insetInlineStart = `${((targetBounds.x + targetBounds.width) - (labelBounds.width + 6))}px`;
152 label.style.insetBlockStart = `${(targetBounds.y + (targetBounds.height - labelBounds.height) / 2)}px`;
156 const wrapper = this.container.querySelector('.shortcut-container');
158 this.hintAbortController?.abort();
159 this.hintsShowing = false;