User:Monster Domosed/common.js: Difference between revisions

No edit summary
No edit summary
Line 1: Line 1:
/*  Patch Formatter  */
/*  Patch Formatter  */
( function () {
( function () {
'use strict';
'use strict';


// Copy-safe "" and "" — never write them as literals in this editor.
var L = '{' + '{';
var L = '{' + '{';
var R = '}' + '}';
var R = '}' + '}';
Line 179: Line 169:
if ( !app ) { return; }
if ( !app ) { return; }


app.innerHTML =
var inputWidget = new OO.ui.MultilineTextInputWidget( {
'<p><strong>Raw patch notes</strong></p>' +
rows: 14,
'<textarea id="pf-input" rows="14" style="width:100%;font-family:monospace;"></textarea>' +
placeholder: 'Raw patch notes',
'<p style="margin-top:8px;">' +
autofocus: true
'<button id="pf-run" type="button">Format</button> ' +
} );
'<button id="pf-copy" type="button">Copy output</button>' +
 
'<span id="pf-status" style="margin-left:10px;"></span>' +
var outputWidget = new OO.ui.MultilineTextInputWidget( {
'</p>' +
rows: 20,
'<p><strong>Formatted wikitext</strong></p>' +
readOnly: true
'<textarea id="pf-output" rows="20" style="width:100%;font-family:monospace;" readonly></textarea>';
} );
 
var formatButton = new OO.ui.ButtonWidget( {
label: 'Format',
flags: [ 'primary', 'progressive' ]
} );
 
var copyButton = new OO.ui.ButtonWidget( {
label: 'Copy output',
flags: [ 'progressive' ]
} );
 
var statusLabel = new OO.ui.LabelWidget( {
label: ''
} );
 
var buttonRow = new OO.ui.HorizontalLayout( {
items: [ formatButton, copyButton, statusLabel ]
} );
var inputField = new OO.ui.FieldLayout( inputWidget, {
label: 'Raw patch notes',
align: 'top'
} );
 
var outputField = new OO.ui.FieldLayout( outputWidget, {
label: 'Formatted wikitext',
align: 'top'
} );
 
// Fieldset groups everything in the required order.
var fieldset = new OO.ui.FieldsetLayout();
fieldset.addItems( [
inputField,
new OO.ui.FieldLayout( buttonRow, { align: 'top' } ),
outputField
] );
 
// Main wrapper panel.
var panel = new OO.ui.PanelLayout( {
expanded: false,
padded: true,
framed: true
} );
panel.$element.append( fieldset.$element );


var input = document.getElementById( 'pf-input' );
app.appendChild( panel.$element[ 0 ] );
var output = document.getElementById( 'pf-output' );
var runBtn = document.getElementById( 'pf-run' );
var copyBtn = document.getElementById( 'pf-copy' );
var status = document.getElementById( 'pf-status' );


var listsPromise = null;
var listsPromise = null;
Line 208: Line 237:
}
}


runBtn.addEventListener( 'click', function () {
formatButton.on( 'click', function () {
status.textContent = 'Loading wiki data...';
formatButton.setDisabled( true );
runBtn.disabled = true;
statusLabel.setLabel( 'Loading wiki data...' );
loadLists().then( function ( lists ) {
loadLists().then( function ( lists ) {
var heroes = lists[ 0 ], items = lists[ 1 ], abilities = lists[ 2 ];
var heroes = lists[ 0 ], items = lists[ 1 ], abilities = lists[ 2 ];
var entries = parseEntries( input.value );
var entries = parseEntries( inputWidget.getValue() );
output.value = buildOutput( entries, heroes, abilities, items, GAME_MODE_SECTIONS );
var result = buildOutput( entries, heroes, abilities, items, GAME_MODE_SECTIONS );
status.textContent = 'Processed ' + entries.length + ' entries (' +
outputWidget.setValue( result );
statusLabel.setLabel(
'Processed ' + entries.length + ' entries (' +
heroes.length + ' heroes, ' + abilities.length + ' abilities, ' +
heroes.length + ' heroes, ' + abilities.length + ' abilities, ' +
items.length + ' items).';
items.length + ' items).'
);
} ).catch( function ( err ) {
} ).catch( function ( err ) {
status.textContent = 'Error: ' + err;
statusLabel.setLabel( 'Error: ' + err );
} ).then( function () {
} ).then( function () {
runBtn.disabled = false;
formatButton.setDisabled( false );
} );
} );
} );
} );


copyBtn.addEventListener( 'click', function () {
copyButton.on( 'click', function () {
if ( navigator.clipboard ) {
if ( navigator.clipboard ) {
navigator.clipboard.writeText( output.value ).then( function () {
navigator.clipboard.writeText( outputWidget.getValue() ).then( function () {
status.textContent = 'Copied.';
statusLabel.setLabel( 'Copied.' );
} );
} );
} else {
} else {
output.select();
outputWidget.$input.select();
document.execCommand( 'copy' );
document.execCommand( 'copy' );
status.textContent = 'Copied.';
statusLabel.setLabel( 'Copied.' );
}
}
} );
} );
}
}


mw.loader.using( [ 'mediawiki.api', 'mediawiki.util' ] ).then( function () {
mw.loader.using( [
'oojs-ui',
'oojs-ui-widgets',
'oojs-ui.styles',
'mediawiki.api',
'mediawiki.util'
] ).then( function () {
$( init );
$( init );
} );
} );