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

No edit summary
No edit summary
Line 20: Line 20:
// ---- API ------------------------------------------------------------
// ---- API ------------------------------------------------------------
function getWikiData( templateName ) {
function getWikiData( templateName ) {
console.log('[PatchFormatter] 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 ) {
console.log('[PatchFormatter] API response:', templateName);


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( function ( item ) {
.filter( Boolean );
return item.length > 0;
} );
} );
} );
}
}
Line 49: Line 43:
// ---- PARSING --------------------------------------------------------
// ---- PARSING --------------------------------------------------------
function parseEntries( rawText ) {
function parseEntries( rawText ) {
console.log('[PatchFormatter] parseEntries');
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 ) {
.map( function ( e ) {
return e.replace( /\s+/g, ' ' ).trim();
return e.replace( /\s+/g, ' ' ).trim();
} ).filter( Boolean );
} )
.filter( Boolean );
}
}


// ---- CLASSIFY -------------------------------------------------------
// ---- FORMAT ---------------------------------------------------------
function classify( entry, heroes, items, gameModes ) {
var idx = entry.indexOf( ':' );
 
if ( idx !== -1 ) {
var prefix = entry.slice( 0, idx ).trim();
var rest = entry.slice( idx + 1 ).trim();
 
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 ];
}
 
return [ 'general', null, entry ];
}
 
// ---- FORMAT BODY ----------------------------------------------------
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 ) {
.sort( function ( a, b ) { return b.length - a.length; } )
return b.length - a.length;
.forEach( function ( term ) {
} ).forEach( function ( term ) {


var pattern = new RegExp( '(?<!\\w)' + escapeRegExp( term ) + '(?!\\w)', 'g' );
var pattern = new RegExp( '(?<!\\w)' + escapeRegExp( term ) + '(?!\\w)', 'g' );


text = text.replace( pattern, function () {
text = text.replace( pattern, function () {
return protect(
return protect(
template.replace( '%s', function () {
template.replace( '%s', term )
return term;
);
} )
);
} );
} );
} );
} );
}
}


Line 139: Line 112:


// ---- OUTPUT ---------------------------------------------------------
// ---- OUTPUT ---------------------------------------------------------
function buildOutput( entries, heroes, abilities, items, gameModes ) {
function buildOutput( entries, heroes, abilities, items ) {
console.log('[PatchFormatter] buildOutput');


var general = [];
var out = [];


entries.forEach( function ( entry ) {
entries.forEach( function ( entry ) {
var c = classify( entry, heroes, items, gameModes );
out.push( '* ' + formatBody( entry, heroes, abilities, items ) );
var body = formatBody( c[ 2 ], heroes, abilities, items );
 
if ( c[ 0 ] === 'general' ) {
general.push( body );
}
} );
 
var out = general.map( function ( e ) {
return '* ' + e;
} );
} );


Line 162: Line 125:
// ---- INIT -----------------------------------------------------------
// ---- INIT -----------------------------------------------------------
function init() {
function init() {
console.log('[PatchFormatter] init START');
 
console.log('[PatchFormatter] init');


var app = document.getElementById( 'patchformatter' );
var app = document.getElementById( 'patchformatter' );


if ( !app ) {
if ( !app ) {
console.error('[PatchFormatter] container not found');
console.error('[PatchFormatter] container missing');
return;
return;
}
}


console.log('[PatchFormatter] container OK');
// --- INPUT (OOUI)
 
if ( !window.OO || !OO.ui ) {
console.error('[PatchFormatter] OOUI NOT AVAILABLE');
return;
}
 
console.log('[PatchFormatter] OOUI OK');
 
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 layout = new OO.ui.FieldsetLayout();
// --- UI CONTAINER (simple safe DOM, no broken layouts)
var container = document.createElement('div');


layout.$element.append(
container.appendChild( inputWidget.$element[ 0 ] );
inputWidget.$element,
container.appendChild( formatButton.$element[ 0 ] );
formatButton.$element,
container.appendChild( copyButton.$element[ 0 ] );
copyButton.$element,
container.appendChild( status.$element[ 0 ] );
status.$element,
container.appendChild( outputWidget.$element[ 0 ] );
outputWidget.$element
);


app.appendChild( layout.$element[ 0 ] );
app.appendChild( container );


console.log('[PatchFormatter] UI READY');
// ---- EVENTS -----------------------------------------------------


formatButton.on( 'click', function () {
formatButton.on( 'click', function () {
console.log('[PatchFormatter] FORMAT CLICK');
 
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 ) {
console.log('[PatchFormatter] DATA READY');


var heroes = lists[ 0 ];
var heroes = lists[ 0 ];
Line 232: Line 188:
var abilities = lists[ 2 ];
var abilities = lists[ 2 ];


var entries = parseEntries( inputWidget.getValue() );
// IMPORTANT FIX: OOUI INPUT VALUE
var raw = inputWidget.getValue();
 
console.log('[PatchFormatter] input length:', raw.length);


var result = buildOutput( entries, heroes, abilities, items, GAME_MODE_SECTIONS );
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 ( e ) {
 
console.error('[PatchFormatter] ERROR', e);
} ).catch( function ( err ) {
 
console.error('[PatchFormatter] ERROR', err);
status.setLabel( 'Error' );
status.setLabel( 'Error' );
} );
} );
} );
} );


copyButton.on( 'click', function () {
copyButton.on( 'click', function () {
console.log('[PatchFormatter] COPY CLICK');


navigator.clipboard.writeText(
navigator.clipboard.writeText(
Line 257: Line 222:


// ---- BOOT -----------------------------------------------------------
// ---- BOOT -----------------------------------------------------------
console.log('[PatchFormatter] loader start');
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);
} );
} );