From 78f8398382cdbad75da1c068abbb46ad6cbbbb86 Mon Sep 17 00:00:00 2001 From: Simon LEONARD Date: Thu, 17 Mar 2022 10:31:57 +0100 Subject: [PATCH] webclient: tab key support Tabulation key adds 4 spaces at current cursor position Signed-off-by: Simon LEONARD --- static/js/index.js | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/static/js/index.js b/static/js/index.js index d05ec70..8df8bda 100644 --- a/static/js/index.js +++ b/static/js/index.js @@ -22,10 +22,29 @@ const onInput = () => { textarea.addEventListener('input', onInput); onInput(); +const indent = (spaces = 4) => { + let cursorPosition = textarea.selectionStart; + const before = textarea.value.substring(0, cursorPosition); + const after = textarea.value.substring(cursorPosition, textarea.value.length); + + // add 4 spaces + textarea.value = before + ' '.repeat(spaces) + after; + cursorPosition += spaces; + + // place the cursor accordingly + textarea.selectionStart = cursorPosition; + textarea.selectionEnd = cursorPosition; + textarea.focus(); +} + document.body.addEventListener('keydown', (e) => { if (e.key === 'Enter' && e.ctrlKey) { form.submit(); } + if (e.key === 'Tab' && !e.ctrlKey) { + preventDefaults(e); + indent(); + } }); async function postData(url = '', data) { @@ -118,4 +137,4 @@ document.onpaste = function (pasteEvent) { console.info(err + " url: " + url); }); } -} \ No newline at end of file +}