|
|
4c79b5 |
function addWikiFormattingToolbar(textarea) {
|
|
|
4c79b5 |
if ((typeof(document["selection"]) == "undefined")
|
|
|
4c79b5 |
&& (typeof(textarea["setSelectionRange"]) == "undefined")) {
|
|
|
4c79b5 |
return;
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
var toolbar = document.createElement("div");
|
|
|
4c79b5 |
toolbar.className = "wikitoolbar";
|
|
|
4c79b5 |
|
|
|
4c79b5 |
function addButton(id, title, fn) {
|
|
|
4c79b5 |
var a = document.createElement("a");
|
|
|
4c79b5 |
a.href = "#";
|
|
|
4c79b5 |
a.id = id;
|
|
|
4c79b5 |
a.title = title;
|
|
|
4c79b5 |
a.onclick = function() { try { fn() } catch (e) { } return false };
|
|
|
4c79b5 |
a.tabIndex = 400;
|
|
|
4c79b5 |
toolbar.appendChild(a);
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
function encloseSelection(prefix, suffix) {
|
|
|
4c79b5 |
textarea.focus();
|
|
|
4c79b5 |
var start, end, sel, scrollPos, subst;
|
|
|
4c79b5 |
if (typeof(document["selection"]) != "undefined") {
|
|
|
4c79b5 |
sel = document.selection.createRange().text;
|
|
|
4c79b5 |
} else if (typeof(textarea["setSelectionRange"]) != "undefined") {
|
|
|
4c79b5 |
start = textarea.selectionStart;
|
|
|
4c79b5 |
end = textarea.selectionEnd;
|
|
|
4c79b5 |
scrollPos = textarea.scrollTop;
|
|
|
4c79b5 |
sel = textarea.value.substring(start, end);
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
if (sel.match(/ $/)) { // exclude ending space char, if any
|
|
|
4c79b5 |
sel = sel.substring(0, sel.length - 1);
|
|
|
4c79b5 |
suffix = suffix + " ";
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
subst = prefix + sel + suffix;
|
|
|
4c79b5 |
if (typeof(document["selection"]) != "undefined") {
|
|
|
4c79b5 |
var range = document.selection.createRange().text = subst;
|
|
|
4c79b5 |
textarea.caretPos -= suffix.length;
|
|
|
4c79b5 |
} else if (typeof(textarea["setSelectionRange"]) != "undefined") {
|
|
|
4c79b5 |
textarea.value = textarea.value.substring(0, start) + subst +
|
|
|
4c79b5 |
textarea.value.substring(end);
|
|
|
4c79b5 |
if (sel) {
|
|
|
4c79b5 |
textarea.setSelectionRange(start + subst.length, start + subst.length);
|
|
|
4c79b5 |
} else {
|
|
|
4c79b5 |
textarea.setSelectionRange(start + prefix.length, start + prefix.length);
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
textarea.scrollTop = scrollPos;
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
addButton("strong", "Bold text: '''Example'''", function() {
|
|
|
4c79b5 |
encloseSelection("'''", "'''");
|
|
|
4c79b5 |
});
|
|
|
4c79b5 |
addButton("em", "Italic text: ''Example''", function() {
|
|
|
4c79b5 |
encloseSelection("''", "''");
|
|
|
4c79b5 |
});
|
|
|
4c79b5 |
addButton("heading", "Heading: == Example ==", function() {
|
|
|
4c79b5 |
encloseSelection("\n== ", " ==\n", "Heading");
|
|
|
4c79b5 |
});
|
|
|
4c79b5 |
addButton("link", "Link: [http://www.example.com/ Example]", function() {
|
|
|
4c79b5 |
encloseSelection("[", "]");
|
|
|
4c79b5 |
});
|
|
|
4c79b5 |
addButton("code", "Code block: {{{ example }}}", function() {
|
|
|
4c79b5 |
encloseSelection("\n{{{\n", "\n}}}\n");
|
|
|
4c79b5 |
});
|
|
|
4c79b5 |
addButton("hr", "Horizontal rule: ----", function() {
|
|
|
4c79b5 |
encloseSelection("\n----\n", "");
|
|
|
4c79b5 |
});
|
|
|
4c79b5 |
addButton("np", "New paragraph", function() {
|
|
|
4c79b5 |
encloseSelection("\n\n", "");
|
|
|
4c79b5 |
});
|
|
|
4c79b5 |
addButton("br", "Line break: [[BR]]", function() {
|
|
|
4c79b5 |
encloseSelection("[[BR]]\n", "");
|
|
|
4c79b5 |
});
|
|
|
4c79b5 |
|
|
|
4c79b5 |
textarea.parentNode.insertBefore(toolbar, textarea);
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
// Add the toolbar to all <textarea> elements on the page with the class
|
|
|
4c79b5 |
// 'wikitext'.
|
|
|
4c79b5 |
var re = /\bwikitext\b/;
|
|
|
4c79b5 |
var textareas = document.getElementsByTagName("textarea");
|
|
|
4c79b5 |
for (var i = 0; i < textareas.length; i++) {
|
|
|
4c79b5 |
var textarea = textareas[i];
|
|
|
4c79b5 |
if (textarea.className && re.test(textarea.className)) {
|
|
|
4c79b5 |
addWikiFormattingToolbar(textarea);
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
}
|