Utilisateur:Zelda/japan.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.//<pre><nowiki>
/**
* Ajout d'un bouton à la toolbar d'édition
* fonction copiée depuis insertButton()
*/
function addJapanButton() {
var toolbar = document.getElementById("toolbar");
if (!toolbar) return;
var image = document.createElement("img");
image.width = 23;
image.height = 22;
image.src = "http://upload.wikimedia.org/wikipedia/commons/e/ec/Button_base.png";
image.border = 0;
image.style.cursor = "pointer";
var ref = document.createElement("a");
ref.setAttribute("href", "javascript:japanConvert()");
ref.setAttribute("accesskey", "<");
ref.setAttribute("title", "Conversion en macrons");
ref.appendChild(image);
toolbar.appendChild(ref);
}
/**
* Conversion des accents circonflexes vers des macrons
*/
function convertToMacrons(textToConvert) {
var text = textToConvert;
text = text.replace(new RegExp("aa", "g"), "ā").replace(new RegExp("Aa", "g"), "Ā");
text = text.replace(new RegExp("ii", "g"), "ī").replace(new RegExp("Ii", "g"), "Ī");
text = text.replace(new RegExp("ee", "g"), "ē").replace(new RegExp("Ee", "g"), "Ē");
text = text.replace(new RegExp("uu", "g"), "ū").replace(new RegExp("Uu", "g"), "Ū");
text = text.replace(new RegExp("oo", "g"), "ō").replace(new RegExp("Oo", "g"), "Ō");
text = text.replace(new RegExp("ou", "g"), "ō").replace(new RegExp("Ou", "g"), "Ō");
text = text.replace(new RegExp("â", "g"), "ā").replace(new RegExp("Â", "g"), "Ā");
text = text.replace(new RegExp("î", "g"), "ī").replace(new RegExp("Î", "g"), "Ī");
text = text.replace(new RegExp("ê", "g"), "ē").replace(new RegExp("Ê", "g"), "Ē");
text = text.replace(new RegExp("û", "g"), "ū").replace(new RegExp("Û", "g"), "Ū");
text = text.replace(new RegExp("ô", "g"), "ō").replace(new RegExp("Ô", "g"), "Ō");
return text;
}
/**
* fonction de conversion de texte sélectionnée
* inspirée de insertTags(), elle même inspirée de phpBB
*/
function japanConvert() {
var txtarea = document.getElementById("wpTextbox1");
// IE
if (document.selection && !is_gecko) {
var range = document.selection.createRange();
var theSelection = range.text;
if (!theSelection) {
return;
}
txtarea.focus();
range.text = convertToMacrons(theSelection);
// Mozilla
} else if(txtarea.selectionStart || txtarea.selectionStart == '0') {
var replaced = false;
var startPos = txtarea.selectionStart;
var endPos = txtarea.selectionEnd;
if (endPos-startPos) {
replaced = true;
}
if (! replaced) {
return;
}
var scrollTop = txtarea.scrollTop;
var theSelection = (txtarea.value).substring(startPos, endPos);
var replacedSelection = convertToMacrons(theSelection);
txtarea.value = txtarea.value.substring(0, startPos) + replacedSelection +
txtarea.value.substring(endPos, txtarea.value.length);
txtarea.focus();
//set new selection
var cPos = startPos+(replacedSelection.length);
txtarea.selectionStart = cPos;
txtarea.selectionEnd = cPos;
txtarea.scrollTop = scrollTop;
}
}
$(addJapanButton);
//</nowiki></pre>