User:So9q/EditTranslations.js
Jump to navigation
Jump to search
Note: You may have to bypass your browser’s cache to see the changes. In addition, after saving a sitewide CSS file such as MediaWiki:Common.css, it will take 5-10 minutes before the changes take effect, even if you clear your cache.
- Mozilla / Firefox / Safari: hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (Command-R on a Macintosh);
- Konqueror and Chrome: click Reload or press F5;
- Opera: clear the cache in Tools → Preferences;
- Internet Explorer: hold Ctrl while clicking Refresh, or press Ctrl-F5.
- This script lacks a documentation subpage. Please create it.
- Useful links: root page • root page’s subpages • links • redirects • your own
// So9qs EditTranslations based on MediaWiki:Gadget-DefSideBoxes.js
// <nowiki>
function findLang(object) {
lang = $(object).parentsUntil("ul").find("[lang]").attr("lang");
console.log("lang: " + lang);
return lang;
}
function editTranslation(qq) {
// initialize the editor to download the wikitext
(new Editor).withCurrentText(function(wikitext) {
console.log("editTranslation :)")
if (qq.YREditing) {
return;
}
qq.YREditing = true;
// find lang of the clicked one and gloss from the trans-top header
// find lang
lang = findLang(qq);
// find gloss and trim it
var translationGloss = $(qq).parentsUntil("table").parent().first().attr("data-gloss").trim();
console.log("gloss: " + translationGloss);
// split the wikitext to make it manageable
var array = wikitext.split('\n');
// find our boundaries based on the gloss
var slicestart = array.indexOf('{{trans-top|' + translationGloss + '}}') + 1;
// search from our gloss
var sliceend = array.indexOf('{{trans-bottom}}', slicestart);
// extract the section of interest
var trans = array.slice(slicestart, sliceend);
console.log("start: " + slicestart);
console.log("end: " + sliceend);
console.log("sliced: " + trans);
// find our target line
oldline = trans.find(function(item){
return item.match("/|" + lang + "|/");
});
// the index has to be within our boundaries
oldlineindex = array.indexOf(oldline);
console.log("lineindex:" + oldlineindex);
console.log("line:" + oldline);
if (oldlineindex < sliceend || oldlineindex > slicestart) {
// go ahead
/* pseudocode for rest of function:
show the form
replace the oldline at oldlineindex with newline
done :)
*/
// save the old translationline
/* disabled because of unknown error
var temporarydefholder = qq.insertBefore(newNode('span', {
'style': 'display:none;'}),
$(qq).child().eq[1].nodeName.toLowerCase() != "form" ?
qq.childNodes[1] : qq.childNodes[2]); */
// initiate the editor.js
new AdderWrapper(new Editor(), {
'createForm': function() {
return trform = newNode('form', {
'style': 'width:100%;display:inline-block;margin-bottom:-6px;'
}, newNode('table', {
'style': 'width:100%;'
}, newNode('tr', newNode('td', newNode('textarea', {
//'style': 'height:17px;width:100%;',
'name': 'definition',
'size': 80,
'value': oldline,
'rows': 3,
'cols': 50
})
), newNode('td', {
'style': 'white-space:nowrap;'
}, newNode('div', {
'class': "DSBRedButton"
}, newNode('div', {
click: function() {
temporarydefholder.style.display = 'inline';
trform.style.display = 'none';
qq.YREditing = false;
}
}, "Discard Changes")), newNode('div', {
'class': "DSBGreenButton"
}, newNode('div', {
click: function() {
trform.onsubmit();
}
}, "Preview Changes ►"))))));
},
'fields': {
'translation': function(txt, error) {
return txt || error("Please specify a translation.");
}
},
'onsubmit': function(values, render) {
render(values.translation, function(newhtml) {
editor = new Editor();
//var findnumberofdefs = findnumberofdefs_(qq); // apparently this is sometimes necessary
var addedspan = newNode('span');
addedspan.innerHTML = newhtml;
//var updatecatscallback;
editor.addEdit({
edit: function(wikitext) {
array[oldlineindex] = values.translation;
wikitext = array.join("\n");
ccc = wikitext;
return wikitext;
},
redo: function() {
/*trform.parentNode.insertBefore(addedspan, trform);
trform.style.display = temporarydefholder.style.display = 'none';
//updatecatscallback = window.tabbedLanguages && updateCategories(findtopli(trform.parentNode).parentNode.parentNode, values.translation, olddef)
qq.YREditing = false;*/
},
undo: function() {
/*trform.parentNode.removeChild(addedspan);
trform.style.display = "inline";
trform.firstChild.firstChild.firstChild.firstChild.value = olddef;
//updatecatscallback && updatecatscallback();
qq.YREditing = true;*/
},
summary: "translation of " + lang + " changed to: " + values.translation
}, addedspan);
}); //render
} //onsubmit
}); //AdderWrapper
} // if check for index
else {
txt = "Error: Two or more translation sections of this language are identical and the code currently does not support this.";
console.info(txt);
error(txt);
}
}); // withCurrentText
} //editTranslation
jQuery(document).ready(function() {
if (mw.config.get('wgAction') == 'view' && (
mw.config.get('wgNamespaceNumber') === 0 ||
mw.config.get('wgPageName') == "Wiktionary:Sandbox" ||
mw.config.get('wgPageName') == "User:So9q/sandbox") &&
!/&printable=yes|&diff=|&oldid=/.test(window.location.search) &&
!window.loadedYREdit) {
window.loadedYREdit = true;
// enable for translation li's
$("[lang]").not(".interlanguage-link-target").parentsUntil("ul").each(
function() {
// only run in a translation section
if ($(this).parent().prop("tagName") == "UL" &&
$(this).parent().parent().attr("class") == "translations-cell") {
var txt = $("<span></span>").attr("class", "addusexbutton").html(
$("<a></a>").text("edit translation").attr({
// pass this object to editTranslation
"onclick" : "editTranslation(this)",
"title" : "Edit translation"})
);
$(this).append(txt);
}
});
}
});
// </nowiki>