Utente:Tino/TradottoDa.js: differenze tra le versioni
Contenuto cancellato Contenuto aggiunto
Nessun oggetto della modifica |
rimuovo categorie |
||
(7 versioni intermedie di uno stesso utente non sono mostrate) | |||
Riga 10:
* @author [[Utente:Tino]]
*/
/* <nowiki> */
(function (mw, $) {
'use strict';
// configuration vars
var keyCode = 89; // y key
var shift = false;
var ctrl = true;
var inputArea = $('#wpTextbox1');
var cursor = inputArea.textSelection('getCaretPosition');▼
// flag for default browser behaviour on the shortcut
var keystrokeDefault = true;
/*
* \brief Check whether the script should perform its action for the
* trigger event.
* @param e Triggering event.
*/
function triggerCondition(e) {
return e.altKey == alt &&
e.shiftKey == shift &&
e.keyCode === keyCode &&
}
/*
Riga 46 ⟶ 70:
];
// text
var text = '{{tradotto da|' + lang + '|';
var index;
Riga 68 ⟶ 92:
// insert template completion
start: cursor - 2,▼
▲ end: cursor
▲ });
inputArea.textSelection('encapsulateSelection', {
peri: text,
Riga 99 ⟶ 119:
dataType: 'jsonp',
success: function(data) {
if (!data.success)
return;
var wiki = lang + 'wiki';
Riga 104 ⟶ 127:
var index;
for (index in data.entities) {
// the voice may not exist in the specified language
if (!data.entities[index].sitelinks[wiki])
var title = data.entities[index].sitelinks[wiki].title;
Riga 115 ⟶ 142:
$(function() {
inputArea.keydown(function(e) {
//
if (!triggerCondition(e
▲ mw.config.get('wgNamespaceNumber') === 1 /* talkpage */) {
var prevText, cursor, beginning, langMatch;
// get language from the input in the textarea (last 2 chars)▼
// get cursor position and select "{{tradotto da|ll" on keypress
cursor =
prevText = inputArea.val().substring(0,
beginning = prevText.toLowerCase().lastIndexOf('{{tradotto da');
▲ }
prevText = prevText.substring(beginning);
{start: beginning, end: cursor});
langMatch = prevText.match(/^{{tradotto da\|([a-z]{2})$/);
if (!langMatch || !langMatch[1])
return;
queryWikidata(langMatch[1]);
});
// prevent default browser action, when needed only
inputArea.keypress(function(e) {
if (
▲ e.ctrlKey && e.keyCode === 89 /* "y" key */ ) {
return keystrokeDefault;
▲ }
});
});
}(mediaWiki, jQuery));
/* </nowiki> */
|