Utilisateur:Flo/Édition.js
Note : après avoir enregistré la page, vous devrez forcer le rechargement complet du cache de votre navigateur pour voir les changements.
Mozilla / Firefox / Konqueror / Safari : maintenez la touche Majuscule (Shift) en cliquant sur le bouton Actualiser (Reload) ou pressez Maj-Ctrl-R (Cmd-R sur Apple Mac) ;
Chrome / Internet Explorer / Opera : maintenez la touche Ctrl en cliquant sur le bouton Actualiser ou pressez Ctrl-F5.// http://www.mediawiki.org/wiki/Extension:WikiEditor/Toolbar_customization
// http://svn.wikimedia.org/viewvc/mediawiki/trunk/extensions/WikiEditor/modules/jquery.wikiEditor.toolbar.config.js?view=markup
var vectorButtons = [];
var i = 0;
// To add a booklet section:
vectorButtons[i++] = {
'sections': {
'typo': {
'type': 'booklet',
'label': 'Typo'
}
}
};
// To add a special characters page
vectorButtons[i++] = {
'section': 'typo',
'pages': {
'typo': {
'layout': 'characters',
'label': 'Typo',
'characters': [
{label: '\x5Cmathrm', labelMsg: 'différentielles',
action: {
type: 'encapsulate',
options: {pre: "\x5Cmathrm ", peri:"", post: "", regex:/([^a-z\\])(d)(?=(?:[^<]|<(?!math>|\/math>))*<\/math>)/ig, regexReplace:'$1\x5Cmathrm $2'}
}},
{label: '{{math}}',
action: {
type: 'encapsulate',
options: {pre: "{{math|", peri:"§=", post: "}}"} //options
}},
{label: 'code/',
action: {
type: 'encapsulate',
options: {pre: "<code>", peri:"", post: "</code>"} //options
}},
{label: 'center/', labelMsg: 'centrer les équations',
action: {
type: 'encapsulate',
options: {pre: "<center>", peri:"", post: "</center>", ownline:false, regex:/\n(?::\s*)?(<math>(?:[^<]|<(?!\/math>))*<\/math>[^\n]*)/g, regexReplace:'\n<center>$1</center>'} //options
}},
{label: 'math/',
action: {
type: 'encapsulate',
options: {pre: "<math>", peri:"", post: "</math>", regex:/''(([^']|[^']'[^'])*)''/ig, regexReplace:'<math>$1</math>' } //options
}},
{label: '« »',
action: {
type: 'encapsulate',
options: {pre: "« ", peri:" ", post: " »", regex:/([ ,;.:])"([^"]+)"/g, regexReplace:"$1« $2 »" } //options "
}},
{label: '-',
action: {
type: 'encapsulate',
options: {pre: " - ", peri:"", post: "", regex:/ - (?!(?:[^<]|<(?!math>|\/math>))*<\/math>)/g, regexReplace:" — " }
}},
{label: '{{unité}}',
action: {
type: 'encapsulate',
options: {regex:/([0-9]+)[,\.]([0-9]+)\s?([a-z%/]+)/ig, regexReplace:'{{unité|$1.$2|$3}}' } //options
}},
//{label: 'HTML2T<sub>e</sub>X', action: { type: 'callback', execute: regexpReplace(/{(.)}/ig,"$1")}},
{label: 'sub/',
action: {
type: 'encapsulate',
options: {pre: "_{", peri:"", post: "}", regex:/<sub>((?:[^<]|<(?!\/sub>))*)<\/sub>/ig, regexReplace:"_{$1}"} //
}},
{label: 'sup/',
action: {
type: 'encapsulate',
options: {pre: "^{", peri:"", post: "}", regex:/<sup>((?:[^<]|<(?!\/sup>))*)<\/sup>/ig, regexReplace:"^{$1}"} //options
}},
{label: '()',
action: {
type: 'encapsulate',
options: {pre: "\left", peri:"", post: "\right"} //options
}}
] // characters
} // TeX
}
};
/*
j'ai 2.43 foo/bar
<math>foo \mathrm dx dydz</math>.bar
bar
*/
if ( typeof $j != 'undefined' && typeof $.fn.wikiEditor != 'undefined' ) {
// $(document).ready( function() {
$( function() {
for (var i = 0; i < vectorButtons.length; i++) {
$( '#wpTextbox1' ).wikiEditor( 'addToToolbar', vectorButtons[i] );
}
} );//function
}//if
/* CHAMP REMPLACER DANS POPUPS FORMULE TeX */
var TeXreplace = {
patterns : [],
replaces : [],
init : function(){
TeXreplace.build();
},
build : function(){
var xtend = document.createElement('div');
$(xtend).addClass('oo-ui-layout oo-ui-labelElement oo-ui-fieldLayout oo-ui-fieldLayout-align-top');
$(xtend).attr('id','bobby');
$(xtend).html('<label class="oo-ui-fieldLayout-body"><span class="oo-ui-labelElement-label">Remplacer</span></label>'
+ '<div class="oo-ui-fieldLayout-field">'
+ '<div class="oo-ui-widget oo-ui-widget-enabled oo-ui-inputWidget oo-ui-textInputWidget-labelPosition-after oo-ui-textInputWidget-labelPosition-before oo-ui-textInputWidget" style="width:42%; display:inline-block"><input type="text" id="myPattern" name="myPattern" title="Expression régulière" list="patternList"></input><datalist id="patternList"></datalist></div>'
+ '<div class="oo-ui-widget oo-ui-widget-enabled oo-ui-inputWidget oo-ui-textInputWidget-labelPosition-after oo-ui-textInputWidget-labelPosition-before oo-ui-textInputWidget" style="width:45%; display:inline-block"><input type="text" id="myReplace" name="myReplace" title="Motif de remplacement" list="replaceList"><datalist id="replaceList"></datalist></input></div>'
+ '<div class="oo-ui-widget oo-ui-buttonElement oo-ui-labelElement oo-ui-flaggedElement-primary oo-ui-flaggedElement-constructive oo-ui-buttonWidget oo-ui-actionWidget oo-ui-buttonElement-framed oo-ui-widget-enabled"><a role="button" class="oo-ui-buttonElement-button" id="boutonRemplacer"><span class="oo-ui-labelElement-label">→</span></a></div>'
+ '</div>');
$("body").delegate(".oo-ui-widget.oo-ui-selectWidget.ve-ui-contextSelectWidget.ve-ui-desktopContext-menu:has(.oo-ui-icon-math)","click",function(){
if($('.ve-ui-mwMathInspector-content #bobby')[0]) return;
$(".ve-ui-mwMathInspector-content").prop('name','formulaire-TeX');
$(".ve-ui-mwMathInspector-content div.oo-ui-fieldLayout").first().after(xtend);
});
$("body").delegate('#boutonRemplacer','click',TeXreplace.replace);
console.log('TeXreplace ready');
},
replace : function(){
var myPattern = $('#myPattern').val();
var myReplace = $('#myReplace').val();
var txtarea = $('.ve-ui-mwMathInspector-content textarea').get(0); // objet DOM
var regexp = new RegExp(myPattern,'g');
var selection = theSelection(txtarea);
if(!selection) {console.log('erreur'); return false;}
var replacedSelection = selection.value.replace(regexp, myReplace);
var startPos = selection.start;
var endPos = selection.end;
txtarea.value = txtarea.value.substring(0, startPos) + replacedSelection + txtarea.value.substring(endPos, txtarea.value.length);
txtarea.setSelectionRange(startPos, startPos + replacedSelection.length);
if( $.inArray(myReplace,TeXreplace.replaces) == -1 ){ // si pas déjà ajouté
$('#replaceList').append('<option value="' + myReplace + '"/>');
TeXreplace.replaces.push(myReplace);
}
if( $.inArray(myPattern,TeXreplace.patterns) == -1 ){ // si pas déjà ajouté
$('#patternList').append('<option value="' + myPattern + '"/>');
TeXreplace.patterns.push(myPattern);
}
}
};
mw.hook( 've.activationComplete' ).add(TeXreplace.init);
// Tiré de [[User:Zelda/regexp.js]]
// Tableau des boutons
var regexpButtons = [];
function theSelection(area) {
if(area.selectionStart === null) return false;
var startPos = area.selectionStart;
var endPos = area.selectionEnd;
/*if (endPos < startPos) {
startPos = 0;
endPos = area.value.length;
}*/
var theSelection = (area.value).substring(startPos, endPos);
return {start:startPos, end:endPos, value:theSelection};
}
function regexpReplace(regexp, replace) {
if(!regexp) return false;
var txtarea = byId("wpTextbox1");
var selection = theSelection(txtarea);
if(!selection) return false;
var scrollTop = txtarea.scrollTop;
var replacedSelection = selection.value.replace(regexp, replace);
var startPos = selection.start;
var endPos = selection.end;
txtarea.value = txtarea.value.substring(0, startPos) + replacedSelection +
txtarea.value.substring(endPos, txtarea.value.length);
txtarea.setSelectionRange(startPos, startPos + replacedSelection.length);
txtarea.scrollTop = scrollTop;
txtarea.focus();
}
window.lastPattern = "";
window.lastFlags = "ig";
window.lastReplace = "";
window.left1 = "\\left"; window.right1 = "\\right"; window.def1 = "";
function expression(pattern,flags) {
if( pattern != null ) lastPattern = pattern;
if( flags != null ) lastFlags = flags;
if(
(newPattern = prompt( 'Expression rationnelle :', lastPattern )) == null
||
(newFlags = prompt( 'Option(s)\nCombinaison de m (multiline), i (case insensitive) et g (global)', lastFlags )) == null
) {
return false;
} else {
window.lastPattern = newPattern;
window.lastFlags = newFlags;
return new RegExp(newPattern, newFlags);
}
}
function remplacer(laSélection) {
var motif,reg;
if( laSélection ) {
var txtarea = byId("wpTextbox1");
motif = theSelection(txtarea).value;
txtarea.setSelectionRange(0,txtarea.length);
} else motif = null;
if(
(reg = expression(motif)) != false
&& (lastReplace = prompt('Remplacer par :',lastReplace)) !== null
)
regexpReplace(reg, lastReplace);
else
alert("Erreur");
}
function nouveauxBoutons() {
addCustomButton('https://upload.wikimedia.org/wikipedia/commons/3/3e/Sharp_button.png',
'Code','<code>','</code>','','');
//addCustomButton('https://upload.wikimedia.org/wikipedia/commons/b/b5/Button_mini_stub.png',
// 'Bloc emphase','{{bloc emphase|','}}','','');
addCustomButton('https://upload.wikimedia.org/wikipedia/commons/5/52/Button_grec.png',
'Math','{{math|§=','}}','','');
//https://upload.wikimedia.org/wikipedia/commons/f/f5/Button_l_ln.png
ajouteBouton("https://upload.wikimedia.org/wikipedia/commons/5/59/Button_replace.png",
"Remplacer","remplacer(false)","remplacer(true)");
ajouteBouton("https://fr.wikipedia.org/skins-1.5/common/images/button_math.png",
"Remplacer dans/hors <math/>",
"lastPattern = '(?=(?:[^<]|<(?!math>|\\/math>))*<\\/math>)'; remplacer()",
"lastPattern = '(?!(?:[^<]|<(?!math>|\\/math>))*<\\/math>)'; remplacer()");
ajouteBouton("https://upload.wikimedia.org/wikipedia/commons/7/72/Button_span_2.png",
"Remplacer une balise", "lastPattern = '<sub>((?:[^<]|<(?!/sub>))*)</sub>'; remplacer();");
ajouteBouton("https://upload.wikimedia.org/wikipedia/commons/1/12/Button_find.png",
"Chercher/compter", "chercher(false)", "chercher(true)");
ajouteBouton("https://upload.wikimedia.org/wikipedia/commons/0/03/Button_paragraphe_1.png",
"Insérer 1",
"insertTags(left1,right1,def1)",
"left1 = prompt('Gauche :',left1); right1 = prompt('Droite :',right1); def1 = prompt('Défaut :',def1)");
ajouteBouton('https://upload.wikimedia.org/wikipedia/commons/5/5f/Button_center.png',
'Centrer/avec saut de ligne',
"insertTags('<center>','</center>')","insertTags('\\n<center>','</center>\\n')");
ajouteBouton('https://upload.wikimedia.org/wikipedia/commons/2/26/Button_latinas.png',
'Guillemets/Nettoyer la ponctuation', 'insertTags("« "," »","")','ponctuation()');
// https://upload.wikimedia.org/wikipedia/commons/4/4d/Button_quotes.png
ajouteBouton("https://upload.wikimedia.org/wikipedia/commons/8/8e/Button_shifting.png",
"Indenter/Désindenter",
"regexpReplace(/\\n/g,'\\n:');", "regexpReplace(/\\n:/g,'\\n')");
ajouteBouton("https://upload.wikimedia.org/wikipedia/commons/6/66/Button_mort.png",
"Centrer les équations",
"regexpReplace(/\\n(?::\\s*)?(<math>(?:[^<]|<(?!\\/math>))*<\\/math>[^\\n]*)/g, '\\n<center>$1</center>');");
// https://upload.wikimedia.org/wikipedia/commons/f/ff/Button_arrow_right.png
ajouteBouton("https://upload.wikimedia.org/wikipedia/commons/4/4e/Button_romain.png",
"Différentielles",
"insertTags('\x5C\x5Cmathrm','','');",
"regexpReplace(/([^a-z\\\\])(d)(?=(?:[^<]|<(?!math>|\\/math>))*<\\/math>)/ig, '$1\x5C\x5Cmathrm $2'); regexpReplace(/\\\\mathrm\\s*\\\\mathrm/ig,'\x5C\x5Cmathrm');");
// https://upload.wikimedia.org/wikipedia/commons/2/2e/Button_broom.png
ajouteBouton("https://upload.wikimedia.org/wikipedia/commons/c/c7/Button_polytonique.png",
"Italique2TeX (un/plusieurs caractères)",
"regexpReplace(/([^'])''([^'])''([^'])/ig,'$1<math>$2</math>$3')",
"regexpReplace(/''(([^']|[^']'[^'])*)''/ig,'<math>$1</math>')");
ajouteBouton("https://upload.wikimedia.org/wikipedia/commons/6/6a/Button_sup_letter.png",
"HTML2TeX", "HTML2TeX(false)", "HTML2TeX(true)");
}
function chercher(disp) {
var area = byId('wpTextbox1');
var sel = theSelection(area); var tab = [];
if( (tab = sel.value.match(expression())) === null ) {
alert("Le motif ne concorde pas !");
return false;
}
var index = sel.value.indexOf(tab[0]);
area.focus();
area.setSelectionRange(sel.start + index, sel.start + index + tab[0].length);
if(disp) alert( tab.length + " occurrences" );
return true;
}
function hiérarchie() {
regexpReplace( /\n==== *([^=]+) *==== ?\n/g , "\n===== $1 =====\n" );
regexpReplace( /\n=== *([^=]+) *=== ?\n/g , "\n==== $1 ====\n" );
regexpReplace( /\n== *([^=]+) *== ?\n/g , "\n=== $1 ===\n" );
regexpReplace( /\n= *([^=]+) *= ?\\n/g , "\n== $1 ==\n" );
}
function ponctuation() {
regexpReplace( / - (?!(?:[^<]|<(?!math>|\/math>))*<\/math>)/g, " — ");
regexpReplace( /([ ,;.:])"([^"]+)"/g, "$1« $2 »");
regexpReplace( /([^\s])(:(?!(?:[^\[])*\])|;)/g, "$1 $2");
regexpReplace( /,([^\s])(?!(?:[^<]|<(?!math>|\/math>))*<\/math>)/g, ", $1");
}
function HTML2TeX(frac) {
var unicode = "– α β γ Γ δ Δ ε ζ η θ ι κ λ μ ν ξ π ρ σ Σ τ υ φ ϕ χ ψ Ψ ω Ω ∇ ² ³ ∫ ∬ ∭ ∂ ⋅ ∞".split(/ /g);
var tex = "- \\alpha \\beta \\gamma \\Gamma \\delta \\Delta \\epsilon \\zeta \\eta \\theta \\iota \\kappa \\lambda \\mu \\nu \\xi \\pi \\rho \\sigma \\Sigma \\tau \\upsilon \\varphi \\phi \\chi \\psi \\Psi \\omega \\Omega \\nabla ^2 ^3 \\int \\iint \\iiint \\partial \\cdot \\infty".split(/ /g);
for( var i in unicode ) {
regexpReplace(new RegExp(unicode[i] + "(?=(?:[^<]|<(?!math>|/math>))*</math>)","g") , tex[i]);
}
regexpReplace(/<sub>((?:[^<]|<(?!\/sub>))*)<\/sub>/ig , "_{$1}");
regexpReplace(/<sup>((?:[^<]|<(?!\/sup>))*)<\/sup>/ig , "^{$1}");
if(frac){
regexpReplace(/\(([^\(\/]+)\/([^\)\/]+)\)(?=(?:[^<]|<(?!math>|\/math>))*<\/math>)/ig, "\\frac{$1}{$2}" );
regexpReplace(/\(([^\(\)]+)\)\^{-1}/g,"\\frac{1}{$1}");
}
regexpReplace(/''(?=(?:[^<]|<(?!math>|\/math>))*<\/math>)/ig, "" );
regexpReplace(/{(.)}/ig,"$1");
}