webclient: tab key support

Tabulation key adds 4 spaces at current cursor position

Signed-off-by: Simon LEONARD <git-1001af4@sinux.sh>
This commit is contained in:
Simon LEONARD
2022-03-17 10:31:57 +01:00
committed by Gunwant Jain
parent 0305742d6d
commit 78f8398382

View File

@@ -22,10 +22,29 @@ const onInput = () => {
textarea.addEventListener('input', onInput); textarea.addEventListener('input', onInput);
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) => { document.body.addEventListener('keydown', (e) => {
if (e.key === 'Enter' && e.ctrlKey) { if (e.key === 'Enter' && e.ctrlKey) {
form.submit(); form.submit();
} }
if (e.key === 'Tab' && !e.ctrlKey) {
preventDefaults(e);
indent();
}
}); });
async function postData(url = '', data) { async function postData(url = '', data) {
@@ -118,4 +137,4 @@ document.onpaste = function (pasteEvent) {
console.info(err + " url: " + url); console.info(err + " url: " + url);
}); });
} }
} }