MediaWiki:Gadget-LiveRC.js/Extensions/ToolbarExtension.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.
/* 
 --------------------------------------------------------------------------------------
 ---------LLLL---------III--------------------------RRRRRRRRRR--------CCCCC------------
 ---------LLLL---------III--------------------------RRRRRRRRRRRR----CCCCCCCCC----------
 ---------LLLL--------------------------------------RRR------RRR---CCC-----CCC---------
 ---------LLLL---------III--VV-----VV--EEEEEEEEE----RRR------RRR--CCC------------------
 ---------LLLL---------III---VV---VV---EEE----------RRRRRRRRRRR---CCC------------------
 ---------LLLL---------III---VV---VV---EEEEEE-------RRRRRRRRRR----CCC------------------
 ---------LLLL---------III----VV-VV----EEEEEE-------RRR-----RRR----CCC-----CCC---------
 ---------LLLLLLLLLLL--III----VVVVV----EEE----------RRR------RRR----CCCCCCCCC----------
 ---------LLLLLLLLLLL--III-----VVV-----EEEEEEEEE----RRR-------RRR-----CCCCC------------
 --------------------------------------------------------------------------------------
 
'''Extension de LiveRC'''
 
Permet d'avoir une barre d'outils lors d'une modification 

 
* Licence : ...?
* Documentation :
* Auteur : [[:fr:User:Dr Brains]]
* Développement et maintenance :
 
 
{{Catégorisation JS|LiveRC}}
 
<source lang=javascript> */
if (typeof(LiveRC_AddHook)==="function") { // DÉBUT IF
 
LiveRC_Config["InstalledLiveRCExtensions"].push("ToolbarExtension");
/* ************************************************************************************************************************************************ */

window.Custom_lrcEditToolBarSetup = [];
window.lrcEditToolBarSetup = [
  { iconid : "ToolbarIcon_bold", before : "'''", sampletext : "", after : "'''"},
  { iconid : "ToolbarIcon_italic", before : "''", sampletext : "", after : "''"},
  { iconid : "ToolbarIcon_underline", before : "<u>", sampletext : "", after : "</u>"},
  { iconid : "ToolbarIcon_strike", before : "<s>", sampletext : "", after : "</s>"},
  { iconid : "ToolbarIcon_sup", before : "<sup>", sampletext : "", after : "</sup>"},
  { iconid : "ToolbarIcon_sub", before : "<sub>", sampletext : "", after : "</sub>"},
  { iconid : "ToolbarIcon_big", before : "<big>", sampletext : "", after : "</big>"},
  { iconid : "ToolbarIcon_small", before : "", sampletext : "", after : ""},
  { iconid : "ToolbarIcon_headline2", before : "== ", sampletext : "", after : " =="},
  { iconid : "ToolbarIcon_headline3", before : "=== ", sampletext : "", after : " ==="},
  { iconid : "ToolbarIcon_headline4", before : "==== ", sampletext : "", after : " ===="},
  { iconid : "ToolbarIcon_headline5", before : "===== ", sampletext : "", after : " ====="},
  { iconid : "ToolbarIcon_link", before : "[[", sampletext : "", after : "]]"},
  { iconid : "ToolbarIcon_extlink", before : "[", sampletext : "", after : "]"},
  { iconid : "ToolbarIcon_category", before : "[[Category:", sampletext : "", after : "]]"}, 
  { iconid : "ToolbarIcon_template", before : "{{", sampletext : "", after : "}}"},
  { iconid : "ToolbarIcon_comment", before : "<!-- ", sampletext : "", after : " -->"},
  { iconid : "ToolbarIcon_enum", before : "\n# élément 1\n# élément 2\n# élément 3", sampletext : "", after : ""},
  { iconid : "ToolbarIcon_list", before : '\n* élément A\n* élément B\n* élément C', sampletext : "", after : ""},
  { iconid : "ToolbarIcon_image", before : "[[File:", sampletext : "Exemple.jpg", after : "|thumb|Description.]]"},
  { iconid : "ToolbarIcon_media", before : "[[File:", sampletext : "Exemple.ogg", after : "|thumb|Description.]]"},
  { iconid : "ToolbarIcon_gallery", before : "\n<gallery>\nExemple.jpg|[[Tournesol]]\nExemple1.jpg|[[La Joconde]]\nExemple2.jpg|Un [[hamster]]\n</gallery>\n", sampletext : "", after : ""},
  { iconid : "ToolbarIcon_math", before : "<math>", sampletext : "\\rho=\\sqrt{x_0^2+y_0^2}", after : "</math>"},
  { iconid : "ToolbarIcon_nowiki", before : "<nowiki"+">", sampletext : "", after : "</nowiki"+">"},
  { iconid : "ToolbarIcon_sign", before : "-- ~~"+"~~", sampletext : "", after : ""},
  { iconid : "ToolbarIcon_hr", before : "--"+"--", sampletext : "", after : ""},
  { iconid : "ToolbarIcon_br", before : "<br>", sampletext : "", after : ""},
  { iconid : "ToolbarIcon_redirect", before : "#REDIRECTION [[", sampletext : "", after : "]]"},
  { iconid : "ToolbarIcon_table", before : "{| class=\"wikitable\"\n", sampletext : "|-\n! titre 1\n! titre 2\n! titre 3\n|-\n| rangée 1, case 1\n| rangée 1, case 2\n| rangée 1, case 3\n|-\n| rangée 2, case 1\n| rangée 2, case 2\n| rangée 2, case 3", after : "\n|}"}, 
  { iconid : "ToolbarIcon_ref", before : "<ref>", sampletext : "", after : "</ref>"},
  { iconid : "ToolbarIcon_references", before : "<references />", sampletext : "", after : ""}
];

  // Icônes

lrcIcons["ToolbarIcon_bold"] = {
  type:0,
  src:"e/e2/Button_bold.png",
  width:23,
  height:23
};
lrcIcons["ToolbarIcon_italic"] = {
  type:0,
  src:"1/1d/Button_italic.png",
  width:23,
  height:23
};
lrcIcons["ToolbarIcon_underline"] = {
  type:0,
  src:"f/fd/Button_underline.png",
  width:23,
  height:23
};
lrcIcons["ToolbarIcon_strike"] = {
  type:0,
  src:"3/30/Btn_toolbar_rayer.png",
  width:23,
  height:23
};
lrcIcons["ToolbarIcon_sup"] = {
  type:0,
  src:"6/6a/Button_sup_letter.png",
  width:23,
  height:23
};
lrcIcons["ToolbarIcon_sub"] = {
  type:0,
  src:"a/aa/Button_sub_letter.png",
  width:23,
  height:23
};
lrcIcons["ToolbarIcon_big"] = {
  type:0,
  src:"8/89/Button_bigger.png",
  width:23,
  height:23
};
lrcIcons["ToolbarIcon_small"] = {
  type:0,
  src:"0/0d/Button_smaller.png",
  width:23,
  height:23
};
lrcIcons["ToolbarIcon_headline2"] = {
  type:0,
  src:"7/78/Button_head_A2.png",
  width:23,
  height:23
};
lrcIcons["ToolbarIcon_headline3"] = {
  type:0,
  src:"4/4f/Button_head_A3.png",
  width:23,
  height:23
};
lrcIcons["ToolbarIcon_headline4"] = {
  type:0,
  src:"1/14/Button_head_A4.png",
  width:23,
  height:23
};
lrcIcons["ToolbarIcon_headline5"] = {
  type:0,
  src:"8/8c/Button_head_A5.png",
  width:23,
  height:23
};
lrcIcons["ToolbarIcon_link"] = {
  type:0,
  src:"c/c0/Button_link.png",
  width:23,
  height:23
};
lrcIcons["ToolbarIcon_extlink"] = {
  type:0,
  src:"e/ec/Button_extlink.png",
  width:23,
  height:23
};
lrcIcons["ToolbarIcon_category"] = {
  type:0,
  src:"b/b4/Button_category03.png",
  width:23,
  height:23
};
lrcIcons["ToolbarIcon_template"] = {
  type:0,
  src:"3/3b/Button_template_alt.png",
  width:23,
  height:23
};
lrcIcons["ToolbarIcon_comment"] = {
  type:0,
  src:"1/1b/Button_hide_wiki_tag.png",
  width:23,
  height:23
};
lrcIcons["ToolbarIcon_enum"] = {
  type:0,
  src:"8/88/Btn_toolbar_enum.png",
  width:23,
  height:23
};
lrcIcons["ToolbarIcon_list"] = {
  type:0,
  src:"1/11/Btn_toolbar_liste.png",
  width:23,
  height:23
};
lrcIcons["ToolbarIcon_image"] = {
  type:0,
  src:"d/de/Button_image.png",
  width:23,
  height:23
};
lrcIcons["ToolbarIcon_media"] = {
  type:0,
  src:"1/19/Button_media.png",
  width:23,
  height:23
};
lrcIcons["ToolbarIcon_gallery"] = {
  type:0,
  src:"9/9e/Btn_toolbar_gallery.png",
  width:23,
  height:23
};
lrcIcons["ToolbarIcon_math"] = {
  type:0,
  src:"5/5b/Math_icon.png",
  width:23,
  height:23
};
lrcIcons["ToolbarIcon_nowiki"] = {
  type:0,
  src:"8/82/Nowiki_icon.png",
  width:23,
  height:23
};
lrcIcons["ToolbarIcon_sign"] = {
  type:0,
  src:"6/6d/Button_sig.png",
  width:23,
  height:23
};
lrcIcons["ToolbarIcon_hr"] = {
  type:0,
  src:"0/0d/Button_hr.png",
  width:23,
  height:23
};
lrcIcons["ToolbarIcon_br"] = {
  type:0,
  src:"1/13/Button_enter.png",
  width:23,
  height:23
};
lrcIcons["ToolbarIcon_redirect"] = {
  type:0,
  src:"c/c8/Button_redirect.png",
  width:23,
  height:23
};
lrcIcons["ToolbarIcon_table"] = {
  type:0,
  src:"0/04/Button_array.png",
  width:23,
  height:23
};
lrcIcons["ToolbarIcon_ref"] = {
  type:0,
  src:"c/c4/Button_ref.png",
  width:23,
  height:23
};
lrcIcons["ToolbarIcon_references"] = {
  type:0,
  src:"6/64/Buttonrefvs8.png",
  width:23,
  height:23
};
 

 // Textes

lrcTexts["ToolbarIcon_bold_title"] = "Texte en gras";
lrcTexts["ToolbarIcon_italic_title"] = "Texte en italique";
lrcTexts["ToolbarIcon_underline_title"] = "Texte souligné";
lrcTexts["ToolbarIcon_strike_title"] = "Texte barré";
lrcTexts["ToolbarIcon_sup_title"] = "Texte en exposant";
lrcTexts["ToolbarIcon_sub_title"] = "Texte en indice";
lrcTexts["ToolbarIcon_big_title"] = "Texte en grand";
lrcTexts["ToolbarIcon_small_title"] = "Texte en petit";
lrcTexts["ToolbarIcon_headline2_title"] = "Chapitre de niveau 2";
lrcTexts["ToolbarIcon_headline3_title"] = "Chapitre de niveau 3";
lrcTexts["ToolbarIcon_headline4_title"] = "Chapitre de niveau 4";
lrcTexts["ToolbarIcon_headline5_title"] = "Chapitre de niveau 5";
lrcTexts["ToolbarIcon_link_title"] = "Lien interne";
lrcTexts["ToolbarIcon_extlink_title"] = "Lien externe";
lrcTexts["ToolbarIcon_category_title"] = "Catégorie";
lrcTexts["ToolbarIcon_template_title"] = "Modèle";
lrcTexts["ToolbarIcon_comment_title"] = "Commentaire caché";
lrcTexts["ToolbarIcon_enum_title"] = "Énumération";
lrcTexts["ToolbarIcon_list_title"] = "Liste";
lrcTexts["ToolbarIcon_image_title"] = "Image";
lrcTexts["ToolbarIcon_media_title"] = "Média";
lrcTexts["ToolbarIcon_gallery_title"] = "Galerie d'images";
lrcTexts["ToolbarIcon_math_title"] = "Expression mathématique (format LaTeX)";
lrcTexts["ToolbarIcon_nowiki_title"] = "Ignorer le format wiki";
lrcTexts["ToolbarIcon_sign_title"] = "Signature datée";
lrcTexts["ToolbarIcon_hr_title"] = "Ligne horizontale";
lrcTexts["ToolbarIcon_br_title"] = "Saut de ligne";
lrcTexts["ToolbarIcon_redirect_title"] = "Redirection";
lrcTexts["ToolbarIcon_table_title"] = "Tableau";
lrcTexts["ToolbarIcon_ref_title"] = "Référence";
lrcTexts["ToolbarIcon_references_title"] = "Index des références";

lrcTexts["ToolbarIconStandardDesc"] = "[Toolbar] « $1 »";
lrcTexts["ToolbarIconTooltipStandardDesc"] = "[Toolbar] Infobulle du bouton « $1 »";


lrcParamDesc['DesclrcEditToolBarSetup'] = 'Paramètres de l’extension Toolbar';
lrcParamDesc['DesclrcEditToolBarSetup_short'] = 'Toolbar';
lrcParamDesc['DescToolbarIconStandardDesc'] = '[Toolbar] Description standard d’un bouton';
lrcParamDesc['DescToolbarIconTooltipStandardDesc'] = '[Toolbar] Description standard d’une infobulle de bouton';

lstParamMenuTabs["lrcEditToolBarSetup"] = true;

window.ToolbarExtension_Preprocess = function(){
  var buttons = Custom_lrcEditToolBarSetup;
  if(!buttons || buttons.length===0) buttons = lrcEditToolBarSetup;
  for(var a=0,l=buttons.length;a<l;a++){
    var ID = buttons[a].iconid;
    var Tooltip = lrcMakeText((ID+"_title"));
    lrcParamDesc[('Desc'+ID)] = lrcMakeText("ToolbarIconStandardDesc").split("$1").join(Tooltip);
    lrcParamDesc[('Desc'+ID+"_title")] = lrcMakeText("ToolbarIconTooltipStandardDesc").split("$1").join(Tooltip);
  }
}

window.ToolbarExtension_Init = function(){
  var preview = document.getElementById('livePreview');
  if(!preview) return;
  var TextBox = getElementWithId("wpTextbox1", 'textarea', preview);
  if(!TextBox) return;
  var Toolbar = getElementWithId("LiveRC_EditToolBar", 'div', preview);
  if(!Toolbar){
    Toolbar = document.createElement('div');
    Toolbar.id = "LiveRC_EditToolBar";
    TextBox.parentNode.insertBefore(Toolbar, TextBox);
  }
  var buttons = Custom_lrcEditToolBarSetup;
  if(!buttons || buttons.length===0) buttons = lrcEditToolBarSetup;
  for(var a=0,l=buttons.length;a<l;a++){
    var ThisButton = buttons[a]
    var ID = ThisButton.iconid;
    var Link = document.createElement('a');
    Link.innerHTML = lrcMakeIcon(ID);
    Link.id = ID+"_LINK";
    Link.href = "javascript:;";
    Link.onclick = function(){ 
      ToolbarExtension_InsertTag(this);
    }
    Toolbar.appendChild(Link);
  }
}


window.ToolbarExtension_InsertTag = function(Link){
  if(!Link) return;
  var ID = Link.id;
  if(!ID) return;
  ID = ID.split("_LINK").join("");
  var buttons = Custom_lrcEditToolBarSetup;
  if(!buttons || buttons.length==0) buttons = lrcEditToolBarSetup;
  for(var a=0,l=buttons.length;a<l;a++){
    var ThisButton = buttons[a];
    var ThisButtonID = ThisButton.iconid;
    if(ThisButtonID !== ID) continue;
    ToolbarExtension_ReallyInsertTag(ThisButton);
    return false;
  }
  return false;
}

window.ToolbarExtension_ReallyInsertTag = function(ThisButton){
  var iconid = ThisButton.iconid;
  var tagOpen = ThisButton.before;
  var sampleText = ThisButton.sampletext;
  var tagClose = ThisButton.after;
  var preview = document.getElementById('livePreview');
  if(!preview) return;
  var TextBox = getElementWithId("wpTextbox1", 'textarea', preview);
  if(!TextBox) return; 
  function ToolbarExtension_checkSelectedText(){
    if(!selText) {
      selText = sampleText;
      isSample = true;
    }else if(selText.charAt(selText.length - 1) == ' ') { //exclude ending space char
      selText = selText.substring(0, selText.length - 1);
      tagClose += ' ';
    }
  }
  var selText, isSample = false;
  var winScroll = TextBox.scrollTop;
  TextBox.focus(); 
  if(document.selection && document.selection.createRange){
    var range = document.selection.createRange();
    selText = range.text;
    ToolbarExtension_checkSelectedText();
    range.text = tagOpen + selText + tagClose;
    if(isSample && range.moveStart) {
      if(window.opera) tagClose = tagClose.replace(/\n/g,'');
      range.moveStart('character', - tagClose.length - selText.length); 
      range.moveEnd('character', - tagClose.length);    
    }
    range.select();
  }else if(TextBox.selectionStart || TextBox.selectionStart == '0'){
    var startPos = TextBox.selectionStart;
    var endPos = TextBox.selectionEnd;
    selText = TextBox.value.substring(startPos, endPos);
    ToolbarExtension_checkSelectedText();
    TextBox.value = TextBox.value.substring(0, startPos) + tagOpen + selText + tagClose + TextBox.value.substring(endPos, TextBox.value.length);
  }
  TextBox.scrollTop = winScroll;
  return false;
}

LiveRC_AddHook("BeforeInitActivationProcess", ToolbarExtension_Preprocess);


LiveRC_AddHook("AfterPreviewEdit", ToolbarExtension_Init);


LiveRC_AddHook("AfterFillParamPanel", function(){
  LiveRC_ManageParams_Fill(lrcEditToolBarSetup, "lrcEditToolBarSetup", false, true);
});

window.ToolbarExtension_TransformOptions = function(){
  var Fieldset = document.getElementById("LiveRC_OptionsContent_lrcEditToolBarSetup");
  if(!Fieldset) return;
  var Lis = Fieldset.getElementsByTagName('li');
  for(var a=0,l=Lis.length;a<l;a++){
    var Li = Lis[a];
    var ThisItemName = false;
    var Inputs = Li.getElementsByTagName('input');
    for(var b=0,m=Inputs.length;b<m;b++){
      if(Inputs[b].name === "iconid") ThisItemName = Inputs[b].value;
    }
    if(!ThisItemName) continue;
    var Label = document.createElement('label');
    lrcAddClass(Label, "lrcIcons_Label");
    Label.innerHTML = lrcMakeText(ThisItemName+"_title");
    Li.appendChild(document.createTextNode(" : "));
    Li.appendChild(Label);
    var DeleteLink = Li.getElementsByTagName('a')[0];
    DeleteLink.parentNode.removeChild(DeleteLink);
    Li.insertBefore(document.createTextNode(" "), Li.firstChild);
    Li.insertBefore(DeleteLink, Li.firstChild);
  } 
}

LiveRC_AddHook("AfterCreateParamPanel", ToolbarExtension_TransformOptions);

/* ************************************************************************************************************************************************ */
} // FIN IF
//</source>