# HG changeset patch # User HIROSE Yuuji # Date 1593168914 -32400 # Node ID 9c4e16c173db1fe47f6200642b80e22ecf792722 # Parent 5e980a6c7524494d823cca7763ca6fb17f50f345 Add support for small help on describing markdown diff -r 5e980a6c7524 -r 9c4e16c173db s4-main.js --- a/s4-main.js Fri Jun 26 11:41:45 2020 +0900 +++ b/s4-main.js Fri Jun 26 19:55:14 2020 +0900 @@ -72,6 +72,33 @@ u.checked = !u.checked; } } + function helpMarkdown(e) { + //alert(e.keyCode); + if (e.keyCode == 13) { + e.preventDefault(); + var area = e.target; + var pos = area.selectionStart, text = area.value; + var last = text.lastIndexOf("\n", pos-1); + var line = last ? text.substring(last+1) : text; + var tail = text.substring(pos-2, pos); + var add = "", offset = 0; + if (line.startsWith("* ")) { + add = (tail==" ") ? " " : "* "; + } else if (line.match(/^([1-9][0-9]*)\. /)) { + ln = parseInt(RegExp.$1); + add = (tail==" ") ? " ".repeat(RegExp.$1.length+2) + : (ln+1)+". "; + } else if (line.match(/^\|( *).+\|/)) { + add = "|" + RegExp.$1 + " |"; + offset = -2; + } + area.value = text.substring(0, pos) + "\n" + add; + //area.setSelectionRange(pos+length(add)); + area.selectionStart=area.selectionEnd + = (area.selectionStart + offset); + } + } + /* Init event listeners */ function addFileInput() { var inpfile = collectElementsByAttr("input", "name", "image"); if (!inpfile) return; @@ -112,6 +139,11 @@ }); } } + function initTextarea() { + var te = collectElementsByAttr("textarea", "name", "text"); + if (!te || !te[0]) return; + te[0].addEventListener("keydown", helpMarkdown, false); + } function initBlogs() { // Auto-complete #xxxx var check = collectElementsByAttr("input", "name", "notifyto"); @@ -180,6 +212,7 @@ initGrpAction(); initBlogs(); initFileInput(); + initTextarea(); } document.addEventListener('DOMContentLoaded', init, null); })();