- Notifications
You must be signed in to change notification settings - Fork1k
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
base:main
Are you sure you want to change the base?
Changes fromall commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"; | ||
@@ -147,12 +148,30 @@ const TerminalPage: FC = () => { | ||
}), | ||
); | ||
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", | ||
); | ||
} | ||
}); | ||
} | ||
}; | ||
Parkreiner marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
// 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( | ||
@@ -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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 the There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
Uh oh!
There was an error while loading.Please reload this page.