From eca68b76a00a46b28182886b4e65f9e21a7d8580 Mon Sep 17 00:00:00 2001 From: Javier Segarra Date: Tue, 3 Sep 2024 10:31:01 +0200 Subject: [PATCH] perf: improve mainShrotcutMixin --- src/boot/mainShortcutMixin.js | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/src/boot/mainShortcutMixin.js b/src/boot/mainShortcutMixin.js index c4d37fd73..3b5c604b7 100644 --- a/src/boot/mainShortcutMixin.js +++ b/src/boot/mainShortcutMixin.js @@ -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); }, };