User:Monster Domosed/common.js: Difference between revisionsGive feedback
No edit summary |
No edit summary |
||
| Line 1: | Line 1: | ||
/* Patch Formatter */ | /* Patch Formatter */ | ||
( function () { | ( function () { | ||
'use strict'; | 'use strict'; | ||
var L = '{' + '{'; | var L = '{' + '{'; | ||
var R = '}' + '}'; | var R = '}' + '}'; | ||
| Line 179: | Line 169: | ||
if ( !app ) { return; } | if ( !app ) { return; } | ||
var inputWidget = new OO.ui.MultilineTextInputWidget( { | |||
' | rows: 14, | ||
placeholder: 'Raw patch notes', | |||
' | autofocus: true | ||
} ); | |||
var outputWidget = new OO.ui.MultilineTextInputWidget( { | |||
' | rows: 20, | ||
' | readOnly: true | ||
} ); | |||
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 ); | |||
app.appendChild( panel.$element[ 0 ] ); | |||
var listsPromise = null; | var listsPromise = null; | ||
| Line 208: | Line 237: | ||
} | } | ||
formatButton.on( 'click', function () { | |||
formatButton.setDisabled( 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( | var entries = parseEntries( inputWidget.getValue() ); | ||
var result = buildOutput( entries, heroes, abilities, items, GAME_MODE_SECTIONS ); | |||
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 ) { | ||
statusLabel.setLabel( 'Error: ' + err ); | |||
} ).then( function () { | } ).then( function () { | ||
formatButton.setDisabled( false ); | |||
} ); | } ); | ||
} ); | } ); | ||
copyButton.on( 'click', function () { | |||
if ( navigator.clipboard ) { | if ( navigator.clipboard ) { | ||
navigator.clipboard.writeText( | navigator.clipboard.writeText( outputWidget.getValue() ).then( function () { | ||
statusLabel.setLabel( 'Copied.' ); | |||
} ); | } ); | ||
} else { | } else { | ||
outputWidget.$input.select(); | |||
document.execCommand( 'copy' ); | document.execCommand( 'copy' ); | ||
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 ); | ||
} ); | } ); | ||