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 wrap: on
line diff
--- 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);
 })();

yatex.org