Add Ctrl+Insert and Shift+Insert as new Copy & Paste hot keys. Fixed the problem that pasted data was not send to the remote

This commit is contained in:
NI
2020-08-15 21:57:43 +08:00
parent d86c92867c
commit 5fa3616a73

View File

@@ -129,6 +129,7 @@ class Term {
constructor(control) { constructor(control) {
const resizeDelayInterval = 500; const resizeDelayInterval = 500;
this.control = control;
this.closed = false; this.closed = false;
this.fontSize = termDefaultFontSize; this.fontSize = termDefaultFontSize;
this.term = new Terminal({ this.term = new Terminal({
@@ -145,7 +146,7 @@ class Term {
this.term.loadAddon(new WebLinksAddon()); this.term.loadAddon(new WebLinksAddon());
this.term.setOption("theme", { this.term.setOption("theme", {
background: control.activeColor(), background: this.control.activeColor(),
}); });
this.term.onData((data) => { this.term.onData((data) => {
@@ -153,7 +154,7 @@ class Term {
return; return;
} }
control.send(data); this.control.send(data);
}); });
this.term.onBinary((data) => { this.term.onBinary((data) => {
@@ -161,7 +162,7 @@ class Term {
return; return;
} }
control.sendBinary(data); this.control.sendBinary(data);
}); });
this.term.onKey((ev) => { this.term.onKey((ev) => {
@@ -169,7 +170,7 @@ class Term {
return; return;
} }
if (!control.echo()) { if (!this.control.echo()) {
return; return;
} }
@@ -179,13 +180,13 @@ class Term {
!ev.domEvent.ctrlKey && !ev.domEvent.ctrlKey &&
!ev.domEvent.metaKey; !ev.domEvent.metaKey;
switch (ev.domEvent.key.toLowerCase()) { switch (ev.domEvent.key) {
case "enter": case "Enter":
ev.domEvent.preventDefault(); ev.domEvent.preventDefault();
this.writeStr("\r\n"); this.writeStr("\r\n");
break; break;
case "backspace": case "Backspace":
ev.domEvent.preventDefault(); ev.domEvent.preventDefault();
this.writeStr("\b \b"); this.writeStr("\b \b");
break; break;
@@ -198,6 +199,45 @@ class Term {
} }
}); });
this.term.attachCustomKeyEventHandler(async (ev) => {
if (this.closed) {
return true;
}
if (ev.type == "keyup" && (
(ev.key.toLowerCase() === "v" && ev.shiftKey && ev.ctrlKey) ||
(ev.key === "Insert" && ev.shiftKey)
)) {
try {
let text = await window.navigator.clipboard.readText();
this.writeEchoStr(text);
} catch (e) {
alert(
"Unable to paste: " +
e +
". Please try again without using the Control+Shift+V / " +
"Shift+Insert hot key"
);
}
return false;
}
if (ev.type == "keyup" && (
(ev.key.toLowerCase() === "c" && ev.shiftKey && ev.ctrlKey) ||
(ev.key === "Insert" && ev.ctrlKey)
)) {
try {
window.navigator.clipboard.writeText(this.term.getSelection());
} catch (e) {
alert("Unable to copy: " + e);
}
return false;
}
return true;
});
let resizeDelay = null, let resizeDelay = null,
oldRows = 0, oldRows = 0,
oldCols = 0; oldCols = 0;
@@ -230,7 +270,7 @@ class Term {
return; return;
} }
control.resize({ this.control.resize({
rows: dim.rows, rows: dim.rows,
cols: dim.cols, cols: dim.cols,
}); });
@@ -248,38 +288,6 @@ class Term {
this.term.textarea.addEventListener("focus", callbacks.focus); this.term.textarea.addEventListener("focus", callbacks.focus);
this.term.textarea.addEventListener("blur", callbacks.blur); this.term.textarea.addEventListener("blur", callbacks.blur);
this.term.textarea.addEventListener("keyup", async (ev) => {
if (this.closed) {
return;
}
if (ev.ctrlKey && ev.shiftKey) {
switch (ev.keyCode) {
case 86:
try {
let text = await window.navigator.clipboard.readText();
this.writeStr(text);
} catch (e) {
alert(
"Unable to paste: " +
e +
". Please try again without using the Control+Shift+V hot key"
);
}
return;
case 67:
try {
window.navigator.clipboard.writeText(this.term.getSelection());
} catch (e) {
alert("Unable to copy: " + e);
}
return;
}
}
});
this.refit(); this.refit();
} }
@@ -295,6 +303,20 @@ class Term {
} }
} }
writeEchoStr(d) {
if (this.closed) {
return;
}
this.control.send(d);
if (!this.control.echo()) {
return;
}
this.writeStr(d);
}
writeStr(d) { writeStr(d) {
if (this.closed) { if (this.closed) {
return; return;