MediaWiki:Gadget-sk-ve.js
Uwaga: aby zobaczyć zmiany po opublikowaniu, może zajść potrzeba wyczyszczenia pamięci podręcznej przeglądarki.
- Firefox / Safari: Przytrzymaj Shift podczas klikania Odśwież bieżącą stronę, lub naciśnij klawisze Ctrl+F5, lub Ctrl+R (⌘-R na komputerze Mac)
- Google Chrome: Naciśnij Ctrl-Shift-R (⌘-Shift-R na komputerze Mac)
- Internet Explorer / Edge: Przytrzymaj Ctrl, jednocześnie klikając Odśwież, lub naciśnij klawisze Ctrl+F5
- Opera: Naciśnij klawisze Ctrl+F5.
/* global OO, mw, ve */
// expected init
/**
mw.hook('userjs.wp_sk.ve_sk.loaded').add((WpSkVisualCode) => {
mw.hook( 've.activationComplete' ).add( () => {
var ve_sk = new WpSkVisualCode();
if (typeof wp_sk != 'object') {
mw.loader.load('https://pl.wikipedia.org/w/index.php?action=raw&ctype=text/javascript&title=MediaWiki:Gadget-sk-local.js');
mw.loader.load('https://pl.wikipedia.org/w/index.php?action=raw&ctype=text/javascript&title=MediaWiki:Gadget-sk.js');
}
mw.hook('userjs.wp_sk.loaded').add((wp_sk) => {
ve_sk.init(wp_sk);
});
} );
});
/**/
/**
* VE code editor edition (VECE is also called 2017 editor).
*/
//WpSkVisualCode = class {
class WpSkVisualCode {
init(wp_sk) {
this.wp_sk = wp_sk;
// define codeCleanup icon
mw.util.addCSS('.oo-ui-icon-codeCleanup {background-image: url(https://upload.wikimedia.org/wikipedia/commons/thumb/2/2c/Broom_icon.svg/22px-Broom_icon.svg.png);}');
// define tool
function WpSkTool() {
WpSkTool.super.apply( this, arguments );
}
OO.inheritClass( WpSkTool, OO.ui.Tool );
WpSkTool.static.name = 'myWpSkTool';
WpSkTool.static.icon = 'codeCleanup';
WpSkTool.static.title = 'Sprzątanie kodu';
WpSkTool.static.autoAddToCatchall = false;
// disable for visual mode
WpSkTool.prototype.isExecutable = function() {
var surface = ve.init.target.getSurface();
return surface && surface.getMode() === 'source';
};
WpSkTool.prototype.onUpdateState = function () {};
WpSkTool.prototype.onSelect = () => {
console.log('[WpSkTool]', 'onSelect', wp_sk, this, WpSkTool);
this.cleanup();
//this.setActive( false );
};
// append to VE toolbar
const toolbar = ve.init.target.getToolbar();
const myToolGroup = new OO.ui.BarToolGroup( toolbar, {
title: 'SK',
include: [ 'myWpSkTool' ]
} );
ve.ui.toolFactory.register( WpSkTool );
toolbar.addItems( [ myToolGroup ] );
}
/**
* Get selected or full text.
* @returns {str, start, end, isFull}
*/
getSelection() {
const surfacem = ve.init.target.getSurface().model;
let start = surfacem.selection.range.start;
let end = surfacem.selection.range.end;
let isFull = start == end;
let str = isFull ? surfacem.getDom() : surfacem.getFragment().getText(true);
// On Windows `getFragment().getText(true)` might return double `\n` so... we might need to take care of this...
if (!isFull) {
let fullByDom = surfacem.getDom();
let fullByRange = surfacem.getLinearFragment(new ve.Range(1, surfacem.getDocument().data.getLength())).getText(true);
if (fullByDom != fullByRange) {
console.log('ve_sk: getSelection', {fullByDom, fullByRange});
str = str.replace(/\n\n/g, '\n');
}
}
console.log('ve_sk: getSelection', {str, start, end, isFull});
return {str, start, end, isFull};
}
/**
* Replace selected/full text.
*
* @param {Sring} str New text.
* @param {Number} start
* @param {Number} end
*/
replaceText (str, start, end)
{
console.log('ve_sk: replaceText', {str, start, end});
const surfacem = ve.init.target.getSurface().model;
let isFull = start == end;
// note: ve.Range starts at 1
let range = isFull ? new ve.Range(1, surfacem.getDocument().data.getLength()) : new ve.Range(start, end);
surfacem.getLinearFragment(range).insertContent(str);
}
/** wp_sk.cleanup replacement. */
cleanup ()
{
const wp_sk = this.wp_sk;
//
// Pobierz zaznaczony fragment (całość jeśli nic nie zaznaczone)
//
let {str, start, end, isFull} = this.getSelection();
if (isFull) {
str = str.replace(/\n+$/,''); // bez końcowych enterów
}
//
// Wywołanie czyściciela
//
var str_pre = str;
str = wp_sk.cleaner(str);
wp_sk.nochanges = (str==str_pre);
let hasChanges = !wp_sk.nochanges;
//
// zapisanie zmian
//
if (hasChanges)
{
//sel_t.qsetSelStr(input, str, true);
this.replaceText(str, start, end);
}
var summary = 'WP:SK zakończone: ' + (hasChanges ? 'przejrzyj zmiany!' : 'brak zmian');
mw.notify(summary);
// input.focus();
}
}
// ~export
mw.hook('userjs.wp_sk.ve_sk.loaded').fire(WpSkVisualCode);