MediaWiki:Gadget-wikEd.js

From Eigenvector Research Documentation Wiki
Jump to navigation Jump to search

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Press Ctrl-F5.
// <pre><nowiki>
//  _________________________________________________________________________________________
// |                                                                                         |
// |                    === WARNING: GLOBAL GADGET FILE ===                                  |
// |                  Changes to this page affect many users.                                |
// | Please discuss changes on the talk page or on [[Wikipedia_talk:Gadget]] before editing. |
// |_________________________________________________________________________________________|
//
// Imported from version 0.9.64f as of August 2, 2008, 2008, from [[User:Cacycle/wikEd.js]]
// wikEd is a full-featured in-browser editor for Wikipedia, see [[User:Cacycle/wikEd]]

// gadget-specific customization of wikEd:

// define  custom buttons (id, class, popup title, image src, width, height, alt text, onClick and parameters)
var wikEdButton = {};
wikEdButton[100] = ['wikEdTT', 'wikEdButton', 'Make the selection a html TT element', 'http://upload.wikimedia.org/wikipedia/commons/5/5f/Btn_toolbar_tt.png', '16', '16', 'TT', 'javascript:WikEdEditButton(this, this.id, null, TestHandler);' ];
wikEdButton[101] = ['wikEdTest', 'wikEdButton', 'Make the selection a html PRE element', 'http://upload.wikimedia.org/wikipedia/commons/2/23/Button_code.png', '16', '16', 'PRE', 'javascript:WikEdEditButton(this, this.id, null, TestHandler2);' ];

// define custom button bars (id outer, class outer, id inner, class inner, height, grip title, button numbers)
var wikEdButtonBar = {};
wikEdButtonBar['custom1'] = ['wikEdButtonBarCustom1',  'wikEdButtonBarCustom1',  'wikEdButtonsCustom1',  'wikEdButtonsCustom1',  44, 'My custom buttons', [100, 'br', 101] ];
wikEdButtonBar['custom2'] = ['wikEdButtonBarCustom2',  'wikEdButtonBarCustom2',  'wikEdButtonsCustom2',  'wikEdButtonsCustom2',  44, 'My custom buttons', [100, 'br', 101] ];

// define the function which is called upon clicking the custom button
// this example code adds or removes tt tags around the selected text

function TestHandler(obj) {

// select the appropriate text change target (whole, selection, cursor, focusWord, focusLine, selectionWord, or selectionLine)
//   focus... is the text under the cursor; ...Word and ...Line extend the target to the start/end of the word or line
  WikEdGetText(obj, 'selection, cursor');
  if (obj.selection.plain != '') {
    obj.changed = obj.selection;
  }
  else {
    obj.changed = obj.cursor;
  }

// make the changes to the plain target text

// remove the previously added formatting
  if ( /&lt;tt&gt;(.*?)&lt;\/tt&gt;/i.test(obj.changed.plain) ) {
    obj.changed.plain = obj.changed.plain.replace(/&lt;tt&gt;(.*?)&lt;\/tt&gt;/gi, '$1');
  }

// add the text formatting
 else {
    obj.changed.plain = '&lt;tt&gt;' + obj.changed.plain + '&lt;/tt&gt;';
    obj.changed.plain = obj.changed.plain.replace(/(&lt;tt&gt;)( *)(.*?)( *)(&lt;\/tt&gt;)/, '$2$1$3$5$4');
  }

// keep the changed text selected, needed to remove the formatting with a second custom button click
  obj.changed.keepSel = true;

  return;
}

function TestHandler2(obj) {

// select the appropriate text change target (whole, selection, cursor, focusWord, focusLine, selectionWord, or selectionLine)
//   focus... is the text under the cursor; ...Word and ...Line extend the target to the start/end of the word or line
  WikEdGetText(obj, 'selection, cursor');
  if (obj.selection.plain != '') {
    obj.changed = obj.selection;
  }
  else {
    obj.changed = obj.cursor;
  }

// make the changes to the plain target text

// remove the previously added formatting
  if ( /&lt;pre&gt;(.*?)&lt;\/pre&gt;/i.test(obj.changed.plain) ) {
    obj.changed.plain = obj.changed.plain.replace(/&lt;pre&gt;(.*?)&lt;\/pre&gt;/gi, '$1');
  }

// add the text formatting
 else {
    obj.changed.plain = '&lt;pre&gt;' + obj.changed.plain + '&lt;/pre&gt;';
    obj.changed.plain = obj.changed.plain.replace(/(&lt;pre&gt;)( *)(.*?)( *)(&lt;\/pre&gt;)/, '$2$1$3$5$4');
  }

// keep the changed text selected, needed to remove the formatting with a second custom button click
  obj.changed.keepSel = true;

  return;
}



// version number, please also update [[User:Cacycle/wikEd_gadget_version]] when updating this page
var wikEdAutoUpdateUrl = 'http://en.wikipedia.org/w/index.php?title=User:Cacycle/wikEd_gadget_version&action=raw&maxage=0';

// start of pasted code below this line:
// ____________________________________________________________________________</nowiki></pre>

// install [[Wikipedia:User:Cacycle/wikEd]] in-browser text editor
document.write('<script type="text/javascript" src="'
+ 'http://en.wikipedia.org/w/index.php?title=User:Cacycle/wikEd.js'
+ '&action=raw&ctype=text/javascript"></' + 'script>');