edit-assist.pl 6.1 KB

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