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

UI: Enable right click to paste in terminal #34

Merged
merged 8 commits into from
Aug 15, 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
16 changes: 16 additions & 0 deletions frontend/src/hooks/useTerminal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ export const useTerminal = (commands: Command[] = []) => {

let resizeObserver: ResizeObserver;
let commandBuffer = "";
const terminalElement = terminal.current?.element;
const handleContextMenu = (e: MouseEvent) => {
e.preventDefault();
navigator.clipboard.readText().then((text) => {
terminal.current?.write(text);
commandBuffer += text;
});
};

if (ref.current) {
/* Initialize the terminal in the DOM */
Expand Down Expand Up @@ -74,6 +82,11 @@ export const useTerminal = (commands: Command[] = []) => {
return true;
});

if (terminalElement) {
// right click to paste
terminalElement.addEventListener("contextmenu", handleContextMenu);
}

/* Listen for resize events */
resizeObserver = new ResizeObserver(() => {
fitAddon.current?.fit();
Expand All @@ -82,6 +95,9 @@ export const useTerminal = (commands: Command[] = []) => {
}

return () => {
if (terminalElement) {
terminalElement.removeEventListener("contextmenu", handleContextMenu);
}
terminal.current?.dispose();
resizeObserver.disconnect();
};
Expand Down
6 changes: 5 additions & 1 deletion frontend/src/services/terminalService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ import ActionType from "#/types/ActionType";
import Session from "./session";

export function sendTerminalCommand(command: string): void {
const event = { action: ActionType.RUN, args: { command } };
// replace END OF TEXT character copied from terminal
// eslint-disable-next-line no-control-regex
const cleanedCommand = command.replace(/\u0003+/, "");
if (!cleanedCommand) return;
const event = { action: ActionType.RUN, args: { command: cleanedCommand } };
const eventString = JSON.stringify(event);
Session.send(eventString);
}
Loading