edit.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. // this function generates the actual toolbar buttons with localized text
  2. // we use it to avoid creating the toolbar where javascript is not enabled
  3. function addButton(imageFile, speedTip, tagOpen, tagClose, sampleText, imageId) {
  4. // Don't generate buttons for browsers which don't fully
  5. // support it.
  6. mwEditButtons[mwEditButtons.length] =
  7. {"imageId": imageId,
  8. "imageFile": imageFile,
  9. "speedTip": speedTip,
  10. "tagOpen": tagOpen,
  11. "tagClose": tagClose,
  12. "sampleText": sampleText};
  13. }
  14. // this function generates the actual toolbar buttons with localized text
  15. // we use it to avoid creating the toolbar where javascript is not enabled
  16. function mwInsertEditButton(parent, item) {
  17. var image = document.createElement("img");
  18. image.width = 23;
  19. image.height = 22;
  20. image.className = "mw-toolbar-editbutton";
  21. if (item.imageId) image.id = item.imageId;
  22. image.src = item.imageFile;
  23. image.border = 0;
  24. image.alt = item.speedTip;
  25. image.title = item.speedTip;
  26. image.style.cursor = "pointer";
  27. image.onclick = function() {
  28. insertTags(item.tagOpen, item.tagClose, item.sampleText);
  29. return false;
  30. };
  31. parent.appendChild(image);
  32. return true;
  33. }
  34. function mwSetupToolbar() {
  35. var toolbar = document.getElementById('toolbar');
  36. if (!toolbar) { return false; }
  37. var textbox = document.getElementById('wpTextbox1');
  38. if (!textbox) { return false; }
  39. // Don't generate buttons for browsers which don't fully
  40. // support it.
  41. if (!(document.selection && document.selection.createRange)
  42. && textbox.selectionStart === null) {
  43. return false;
  44. }
  45. for (var i = 0; i < mwEditButtons.length; i++) {
  46. mwInsertEditButton(toolbar, mwEditButtons[i]);
  47. }
  48. for (var i = 0; i < mwCustomEditButtons.length; i++) {
  49. mwInsertEditButton(toolbar, mwCustomEditButtons[i]);
  50. }
  51. return true;
  52. }
  53. // apply tagOpen/tagClose to selection in textarea,
  54. // use sampleText instead of selection if there is none
  55. function insertTags(tagOpen, tagClose, sampleText) {
  56. var txtarea;
  57. if (document.editform) {
  58. txtarea = document.editform.wpTextbox1;
  59. } else {
  60. // some alternate form? take the first one we can find
  61. var areas = document.getElementsByTagName('textarea');
  62. txtarea = areas[0];
  63. }
  64. var selText, isSample = false;
  65. if (document.selection && document.selection.createRange) { // IE/Opera
  66. //save window scroll position
  67. if (document.documentElement && document.documentElement.scrollTop)
  68. var winScroll = document.documentElement.scrollTop
  69. else if (document.body)
  70. var winScroll = document.body.scrollTop;
  71. //get current selection
  72. txtarea.focus();
  73. var range = document.selection.createRange();
  74. selText = range.text;
  75. //insert tags
  76. checkSelectedText();
  77. range.text = tagOpen + selText + tagClose;
  78. //mark sample text as selected
  79. if (isSample && range.moveStart) {
  80. if (window.opera)
  81. tagClose = tagClose.replace(/\n/g,'');
  82. range.moveStart('character', - tagClose.length - selText.length);
  83. range.moveEnd('character', - tagClose.length);
  84. }
  85. range.select();
  86. //restore window scroll position
  87. if (document.documentElement && document.documentElement.scrollTop)
  88. document.documentElement.scrollTop = winScroll
  89. else if (document.body)
  90. document.body.scrollTop = winScroll;
  91. } else if (txtarea.selectionStart || txtarea.selectionStart == '0') { // Mozilla
  92. //save textarea scroll position
  93. var textScroll = txtarea.scrollTop;
  94. //get current selection
  95. txtarea.focus();
  96. var startPos = txtarea.selectionStart;
  97. var endPos = txtarea.selectionEnd;
  98. selText = txtarea.value.substring(startPos, endPos);
  99. //insert tags
  100. checkSelectedText();
  101. txtarea.value = txtarea.value.substring(0, startPos)
  102. + tagOpen + selText + tagClose
  103. + txtarea.value.substring(endPos, txtarea.value.length);
  104. //set new selection
  105. if (isSample) {
  106. txtarea.selectionStart = startPos + tagOpen.length;
  107. txtarea.selectionEnd = startPos + tagOpen.length + selText.length;
  108. } else {
  109. txtarea.selectionStart = startPos + tagOpen.length + selText.length + tagClose.length;
  110. txtarea.selectionEnd = txtarea.selectionStart;
  111. }
  112. //restore textarea scroll position
  113. txtarea.scrollTop = textScroll;
  114. }
  115. function checkSelectedText(){
  116. if (!selText) {
  117. selText = sampleText;
  118. isSample = true;
  119. } else if (selText.charAt(selText.length - 1) == ' ') { //exclude ending space char
  120. selText = selText.substring(0, selText.length - 1);
  121. tagClose += ' '
  122. }
  123. }
  124. }
  125. /**
  126. * Restore the edit box scroll state following a preview operation,
  127. * and set up a form submission handler to remember this state
  128. */
  129. function scrollEditBox() {
  130. var editBox = document.getElementById( 'wpTextbox1' );
  131. var scrollTop = document.getElementById( 'wpScrolltop' );
  132. var editForm = document.getElementById( 'editform' );
  133. if( editBox && scrollTop ) {
  134. if( scrollTop.value )
  135. editBox.scrollTop = scrollTop.value;
  136. addHandler( editForm, 'submit', function() {
  137. document.getElementById( 'wpScrolltop' ).value = document.getElementById( 'wpTextbox1' ).scrollTop;
  138. } );
  139. }
  140. }
  141. hookEvent( 'load', scrollEditBox );
  142. hookEvent( 'load', mwSetupToolbar );