2023-04-18 17:20:02 -04:00
|
|
|
import {Component} from './component';
|
2022-11-15 06:24:31 -05:00
|
|
|
|
2022-11-09 09:40:44 -05:00
|
|
|
/**
|
|
|
|
* Keys to ignore when recording shortcuts.
|
|
|
|
* @type {string[]}
|
|
|
|
*/
|
|
|
|
const ignoreKeys = ['Control', 'Alt', 'Shift', 'Meta', 'Super', ' ', '+', 'Tab', 'Escape'];
|
|
|
|
|
2022-11-15 06:24:31 -05:00
|
|
|
export class ShortcutInput extends Component {
|
2022-11-09 09:40:44 -05:00
|
|
|
|
|
|
|
setup() {
|
|
|
|
this.input = this.$el;
|
|
|
|
|
|
|
|
this.setupListeners();
|
|
|
|
}
|
|
|
|
|
|
|
|
setupListeners() {
|
|
|
|
this.listenerRecordKey = this.listenerRecordKey.bind(this);
|
|
|
|
|
|
|
|
this.input.addEventListener('focus', () => {
|
2023-04-18 17:20:02 -04:00
|
|
|
this.startListeningForInput();
|
2022-11-09 09:40:44 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
this.input.addEventListener('blur', () => {
|
|
|
|
this.stopListeningForInput();
|
2023-04-18 17:20:02 -04:00
|
|
|
});
|
2022-11-09 09:40:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
startListeningForInput() {
|
2023-04-18 17:20:02 -04:00
|
|
|
this.input.addEventListener('keydown', this.listenerRecordKey);
|
2022-11-09 09:40:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {KeyboardEvent} event
|
|
|
|
*/
|
|
|
|
listenerRecordKey(event) {
|
|
|
|
if (ignoreKeys.includes(event.key)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const keys = [
|
|
|
|
event.ctrlKey ? 'Ctrl' : '',
|
|
|
|
event.metaKey ? 'Cmd' : '',
|
|
|
|
event.key,
|
|
|
|
];
|
|
|
|
|
|
|
|
this.input.value = keys.filter(s => Boolean(s)).join(' + ');
|
|
|
|
}
|
|
|
|
|
|
|
|
stopListeningForInput() {
|
|
|
|
this.input.removeEventListener('keydown', this.listenerRecordKey);
|
|
|
|
}
|
|
|
|
|
2023-04-18 17:20:02 -04:00
|
|
|
}
|