MediaWiki talk:Edittools.js

From Wikimedia Commons, the free media repository
Jump to navigation Jump to search

Script origin[edit]

Just a note:

This came from the English Wiktionary, and has seen some impovements of its own. Also, wikt:MediaWiki:Monobook.js has been updated to set a cookie, and preserve the last selected item, for the next time you edit. So if you often are stuck in one particular character set, you don't have to re-select it every time.

Enjoy!

--Connel MacKenzie 03:11, 2 July 2006 (UTC)[reply]

Thanks for your improvement. I tried to adapt it. However it sadly doesn't work in Commons. I have thatfor reverted myself and will investigate later what I did wrong. Arnomane 11:24, 2 July 2006 (UTC)[reply]


Update?[edit]

This handy script is somewhat old and could take some excellent advantage of the new features available to MediaWiki JS. Examples are getElementsByClassName(), addOnloadHook(), and wgAction. This should, also, hypothetically work in any skin, not just monobook.

Here is an updated version, tested on test.wikipedia. The advantage of this schema is, you don't need to synch the list of <p> blocks in the MediaWiki:Edittools with the select option, as it is generated in the DOM automatically. It grabs the ID of the <p> tag, strips underscores, and uses it in the <option> tag.

// requires <div id="specialchars"> containing at least two <p class="specialbasic" id="SOMENAME">

if(wgAction=='edit' && load_edittools==true) addOnloadHook(edittoolsTabs)
function edittoolsTabs()  {
  var spec = document.getElementById('specialchars');
  if(!spec) return;
  var sb = getElementsByClassName(spec,'p','specialbasic');
  if(sb.length<=1) return; //only care if there is more than one

  var sel = document.createElement('select');
  sel.style.display = 'inline';
  sel.setAttribute('onchange','chooseCharSubset(selectedIndex)');

  for(var i=0;i<sb.length;i++) {
    var o = document.createElement('option');
    o.appendChild(document.createTextNode(sb[i].id.replace(/_/g,' ')));
    sel.appendChild(o);
  }
  spec.insertBefore(sel,spec.firstChild.nextSibling);
}

function chooseCharSubset(seld) {
 var sb = getElementsByClassName(document.getElementById('specialchars'),'p','specialbasic');
 for (var i = 0; i < sb.length ; i++) {
  sb[i].style.display = i == seld ? 'inline' : 'none';
 }
}

Feel free to use it, test it, discuss it, modifiy it, etc. Splarka 04:49, 17 August 2007 (UTC)[reply]

But shouldn't that be spec.insertBefore(sel,spec.firstChild);? Lupo 12:26, 22 March 2008 (UTC)[reply]

Agree. BTW, is there any reason we have such ugly styled links? Personally, I think the browser's standard button look much nicer. The following little snippet converts all these links into buttons:

function editButtons ()
{
  var edit_bar = document.getElementById ('specialchars');
  if (!edit_bar) return;
  var links = edit_bar.getElementsByTagName ('a');
  // 'links' is a *live* collection!
  while (links.length) {
    var b = document.createElement ('input');
    b.type = 'button';
    b.style.fontSize = '0.9em';
    b.style.paddingLeft = '1px';
    b.style.paddingRight = '1px';
    b.style.marginLeft = '1px';
    b.onclick = links[0].onclick;
    b.value = links[0].firstChild.data;
    var parent = links[0].parentNode;
    parent.replaceChild (b, links[0]);
    var margin_added = false;
    // Remove text nodes (nodeType == Node.TEXT_NODE, but IE6 doesn't know that...)
    // Insert some spacing where desired.
    while (b.nextSibling && b.nextSibling.nodeType == 3) {
      if (!margin_added && b.nextSibling.data.search(/\S/) >= 0) {
        b.style.marginRight = '4px';
        margin_added = true;
      }
      parent.removeChild (b.nextSibling);
    }
  }
}

(Tested on FF2.0.0.11/12, FF3b4, IE6, Opera 9.26, Opera 9.50b, and Safari 3.0.4.) Lupo 23:25, 21 March 2008 (UTC)[reply]

Rewritten[edit]

I've rewritten the whole script as outlined here, with some additional improvements:

  • We've got encoded Unicode characters in some IDs. Handling added in the selector setup.
  • Changed links to buttons.
  • Enabled the edit bar for all input fields, not just the first textarea.

If this causes problems, feel free to revert (or to selectively comment out calls from EditTools.setup). Lupo 10:45, 24 March 2008 (UTC)[reply]

Changing default subset[edit]

{{JS migration|done=no}} I was wondering if wouldn't be interesting to substitute the '0' in this code:

EditTools.chooseCharSubset
     (0
      , (wgAction != 'submit')
        && EditTools_set_focus_initially
        && (wgCanonicalNamespace != 'Special' || wgCanonicalSpecialPageName != 'Upload')
     );

for a variable, and define it to 0 by default. This way, it seems possible to someone use another subset by default, redefining the variable value to the corresponding integer in his monobook.

By the way, could somebody update the following comment, please?

"Uses several functions from MediaWiki:Common.js: getElementsByClassName, hookEvent, addEvent"

getElementsByClassName and hookEvent are now part of wikibits.js. Thanks! Helder 21:38, 21 May 2009 (UTC)

This request has been in the edit protected category for a while now, and I have seen it a lot of times but don't feel secure enough to edit this page but its getting to long before somebody answers so I will poke a admin on his talkpage, I guess it will be handled soon now, thanks for waiting. Huib talk 09:33, 5 September 2009 (UTC)[reply]
Done. The variable is called EditTools_initial_subset. Lupo 06:50, 7 September 2009 (UTC)[reply]
Thank you! Helder 17:25, 8 September 2009 (UTC)
I think it still needs some adjustments, because when we choose a subset other than the "Standard", the buttons changes (ok!) but the "Standard" label is shown instead of the name of the new group of buttons...
By the way, the Edittools doesn't seems to be working with the new "vector" skin. Helder 17:54, 8 September 2009 (UTC)
Evidently; you're right. Will take a look, but it might take a few days (no time right now). One would need to also set the selected state of the children of sel. As for it not working in vector: vector has its own implementation of all this. Don't know if the mechanism used here (using MediaWiki:Edittools) might work at all on vector. If not, we'll need to switch this off on vector in MediaWiki:Common.js. Lupo 21:48, 8 September 2009 (UTC)[reply]
✓ Done Initial selection in the drop-down is fixed. Also enabled in all skins. (Previously, it was loaded only on monobook.) Lupo 07:05, 9 September 2009 (UTC)[reply]
Thank you again! =D
Just an detail: what about use sel.options[initial].selected=true out of the loop instead of checking if "i == initial" on each iteration? It seems to be a little bit more efficient... Helder 12:35, 9 September 2009 (UTC)

Edit toolbar stopped working[edit]

The edit toolbar has stopped working for me. I can still see the buttons, but nothing happens when I click them. I get the following error in Firefox:

Fel: txtarea is not defined
Källkodsfil: http://commons.wikimedia.org/w/index.php?title=MediaWiki:Edittools.js&action=raw&ctype=text/javascript
Rad: 167

/Ö 12:28, 27 October 2010 (UTC)[reply]

Should be fixed now. It should work after you reload your browser's cache while on an edit page. Lupo 13:38, 27 October 2010 (UTC)[reply]

Upd: Code need optimization[edit]

I (or someone?) would made an update of this code, most of the code for the WikiEditor is redundant (or odd) to the WikiEditor-Extension-API (I would also made on option for both versions together and some adjustment to the enWP version)⁈ I personally saw this as I would take this script as source for my new tool: meta:User:Perhelion/WikiEditorEmoticons.jsUser: Perhelion (Commons: = crap?)15:01, 17 August 2014 (UTC)[reply]

Additional, it should be possible to add some own custom buttons, there is so many special stuff on Commons!User: Perhelion (Commons: = crap?)17:56, 19 August 2014 (UTC)[reply]

insertTags[edit]

@Rillke: This script causes the warning Use of "insertTags" is deprecated. Use mw.toolbar.insertTags instead. Helder 16:36, 12 January 2015 (UTC)[reply]

Warum immer ich? Okay, I'll do the replace :) -- Rillke(q?) 16:38, 12 January 2015 (UTC)[reply]
Blame WikiBlame, which found Special:Diff/64926392 . Helder 16:43, 12 January 2015 (UTC)[reply]
Before it was implicit global; much worse; inserted in Special:Diff/35315257. -- Rillke(q?) 17:01, 12 January 2015 (UTC)[reply]
Ah, ok.
BTW: about your question, this was the usability initiative which implemented mw:Extension:WikiEditor. Helder 19:56, 13 January 2015 (UTC)[reply]
@He7d3r: And now I know why this code was still in place: Special:Diff/146426615. Also of note: Pressing an edit button (or link) still calls the global function through an onclick="javascript:insertTags()". -- Rillke(q?) 03:49, 14 January 2015 (UTC)[reply]

Legacy JavaScript[edit]

Template:JS migration Hello! This script has been detected as using deprecated parameters that need to be replaced with the updated version. Examples include addOnloadHook() needs to be replaced with $(); all wgGlobalVariables need to be properly gotten with mw.config.get( 'wgGlobalVariable' ); and addPortletLink needs to be called with mw.util.addPortletLink. Please see MW:ResourceLoader/Legacy JavaScript for details. Thank you. — {{U|Technical 13}} (etc) 21:54, 18 January 2015 (UTC)[reply]

I recommend to replace this gadget with w:MediaWiki:Edittools.js. The current usage is annoying (in this time).User: Perhelion (Commons: = crap?)  12:33, 30 May 2015 (UTC)[reply]

update for future <charinsert> change[edit]

{{Editprotected}}

I'm planning to make some changes (https://gerrit.wikimedia.org/r/#/c/288922/) to the charinsert tool that are incompatible with this script. Would someone be able to update this script to be like https://commons.wikimedia.org/w/index.php?title=User:BWolff_%28WMF%29/sandbox.js&oldid=196384850 (diff) in order for it to be compatible with future changes?

Thanks. BWolff (WMF) (talk) 12:36, 16 May 2016 (UTC)[reply]

✓ Done Awesome! Thank you! ~riley (talk) 23:41, 16 May 2016 (UTC)[reply]

ext.wikiEditor[edit]

@Krinkle: the last change don't works for me, I get sporadically empty Edittools now. I guess it is as the code comment tells? -- User: Perhelion 18:27, 28 February 2019 (UTC)[reply]