s4

changeset 847:a9e147e355fd

Do not file helper when no pattern matches
author HIROSE Yuuji <yuuji@gentei.org>
date Fri, 26 Jun 2020 22:28:59 +0900
parents 9c4e16c173db
children 770f2eed24a1
files s4-main.js
diffstat 1 files changed, 22 insertions(+), 10 deletions(-) [+]
line diff
     1.1 --- a/s4-main.js	Fri Jun 26 19:55:14 2020 +0900
     1.2 +++ b/s4-main.js	Fri Jun 26 22:28:59 2020 +0900
     1.3 @@ -75,27 +75,39 @@
     1.4      function helpMarkdown(e) {
     1.5  	//alert(e.keyCode);
     1.6  	if (e.keyCode == 13) {
     1.7 -	    e.preventDefault();
     1.8  	    var area = e.target;
     1.9  	    var pos  = area.selectionStart, text = area.value;
    1.10 +	    if (pos==0) return;
    1.11  	    var last = text.lastIndexOf("\n", pos-1);
    1.12 +	    var rest = text.substring(pos);
    1.13  	    var line = last ? text.substring(last+1) : text;
    1.14 -	    var tail = text.substring(pos-2, pos);
    1.15 -	    var add  = "", offset = 0;
    1.16 +	    var tail = text.substring(pos-2, pos), br = (tail=="  ");
    1.17 +	    var add  = "", offset = 1;
    1.18  	    if (line.startsWith("* ")) {
    1.19 -		add = (tail=="  ") ? "  " : "* ";
    1.20 +		add =  "* ";
    1.21 +		offset += add.length;
    1.22 +		if (br) {
    1.23 +		    add = "  " + "\n" + add;
    1.24 +		}
    1.25  	    } else if (line.match(/^([1-9][0-9]*)\. /)) {
    1.26  		ln = parseInt(RegExp.$1);
    1.27 -		add = (tail=="  ") ? " ".repeat(RegExp.$1.length+2)
    1.28 -		    : (ln+1)+". ";
    1.29 +		add = (ln+1)+". ";
    1.30 +		offset += add.length;
    1.31 +		if (br) {
    1.32 +		    add = " ".repeat(RegExp.$1.length+2) + "\n" + add;
    1.33 +		}
    1.34  	    } else if (line.match(/^\|(  *).+\|/)) {
    1.35 +		alert("table");
    1.36  		add = "|" + RegExp.$1 + " |";
    1.37 -		offset = -2;
    1.38 +		offset += add.length-2;
    1.39 +	    } else {
    1.40 +		return;
    1.41  	    }
    1.42 -	    area.value = text.substring(0, pos) + "\n" + add;
    1.43 +	    e.preventDefault();
    1.44 +	    area.value = text.substring(0, pos) + "\n" + add + rest;
    1.45  	    //area.setSelectionRange(pos+length(add));
    1.46 -	    area.selectionStart=area.selectionEnd
    1.47 -		= (area.selectionStart + offset);
    1.48 +	    area.selectionStart=area.selectionEnd = (pos + offset);
    1.49 +	    
    1.50  	}
    1.51      }
    1.52      /* Init event listeners */