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 an issue with copying text #1431

Merged
merged 2 commits into from
Aug 22, 2024
Merged
Show file tree
Hide file tree
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
43 changes: 28 additions & 15 deletions ui/src/hooks/helper/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,19 @@ const { message } = createDiscreteApi(['message']);
* @param e
* @param config
* @param socket
* @param terminalId
* @param termSelectionText
* @param k8s_id
*/
export const handleContextMenu = async (
e: MouseEvent,
config: ILunaConfig,
socket: WebSocket,
termSelectionText: string
terminalId: string,
termSelectionText: string,
k8s_id: string | undefined
) => {
e.preventDefault();
if (e.ctrlKey || config.quickPaste !== '1') return;

let text: string = '';
Expand All @@ -44,14 +49,20 @@ export const handleContextMenu = async (
text = await readText();
} catch (e) {
if (termSelectionText !== '') text = termSelectionText;
message.info(`${e}`);
}

socket.send(formatMessage('1', 'TERMINAL_DATA', text));

e.preventDefault();

return text;
if (k8s_id) {
socket.send(
JSON.stringify({
id: terminalId,
k8s_id,
type: 'TERMINAL_K8S_DATA',
data: text
})
);
} else {
socket.send(formatMessage(terminalId, 'TERMINAL_DATA', text));
}
};

/**
Expand Down Expand Up @@ -131,14 +142,16 @@ export const handleCustomKey = (e: KeyboardEvent, terminal: Terminal): boolean =
export const handleTerminalSelection = async (terminal: Terminal, termSelectionText: Ref<string>) => {
termSelectionText.value = terminal.getSelection().trim();

clipboard
.writeText(termSelectionText.value)
.then(() => {
message.success('Copied!');
})
.catch(e => {
message.error(`Copy Error for ${e}`);
});
if (termSelectionText.value !== '') {
clipboard
.writeText(termSelectionText.value)
.then(() => {})
.catch(e => {
message.error(`Copy Error for ${e}`);
});
} else {
message.warning('Please select the text before copying');
}
};

/**
Expand Down
67 changes: 8 additions & 59 deletions ui/src/hooks/useTerminal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,14 @@ export const useTerminal = async (el: HTMLElement, option: ICallbackOptions): Pr
el.addEventListener(
'contextmenu',
(e: MouseEvent) => {
handleContextMenu(e, lunaConfig, socket!, termSelectionText.value);
handleContextMenu(
e,
lunaConfig,
socket!,
terminalId.value,
termSelectionText.value,
k8s_id.value
);
},
false
);
Expand Down Expand Up @@ -482,64 +489,6 @@ export const useTerminal = async (el: HTMLElement, option: ICallbackOptions): Pr
}
}

// if (option.type === 'common') {
// const [socketResult, terminalResult] = await Promise.allSettled([
// createSocket(),
// createTerminal(lunaConfig)
// ]);
//
// if (socketResult.status === 'fulfilled' && terminalResult.status === 'fulfilled') {
// if (socketResult.value) {
// socket = socketResult.value;
// }
// terminal = terminalResult.value;
//
// initElEvent();
// initSocketEvent();
// initTerminalEvent();
// initCustomWindowEvent();
//
// const { createSentry } = useSentry(lastSendTime, option.i18nCallBack);
//
// sentry = createSentry(socket, terminal);
// } else {
// if (socketResult.status === 'rejected') {
// message.error('Socket error:', socketResult.reason);
// }
// if (terminalResult.status === 'rejected') {
// message.error('Terminal error:', terminalResult.reason);
// }
// }
// } else {
// terminal = await createTerminal(lunaConfig);
// socket = option.transSocket!;
//
// initElEvent();
// initTerminalEvent();
// initCustomWindowEvent();
//
// const { createSentry } = useSentry(lastSendTime, option.i18nCallBack);
//
// sentry = createSentry(socket, terminal);
//
// const { currentTab } = storeToRefs(useTerminalStore());
//
// const messageHandlers = {
// [currentTab.value]: (e: MessageEvent) => {
// handleK8sMessage(JSON.parse(e.data));
// }
// };
//
// if (option.transSocket) {
// option.transSocket.addEventListener('message', (e: MessageEvent) => {
// const handler = messageHandlers[currentTab.value];
// if (handler) {
// handler(e);
// }
// });
// }
// }

return terminal;
};

Expand Down
Loading