This commit is contained in:
Florine W. Dekker 2019-11-03 17:23:36 +01:00
parent e2fff21f95
commit d1b4bc4c27
Signed by: FWDekker
GPG Key ID: B1B567AF58D6EE0F
1 changed files with 13 additions and 5 deletions

View File

@ -55,9 +55,9 @@ export class Terminal {
this.inputHistory = new InputHistory(); this.inputHistory = new InputHistory();
this.shell = new Shell(this.inputHistory); this.shell = new Shell(this.inputHistory);
this.terminal.addEventListener("click", this.onclick.bind(this)); document.addEventListener("click", this.onclick.bind(this));
this.terminal.addEventListener("keypress", this.onkeypress.bind(this)); document.addEventListener("keypress", this.onkeypress.bind(this));
this.terminal.addEventListener("keydown", this.onkeydown.bind(this)); document.addEventListener("keydown", this.onkeydown.bind(this));
let scrollStartPosition: number = 0; let scrollStartPosition: number = 0;
this.terminal.addEventListener("wheel", (event: WheelEvent) => { this.terminal.addEventListener("wheel", (event: WheelEvent) => {
@ -245,7 +245,9 @@ export class Terminal {
* Handles click events. * Handles click events.
*/ */
private onclick(): void { private onclick(): void {
this.input.focus(); // Focus on input unless user has text selected. This allows user to copy text
if ((document.getSelection() || "").toString() === "")
this.input.focus();
} }
/** /**
@ -255,6 +257,10 @@ export class Terminal {
*/ */
private onkeypress(event: KeyboardEvent): void { private onkeypress(event: KeyboardEvent): void {
this.scroll = 0; this.scroll = 0;
// If user types anywhere, move caret to end of input, unless user was already focused on input
if (this.input !== document.activeElement)
setTimeout(() => moveCaretToEndOf(this.input), 0);
switch (event.key.toLowerCase()) { switch (event.key.toLowerCase()) {
case "enter": case "enter":
this.processInput(this.inputText.replaceAll(/ /, " ")); this.processInput(this.inputText.replaceAll(/ /, " "));
@ -270,6 +276,7 @@ export class Terminal {
*/ */
private onkeydown(event: KeyboardEvent): void { private onkeydown(event: KeyboardEvent): void {
this.scroll = 0; this.scroll = 0;
switch (event.key.toLowerCase()) { switch (event.key.toLowerCase()) {
case "arrowup": case "arrowup":
this.inputText = this.inputHistory.previousEntry(); this.inputText = this.inputHistory.previousEntry();
@ -283,7 +290,8 @@ export class Terminal {
event.preventDefault(); event.preventDefault();
break; break;
case "c": case "c":
if (event.ctrlKey) { // Only if focused on the input as to not prevent copying of selected text
if (event.ctrlKey && this.input === document.activeElement) {
this.ignoreInput(); this.ignoreInput();
event.preventDefault(); event.preventDefault();
} }