diff options
Diffstat (limited to 'tears.js')
-rw-r--r-- | tears.js | 140 |
1 files changed, 138 insertions, 2 deletions
@@ -24,12 +24,116 @@ function pick(src) { } } + +function insert(text) { + var t = document.getElementsByName('tekst')[0]; + if(!t) return; + if(t.selectionStart || t.selectionStart == '0') { + var pos = t.selectionStart; + var end = t.selectionEnd; + t.value = t.value.substring(0, pos) + text + t.value.substring(end, t.value.length); + } + var cpos = t.selectionStart; + t.setSelectionRange(cpos, cpos); + t.focus(); +} + +function wrap(prefix, postfix) { + var t = document.getElementsByName('tekst')[0]; + if(!t) return; + if(t.selectionStart || t.selectionStart == '0') { + var pos = t.selectionStart; + var end = t.selectionEnd; + var n = t.value.indexOf("\n", pos); + if(n > 0 && n < end) end = n; + t.value = t.value.substring(0, pos) + prefix + t.value.substring(pos, end) + postfix + t.value.substring(end, t.value.length); + } + var cpos = end + prefix.length + postfix.length; + t.setSelectionRange(cpos, cpos); + t.focus(); +} + +function prepend(text) { + var t = document.getElementsByName('tekst')[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; + } + for(;end > pos; end--) { + if(t.value.charAt(end - 1) != "\n") break; + } + var quote = ""; + var cpos = end; + if(t.value.charAt(pos - 1) != "\n") { + quote += "\n"; + cpos += 1; + } + if(pos == end) { + quote += text; + cpos += text.length; + } else { + var lines = t.value.substring(pos, end).split("\n"); + for(var i=0; i < lines.length; i++) { + quote += text + lines[i] + "\n"; + cpos += text.length; + } + quote = quote.slice(0, -1); + } + t.value = t.value.substring(0, pos) + quote + t.value.substring(end, t.value.length); + } + t.setSelectionRange(cpos, cpos); + t.focus(); +} + +function marxup_bold(e) { + wrap("*", "*"); +} + +function marxup_header(e) { + prepend("= "); +} + +function marxup_italics(e) { + wrap("_", "_"); +} + +function marxup_small(e) { + wrap("~", "~"); +} + +function marxup_link(e) { + wrap("[", "]"); +} + +function marxup_quote(e) { + prepend("> "); +} + +function marxup_list(e) { + prepend("* "); +} + +function button(node, html, fun) { + var a = document.createElement("a"); + a.innerHTML = html; + a.href = "#"; + a.onclick = fun; + node.parentNode.insertBefore(a, node.nextSibling); +} + +function orz(src) { + insert("\n{" + src.replace(".t.jpg", "") + "}\n"); +} + document.addEventListener("DOMContentLoaded", function(e) { var field = document.getElementsByName('publisert')[0]; if(field) { new Pikaday({ field: field, format: 'YYYY-MM-DD' }); } - +/* var els = document.getElementsByClassName('orz'); for(var i=0; i < els.length; i++) { els[i].onclick = function(e) { @@ -38,5 +142,37 @@ document.addEventListener("DOMContentLoaded", function(e) { return false; } } -}, false); +*/ + + var title = document.getElementsByTagName('h1')[0]; + var headers = document.getElementsByTagName("h2"); + if(headers.length > 0) { + var menu = document.createElement("div"); menu.className = 'magisk meny'; + for(var i = 0; i < headers.length; i++) { + var a = document.createElement("a"); + var id = "h-" + i; headers[i].id = id; a.href = "#" + id; + a.textContent = headers[i].textContent; + menu.appendChild(a); + menu.appendChild(document.createElement("br")); + } + title.parentNode.insertBefore(menu, title.nextSibling); + } + +// var orz = document.getElementById('orz'); + var orz = document.getElementsByClassName('orz')[0]; + if(orz) { + orz.onclick = function(e) { + window.open("/orz"); + 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); + button(orz, "<small>liten</small>", marxup_small); + button(orz, "= overskrift", marxup_header); + } +}); + |