1/*
2 * Comment Syntax plugin for DokuWiki
3 * a toolbar button action to toggle encomment/uncomment selected text
4 *
5 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author     Satoshi Sahara <sahara.satoshi@gmail.com>
7 */
8function addBtnActionToggleCommentBlock(btn, props, edid) {
9
10    jQuery(btn).click(function(){
11        var comment = '';
12        var selection = DWgetSelection(document.getElementById('wiki__text'));
13        if (selection.getLength()) {
14            comment = selection.getText();
15            prevchar = selection.obj.value.substring(selection.start-1,selection.start);
16            nextchar = selection.obj.value.substring(selection.end,selection.end+1);
17            lastchar = comment.substr(-1,1);
18            if (!prevchar) prevchar = "\n";
19            if (!nextchar) nextchar = "\n";
20        }
21        if (comment == '') {
22            alert('no text selected');
23            return false;
24        }
25
26        if (comment.match(/^\s*\/\*/) && comment.match(/\*\/\s*$/)) {
27            // uncomment action
28            if (prevchar == "\n") {
29                comment = comment.replace(/^ *\/\*+ *\n?/,''); // uncomment left
30            } else {
31                comment = comment.replace(/^ *\/\*+ */,'');
32            }
33            comment = comment.replace(/ *\*+\/\s*$/,'');   // uncomment right
34            document.getElementById('wiki__text').focus();
35            pasteText(selection, comment);
36
37        } else {
38            // encomment action
39            if (prevchar == "\n" && lastchar == "\n") {
40                // Line selected
41                comment = "/*\n" + comment + " */\n";
42            } else {
43                if (comment.match(/^\n/)) {
44                    comment = comment.replace(/^\n?/,"/*\n");
45                    comment = comment + " */";
46                } else {
47                    comment = "/* " + comment + " */";
48                }
49                if (!(prevchar.match(/\s/))) { comment = " " + comment; }
50                if (!(nextchar.match(/\s/))) { comment = comment + " "; }
51            }
52            document.getElementById('wiki__text').focus();
53            pasteText(selection, comment);
54        }
55        return true;
56    });
57    return false;
58}
59