Корисник:Automatik/editor.js
Напомена: По објавувањето може да треба да го исчистите меѓускладот (кеш) на прелистувачот за да можете ги видите промените.
- Firefox / Safari: Држете Shift и стиснете на Превчитај (Reload) или притиснете Ctrl-F5 или Ctrl-R (⌘-R на Mac);
- Google Chrome: Притиснете Ctrl-Shift-R (⌘-R на Mac)
- Edge: Држете Ctrl додека притискате на Refresh или притиснете Ctrl-F5.
/**
* File [[MediaWiki:Gadget-editor.js]]
*
* This editor is inspired by Conrad.Irwin's editor.js.
* https://en.wiktionary.org/wiki/User:Conrad.Irwin/editor.js
*/
// Singleton editor
window.editor = (function () {
/* global $, mw, wgPageName, wgScript, wgArticleId, wgRevisionId, wgCurRevisionId */
if (mw.config.get( 'wgRevisionId' ) !== mw.config.get( 'wgCurRevisionId' )) {
return {};
}
// private variables and functions
var ed = { enabled: true },
api,
elem,
history,
// Points at the action that has been carried out
cursor;
function update_disabled() {
elem.find('.ed-undo').prop('disabled', cursor === 0);
elem.find('.ed-redo').prop('disabled', cursor === history.length - 1);
elem.find('.ed-save').prop('disabled', history[cursor].save !== 'yes');
}
function show() {
elem.show();
if (elem.hasClass('ed-highlight')) {
setTimeout(function () {
elem.removeClass('ed-highlight');
}, 500);
}
}
// public methods
ed.edit = edit;
function edit(rev) {
init();
history.length = cursor + 1;
if (!rev.save) {
rev.save = 'yes';
} else if (rev.save === 'ifprev') {
rev.save = history[cursor].save;
}
history.push(rev);
redo();
show();
}
ed.undo = undo;
function undo() {
history[cursor].undo();
cursor--;
update_disabled();
}
ed.undo_all = undo_all;
function undo_all() {
while (cursor) {
undo();
}
elem.hide();
}
ed.redo = redo;
function redo() {
history[cursor + 1].redo();
cursor++;
update_disabled();
}
ed.save = save;
function save() {
var wikitext = history[cursor].wikitext;
// Allow callbacks to make last-minute modifications before saving
for (var i = cursor; i; i--) {
if (history[i].onsave) {
wikitext = history[i].onsave(wikitext);
}
}
var log = $('<div>', {'class': 'ed-save'})
.append(
$('<small>', {text: summary()}),
' ',
$('<b>', {text: 'Loading...'})
).appendTo(elem.find('.ed-inner'));
api.post({
action: 'edit',
title: mw.config.get('wgPageName'),
text: wikitext,
summary: summary(true),
notminor: '',
token: mw.user.tokens.values.editToken
}).done(function (data) {
if (!data.edit || data.edit.result !== 'Success') {
log.addClass('error').find('b').text('An error occurred while saving.');
return;
}
$('.ed-added').removeClass('ed-added');
history.length = 1;
cursor = 0;
log.find('b').text('Saved');
log.append(
' ',
$('<a>', {
text: 'Show the changes',
href: mw.config.get('wgScript') +
'?title=' + encodeURIComponent(mw.config.get('wgPageName')) +
'&diff=' + data.edit.newrevid +
'&oldid=' + data.edit.oldrevid
})
);
history[0].wikitext = wikitext;
update_disabled();
}).fail(function (error) {
log.find('b').addClass('error').text('An error occurred while saving.');
return;
});
}
ed.wikitext = wikitext;
function wikitext() {
return init() ||
new $.Deferred()
.resolve(history[cursor].wikitext)
.promise();
}
ed.summary = summary;
function summary(add_assisted) {
var parts = {};
for (var i = 1; i <= cursor; i++) {
var h = history[i];
if (!h.summary) {
continue;
}
if (h.summary_type) {
parts[h.summary_type] = h.summary;
} else if (!parts._) {
parts._ = h.summary;
} else {
parts._ += '; ' + h.summary;
}
}
return $.map(parts, function (x) { return x; }).join('; ') +
(add_assisted ? ' (assisted)' : '');
}
ed.init = init;
function init() {
if (elem) {
return;
}
// Warn before leaving the page if there are unsaved changes.
$(window).on('beforeunload', function () {
if (cursor)
return 'The translation table contains unsaved changes. Please save them before leaving!';
});
history = [{
redo: null,
undo: null,
wikitext: null,
save: 'no'
}];
cursor = 0;
elem = $('<div>', { 'class': 'ed-box ed-highlight' })
.append(
'<a class="ed-close" href="#">×</a>' +
'<div class="ed-inner">' +
'<button class="ed-save" accesskey="s">Save changes</button><br>' +
'<button class="ed-undo">Undo</button>' +
'<button class="ed-redo" disabled>Redo</button>' +
'</div>');
elem.find('.ed-close').click(function (e) { undo_all(); e.preventDefault(); });
elem.find('.ed-save').click(save);
elem.find('.ed-undo').click(undo);
elem.find('.ed-redo').click(redo);
elem.appendTo('body');
api = new mw.Api();
return api.get({
action: 'query',
prop: 'revisions',
titles: mw.config.get('wgPageName'),
rvprop: 'content',
}).then(function (data) {
var wikitext = data.query.pages[mw.config.get('wgArticleId')].revisions[0]['*'];
// Some scripts are strict, so remove trailing whitespace on non-empty lines
wikitext = wikitext.replace(/(\S)[ \t]+(\n|$)/g, '$1$2');
history[0].wikitext = wikitext;
return wikitext;
});
}
return ed;
})();