/* Add callbacks on text formatting buttons */ function editor_bold(e) { let ta = document.querySelector(".editor textarea"); let indexStart = ta.selectionStart; let indexEnd = ta.selectionEnd; let txt = ta.value.substring(indexStart, indexEnd); ta.value += '\n' + 'bold'; } // Tab insert some spaces // Ctrl+Enter send the form ta = document.querySelector(".editor textarea"); ta.addEventListener('keydown', function(e) { let keyCode = e.keyCode || e.which; if (keyCode == 9) { e.preventDefault(); let start = e.target.selectionStart; let end = e.target.selectionEnd; // set textarea value to: text before caret + tab + text after caret e.target.value = e.target.value.substring(0, start) + "\t" + e.target.value.substring(end); e.target.selectionEnd = start + 1; } if (e.ctrlKey && keyCode == 13) { let e = e.target; while(! (e instanceof HTMLFormElement)) { e = e.parentNode; } try { e.submit(); } catch(exception) { e.submit.click(); } } });