| 1 | | function addWikiFormattingToolbar(textarea) { |
| 2 | | if ((typeof(document["selection"]) == "undefined") |
| 3 | | && (typeof(textarea["setSelectionRange"]) == "undefined")) { |
| 4 | | return; |
| 5 | | } |
| 6 | | |
| 7 | | var toolbar = document.createElement("div"); |
| 8 | | toolbar.className = "wikitoolbar"; |
| 9 | | |
| 10 | | function addButton(id, title, fn) { |
| 11 | | var a = document.createElement("a"); |
| 12 | | a.href = "#"; |
| 13 | | a.id = id; |
| 14 | | a.title = title; |
| 15 | | a.onclick = function() { try { fn() } catch (e) { } return false }; |
| 16 | | a.tabIndex = 400; |
| 17 | | toolbar.appendChild(a); |
| 18 | | } |
| 19 | | |
| 20 | | function encloseSelection(prefix, suffix) { |
| 21 | | textarea.focus(); |
| 22 | | var start, end, sel, scrollPos, subst; |
| 23 | | if (typeof(document["selection"]) != "undefined") { |
| 24 | | sel = document.selection.createRange().text; |
| 25 | | } else if (typeof(textarea["setSelectionRange"]) != "undefined") { |
| 26 | | start = textarea.selectionStart; |
| 27 | | end = textarea.selectionEnd; |
| 28 | | scrollPos = textarea.scrollTop; |
| 29 | | sel = textarea.value.substring(start, end); |
| 30 | | } |
| 31 | | if (sel.match(/ $/)) { // exclude ending space char, if any |
| 32 | | sel = sel.substring(0, sel.length - 1); |
| 33 | | suffix = suffix + " "; |
| 34 | | } |
| 35 | | subst = prefix + sel + suffix; |
| 36 | | if (typeof(document["selection"]) != "undefined") { |
| 37 | | var range = document.selection.createRange().text = subst; |
| 38 | | textarea.caretPos -= suffix.length; |
| 39 | | } else if (typeof(textarea["setSelectionRange"]) != "undefined") { |
| 40 | | textarea.value = textarea.value.substring(0, start) + subst + |
| 41 | | textarea.value.substring(end); |
| 42 | | if (sel) { |
| 43 | | textarea.setSelectionRange(start + subst.length, start + subst.length); |
| 44 | | } else { |
| 45 | | textarea.setSelectionRange(start + prefix.length, start + prefix.length); |
| 46 | | } |
| 47 | | textarea.scrollTop = scrollPos; |
| 48 | | } |
| 49 | | } |
| 50 | | |
| 51 | | addButton("strong", "Bold text: '''Example'''", function() { |
| 52 | | encloseSelection("'''", "'''"); |
| 53 | | }); |
| 54 | | addButton("em", "Italic text: ''Example''", function() { |
| 55 | | encloseSelection("''", "''"); |
| 56 | | }); |
| 57 | | addButton("heading", "Heading: == Example ==", function() { |
| 58 | | encloseSelection("\n== ", " ==\n", "Heading"); |
| 59 | | }); |
| 60 | | addButton("link", "Link: [http://www.example.com/ Example]", function() { |
| 61 | | encloseSelection("[", "]"); |
| 62 | | }); |
| 63 | | addButton("code", "Code block: {{{ example }}}", function() { |
| 64 | | encloseSelection("\n{{{\n", "\n}}}\n"); |
| 65 | | }); |
| 66 | | addButton("hr", "Horizontal rule: ----", function() { |
| 67 | | encloseSelection("\n----\n", ""); |
| 68 | | }); |
| 69 | | addButton("np", "New paragraph", function() { |
| 70 | | encloseSelection("\n\n", ""); |
| 71 | | }); |
| 72 | | addButton("br", "Line break: [[BR]]", function() { |
| 73 | | encloseSelection("[[BR]]\n", ""); |
| 74 | | }); |
| 75 | | |
| 76 | | textarea.parentNode.insertBefore(toolbar, textarea); |
| 77 | | } |
| 78 | | |
| 79 | | // Add the toolbar to all <textarea> elements on the page with the class |
| 80 | | // 'wikitext'. |
| 81 | | var re = /\bwikitext\b/; |
| 82 | | var textareas = document.getElementsByTagName("textarea"); |
| 83 | | for (var i = 0; i < textareas.length; i++) { |
| 84 | | var textarea = textareas[i]; |
| 85 | | if (textarea.className && re.test(textarea.className)) { |
| 86 | | addWikiFormattingToolbar(textarea); |
| 87 | | } |
| 88 | | } |