Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed: Fixed the issue that switch tab twince #1624

Merged
merged 1 commit into from
Jan 14, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions ui/src/hooks/helper/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Ref } from 'vue';
import type { ILunaConfig } from '@/hooks/interface';

import { Terminal } from '@xterm/xterm';
import { useDebounceFn } from '@vueuse/core';
import { useLogger } from '@/hooks/useLogger.ts';
import { formatMessage, handleError, sendEventToLuna } from '@/components/CustomTerminal/helper';

Expand Down Expand Up @@ -124,19 +125,31 @@ export const handleCustomKey = (
lunaId: string,
origin: string
): boolean => {

const debouncedSwitchTab = useDebounceFn((lunaId: string, origin: string, key: string) => {
switch (key) {
case 'ArrowRight':
sendEventToLuna('KEYEVENT', 'alt+shift+right', lunaId, origin);
break;
case 'ArrowLeft':
sendEventToLuna('KEYEVENT', 'alt+shift+left', lunaId, origin);
break;
}
}, 500);

if (e.altKey && e.shiftKey && (e.key === 'ArrowRight' || e.key === 'ArrowLeft')) {
switch (e.key) {
case 'ArrowRight':
if (lunaId && origin) {
sendEventToLuna('KEYEVENT', 'alt+shift+right', lunaId, origin);
debouncedSwitchTab(lunaId, origin, 'ArrowRight');
} else {
mittBus.emit('alt-shift-right');
}

break;
case 'ArrowLeft':
if (lunaId && origin) {
sendEventToLuna('KEYEVENT', 'alt+shift+left', lunaId, origin);
debouncedSwitchTab(lunaId, origin, 'ArrowLeft');
} else {
mittBus.emit('alt-shift-left');
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are several issues in this code related to the handling of custom keys:

  1. The debouncedSwitchTab function is used without being defined before it uses this context.

  2. The variable name "origin" in both cases should be consistent and not change between methods.

  3. There's no logic implemented in the event handler that sends messages to Luna UI based on user actions like arrow movements.

  4. In terms of optimizations, using TypeScript or static type checking could help catch logical mistakes at compile time.

  5. It would also benefit to add error checks inside functions to ensure things don't blow up (for instance, by sending events with invalid arguments).

  6. Consider reorganizing these pieces into a more reusable way so they can apply to different contexts rather than repeating logic within every method.

Expand Down
Loading