Mercurial > hgrepos > hgweb.cgi > 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, 98 insertions(+), 12 deletions(-) [+] |
line wrap: on
line diff
--- a/s4-main.js Tue Jun 30 08:52:20 2020 +0900 +++ b/s4-main.js Tue Jun 30 10:55:43 2020 +0900 @@ -72,15 +72,59 @@ u.checked = !u.checked; } } - function helpMarkdown(e) { - //alert(e.keyCode); - if (e.keyCode == 13) { + function renumberOL(str, start) { + var stra = str.split("\n"); + + for (var i=1; i<stra.length; i++) { + if (stra[i].match(/^[1-9][0-9]*\. /)) { + let orig=stra[i]; + stra[i] = (++start)+". "+RegExp.rightContext; + } else if (stra[i].match(/^ /)) { + continue; + } else + break; + } + return stra.join("\n"); + } + function submitThisForm(input) { + for (var elm=input.parentNode; elm; elm = elm.parentNode) { + if (elm.nodeName.match(/form/i)) { + elm.submit(); + return true; + } + } + return false; + } + function helpMarkdownBS(e) { + var area = e.target, pos = area.selectionStart, text = area.value; + if (area.selectionStart != area.selectionEnd) return; + if (pos<2) return; + if (text.substr(pos-1, 2)=="\n\n") return; + var bol = text.lastIndexOf("\n", pos-1), + eol = text.indexOf("\n", pos); + if (bol<=0 || bol==eol) return; + var thisline = text.substring(bol+1, eol==-1 ? text.length : eol); + thisline = text.substring(bol+1, pos); + if (thisline == "* ") { + area.setSelectionRange(pos-2, pos); + } else if (thisline.match(/^[1-9][0-9]*\. $/)) { + area.setSelectionRange(pos-RegExp.lastMatch.length, pos); + } + } + function helpMarkdownEnter(e) { + if (e.keyCode == 13 && !e.shiftKey) { + if (e.metaKey && submitThisForm(e.target)) { + e.preventDefault(); + return; + } var area = e.target; var pos = area.selectionStart, text = area.value; if (pos==0) return; var last = text.lastIndexOf("\n", pos-1); - var rest = text.substring(pos); - var line = last ? text.substring(last+1) : text; + var rest = text.substring(pos), rest0=rest; + var line = last ? text.substring(last+1, pos) : text; + var next = rest.substring(rest.indexOf("\n"))||rest; + next=next.substring(1); var tail = text.substring(pos-2, pos), br = (tail==" "); var add = "", offset = 1; if (line.startsWith("* ")) { @@ -90,26 +134,68 @@ add = " " + "\n" + add; } } else if (line.match(/^([1-9][0-9]*)\. /)) { - ln = parseInt(RegExp.$1); - add = (ln+1)+". "; - offset += add.length; + var ln = parseInt(RegExp.$1), nn=ln+1, + len = RegExp.lastMatch.length; + add = nn+". "; + let toeol = text.substr(pos, text.indexOf("\n")); if (br) { - add = " ".repeat(RegExp.$1.length+2) + "\n" + add; + if (next.startsWith(add)) { + add=" ".repeat(len); + nn = ln; + } else { + add = " ".repeat(len)+ "\n" + add; + offset -= len+1; + } } - } else if (line.match(/^\|( *).+\|/)) { - alert("table"); + if (next.match(/^[1-9][0-9]*\. /)) + rest = renumberOL(rest, nn); + offset += add.length; + } else if (line.match(/^\|( *).+\|/)) { add = "|" + RegExp.$1 + " |"; offset += add.length-2; } else { return; } e.preventDefault(); - area.value = text.substring(0, pos) + "\n" + add + rest; + if (!document.execCommand("insertText", false, "\n"+add)) { + //Firefox + area.selectionEnd = area.value.length; + area.setRangeText("\n"+add+rest); + area.selectionEnd = null; + } else { + area.selectionEnd = area.value.length; + area.setSelectionRange(area.selectionStart, area.value.length); + document.execCommand("insertText", false, rest); + area.selectionEnd = null; + area.focus(); + } + area.selectionStart = pos+offset; + return; + if (document.execCommand("insertText", false, "\n"+add)) { + //area.setSelectionRange(area.selectionStart, text.length); + // alert("rest=["+rest+"], add=["+add+"]"); + alert(text.substring(pos, area.value.length)); + if (rest != rest0) { + area.setSelectionRange(pos, area.value.length); + return; + document.execCommand("delete"); + } + document.execCommand("insertText", false, rest); + } else { + // Firefox cannot use insertText in textarea... + area.value = text.substring(0, pos) + "\n" + add + rest; + } //area.setSelectionRange(pos+length(add)); area.selectionStart=area.selectionEnd = (pos + offset); } } + function helpMarkdown(e) { + switch (e.keyCode) { + case 8: helpMarkdownBS(e); break; + case 13: helpMarkdownEnter(e); break; + } + } /* Init event listeners */ function addFileInput() { var inpfile = collectElementsByAttr("input", "name", "image");