diff options
author | root <root@pwd.linpro.no> | 2014-02-28 16:50:09 +0100 |
---|---|---|
committer | root <root@pwd.linpro.no> | 2014-02-28 16:50:09 +0100 |
commit | 6e9d8b9713b57c925743e037c6936ad876ca11f7 (patch) | |
tree | 6546ea9416dbeffc0e175a117c5bbc52ae40a881 | |
parent | c9f565d621357397a8e561a0699e653ea306b449 (diff) | |
download | ukulele-6e9d8b9713b57c925743e037c6936ad876ca11f7.tar.xz |
added prepend, fixed quoting and lists
-rw-r--r-- | wiki.js | 75 |
1 files changed, 48 insertions, 27 deletions
@@ -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, "<i>_kursiv_</i>", marxup_italics); button(orz, "<b>*fet*</b>", marxup_bold); |