Skip to content
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
2 changes: 2 additions & 0 deletions src/frontend/Serial.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
} from "react";
import { useXTerm } from "react-xtermjs";
import { FitAddon } from "@xterm/addon-fit";
import { clipboardKeyHandler } from "./clipboard-keys.js";
import {
ConnectionScreen,
type ConnectionStatus,
Expand Down Expand Up @@ -295,6 +296,7 @@ export const Serial = forwardRef<SerialHandle, SerialProps>(function Serial(
terminal.loadAddon(fitAddon);
terminal.options.cursorBlink = true;
terminal.options.scrollback = 10000;
terminal.attachCustomKeyEventHandler(clipboardKeyHandler(terminal));

const onData = terminal.onData((data) => {
if (!connectedRef.current) return;
Expand Down
39 changes: 39 additions & 0 deletions src/frontend/clipboard-keys.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import type { Terminal } from "@xterm/xterm";

type ClipboardTerminal = Pick<
Terminal,
"hasSelection" | "getSelection" | "clearSelection" | "paste"
>;
type TextClipboard = Pick<Clipboard, "readText" | "writeText">;

/**
* Ctrl+Shift+C / Ctrl+Shift+V (Cmd+C / Cmd+V on macOS) copy and paste, as in
* the SSH terminal; every other key goes to the port. Without this nothing
* reaches the clipboard in the desktop app, since Ctrl+C is the device's.
*/
export function clipboardKeyHandler(
terminal: ClipboardTerminal,
clipboard: TextClipboard = navigator.clipboard,
): (event: KeyboardEvent) => boolean {
return (event) => {
const shortcut =
event.type === "keydown" &&
!event.altKey &&
(event.metaKey || (event.ctrlKey && event.shiftKey));
if (!shortcut) return true;
if (event.code === "KeyC") {
if (terminal.hasSelection()) {
void clipboard.writeText(terminal.getSelection());
terminal.clearSelection();
}
return false;
}
if (event.code === "KeyV") {
void clipboard.readText().then((text) => {
if (text) terminal.paste(text);
});
return false;
}
return true;
};
}
57 changes: 57 additions & 0 deletions tests/frontend/clipboard-keys.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { describe, expect, it, vi } from "vitest";
import { clipboardKeyHandler } from "../../src/frontend/clipboard-keys.js";

function setup(selection = "") {
const terminal = {
hasSelection: () => selection !== "",
getSelection: () => selection,
clearSelection: vi.fn(),
paste: vi.fn(),
};
const clipboard = {
writeText: vi.fn(async () => {}),
readText: vi.fn(async () => "pasted"),
};
return {
terminal,
clipboard,
handle: clipboardKeyHandler(terminal, clipboard),
};
}

const key = (code: string, mods: Partial<KeyboardEvent> = {}) =>
({
type: "keydown",
code,
ctrlKey: false,
shiftKey: false,
metaKey: false,
altKey: false,
...mods,
}) as KeyboardEvent;

describe("serial clipboard keys", () => {
it("copies the selection with Ctrl+Shift+C or Cmd+C", () => {
for (const mods of [{ ctrlKey: true, shiftKey: true }, { metaKey: true }]) {
const { terminal, clipboard, handle } = setup("AT+OK");
expect(handle(key("KeyC", mods))).toBe(false);
expect(clipboard.writeText).toHaveBeenCalledWith("AT+OK");
expect(terminal.clearSelection).toHaveBeenCalled();
}
});

it("pastes with Ctrl+Shift+V", async () => {
const { terminal, handle } = setup();
expect(handle(key("KeyV", { ctrlKey: true, shiftKey: true }))).toBe(false);
await vi.waitFor(() =>
expect(terminal.paste).toHaveBeenCalledWith("pasted"),
);
});

it("leaves Ctrl+C and plain keys to the device", () => {
const { clipboard, handle } = setup("AT+OK");
expect(handle(key("KeyC", { ctrlKey: true }))).toBe(true);
expect(handle(key("KeyA"))).toBe(true);
expect(clipboard.writeText).not.toHaveBeenCalled();
});
});