User:Monster Domosed/common.js: Difference between revisionsGive feedback
No edit summary |
No edit summary |
||
| Line 1: | Line 1: | ||
/* | /* Patch Formatter */ | ||
( function () { | ( function () { | ||
'use strict'; | 'use strict'; | ||
| Line 20: | Line 20: | ||
// ---- API ------------------------------------------------------------ | // ---- API ------------------------------------------------------------ | ||
function getWikiData( templateName ) { | function getWikiData( templateName ) { | ||
console.log('[PatchFormatter] getWikiData | console.log('[PatchFormatter] getWikiData:', templateName); | ||
var api = new mw.Api(); | var api = new mw.Api(); | ||
| Line 33: | Line 33: | ||
} ).then( function ( data ) { | } ).then( function ( data ) { | ||
console.log('[PatchFormatter] API response:', templateName | console.log('[PatchFormatter] API response:', templateName); | ||
var wikitext = ( data.expandtemplates && data.expandtemplates.wikitext ) || ''; | var wikitext = ( data.expandtemplates && data.expandtemplates.wikitext ) || ''; | ||
return wikitext.split( ',' ) | |||
.map( function ( item ) { | .map( function ( item ) { | ||
return item.trim().replace( /^'+|'+$/g, '' ); | return item.trim().replace( /^'+|'+$/g, '' ); | ||
| Line 44: | Line 44: | ||
return item.length > 0; | return item.length > 0; | ||
} ); | } ); | ||
} ); | } ); | ||
} | } | ||
| Line 53: | Line 49: | ||
// ---- PARSING -------------------------------------------------------- | // ---- PARSING -------------------------------------------------------- | ||
function parseEntries( rawText ) { | function parseEntries( rawText ) { | ||
console.log('[PatchFormatter] parseEntries | console.log('[PatchFormatter] parseEntries'); | ||
var entries = []; | var entries = []; | ||
| Line 62: | Line 58: | ||
if ( stripped.indexOf( '-' ) === 0 ) { | if ( stripped.indexOf( '-' ) === 0 ) { | ||
if ( current !== null ) | if ( current !== null ) entries.push( current ); | ||
current = stripped.slice( 1 ).trim(); | current = stripped.slice( 1 ).trim(); | ||
} else if ( current !== null ) { | } else if ( current !== null ) { | ||
| Line 71: | Line 65: | ||
} ); | } ); | ||
if ( current !== null ) | if ( current !== null ) entries.push( current ); | ||
return entries | |||
.map( function ( e ) { | .map( function ( e ) { | ||
return e.replace( /\s+/g, ' ' ).trim(); | return e.replace( /\s+/g, ' ' ).trim(); | ||
} ) | } ) | ||
.filter( | .filter( Boolean ); | ||
} | } | ||
| Line 96: | Line 82: | ||
var rest = entry.slice( idx + 1 ).trim(); | var rest = entry.slice( idx + 1 ).trim(); | ||
if ( gameModes.indexOf( prefix ) !== -1 ) | if ( gameModes.indexOf( prefix ) !== -1 ) return [ 'mode', prefix, rest ]; | ||
if ( heroes.indexOf( prefix ) !== -1 ) return [ 'hero', prefix, rest ]; | |||
if ( items.indexOf( prefix ) !== -1 ) return [ 'item', prefix, rest ]; | |||
if ( heroes.indexOf( prefix ) !== -1 ) | |||
if ( items.indexOf( prefix ) !== -1 ) | |||
} | } | ||
| Line 125: | Line 105: | ||
function wrap( terms, template ) { | function wrap( terms, template ) { | ||
terms.slice().sort( function ( a, b ) { | terms.slice() | ||
.sort( function ( a, b ) { return b.length - a.length; } ) | |||
.forEach( function ( term ) { | |||
var pattern = new RegExp( '(?<!\\w)' + escapeRegExp( term ) + '(?!\\w)', 'g' ); | |||
text = text.replace( pattern, function () { | |||
return protect( | |||
template.replace( '%s', function () { | |||
} | return term; | ||
} ) | |||
); | |||
} ); | |||
} ); | } ); | ||
} | } | ||
| Line 158: | Line 140: | ||
// ---- OUTPUT --------------------------------------------------------- | // ---- OUTPUT --------------------------------------------------------- | ||
function buildOutput( entries, heroes, abilities, items, gameModes ) { | function buildOutput( entries, heroes, abilities, items, gameModes ) { | ||
console.log('[PatchFormatter] buildOutput | console.log('[PatchFormatter] buildOutput'); | ||
var general = []; | var general = []; | ||
entries.forEach( function ( entry ) { | entries.forEach( function ( entry ) { | ||
var c = classify( entry, heroes, items, gameModes ); | var c = classify( entry, heroes, items, gameModes ); | ||
var body = formatBody( c[ 2 ], heroes, abilities, items ); | var body = formatBody( c[ 2 ], heroes, abilities, items ); | ||
if ( | if ( c[ 0 ] === 'general' ) { | ||
general.push( body ); | general.push( body ); | ||
} | } | ||
} ); | } ); | ||
var out = | var out = general.map( function ( e ) { | ||
return '* ' + e; | |||
} ); | } ); | ||
return out.join( '\n' ) + '\n'; | |||
return out.join( '\n' | |||
} | } | ||
// ---- | // ---- INIT ----------------------------------------------------------- | ||
function init() { | function init() { | ||
console.log('[PatchFormatter] init | console.log('[PatchFormatter] init START'); | ||
var app = document.getElementById( 'patchformatter' ); | var app = document.getElementById( 'patchformatter' ); | ||
if ( !app ) { | if ( !app ) { | ||
console.error('[PatchFormatter] | console.error('[PatchFormatter] container not found'); | ||
return; | return; | ||
} | } | ||
console.log('[PatchFormatter] container | console.log('[PatchFormatter] container OK'); | ||
console. | if ( !window.OO || !OO.ui ) { | ||
console.error('[PatchFormatter] OOUI NOT AVAILABLE'); | |||
return; | |||
} | |||
console.log('[PatchFormatter] OOUI OK'); | |||
var inputWidget = new OO.ui.MultilineTextInputWidget( { | |||
rows: 14, | |||
placeholder: 'Raw patch notes' | |||
} ); | |||
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' | |||
} ); | |||
var status = new OO.ui.LabelWidget(); | |||
var layout = new OO.ui.FieldsetLayout(); | |||
layout.$element.append( | |||
inputWidget.$element, | |||
formatButton.$element, | |||
copyButton.$element, | |||
status.$element, | |||
outputWidget.$element | |||
); | |||
app.appendChild( layout.$element[ 0 ] ); | |||
console.log('[PatchFormatter] UI READY'); | |||
formatButton.on( 'click', function () { | |||
console.log('[PatchFormatter] FORMAT CLICK'); | |||
status.setLabel( 'Loading...' ); | |||
Promise.all( [ | |||
getWikiData( 'get_released_heroes_list' ), | |||
getWikiData( 'get_all_released_item_names' ), | |||
getWikiData( 'get_all_released_abilities_list' ) | |||
] ).then( function ( lists ) { | |||
console.log('[PatchFormatter] DATA READY'); | |||
var heroes = lists[ 0 ]; | |||
var items = lists[ 1 ]; | |||
var abilities = lists[ 2 ]; | |||
var entries = parseEntries( inputWidget.getValue() ); | |||
var result = buildOutput( entries, heroes, abilities, items, GAME_MODE_SECTIONS ); | |||
outputWidget.setValue( result ); | |||
} ).catch( function ( | status.setLabel( 'Done: ' + entries.length ); | ||
} ).catch( function ( e ) { | |||
console.error('[PatchFormatter] ERROR', e); | |||
status.setLabel( 'Error' ); | |||
} ); | } ); | ||
} ); | |||
copyButton.on( 'click', function () { | |||
console.log('[PatchFormatter] COPY CLICK'); | |||
navigator.clipboard.writeText( | |||
outputWidget.getValue() | |||
); | |||
status.setLabel( 'Copied' ); | |||
} ); | |||
} | |||
} | } | ||
// ---- BOOT ----------------------------------------------------------- | |||
console.log('[PatchFormatter] loader start'); | |||
mw.loader.using( [ | mw.loader.using( [ | ||
| Line 316: | Line 266: | ||
'mediawiki.util' | 'mediawiki.util' | ||
] ).then( function () { | ] ).then( function () { | ||
console.log('[PatchFormatter] loader | |||
console.log('[PatchFormatter] loader OK'); | |||
init(); | |||
} ).catch( function ( e ) { | |||
console.error('[PatchFormatter] LOADER FAILED', e); | |||
} ); | } ); | ||