Add terminal copy on select (#3904)

This commit is contained in:
Juho Heikka authored and GitHub committed 2021-09-29 08:55:39 -04:00
1 parent b41f923931
commit 8bbaf8c59e
4 files changed
+54

No files matched your search

@@ -72,6 +72,20 @@ export const Application = observer(() => {
/>
</section>
<section id="terminalSelection">
<SubTitle title="Terminal copy & paste" />
<FormSwitch
label="Copy on select and paste on right-click"
control={
<Switcher
checked={UserStore.getInstance().terminalCopyOnSelect}
onChange={v => UserStore.getInstance().terminalCopyOnSelect = v.target.checked}
name="terminalCopyOnSelect"
/>
}
/>
</section>
<hr/>
<section id="other">
+23
View File
@@ -29,6 +29,8 @@ import { ThemeStore } from "../../theme.store";
import { boundMethod } from "../../utils";
import { isMac } from "../../../common/vars";
import { camelCase } from "lodash";
import { UserStore } from "../../../common/user-store";
import { clipboard } from "electron";
export class Terminal {
static spawningPool: HTMLElement;
@@ -115,11 +117,13 @@ export class Terminal {
this.xterm.open(Terminal.spawningPool);
this.xterm.registerLinkMatcher(/https?:\/\/[^\s]+/i, this.onClickLink);
this.xterm.attachCustomKeyEventHandler(this.keyHandler);
this.xterm.onSelectionChange(this.onSelectionChange);
// bind events
const onDataHandler = this.xterm.onData(this.onData);
this.viewport.addEventListener("scroll", this.onScroll);
this.elem.addEventListener("contextmenu", this.onContextMenu);
this.api.onReady.addListener(this.onClear, { once: true }); // clear status logs (connecting..)
this.api.onData.addListener(this.onApiData);
window.addEventListener("resize", this.onResize);
@@ -133,6 +137,7 @@ export class Terminal {
() => this.fitAddon.dispose(),
() => this.api.removeAllListeners(),
() => window.removeEventListener("resize", this.onResize),
() => this.elem.removeEventListener("contextmenu", this.onContextMenu),
);
}
@@ -198,6 +203,24 @@ export class Terminal {
window.open(link, "_blank");
};
onContextMenu = () => {
const { terminalCopyOnSelect } = UserStore.getInstance();
const textFromClipboard = clipboard.readText();
if (terminalCopyOnSelect) {
this.xterm.paste(textFromClipboard);
}
};
onSelectionChange = () => {
const { terminalCopyOnSelect } = UserStore.getInstance();
const selection = this.xterm.getSelection().trim();
if (terminalCopyOnSelect && selection !== "") {
clipboard.writeText(selection);
}
};
keyHandler = (evt: KeyboardEvent): boolean => {
const { code, ctrlKey, type, metaKey } = evt;