You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

94 lines
3.0 KiB

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