Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

feat: add copy on ctrl/command+shift+c and selection to web terminal#20129

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
code-asher wants to merge2 commits intomain
base:main
Choose a base branch
Loading
fromasher/webterm-copy
Open
Changes fromall 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
54 changes: 50 additions & 4 deletionssite/src/pages/TerminalPage/TerminalPage.tsx
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -12,6 +12,7 @@ import {
workspaceByOwnerAndName,
workspaceUsage,
} from "api/queries/workspaces";
import { displayError } from "components/GlobalSnackbar/utils";
import { useProxy } from "contexts/ProxyContext";
import { ThemeOverride } from "contexts/ThemeProvider";
import { useEmbeddedMetadata } from "hooks/useEmbeddedMetadata";
Expand DownExpand Up@@ -147,12 +148,30 @@ const TerminalPage: FC = () => {
}),
);

// Make shift+enter send ^[^M (escaped carriage return). Applications
// typically take this to mean to insert a literal newline. There is no way
// to remove this handler, so we must attach it once and rely on a ref to
// send it to the current socket.
const isMac = navigator.platform.match("Mac");

const copySelection = () => {
const selection = terminal.getSelection();
if (selection) {
navigator.clipboard.writeText(selection).catch((err) => {
console.error(err);
if (err.message) {
displayError(`Failed to copy text: ${err.message}`);
} else {
displayError(
"Failed to copy text, but no error message was provided",
);
}
});
}
};

// There is no way to remove this handler, so we must attach it once and
// rely on a ref to send it to the current socket.
const escapedCarriageReturn = "\x1b\r";
terminal.attachCustomKeyEventHandler((ev) => {
// Make shift+enter send ^[^M (escaped carriage return). Applications
// typically take this to mean to insert a literal newline.
if (ev.shiftKey && ev.key === "Enter") {
if (ev.type === "keydown") {
websocketRef.current?.send(
Expand All@@ -163,9 +182,36 @@ const TerminalPage: FC = () => {
}
return false;
}
// Make ctrl+shift+c (command+shift+c on macOS) copy the selected text.
// By default this usually launches the browser dev tools, but users
// expect this keybinding to copy when in the context of the web terminal.
if ((isMac ? ev.metaKey : ev.ctrlKey) && ev.shiftKey && ev.key === "C") {
ev.preventDefault();
if (ev.type === "keydown") {
copySelection();
}
return false;
Copy link
Member

Choose a reason for hiding this comment

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

Just to make sure I'm understanding: what do the boolean return values do for the terminal? I would've thought that it stops event propagation, but then I'm also seeing theev.preventDefault

Copy link
MemberAuthor

Choose a reason for hiding this comment

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

Nope the return does not stop propagation, it only tells xterm.js to do nothing. Although, at the moment xterm.js does not actually have any default behavior for these keybindings, so the default is already to do nothing.

}
return true;
});

// Copy using the clipboard API on selection. This selected text will go
// into the clipboard, not the primary selection, as the browser does not
// give us an API to set the primary selection (only relevant to systems
// that have this distinction, like X11).
//
// We could bind the middle mouse button to paste from the clipboard to
// compensate, but then we would break pasting selections from external
// applications into the web terminal. Not sure which tradeoff is worse; it
// probably varies between users.
//
// In other words, this copied text can be pasted with a keybinding
// (typically ctrl+v, ctrl+shift+v, or shift+insert), but *not* with the
// middle mouse button.
terminal.onSelectionChange(() => {
copySelection();
});

terminal.open(terminalWrapperRef.current);

// We have to fit twice here. It's unknown why, but the first fit will
Expand Down
Loading

[8]ページ先頭

©2009-2025 Movatter.jp