2023-04-18 17:20:02 -04:00
|
|
|
import {Component} from './component';
|
2022-11-15 06:24:31 -05:00
|
|
|
|
2022-11-04 11:20:19 -04:00
|
|
|
function reverseMap(map) {
|
|
|
|
const reversed = {};
|
|
|
|
for (const [key, value] of Object.entries(map)) {
|
|
|
|
reversed[value] = key;
|
|
|
|
}
|
|
|
|
return reversed;
|
|
|
|
}
|
|
|
|
|
2022-11-15 06:24:31 -05:00
|
|
|
export class Shortcuts extends Component {
|
2022-11-04 11:20:19 -04:00
|
|
|
|
|
|
|
setup() {
|
|
|
|
this.container = this.$el;
|
2022-11-08 16:17:45 -05:00
|
|
|
this.mapById = JSON.parse(this.$opts.keyMap);
|
2022-11-04 11:20:19 -04:00
|
|
|
this.mapByShortcut = reverseMap(this.mapById);
|
|
|
|
|
|
|
|
this.hintsShowing = false;
|
2022-11-05 09:39:17 -04:00
|
|
|
|
|
|
|
this.hideHints = this.hideHints.bind(this);
|
2023-04-25 20:42:12 -04:00
|
|
|
this.hintAbortController = null;
|
2022-11-04 11:20:19 -04:00
|
|
|
|
|
|
|
this.setupListeners();
|
|
|
|
}
|
|
|
|
|
|
|
|
setupListeners() {
|
|
|
|
window.addEventListener('keydown', event => {
|
2023-05-08 09:28:03 -04:00
|
|
|
if (event.target.closest('input, select, textarea, .cm-editor')) {
|
2022-11-04 11:20:19 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (event.key === '?') {
|
2023-04-25 20:42:12 -04:00
|
|
|
if (this.hintsShowing) {
|
|
|
|
this.hideHints();
|
|
|
|
} else {
|
|
|
|
this.showHints();
|
|
|
|
}
|
2023-10-23 06:04:09 -04:00
|
|
|
return;
|
2022-11-04 11:20:19 -04:00
|
|
|
}
|
2023-10-23 06:04:09 -04:00
|
|
|
|
|
|
|
this.handleShortcutPress(event);
|
2022-11-04 11:20:19 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-11-09 09:40:44 -05:00
|
|
|
/**
|
|
|
|
* @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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-04 11:20:19 -04:00
|
|
|
/**
|
|
|
|
* Run the given shortcut, and return a boolean to indicate if the event
|
|
|
|
* was successfully handled by a shortcut action.
|
|
|
|
* @param {String} id
|
|
|
|
* @return {boolean}
|
|
|
|
*/
|
|
|
|
runShortcut(id) {
|
|
|
|
const el = this.container.querySelector(`[data-shortcut="${id}"]`);
|
|
|
|
if (!el) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (el.matches('input, textarea, select')) {
|
|
|
|
el.focus();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (el.matches('a, button')) {
|
|
|
|
el.click();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-11-05 09:39:17 -04:00
|
|
|
if (el.matches('div[tabindex]')) {
|
|
|
|
el.click();
|
|
|
|
el.focus();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-04-18 17:20:02 -04:00
|
|
|
console.error('Shortcut attempted to be ran for element type that does not have handling setup', el);
|
2022-11-04 11:20:19 -04:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
showHints() {
|
2022-11-05 09:57:22 -04:00
|
|
|
const wrapper = document.createElement('div');
|
|
|
|
wrapper.classList.add('shortcut-container');
|
|
|
|
this.container.append(wrapper);
|
|
|
|
|
2022-11-04 11:20:19 -04:00
|
|
|
const shortcutEls = this.container.querySelectorAll('[data-shortcut]');
|
2022-11-05 09:39:17 -04:00
|
|
|
const displayedIds = new Set();
|
2022-11-04 11:20:19 -04:00
|
|
|
for (const shortcutEl of shortcutEls) {
|
|
|
|
const id = shortcutEl.getAttribute('data-shortcut');
|
2022-11-05 09:39:17 -04:00
|
|
|
if (displayedIds.has(id)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2022-11-04 11:20:19 -04:00
|
|
|
const key = this.mapById[id];
|
2022-11-05 09:57:22 -04:00
|
|
|
this.showHintLabel(shortcutEl, key, wrapper);
|
2022-11-05 09:39:17 -04:00
|
|
|
displayedIds.add(id);
|
2022-11-04 11:20:19 -04:00
|
|
|
}
|
2022-11-05 09:39:17 -04:00
|
|
|
|
2023-04-25 20:42:12 -04:00
|
|
|
this.hintAbortController = new AbortController();
|
|
|
|
const signal = this.hintAbortController.signal;
|
|
|
|
window.addEventListener('scroll', this.hideHints, {signal});
|
|
|
|
window.addEventListener('focus', this.hideHints, {signal});
|
|
|
|
window.addEventListener('blur', this.hideHints, {signal});
|
|
|
|
window.addEventListener('click', this.hideHints, {signal});
|
2022-11-05 09:39:17 -04:00
|
|
|
|
|
|
|
this.hintsShowing = true;
|
2022-11-04 11:20:19 -04:00
|
|
|
}
|
|
|
|
|
2022-11-05 09:57:22 -04:00
|
|
|
/**
|
|
|
|
* @param {Element} targetEl
|
|
|
|
* @param {String} key
|
|
|
|
* @param {Element} wrapper
|
|
|
|
*/
|
|
|
|
showHintLabel(targetEl, key, wrapper) {
|
2022-11-04 11:20:19 -04:00
|
|
|
const targetBounds = targetEl.getBoundingClientRect();
|
2022-11-05 09:57:22 -04:00
|
|
|
|
2022-11-04 11:20:19 -04:00
|
|
|
const label = document.createElement('div');
|
|
|
|
label.classList.add('shortcut-hint');
|
|
|
|
label.textContent = key;
|
2022-11-05 09:57:22 -04:00
|
|
|
|
|
|
|
const linkage = document.createElement('div');
|
|
|
|
linkage.classList.add('shortcut-linkage');
|
2023-04-18 17:20:02 -04:00
|
|
|
linkage.style.left = `${targetBounds.x}px`;
|
|
|
|
linkage.style.top = `${targetBounds.y}px`;
|
|
|
|
linkage.style.width = `${targetBounds.width}px`;
|
|
|
|
linkage.style.height = `${targetBounds.height}px`;
|
2022-11-05 09:57:22 -04:00
|
|
|
|
|
|
|
wrapper.append(label, linkage);
|
2022-11-04 11:20:19 -04:00
|
|
|
|
|
|
|
const labelBounds = label.getBoundingClientRect();
|
|
|
|
|
2022-11-05 09:57:22 -04:00
|
|
|
label.style.insetInlineStart = `${((targetBounds.x + targetBounds.width) - (labelBounds.width + 6))}px`;
|
2022-11-04 11:20:19 -04:00
|
|
|
label.style.insetBlockStart = `${(targetBounds.y + (targetBounds.height - labelBounds.height) / 2)}px`;
|
|
|
|
}
|
|
|
|
|
|
|
|
hideHints() {
|
2022-11-05 09:57:22 -04:00
|
|
|
const wrapper = this.container.querySelector('.shortcut-container');
|
|
|
|
wrapper.remove();
|
2023-04-25 20:42:12 -04:00
|
|
|
this.hintAbortController?.abort();
|
2022-11-05 09:39:17 -04:00
|
|
|
this.hintsShowing = false;
|
2022-11-04 11:20:19 -04:00
|
|
|
}
|
2023-04-18 17:20:02 -04:00
|
|
|
|
|
|
|
}
|