edit-assist.pl 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. # This program is free software; you can redistribute it and/or modify
  2. # it under the terms of the GNU General Public License as published by
  3. # the Free Software Foundation; either version 3 of the License, or
  4. # (at your option) any later version.
  5. #
  6. # This program is distributed in the hope that it will be useful,
  7. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. # GNU General Public License for more details.
  10. #
  11. # You should have received a copy of the GNU General Public License
  12. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. use strict;
  14. use v5.10;
  15. AddModuleDescription('edit-assist.pl', 'Edit Assist Extension');
  16. our ($q, $HtmlHeaders, @MyInitVariables);
  17. push (@MyInitVariables,
  18. sub {
  19. if ($q->param('action') eq 'edit') {
  20. $HtmlHeaders = qq{
  21. <script type="text/javascript">
  22. function hookEvent(hookName, hookFunct) {
  23. if (window.addEventListener) {
  24. window.addEventListener(hookName, hookFunct, false);
  25. } else if (window.attachEvent) {
  26. window.attachEvent("on" + hookName, hookFunct);
  27. }
  28. }
  29. var mwEditButtons = [];
  30. var mwCustomEditButtons = []; // eg to add in MediaWiki:Common.js
  31. // this function generates the actual toolbar buttons with localized text
  32. // we use it to avoid creating the toolbar where javascript is not enabled
  33. function addButton(imageFile, speedTip, tagOpen, tagClose, sampleText) {
  34. // Don't generate buttons for browsers which don't fully
  35. // support it.
  36. mwEditButtons[mwEditButtons.length] =
  37. {"imageFile": imageFile,
  38. "speedTip": speedTip,
  39. "tagOpen": tagOpen,
  40. "tagClose": tagClose,
  41. "sampleText": sampleText};
  42. }
  43. // this function generates the actual toolbar buttons with localized text
  44. // we use it to avoid creating the toolbar where javascript is not enabled
  45. function mwInsertEditButton(parent, item) {
  46. var image = document.createElement("img");
  47. image.width = 23;
  48. image.height = 22;
  49. image.src = item.imageFile;
  50. image.border = 0;
  51. image.alt = item.speedTip;
  52. image.title = item.speedTip;
  53. image.style.cursor = "pointer";
  54. image.onclick = function() {
  55. insertTags(item.tagOpen, item.tagClose, item.sampleText);
  56. return false;
  57. };
  58. parent.appendChild(image);
  59. return true;
  60. }
  61. function mwSetupToolbar() {
  62. var toolbar;
  63. for (i=0;i<document.getElementsByTagName("div").length; i++) {
  64. if (document.getElementsByTagName("div").item(i).className == "header"){
  65. toolbar = document.getElementsByTagName("div").item(i);
  66. }
  67. }
  68. if (!toolbar) { return false; }
  69. var textbox = document.getElementById('text');
  70. if (!textbox) { return false; }
  71. // Don't generate buttons for browsers which don't fully
  72. // support it.
  73. if (!document.selection && textbox.selectionStart === null) {
  74. return false;
  75. }
  76. for (var i in mwEditButtons) {
  77. mwInsertEditButton(toolbar, mwEditButtons[i]);
  78. }
  79. for (i in mwCustomEditButtons) {
  80. mwInsertEditButton(toolbar, mwCustomEditButtons[i]);
  81. }
  82. return true;
  83. }
  84. // apply tagOpen/tagClose to selection in textarea,
  85. // use sampleText instead of selection if there is none
  86. // copied and adapted from phpBB
  87. function insertTags(tagOpen, tagClose, sampleText) {
  88. var txtarea;
  89. if (document.editform) {
  90. txtarea = document.editform.wpTextbox1;
  91. } else {
  92. // some alternate form? take the first one we can find
  93. var areas = document.getElementsByTagName('textarea');
  94. txtarea = areas[0];
  95. }
  96. // IE
  97. if (document.selection && !is_gecko) {
  98. var theSelection = document.selection.createRange().text;
  99. if (!theSelection) {
  100. theSelection=sampleText;
  101. }
  102. txtarea.focus();
  103. if (theSelection.charAt(theSelection.length - 1) == " ") { // exclude ending space char, if any
  104. theSelection = theSelection.substring(0, theSelection.length - 1);
  105. document.selection.createRange().text = tagOpen + theSelection + tagClose + " ";
  106. } else {
  107. document.selection.createRange().text = tagOpen + theSelection + tagClose;
  108. }
  109. // Mozilla
  110. } else if(txtarea.selectionStart || txtarea.selectionStart == '0') {
  111. var replaced = false;
  112. var startPos = txtarea.selectionStart;
  113. var endPos = txtarea.selectionEnd;
  114. if (endPos-startPos) {
  115. replaced = true;
  116. }
  117. var scrollTop = txtarea.scrollTop;
  118. var myText = (txtarea.value).substring(startPos, endPos);
  119. if (!myText) {
  120. myText=sampleText;
  121. }
  122. var subst;
  123. if (myText.charAt(myText.length - 1) == " ") { // exclude ending space char, if any
  124. subst = tagOpen + myText.substring(0, (myText.length - 1)) + tagClose + " ";
  125. } else {
  126. subst = tagOpen + myText + tagClose;
  127. }
  128. txtarea.value = txtarea.value.substring(0, startPos) + subst +
  129. txtarea.value.substring(endPos, txtarea.value.length);
  130. txtarea.focus();
  131. //set new selection
  132. if (replaced) {
  133. var cPos = startPos+(tagOpen.length+myText.length+tagClose.length);
  134. txtarea.selectionStart = cPos;
  135. txtarea.selectionEnd = cPos;
  136. } else {
  137. txtarea.selectionStart = startPos+tagOpen.length;
  138. txtarea.selectionEnd = startPos+tagOpen.length+myText.length;
  139. }
  140. txtarea.scrollTop = scrollTop;
  141. // All other browsers get no toolbar.
  142. // There was previously support for a crippled "help"
  143. // bar, but that caused more problems than it solved.
  144. }
  145. // reposition cursor if possible
  146. if (txtarea.createTextRange) {
  147. txtarea.caretPos = document.selection.createRange().duplicate();
  148. }
  149. }
  150. addButton('/images/button_bold.png','Bold text','**','**','Bold text');
  151. addButton('/images/button_italic.png','Italic text','//','//','Italic text');
  152. addButton('/images/button_link.png','Internal link','[[',']]','Link title');
  153. addButton('/images/button_extlink.png','External link (remember http:// prefix)','[',']','http://www.example.com link title');
  154. addButton('/images/button_headline.png','Level 2 headline','\\n== ',' ==\\n','Headline text');
  155. addButton('/images/button_image.png','Embedded image','[[image:',']]','Example.jpg');
  156. addButton('/images/button_nowiki.png','Ignore wiki formatting','{{{','}}}','Insert non-formatted text here');
  157. addButton('/images/button_sig.png','Your signature with timestamp','--~~~~','','');
  158. addButton('/images/button_hr.png','Horizontal line','\\n----\\n','','');
  159. hookEvent("load", mwSetupToolbar);
  160. </script>
  161. };
  162. }
  163. });