MediaWiki:Gadget-section.js

Da Wikisource.

Nota: dopo aver pubblicato, potrebbe essere necessario pulire la cache del proprio browser per vedere i cambiamenti.

  • Firefox / Safari: tieni premuto il tasto delle maiuscole Shift e fai clic su Ricarica, oppure premi Ctrl-F5 o Ctrl-R (⌘-R su Mac)
  • Google Chrome: premi Ctrl-Shift-R (⌘-Shift-R su un Mac)
  • Internet Explorer / Edge: tieni premuto il tasto Ctrl e fai clic su Aggiorna, oppure premi Ctrl-F5
  • Opera: premi Ctrl-F5.
//se ci sono già delle section, propone il numero immediatamente successivo alla maggiore
//es. se abbiamo le section 1, 3, 4, propone: 5
function getProssimoNumero() {
	nextNum = 1;
	prefix = 's';
	sectionPresenti = $('#wpTextbox1').val().match(/<section\s+begin\=[\"]?[a-zA-Z]*(\d+)[\"]?\s*\/>/gi);
	if (sectionPresenti !== null) {
		for (i = 0; i < sectionPresenti.length; i++) {
			thisNum = parseInt(sectionPresenti[i].replace(/<section\s+begin\=[\"]?[a-zA-Z]*(\d+)[\"]?\s*\/>/, '$1'));
			if (thisNum >= nextNum) {
				nextNum = thisNum + 1;
				prefix = sectionPresenti[i].replace(/<section\s+begin\=[\"]?([a-zA-Z]*)\d+[\"]?\s*\/>/, '$1');
			}
		}
	}
	return prefix + nextNum;
}

function marcaSection() {
	nome = prompt("Che nome vuoi dare alla section?",  getProssimoNumero());
	if (nome !== null) {
		textArea = $('#wpTextbox1');
		start = textArea[0].selectionStart;
		end = textArea[0].selectionEnd;
		selectedText = textArea.val().substring(start, end);
		replacement = '<section begin="' + nome + '" />' + selectedText + '<section end="'+ nome + '" />';
		textArea.val(textArea.val().substring(0, start) + replacement + textArea.val().substring(end));
	}
}
 
if ( typeof $ != 'undefined' && typeof $.fn.wikiEditor != 'undefined' ) {$( function() {
    $( '#wpTextbox1' ).wikiEditor( 'addToToolbar', {
      'section': 'advanced',
      'group': 'format',
      'tools': {'section': {
          label: 'Aggiunge i tag section attorno al testo selezionato',
          type: 'button',
          icon: '//upload.wikimedia.org/wikipedia/commons/9/97/Button_S_italic.png',
          action: {type: 'callback',
            execute: function(context){marcaSection();} }
}}});});}