diff --git a/js/shared.js b/js/shared.js index b5d8f5d..64b196e 100644 --- a/js/shared.js +++ b/js/shared.js @@ -20,6 +20,7 @@ String.prototype.trimLines = function () { return this.split("\n").map(it => it.trim()).join("\n"); }; + function addOnLoad(fun) { const oldOnLoad = window.onload || (() => { }); @@ -30,6 +31,16 @@ function addOnLoad(fun) { }); } +function moveCaretToEndOf(element) { + const range = document.createRange(); + range.selectNodeContents(element); + range.collapse(false); + + const selection = window.getSelection(); + selection.removeAllRanges(); + selection.addRange(range); +} + function q(query) { return document.querySelector(query); } diff --git a/js/terminal.js b/js/terminal.js index 9c92cdc..cf5923c 100644 --- a/js/terminal.js +++ b/js/terminal.js @@ -144,9 +144,11 @@ class Terminal { switch (e.key.toLowerCase()) { case "arrowup": this.inputText = this._inputHistory.previousEntry(); + window.setTimeout(() => moveCaretToEndOf(this._input), 0); break; case "arrowdown": this.inputText = this._inputHistory.nextEntry(); + window.setTimeout(() => moveCaretToEndOf(this._input), 0); break; case "l": if (e.ctrlKey) {