script.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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. */
  8. function addBtnActionToggleCommentBlock(btn, props, edid) {
  9. jQuery(btn).click(function(){
  10. var comment = '';
  11. var selection = DWgetSelection(document.getElementById('wiki__text'));
  12. if (selection.getLength()) {
  13. comment = selection.getText();
  14. prevchar = selection.obj.value.substring(selection.start-1,selection.start);
  15. nextchar = selection.obj.value.substring(selection.end,selection.end+1);
  16. lastchar = comment.substr(-1,1);
  17. if (!prevchar) prevchar = "\n";
  18. if (!nextchar) nextchar = "\n";
  19. }
  20. if (comment == '') {
  21. alert('no text selected');
  22. return false;
  23. }
  24. if (comment.match(/^\s*\/\*/) && comment.match(/\*\/\s*$/)) {
  25. // uncomment action
  26. if (prevchar == "\n") {
  27. comment = comment.replace(/^ *\/\*+ *\n?/,''); // uncomment left
  28. } else {
  29. comment = comment.replace(/^ *\/\*+ */,'');
  30. }
  31. comment = comment.replace(/ *\*+\/\s*$/,''); // uncomment right
  32. document.getElementById('wiki__text').focus();
  33. pasteText(selection, comment);
  34. } else {
  35. // encomment action
  36. if (prevchar == "\n" && lastchar == "\n") {
  37. // Line selected
  38. comment = "/*\n" + comment + " */\n";
  39. } else {
  40. if (comment.match(/^\n/)) {
  41. comment = comment.replace(/^\n?/,"/*\n");
  42. comment = comment + " */";
  43. } else {
  44. comment = "/* " + comment + " */";
  45. }
  46. if (!(prevchar.match(/\s/))) { comment = " " + comment; }
  47. if (!(nextchar.match(/\s/))) { comment = comment + " "; }
  48. }
  49. document.getElementById('wiki__text').focus();
  50. pasteText(selection, comment);
  51. }
  52. return true;
  53. });
  54. return false;
  55. }