User:Monster Domosed/common.js: Difference between revisionsGive feedback
Jump to navigation
Jump to search
No edit summary |
No edit summary |
||
| Line 6: | Line 6: | ||
var R = '}' + '}'; | var R = '}' + '}'; | ||
// ---- CONFIG --------------------------------------------------------- | |||
var GAME_MODE_SECTIONS = [ 'Street Brawl' ]; | var GAME_MODE_SECTIONS = [ 'Street Brawl' ]; | ||
| Line 58: | Line 59: | ||
} else if ( current !== null ) { | } else if ( current !== null ) { | ||
current += ' ' + stripped; | current += ' ' + stripped; | ||
} | } | ||
| Line 67: | Line 69: | ||
return entries.map( function ( e ) { | return entries.map( function ( e ) { | ||
return e.replace( /\s+/g, ' ' ).trim(); | return e.replace( /\s+/g, ' ' ).trim(); | ||
} ).filter( Boolean ); | } ).filter( Boolean ); | ||
} | } | ||
| Line 102: | Line 106: | ||
} | } | ||
function formatBody( text, heroes, abilities, items | function formatBody( text, heroes, abilities, items ) { | ||
var protectedTokens = []; | var protectedTokens = []; | ||
| Line 112: | Line 116: | ||
} | } | ||
function wrap( terms, template | function wrap( terms, template ) { | ||
terms.slice() | terms.slice() | ||
| Line 120: | Line 124: | ||
.forEach( function ( term ) { | .forEach( function ( term ) { | ||
var | var pattern = new RegExp( | ||
'(?<!\\w)' + escapeRegExp( term ) + '(?!\\w)', | |||
'g' | |||
); | |||
text = text.replace( pattern, function () { | |||
return protect( | |||
} | template.replace( '%s', term ) | ||
); | |||
} ); | |||
} ); | } ); | ||
} | } | ||
wrap( abilities, L + 'AbilityIcon|%s' + R | wrap( abilities, L + 'AbilityIcon|%s' + R ); | ||
wrap( items, L + 'ItemIcon|%s' + R | wrap( items, L + 'ItemIcon|%s' + R ); | ||
wrap( heroes, L + 'HeroIcon|%s' + R | wrap( heroes, L + 'HeroIcon|%s' + R ); | ||
WIKI_LINKS.forEach( function ( pair ) { | WIKI_LINKS.forEach( function ( pair ) { | ||
| Line 181: | Line 161: | ||
var itemSections = {}, itemOrder = []; | var itemSections = {}, itemOrder = []; | ||
var heroSections = {}, heroOrder = []; | var heroSections = {}, heroOrder = []; | ||
entries.forEach( function ( entry ) { | entries.forEach( function ( entry ) { | ||
var c = classify( entry, heroes, items, gameModes ); | var c = classify( entry, heroes, items, gameModes ); | ||
var kind = c[ 0 ]; | |||
var name = c[ 1 ]; | |||
var body = formatBody( c[ 2 ], heroes, abilities, items ); | |||
if ( kind === 'general' ) { | |||
general.push( body ); | general.push( body ); | ||
if ( !modes[ | } else if ( kind === 'mode' ) { | ||
modes[ | |||
modeOrder.push( | if ( !modes[ name ] ) { | ||
modes[ name ] = []; | |||
modeOrder.push( name ); | |||
} | } | ||
modes[ | modes[ name ].push( body ); | ||
} else if ( | } else if ( kind === 'item' ) { | ||
if ( !itemSections[ | if ( !itemSections[ name ] ) { | ||
itemSections[ | itemSections[ name ] = []; | ||
itemOrder.push( | itemOrder.push( name ); | ||
} | } | ||
itemSections[ | itemSections[ name ].push( body ); | ||
} else if ( | } else if ( kind === 'hero' ) { | ||
if ( !heroSections[ | if ( !heroSections[ name ] ) { | ||
heroSections[ | heroSections[ name ] = []; | ||
heroOrder.push( | heroOrder.push( name ); | ||
} | } | ||
heroSections[ | heroSections[ name ].push( body ); | ||
} | } | ||
} ); | } ); | ||
| Line 233: | Line 204: | ||
var out = []; | var out = []; | ||
// General | |||
general.forEach( function ( entry ) { | general.forEach( function ( entry ) { | ||
out.push( '* ' + entry ); | out.push( '* ' + entry ); | ||
} ); | } ); | ||
// Modes | |||
modeOrder.forEach( function ( mode ) { | modeOrder.forEach( function ( mode ) { | ||
| Line 247: | Line 220: | ||
} ); | } ); | ||
// Items | |||
if ( itemOrder.length ) { | if ( itemOrder.length ) { | ||
| Line 263: | Line 237: | ||
} | } | ||
// Heroes | |||
if ( heroOrder.length ) { | if ( heroOrder.length ) { | ||
| Line 279: | Line 254: | ||
} | } | ||
return | return out.join( '\n' ).trim() + '\n'; | ||
} | } | ||
| Line 293: | Line 261: | ||
var app = document.getElementById( 'patchformatter' ); | var app = document.getElementById( 'patchformatter' ); | ||
if ( !app ) return; | |||
if ( !app ) { | |||
return; | |||
} | |||
var inputWidget = new OO.ui.MultilineTextInputWidget( { | var inputWidget = new OO.ui.MultilineTextInputWidget( { | ||
| Line 350: | Line 321: | ||
); | ); | ||
outputWidget.setValue( result | outputWidget.setValue( result ); | ||
} ).catch( function ( e ) { | |||
status.setLabel( 'Error' ); | status.setLabel( 'Error' ); | ||
} ); | } ); | ||
} ); | } ); | ||
copyButton.on( 'click', function () { | copyButton.on( 'click', function () { | ||
navigator.clipboard.writeText( outputWidget.getValue() ); | |||
navigator.clipboard.writeText( | |||
outputWidget.getValue() | |||
); | |||
status.setLabel( 'Copied' ); | status.setLabel( 'Copied' ); | ||
} ); | } ); | ||
} | } | ||
mw.loader.using( [ | mw.loader.using( [ | ||
'oojs-ui-core', | 'oojs-ui-core', | ||
| Line 382: | Line 350: | ||
}() ); | }() ); | ||