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