1 function reverseMap(map) {
3 for (const [key, value] of Object.entries(map)) {
10 * @extends {Component}
15 this.container = this.$el;
16 this.mapById = JSON.parse(this.$opts.keyMap);
17 this.mapByShortcut = reverseMap(this.mapById);
19 this.hintsShowing = false;
21 this.hideHints = this.hideHints.bind(this);
23 this.setupListeners();
27 window.addEventListener('keydown', event => {
29 if (event.target.closest('input, select, textarea')) {
33 this.handleShortcutPress(event);
36 window.addEventListener('keydown', event => {
37 if (event.key === '?') {
38 this.hintsShowing ? this.hideHints() : this.showHints();
44 * @param {KeyboardEvent} event
46 handleShortcutPress(event) {
49 event.ctrlKey ? 'Ctrl' : '',
50 event.metaKey ? 'Cmd' : '',
54 const combo = keys.filter(s => Boolean(s)).join(' + ');
56 const shortcutId = this.mapByShortcut[combo];
58 const wasHandled = this.runShortcut(shortcutId);
60 event.preventDefault();
66 * Run the given shortcut, and return a boolean to indicate if the event
67 * was successfully handled by a shortcut action.
72 const el = this.container.querySelector(`[data-shortcut="${id}"]`);
73 console.info('Shortcut run', el);
78 if (el.matches('input, textarea, select')) {
83 if (el.matches('a, button')) {
88 if (el.matches('div[tabindex]')) {
94 console.error(`Shortcut attempted to be ran for element type that does not have handling setup`, el);
100 const wrapper = document.createElement('div');
101 wrapper.classList.add('shortcut-container');
102 this.container.append(wrapper);
104 const shortcutEls = this.container.querySelectorAll('[data-shortcut]');
105 const displayedIds = new Set();
106 for (const shortcutEl of shortcutEls) {
107 const id = shortcutEl.getAttribute('data-shortcut');
108 if (displayedIds.has(id)) {
112 const key = this.mapById[id];
113 this.showHintLabel(shortcutEl, key, wrapper);
114 displayedIds.add(id);
117 window.addEventListener('scroll', this.hideHints);
118 window.addEventListener('focus', this.hideHints);
119 window.addEventListener('blur', this.hideHints);
120 window.addEventListener('click', this.hideHints);
122 this.hintsShowing = true;
126 * @param {Element} targetEl
127 * @param {String} key
128 * @param {Element} wrapper
130 showHintLabel(targetEl, key, wrapper) {
131 const targetBounds = targetEl.getBoundingClientRect();
133 const label = document.createElement('div');
134 label.classList.add('shortcut-hint');
135 label.textContent = key;
137 const linkage = document.createElement('div');
138 linkage.classList.add('shortcut-linkage');
139 linkage.style.left = targetBounds.x + 'px';
140 linkage.style.top = targetBounds.y + 'px';
141 linkage.style.width = targetBounds.width + 'px';
142 linkage.style.height = targetBounds.height + 'px';
144 wrapper.append(label, linkage);
146 const labelBounds = label.getBoundingClientRect();
148 label.style.insetInlineStart = `${((targetBounds.x + targetBounds.width) - (labelBounds.width + 6))}px`;
149 label.style.insetBlockStart = `${(targetBounds.y + (targetBounds.height - labelBounds.height) / 2)}px`;
153 const wrapper = this.container.querySelector('.shortcut-container');
156 window.removeEventListener('scroll', this.hideHints);
157 window.removeEventListener('focus', this.hideHints);
158 window.removeEventListener('blur', this.hideHints);
159 window.removeEventListener('click', this.hideHints);
161 this.hintsShowing = false;
165 export default Shortcuts;