aboutsummaryrefslogtreecommitdiff
path: root/exampleData/trac-new-ticket/New Ticket_files/wikitoolbar.js
blob: a8f9a54729b12c308b47121182e6e7a60d4632a6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93


(function($){
  
  
  window.addWikiFormattingToolbar = function(textarea) {
    if ((document.selection == undefined)
     && (textarea.setSelectionRange == undefined)) {
      return;
    }
  
    var toolbar = document.createElement("div");
    toolbar.className = "wikitoolbar";
  
    function addButton(id, title, fn) {
      var a = document.createElement("a");
      a.href = "#";
      a.id = id;
      a.title = title;
      a.onclick = function() { try { fn() } catch (e) { } return false };
      a.tabIndex = 400;
      toolbar.appendChild(a);
    }
  
    function encloseSelection(prefix, suffix) {
      textarea.focus();
      var start, end, sel, scrollPos, subst;
      if (document.selection != undefined) {
        sel = document.selection.createRange().text;
      } else if (textarea.setSelectionRange != undefined) {
        start = textarea.selectionStart;
        end = textarea.selectionEnd;
        scrollPos = textarea.scrollTop;
        sel = textarea.value.substring(start, end);
      }
      if (sel.match(/ $/)) { // exclude ending space char, if any
        sel = sel.substring(0, sel.length - 1);
        suffix = suffix + " ";
      }
      subst = prefix + sel + suffix;
      if (document.selection != undefined) {
        var range = document.selection.createRange().text = subst;
        textarea.caretPos -= suffix.length;
      } else if (textarea.setSelectionRange != undefined) {
        textarea.value = textarea.value.substring(0, start) + subst +
                         textarea.value.substring(end);
        if (sel) {
          textarea.setSelectionRange(start + subst.length, start + subst.length);
        } else {
          textarea.setSelectionRange(start + prefix.length, start + prefix.length);
        }
        textarea.scrollTop = scrollPos;
      }
    }
  
    addButton("strong", _("Bold text: '''Example'''"), function() {
      encloseSelection("'''", "'''");
    });
    addButton("em", _("Italic text: ''Example''"), function() {
      encloseSelection("''", "''");
    });
    addButton("heading", _("Heading: == Example =="), function() {
      encloseSelection("\n== ", " ==\n", "Heading");
    });
    addButton("link", _("Link: [http://www.example.com/ Example]"), function() {
      encloseSelection("[", "]");
    });
    addButton("code", _("Code block: {{{ example }}}"), function() {
      encloseSelection("\n{{{\n", "\n}}}\n");
    });
    addButton("hr", _("Horizontal rule: ----"), function() {
      encloseSelection("\n----\n", "");
    });
    addButton("np", _("New paragraph"), function() {
      encloseSelection("\n\n", "");
    });
    addButton("br", _("Line break: [[BR]]"), function() {
      encloseSelection("[[BR]]\n", "");
    });
    addButton("img", _("Image: [[Image()]]"), function() {
      encloseSelection("[[Image(", ")]]");
    });
  
    $(textarea).before(toolbar);
  }

})(jQuery);

// Add the toolbar to all <textarea> elements on the page with the class
// 'wikitext'.
jQuery(document).ready(function($) {
  $("textarea.wikitext").each(function() { addWikiFormattingToolbar(this) });
});