From 6e9d8b9713b57c925743e037c6936ad876ca11f7 Mon Sep 17 00:00:00 2001 From: root Date: Fri, 28 Feb 2014 16:50:09 +0100 Subject: added prepend, fixed quoting and lists --- wiki.js | 75 +++++++++++++++++++++++++++++++++++++++++------------------------ 1 file changed, 48 insertions(+), 27 deletions(-) diff --git a/wiki.js b/wiki.js index 4c6cab4..d9061a0 100644 --- a/wiki.js +++ b/wiki.js @@ -5,9 +5,10 @@ function insert(text) { var pos = t.selectionStart; var end = t.selectionEnd; t.value = t.value.substring(0, pos) + text + t.value.substring(end, t.value.length); - } else { - t.value += s; } + t.focus(); // CONCENTRATE + var cpos = t.selectionStart; + t.setSelectionRange(cpos, cpos); } function wrap(prefix, postfix) { @@ -25,12 +26,49 @@ function wrap(prefix, postfix) { t.setSelectionRange(cpos, cpos); } +function prepend(text) { + var t = document.getElementsByName('t')[0]; + if(!t) return; + if(t.selectionStart || t.selectionStart == '0') { + var pos = t.selectionStart; + var end = t.selectionEnd; + for(;pos < end; pos++) { // let me see you strip + if(t.value.charAt(pos) != "\n") break; + } + for(;end > pos; end--) { // front and back + if(t.value.charAt(end - 1) != "\n") break; + } + var quote = ""; + var cpos = end; + if(t.value.charAt(pos - 1) != "\n") { // in the middle of something? + quote += "\n"; // PRE-pend, dang it + cpos += 1; + } + if(pos == end) { // no selection. text only. final destination. + quote += text; + cpos += text.length; + } else { + var lines = t.value.substring(pos, end).split("\n"); + for(var i=0; i < lines.length; i++) { + // do we want to prepend for empty lines inside selection? + // if not, add check here. DECISIONS + quote += text + lines[i] + "\n"; + cpos += text.length; // should i add 1 here for the newline? who knows! + } + quote = quote.slice(0, -1); // strip bo(g|n)us newline + } + t.value = t.value.substring(0, pos) + quote + t.value.substring(end, t.value.length); + } + t.focus(); + t.setSelectionRange(cpos, cpos); +} + function marxup_bold(e) { wrap("*", "*"); } function marxup_header(e) { - wrap("= ", ""); + prepend("= "); } function marxup_italics(e) { @@ -42,29 +80,11 @@ function marxup_link(e) { } function marxup_quote(e) { - var t = document.getElementsByName('t')[0]; - if(!t) return; - if(t.selectionStart || t.selectionStart == '0') { - var pos = t.selectionStart; - var end = t.selectionEnd; - for(;pos < end; pos++) { - if(t.value.charAt(pos) != "\n") break; - } - var quote = ""; - var lines = t.value.substring(pos, end).split("\n"); - var cpos = end; - for(var i=0; i < lines.length; i++) { - if(lines[i].length > 0) { - quote += "\n> " + lines[i]; - cpos += 2; - } - } - if(t.value.substring(pos - 1, pos) == "\n") - quote = quote.trim() + "\n"; - t.value = t.value.substring(0, pos) + quote + t.value.substring(end, t.value.length); - } - t.focus(); - t.setSelectionRange(cpos, cpos); + prepend("> "); +} + +function marxup_list(e) { + prepend("* "); } function button(node, html, fun) { @@ -75,7 +95,7 @@ function button(node, html, fun) { node.parentNode.insertBefore(a, node.nextSibling); } -function pick(src) { +function orz(src) { insert("\n@" + src.replace(".t.jpg", "") + "\n"); } @@ -101,6 +121,7 @@ document.addEventListener("DOMContentLoaded", function(e) { return false; } button(orz, "> sitat", marxup_quote); + button(orz, "* liste", marxup_list); button(orz, "[lenke]", marxup_link); button(orz, "_kursiv_", marxup_italics); button(orz, "*fet*", marxup_bold); -- cgit v1.0