Skip to content

Commit

Permalink
UI: Enable right click to paste in terminal (#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
SmartManoj authored Aug 15, 2024
1 parent f6cadc7 commit 6cd29a5
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
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);
}

0 comments on commit 6cd29a5

Please sign in to comment.