s4

changeset 846:9c4e16c173db

Add support for small help on describing markdown
author HIROSE Yuuji <yuuji@gentei.org>
date Fri, 26 Jun 2020 19:55:14 +0900
parents 5e980a6c7524
children a9e147e355fd
files s4-main.js
diffstat 1 files changed, 33 insertions(+), 0 deletions(-) [+]
line diff
     1.1 --- a/s4-main.js	Fri Jun 26 11:41:45 2020 +0900
     1.2 +++ b/s4-main.js	Fri Jun 26 19:55:14 2020 +0900
     1.3 @@ -72,6 +72,33 @@
     1.4  	    u.checked = !u.checked;
     1.5  	}
     1.6      }
     1.7 +    function helpMarkdown(e) {
     1.8 +	//alert(e.keyCode);
     1.9 +	if (e.keyCode == 13) {
    1.10 +	    e.preventDefault();
    1.11 +	    var area = e.target;
    1.12 +	    var pos  = area.selectionStart, text = area.value;
    1.13 +	    var last = text.lastIndexOf("\n", pos-1);
    1.14 +	    var line = last ? text.substring(last+1) : text;
    1.15 +	    var tail = text.substring(pos-2, pos);
    1.16 +	    var add  = "", offset = 0;
    1.17 +	    if (line.startsWith("* ")) {
    1.18 +		add = (tail=="  ") ? "  " : "* ";
    1.19 +	    } else if (line.match(/^([1-9][0-9]*)\. /)) {
    1.20 +		ln = parseInt(RegExp.$1);
    1.21 +		add = (tail=="  ") ? " ".repeat(RegExp.$1.length+2)
    1.22 +		    : (ln+1)+". ";
    1.23 +	    } else if (line.match(/^\|(  *).+\|/)) {
    1.24 +		add = "|" + RegExp.$1 + " |";
    1.25 +		offset = -2;
    1.26 +	    }
    1.27 +	    area.value = text.substring(0, pos) + "\n" + add;
    1.28 +	    //area.setSelectionRange(pos+length(add));
    1.29 +	    area.selectionStart=area.selectionEnd
    1.30 +		= (area.selectionStart + offset);
    1.31 +	}
    1.32 +    }
    1.33 +    /* Init event listeners */
    1.34      function addFileInput() {
    1.35  	var inpfile = collectElementsByAttr("input", "name", "image");
    1.36  	if (!inpfile) return;
    1.37 @@ -112,6 +139,11 @@
    1.38  	    });
    1.39  	}
    1.40      }
    1.41 +    function initTextarea() {
    1.42 +	var te = collectElementsByAttr("textarea", "name", "text");
    1.43 +	if (!te || !te[0]) return;
    1.44 +	te[0].addEventListener("keydown", helpMarkdown, false);
    1.45 +    }
    1.46      function initBlogs() {
    1.47  	// Auto-complete #xxxx
    1.48  	var check = collectElementsByAttr("input", "name", "notifyto");
    1.49 @@ -180,6 +212,7 @@
    1.50  	initGrpAction();
    1.51  	initBlogs();
    1.52  	initFileInput();
    1.53 +	initTextarea();
    1.54      }
    1.55      document.addEventListener('DOMContentLoaded', init, null);
    1.56  })();