From d36c57a8ec0606f03304cd952afbe377a33c216d Mon Sep 17 00:00:00 2001 From: zhaojisen <1301338853@qq.com> Date: Fri, 13 Sep 2024 19:10:27 +0800 Subject: [PATCH] fixed: Keyboard shortcuts for k8s --- .../Kubernetes/MainContent/index.vue | 48 +++++++++++++++++++ ui/src/hooks/helper/index.ts | 16 +++++-- ui/src/utils/mittBus.ts | 2 + 3 files changed, 63 insertions(+), 3 deletions(-) diff --git a/ui/src/components/Kubernetes/MainContent/index.vue b/ui/src/components/Kubernetes/MainContent/index.vue index 09c6ed14..b3e4eb78 100644 --- a/ui/src/components/Kubernetes/MainContent/index.vue +++ b/ui/src/components/Kubernetes/MainContent/index.vue @@ -108,6 +108,7 @@ import { useLogger } from '@/hooks/useLogger.ts'; import { useTreeStore } from '@/store/modules/tree.ts'; import { useParamsStore } from '@/store/modules/params.ts'; import { useTerminalStore } from '@/store/modules/terminal.ts'; +import { useDebounceFn } from '@vueuse/core'; const message = useMessage(); const { debug } = useLogger('K8s-CustomTerminal'); @@ -672,6 +673,40 @@ const handleWriteData = async (type: string) => { }); }; +/** + * 切换到上一个 Tab + */ +const switchToPreviousTab = () => { + const currentIndex = panels.value.findIndex(panel => panel.name === nameRef.value); + + if (currentIndex > 0) { + nameRef.value = panels.value[currentIndex - 1].name as string; + } else { + nameRef.value = panels.value[panels.value.length - 1].name as string; + } + + findNodeById(nameRef.value); + + terminalStore.setTerminalConfig('currentTab', nameRef.value); +}; + +/** + * 切换到下一个 Tab + */ +const switchToNextTab = () => { + const currentIndex = panels.value.findIndex(panel => panel.name === nameRef.value); + + if (currentIndex < panels.value.length - 1) { + nameRef.value = panels.value[currentIndex + 1].name as string; + } else { + nameRef.value = panels.value[0].name as string; + } + + findNodeById(nameRef.value); + + terminalStore.setTerminalConfig('currentTab', nameRef.value); +}; + onMounted(() => { nextTick(() => { initializeDraggable(); @@ -754,9 +789,22 @@ onMounted(() => { terminalStore.setTerminalConfig('currentTab', key); deleteUserCounter.value++; }); + + const debouncedSwitchToPreviousTab = useDebounceFn(() => { + switchToPreviousTab(); + }, 200); + + const debouncedSwitchToNextTab = useDebounceFn(() => { + switchToNextTab(); + }, 200); + + mittBus.on('alt-shift-left', debouncedSwitchToPreviousTab); + mittBus.on('alt-shift-right', debouncedSwitchToNextTab); }); onBeforeUnmount(() => { + mittBus.off('alt-shift-left', switchToPreviousTab); + mittBus.off('alt-shift-right', switchToNextTab); mittBus.off('connect-terminal'); }); diff --git a/ui/src/hooks/helper/index.ts b/ui/src/hooks/helper/index.ts index e58dca33..f8d6bf8c 100644 --- a/ui/src/hooks/helper/index.ts +++ b/ui/src/hooks/helper/index.ts @@ -16,6 +16,7 @@ import { createDiscreteApi } from 'naive-ui'; import { readText } from 'clipboard-polyfill'; import { fireEvent, preprocessInput } from '@/utils'; +import mittBus from '@/utils/mittBus.ts'; import * as clipboard from 'clipboard-polyfill'; import { BASE_WS_URL, MaxTimeout } from '@/config'; @@ -125,13 +126,21 @@ export const handleCustomKey = ( if (e.altKey && e.shiftKey && (e.key === 'ArrowRight' || e.key === 'ArrowLeft')) { switch (e.key) { case 'ArrowRight': - sendEventToLuna('KEYEVENT', 'alt+shift+right', lunaId, origin); + if (lunaId && origin) { + sendEventToLuna('KEYEVENT', 'alt+shift+right', lunaId, origin); + } else { + mittBus.emit('alt-shift-right'); + } + break; case 'ArrowLeft': - sendEventToLuna('KEYEVENT', 'alt+shift+left', lunaId, origin); + if (lunaId && origin) { + sendEventToLuna('KEYEVENT', 'alt+shift+left', lunaId, origin); + } else { + mittBus.emit('alt-shift-left'); + } break; } - return false; } @@ -172,6 +181,7 @@ export const handleTerminalSelection = async (terminal: Terminal, termSelectionT * @param config * @param socket */ +// todo export const handleTerminalOnData = ( data: string, type: string, diff --git a/ui/src/utils/mittBus.ts b/ui/src/utils/mittBus.ts index 6ac5d440..a217bfac 100644 --- a/ui/src/utils/mittBus.ts +++ b/ui/src/utils/mittBus.ts @@ -3,6 +3,8 @@ import { customTreeOption } from '@/hooks/interface'; import { shareUser } from '@/views/interface'; type Event = { + 'alt-shift-right': void; + 'alt-shift-left': void; 'open-setting': void; 'fold-tree-click': void; 'show-theme-config': void;