s4

changeset 852:3e09ac711f69

Renumbering text modified
author HIROSE Yuuji <yuuji@gentei.org>
date Tue, 30 Jun 2020 10:55:43 +0900
parents 7ca68e8c2702
children 047e1c3f811e
files s4-main.js
diffstat 1 files changed, 99 insertions(+), 13 deletions(-) [+]
line diff
     1.1 --- a/s4-main.js	Tue Jun 30 08:52:20 2020 +0900
     1.2 +++ b/s4-main.js	Tue Jun 30 10:55:43 2020 +0900
     1.3 @@ -72,15 +72,59 @@
     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 +    function renumberOL(str, start) {
    1.11 +	var stra = str.split("\n");
    1.12 +		
    1.13 +	for (var i=1; i<stra.length; i++) {
    1.14 +	    if (stra[i].match(/^[1-9][0-9]*\. /)) {
    1.15 +		let orig=stra[i];
    1.16 +		stra[i] = (++start)+". "+RegExp.rightContext;
    1.17 +	    } else if (stra[i].match(/^  /)) {
    1.18 +		continue;
    1.19 +	    } else
    1.20 +		break;
    1.21 +	}
    1.22 +	return stra.join("\n");
    1.23 +    }
    1.24 +    function submitThisForm(input) {
    1.25 +	for (var elm=input.parentNode; elm; elm = elm.parentNode) {
    1.26 +	    if (elm.nodeName.match(/form/i)) {
    1.27 +		elm.submit();
    1.28 +		return true;
    1.29 +	    }
    1.30 +	}
    1.31 +	return false;
    1.32 +    }
    1.33 +    function helpMarkdownBS(e) {
    1.34 +	var area = e.target, pos = area.selectionStart, text = area.value;
    1.35 +	if (area.selectionStart != area.selectionEnd) return;
    1.36 +	if (pos<2) return;
    1.37 +	if (text.substr(pos-1, 2)=="\n\n") return;
    1.38 +	var bol  = text.lastIndexOf("\n", pos-1),
    1.39 +	    eol  = text.indexOf("\n", pos);
    1.40 +	if (bol<=0 || bol==eol) return;
    1.41 +	var thisline = text.substring(bol+1, eol==-1 ? text.length : eol);
    1.42 +	thisline = text.substring(bol+1, pos);
    1.43 +	if (thisline == "* ") {
    1.44 +	    area.setSelectionRange(pos-2, pos);
    1.45 +	} else if (thisline.match(/^[1-9][0-9]*\. $/)) {
    1.46 +	    area.setSelectionRange(pos-RegExp.lastMatch.length, pos);
    1.47 +	}
    1.48 +    }
    1.49 +    function helpMarkdownEnter(e) {
    1.50 +	if (e.keyCode == 13 && !e.shiftKey) {
    1.51 +	    if (e.metaKey && submitThisForm(e.target)) {
    1.52 +		e.preventDefault();
    1.53 +		return;
    1.54 +	    }
    1.55  	    var area = e.target;
    1.56  	    var pos  = area.selectionStart, text = area.value;
    1.57  	    if (pos==0) return;
    1.58  	    var last = text.lastIndexOf("\n", pos-1);
    1.59 -	    var rest = text.substring(pos);
    1.60 -	    var line = last ? text.substring(last+1) : text;
    1.61 +	    var rest = text.substring(pos), rest0=rest;
    1.62 +	    var line = last ? text.substring(last+1, pos) : text;
    1.63 +	    var next = rest.substring(rest.indexOf("\n"))||rest;
    1.64 +	    next=next.substring(1);
    1.65  	    var tail = text.substring(pos-2, pos), br = (tail=="  ");
    1.66  	    var add  = "", offset = 1;
    1.67  	    if (line.startsWith("* ")) {
    1.68 @@ -90,26 +134,68 @@
    1.69  		    add = "  " + "\n" + add;
    1.70  		}
    1.71  	    } else if (line.match(/^([1-9][0-9]*)\. /)) {
    1.72 -		ln = parseInt(RegExp.$1);
    1.73 -		add = (ln+1)+". ";
    1.74 +		var ln = parseInt(RegExp.$1), nn=ln+1,
    1.75 +		    len = RegExp.lastMatch.length;
    1.76 +		add = nn+". ";
    1.77 +		let toeol = text.substr(pos, text.indexOf("\n"));
    1.78 +		if (br) {
    1.79 +		    if (next.startsWith(add)) {
    1.80 +			add=" ".repeat(len);
    1.81 +			nn = ln;
    1.82 +		    } else {
    1.83 +			add = " ".repeat(len)+ "\n" + add;
    1.84 +			offset -= len+1;
    1.85 +		    }
    1.86 +		}
    1.87 +		if (next.match(/^[1-9][0-9]*\. /))
    1.88 +		    rest = renumberOL(rest, nn);
    1.89  		offset += add.length;
    1.90 -		if (br) {
    1.91 -		    add = " ".repeat(RegExp.$1.length+2) + "\n" + add;
    1.92 -		}
    1.93 -	    } else if (line.match(/^\|(  *).+\|/)) {
    1.94 -		alert("table");
    1.95 +	    } else if (line.match(/^\|( *).+\|/)) {
    1.96  		add = "|" + RegExp.$1 + " |";
    1.97  		offset += add.length-2;
    1.98  	    } else {
    1.99  		return;
   1.100  	    }
   1.101  	    e.preventDefault();
   1.102 -	    area.value = text.substring(0, pos) + "\n" + add + rest;
   1.103 +	    if (!document.execCommand("insertText", false, "\n"+add)) {
   1.104 +		//Firefox
   1.105 +		area.selectionEnd = area.value.length;
   1.106 +		area.setRangeText("\n"+add+rest);
   1.107 +		area.selectionEnd = null;
   1.108 +	    } else {
   1.109 +		area.selectionEnd = area.value.length;
   1.110 +		area.setSelectionRange(area.selectionStart, area.value.length);
   1.111 +		document.execCommand("insertText", false, rest);
   1.112 +		area.selectionEnd = null;
   1.113 +		area.focus();
   1.114 +	    }
   1.115 +	    area.selectionStart = pos+offset;
   1.116 +	    return;
   1.117 +	    if (document.execCommand("insertText", false, "\n"+add)) {
   1.118 +		//area.setSelectionRange(area.selectionStart, text.length);
   1.119 +		// alert("rest=["+rest+"], add=["+add+"]");
   1.120 +		alert(text.substring(pos, area.value.length));
   1.121 +		if (rest != rest0) {
   1.122 +		    area.setSelectionRange(pos, area.value.length);
   1.123 +		    return;
   1.124 +		    document.execCommand("delete");
   1.125 +		}
   1.126 +		document.execCommand("insertText", false, rest);
   1.127 +	    } else {
   1.128 +		// Firefox cannot use insertText in textarea...
   1.129 +		area.value = text.substring(0, pos) + "\n" + add + rest;
   1.130 +	    }
   1.131  	    //area.setSelectionRange(pos+length(add));
   1.132  	    area.selectionStart=area.selectionEnd = (pos + offset);
   1.133  	    
   1.134  	}
   1.135      }
   1.136 +    function helpMarkdown(e) {
   1.137 +	switch (e.keyCode) {
   1.138 +	case  8: helpMarkdownBS(e); break;
   1.139 +	case 13: helpMarkdownEnter(e); break;
   1.140 +	}
   1.141 +    }
   1.142      /* Init event listeners */
   1.143      function addFileInput() {
   1.144  	var inpfile = collectElementsByAttr("input", "name", "image");