From b5798c8ba6210c9990b153cf4461d5a0d29086ea Mon Sep 17 00:00:00 2001 From: Jcparkyn <51850908+Jcparkyn@users.noreply.github.com> Date: Wed, 22 Jul 2020 17:31:28 +1000 Subject: [PATCH] Automatically focus output edit field --- Nodexr/wwwroot/js/ContentEditable.js | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/Nodexr/wwwroot/js/ContentEditable.js b/Nodexr/wwwroot/js/ContentEditable.js index a3b9468d..5573048c 100644 --- a/Nodexr/wwwroot/js/ContentEditable.js +++ b/Nodexr/wwwroot/js/ContentEditable.js @@ -7,8 +7,7 @@ window.contentEditable = { initContentEditable: function (div, instance, textToDisplay) { div.innerText = textToDisplay; div.addEventListener("input", function () { - let text = div.innerText;//contentEditable.strip(div.innerText); - instance.invokeMethodAsync("GetUpdatedTextFromJavascript", text); + instance.invokeMethodAsync("GetUpdatedTextFromJavascript", div.innerText); }); try { @@ -17,6 +16,16 @@ window.contentEditable = { catch (e) { contentEditable.setupFallbackPlaintextOnly(div); } + contentEditable.moveCursorToEnd(div); + }, + + moveCursorToEnd: function (elem) { + let s = window.getSelection(); + let r = document.createRange(); + r.setStart(elem, 1); + r.setEnd(elem, 1); + s.removeAllRanges(); + s.addRange(r); }, setupFallbackPlaintextOnly: function (elem) { @@ -55,12 +64,4 @@ window.contentEditable = { document.selection.createRange().text = text; } }, - - strip: function(html) { - let tempDiv = document.createElement("div"); - tempDiv.innerText = html; - let text = tempDiv.innerText; - tempDiv.remove(); - return text; - } } \ No newline at end of file