Utente:L0ll0/abbr.js
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.
var userApp = window.userApp || {};
(function (app) {
"use strict";
var getSelection = function () {
var elTextarea = mw.activeElement; // elemento dom wpTextbox1
var sText = $(elTextarea).val();
var aSelection = [];
aSelection[0] = sText.substring(0, elTextarea.selectionStart);
aSelection[1] = sText.substring(elTextarea.selectionStart, elTextarea.selectionEnd);
aSelection[2] = sText.substring(elTextarea.selectionEnd);
return aSelection;
};
// riscrive wpTextbox1 e ristabilisce il focus/la selezione attiva
// riceve un array tipo [testo precedente, testo selezionato, testo seguente]
var writeSelection = function (aSelection) {
var elTextarea = mw.activeElement; // elemento dom wpTextbox1=$("#wpTextbox1")[0]; // elemento dom wpTextbox1
elTextarea.value = aSelection.join("");
elTextarea.selectionStart = aSelection[0].length;
elTextarea.selectionEnd = aSelection[0].length + aSelection[1].length;
elTextarea.focus();
};
var doAbbreviation = function (sText) {
var sLast = "";
var sSecondLast = "";
//caso vocale + m/n
sLast = sText.slice(-1);
sSecondLast = sText.slice(-2, -1).normalize("NFD").substr(0, 1);
if (/[aeiou][mn]/.test(sSecondLast + sLast)) {
return sText.slice(0, -2) + (sSecondLast + "\u0304").normalize("NFC");
}
if (sText.slice(-3) === "pre") {
return sText.slice(0, -3) + ("p\u0304").normalize("NFC");
}
if (sText.slice(-2) === "re") {
return sText.slice(0, -3) + ("r\u0304").normalize("NFC");
}
if (sText.slice(-3) === "per") {
return sText.slice(0, -3) + ("<u>p</u>");
}
if (sText.slice(-3) === "che") {
return sText.slice(0, -3) + ("cħ");
}
if (["que", "qua", "qui"].indexOf(sText.slice(-3)) !== -1) {
return sText.slice(0, -3) + ("q\u0304").normalize("NFC");
}
if (sText.slice(-6) === "quando") {
return sText.slice(0, -6) + "qn\u0304".normalize("NFC");
}
if (sText.slice(-5) === "vostr") {
return sText.slice(0, -5) + "vr\u0303".normalize("NFC");
}
if (sText.slice(-5) === "prode") {
return sText.slice(0, -5) + "pr\u0303e".normalize("NFC");
}
return sText;
};
app.abbrRun = function () {
var aSelection = getSelection();
aSelection[0] = doAbbreviation(aSelection[0]);
writeSelection(aSelection);
};
}(userApp));