0
0
Fork 0

perf: improve mainShrotcutMixin

This commit is contained in:
Javier Segarra 2024-09-03 10:31:01 +02:00
parent ea8fde49ff
commit eca68b76a0
1 changed files with 19 additions and 4 deletions

View File

@ -1,8 +1,10 @@
import routes from 'src/router/modules';
import { useRouter } from 'vue-router';
let isNotified = false;
export default {
mounted: function () {
created: function () {
const router = useRouter();
const keyBindingMap = routes
.filter((route) => route.meta.keyBinding)
@ -10,14 +12,27 @@ export default {
map[route.meta.keyBinding.toLowerCase()] = route.path;
return map;
}, {});
const handleKeyDown = (event) => {
const { ctrlKey, altKey, key } = event;
event.preventDefault();
if (ctrlKey && altKey && keyBindingMap[key]) {
if (ctrlKey && altKey && keyBindingMap[key] && !isNotified) {
event.preventDefault();
router.push(keyBindingMap[key]);
isNotified = true;
}
};
const handleKeyUp = (event) => {
const { ctrlKey, altKey } = event;
// Resetea la bandera cuando se sueltan las teclas ctrl o alt
if (!ctrlKey || !altKey) {
isNotified = false;
}
};
window.addEventListener('keydown', handleKeyDown);
window.addEventListener('keyup', handleKeyUp);
},
};