User:Monster Domosed/common.js: Difference between revisionsGive feedback
No edit summary |
No edit summary |
||
| Line 20: | Line 20: | ||
// ---- API ------------------------------------------------------------ | // ---- API ------------------------------------------------------------ | ||
function getWikiData( templateName ) { | function getWikiData( templateName ) { | ||
var api = new mw.Api(); | var api = new mw.Api(); | ||
var text = L + '#invoke:MonsterDomosed/patchformatter|' + templateName + R; | var text = L + '#invoke:MonsterDomosed/patchformatter|' + templateName + R; | ||
| Line 32: | Line 30: | ||
formatversion: 2 | formatversion: 2 | ||
} ).then( function ( data ) { | } ).then( function ( data ) { | ||
var wikitext = ( data.expandtemplates && data.expandtemplates.wikitext ) || ''; | var wikitext = ( data.expandtemplates && data.expandtemplates.wikitext ) || ''; | ||
| Line 41: | Line 37: | ||
return item.trim().replace( /^'+|'+$/g, '' ); | return item.trim().replace( /^'+|'+$/g, '' ); | ||
} ) | } ) | ||
.filter( | .filter( Boolean ); | ||
} ); | } ); | ||
} | } | ||
| Line 49: | Line 43: | ||
// ---- PARSING -------------------------------------------------------- | // ---- PARSING -------------------------------------------------------- | ||
function parseEntries( rawText ) { | function parseEntries( rawText ) { | ||
var entries = []; | var entries = []; | ||
var current = null; | var current = null; | ||
| Line 67: | Line 59: | ||
if ( current !== null ) entries.push( current ); | if ( current !== null ) entries.push( current ); | ||
return entries | return entries.map( function ( e ) { | ||
return e.replace( /\s+/g, ' ' ).trim(); | |||
} ).filter( Boolean ); | |||
} | } | ||
// ---- | // ---- FORMAT --------------------------------------------------------- | ||
function escapeRegExp( s ) { | function escapeRegExp( s ) { | ||
return s.replace( /[.*+?^${}()|[\]\\]/g, '\\$&' ); | return s.replace( /[.*+?^${}()|[\]\\]/g, '\\$&' ); | ||
| Line 96: | Line 70: | ||
function formatBody( text, heroes, abilities, items ) { | function formatBody( text, heroes, abilities, items ) { | ||
var protectedTokens = []; | var protectedTokens = []; | ||
| Line 105: | Line 80: | ||
function wrap( terms, template ) { | function wrap( terms, template ) { | ||
terms.slice() | 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', term ) | |||
); | |||
} ); | } ); | ||
} ); | |||
} | } | ||
| Line 139: | Line 112: | ||
// ---- OUTPUT --------------------------------------------------------- | // ---- OUTPUT --------------------------------------------------------- | ||
function buildOutput( entries, heroes, abilities, items | function buildOutput( entries, heroes, abilities, items ) { | ||
var | var out = []; | ||
entries.forEach( function ( entry ) { | entries.forEach( function ( entry ) { | ||
out.push( '* ' + formatBody( entry, heroes, abilities, items ) ); | |||
} ); | } ); | ||
| Line 162: | Line 125: | ||
// ---- INIT ----------------------------------------------------------- | // ---- INIT ----------------------------------------------------------- | ||
function init() { | function init() { | ||
console.log('[PatchFormatter] init | |||
console.log('[PatchFormatter] init'); | |||
var app = document.getElementById( 'patchformatter' ); | var app = document.getElementById( 'patchformatter' ); | ||
if ( !app ) { | if ( !app ) { | ||
console.error('[PatchFormatter] container | console.error('[PatchFormatter] container missing'); | ||
return; | return; | ||
} | } | ||
// --- INPUT (OOUI) | |||
var inputWidget = new OO.ui.MultilineTextInputWidget( { | var inputWidget = new OO.ui.MultilineTextInputWidget( { | ||
rows: 14, | rows: 14, | ||
| Line 185: | Line 141: | ||
} ); | } ); | ||
// --- OUTPUT (OOUI) | |||
var outputWidget = new OO.ui.MultilineTextInputWidget( { | var outputWidget = new OO.ui.MultilineTextInputWidget( { | ||
rows: 20, | rows: 20, | ||
| Line 190: | Line 147: | ||
} ); | } ); | ||
// --- BUTTONS | |||
var formatButton = new OO.ui.ButtonWidget( { | var formatButton = new OO.ui.ButtonWidget( { | ||
label: 'Format', | label: 'Format', | ||
| Line 196: | Line 154: | ||
var copyButton = new OO.ui.ButtonWidget( { | var copyButton = new OO.ui.ButtonWidget( { | ||
label: 'Copy' | label: 'Copy output' | ||
} ); | } ); | ||
var status = new OO.ui.LabelWidget(); | var status = new OO.ui.LabelWidget(); | ||
var | // --- UI CONTAINER (simple safe DOM, no broken layouts) | ||
var container = document.createElement('div'); | |||
container.appendChild( inputWidget.$element[ 0 ] ); | |||
container.appendChild( formatButton.$element[ 0 ] ); | |||
container.appendChild( copyButton.$element[ 0 ] ); | |||
container.appendChild( status.$element[ 0 ] ); | |||
container.appendChild( outputWidget.$element[ 0 ] ); | |||
app.appendChild( | app.appendChild( container ); | ||
// ---- EVENTS ----------------------------------------------------- | |||
formatButton.on( 'click', function () { | formatButton.on( 'click', function () { | ||
console.log('[PatchFormatter] | |||
console.log('[PatchFormatter] click format'); | |||
status.setLabel( 'Loading...' ); | status.setLabel( 'Loading...' ); | ||
| Line 225: | Line 183: | ||
getWikiData( 'get_all_released_abilities_list' ) | getWikiData( 'get_all_released_abilities_list' ) | ||
] ).then( function ( lists ) { | ] ).then( function ( lists ) { | ||
var heroes = lists[ 0 ]; | var heroes = lists[ 0 ]; | ||
| Line 232: | Line 188: | ||
var abilities = lists[ 2 ]; | var abilities = lists[ 2 ]; | ||
var | // IMPORTANT FIX: OOUI INPUT VALUE | ||
var raw = inputWidget.getValue(); | |||
console.log('[PatchFormatter] input length:', raw.length); | |||
var result = buildOutput( entries, heroes, abilities, items | var entries = parseEntries( raw ); | ||
console.log('[PatchFormatter] entries:', entries.length); | |||
var result = buildOutput( entries, heroes, abilities, items ); | |||
outputWidget.setValue( result ); | outputWidget.setValue( result ); | ||
status.setLabel( 'Done: ' + entries.length ); | status.setLabel( 'Done: ' + entries.length ); | ||
} ).catch( function ( | |||
console.error('[PatchFormatter] ERROR', | } ).catch( function ( err ) { | ||
console.error('[PatchFormatter] ERROR', err); | |||
status.setLabel( 'Error' ); | status.setLabel( 'Error' ); | ||
} ); | } ); | ||
} ); | } ); | ||
copyButton.on( 'click', function () { | copyButton.on( 'click', function () { | ||
navigator.clipboard.writeText( | navigator.clipboard.writeText( | ||
| Line 257: | Line 222: | ||
// ---- BOOT ----------------------------------------------------------- | // ---- BOOT ----------------------------------------------------------- | ||
mw.loader.using( [ | mw.loader.using( [ | ||
'oojs-ui-core', | 'oojs-ui-core', | ||
| Line 265: | Line 228: | ||
'mediawiki.util' | 'mediawiki.util' | ||
] ).then( function () { | ] ).then( function () { | ||
console.log('[PatchFormatter] loader OK'); | console.log('[PatchFormatter] loader OK'); | ||
| Line 271: | Line 234: | ||
} ).catch( function ( e ) { | } ).catch( function ( e ) { | ||
console.error('[PatchFormatter] LOADER FAILED', e); | console.error('[PatchFormatter] LOADER FAILED', e); | ||
} ); | } ); | ||