Usuario:Elisardojm/teclasrapidas.js

// Copiado de https://en.wikipedia.org/wiki/User:GregU/hotkeys.js
// Este código define algúns accesos rápidos de teclado que poden usarse en campos de texto.
// Non funciona con Internet Explorer nin con Chrome.  Vexa a páxina de conversa para ver a documentación.
// por Greg Ubben

// Este código engade algunhas teclas rápidas para engadir de forma sinxela a frecha á dereita (→) e outros 
// caracteres e frases no resumo de edición, caixa de edición, ou outros campos de texto.
// hotkeys[] pode definirse para aumentar as teclas por defecto. Prema Ctrl-? para listar todas as teclas definidas.

function hotkey(e) {
    if (e && e.ctrlKey && hotkeys[String.fromCharCode( e.charCode )])
    {
        var newtxt = hotkeys[String.fromCharCode( e.charCode )];
        var before = this.value.slice( 0, this.selectionStart );
        var after  = this.value.slice( this.selectionEnd );
        var scroll = this.scrollTop;

        if (newtxt == "MENU") {         // prema Ctrl-? para obter a lista de teclas definidas
            var menu = "";
            for (var key in hotkeys) {
                var name  = key.toUpperCase().replace(",", "<").replace(".", ">");
                var value = hotkeys[key].replace(/\n/g, "•").slice(0,40);
                if (value && value != newtxt)
                    menu += "\u200E" + name + " \t" + value + "\n";
            }
            alert( menu );
            return false;
        }
        //  truco para non redefinir Ctrl-F, Ctrl-T, etc. na área de texto principal
        //
        if (newtxt.search(/\[\[WP:|\bfix/i) >= 0 && this.id == "wpTextbox1") {
            return true;
        }
        if (newtxt.search(/^\[*\w./) >= 0 && this.id != "wpTextbox1") {
            if (before.search(/[^\s;,]$/) >= 0)  before += "; ";
            if  (after.search(/^[^\s;,]/) >= 0)  newtxt += "; ";
        }
        this.value = before + newtxt + after;
        this.selectionStart = this.selectionEnd = before.length + newtxt.length;
        this.scrollTop = scroll;       // non perder a nosa posición 
        return false;
    }
    return true;
}

$( function()
{
    var defaultCtrl = {
        "," : "←",                   // Ctrl ,   frecha á esquerda 
        "." : "→",                   // Ctrl .   frecha á dereita
        "-" : "–",                   // Ctrl -   guión curto
        "=" : "—",                   // Ctrl =   guión longo
        "7" : "&" + "nbsp;",         // Ctrl &   espazo sen ruptura
        "l" : "renomear ligazón",    // Ctrl l   resumo de edición: renomear ligazón
        "r" : "desfeito vandalismo", // Ctrl l   resumo de edición: desfeito vandalismo
        "s" : "[[WP:LE]]",           // Ctrl s   ligazón ó libro de estilo
        "/" : "MENU"                 // Ctrl /   lista as teclas rápidas definidas
    };
    var fields = { wpSummary:0, wpTextbox1:0, wpReason:0, wpUploadDescription:0 };

    if (mw.config.get('wgAction') != "view" || mw.config.get('wgCanonicalSpecialPageName'))
    {
        if (typeof(hotkeys) == 'undefined')  hotkeys = {};
        for (var key in defaultCtrl) {
            if (hotkeys[key] == null)
                hotkeys[key] = defaultCtrl[key];
        }
        for (var field in fields) {
            var f = document.getElementById(field);
            if (f)  f.onkeypress = hotkey;
        }
    }
});