Convert ext.wikiEditor module to packageFiles

This allows us to bundle the wgWikiEditorMagicWords and
mw.msg.wikiEditor(?!) config vars that were previously exported globally
in the startup module.

This code is old and crufty, so this is a somewhat minimal conversion:
* Use require() for jquery.wikiEditor.{dialogs,toolbar}.config.js,
  and for jquery.wikiEditor.{dialogs,toolbar}.js
* Don't attempt to convert jquery.wikiEditor.js to something
  require-based, instead just run it and let it set the $.wikiEditor and
  $.fn.wikiEditor globals
* Consolidate ext.wikiEditor.{dialogs,toolbars}.js into
   ext.wikiEditor.js

Bug: T222828
Change-Id: Ia75d685cbde786e8fceb6db36f2436b2beea1499
This commit is contained in:
Roan Kattouw 2019-07-20 01:09:04 -07:00 committed by Krinkle
parent 2075d6d2dc
commit d23d096281
10 changed files with 717 additions and 698 deletions

View file

@ -27,9 +27,6 @@
"GetPreferences": [
"WikiEditorHooks::getPreferences"
],
"ResourceLoaderGetConfigVars": [
"WikiEditorHooks::resourceLoaderGetConfigVars"
],
"EditPageBeforeEditToolbar": [
"WikiEditorHooks::EditPageBeforeEditToolbar"
],
@ -46,15 +43,18 @@
"ResourceModules": {
"ext.wikiEditor": {
"group": "ext.wikiEditor",
"scripts": [
"packageFiles": [
"ext.wikiEditor.js",
"jquery.wikiEditor.js",
"jquery.wikiEditor.toolbar.js",
"jquery.wikiEditor.toolbar.config.js",
"jquery.wikiEditor.dialogs.js",
"jquery.wikiEditor.dialogs.config.js",
"ext.wikiEditor.js",
"ext.wikiEditor.toolbar.js",
"ext.wikiEditor.dialogs.js"
{
"name": "data.json",
"callback": "WikiEditorHooks::getModuleData",
"versionCallback": "WikiEditorHooks::getModuleDataSummary"
}
],
"styles": [
"jquery.wikiEditor.less",

View file

@ -185,20 +185,39 @@ class WikiEditorHooks {
}
/**
* @param array &$vars
* @param ResourceLoaderContext $context
* @param Config $config
* @return array
*/
public static function resourceLoaderGetConfigVars( &$vars ) {
// expose magic words for use by the wikieditor toolbar
self::getMagicWords( $vars );
public static function getModuleData( ResourceLoaderContext $context, Config $config ) {
return [
// expose magic words for use by the wikieditor toolbar
'magicWords' => self::getMagicWords(),
'signature' => self::getSignatureMessage( $context )
];
}
$vars['mw.msg.wikieditor'] = wfMessage( 'sig-text', '~~~~' )->inContentLanguage()->text();
/**
* @param ResourceLoaderContext $context
* @param Config $config
* @return array
*/
public static function getModuleDataSummary( ResourceLoaderContext $context, Config $config ) {
return [
'magicWords' => self::getMagicWords(),
'signature' => self::getSignatureMessage( $context, true )
];
}
private static function getSignatureMessage( MessageLocalizer $ml, $raw = false ) {
$msg = $ml->msg( 'sig-text' )->params( '~~~~' )->inContentLanguage();
return $raw ? $msg->plain() : $msg->text();
}
/**
* Expose useful magic words which are used by the wikieditor toolbar
* @param array &$vars
*/
private static function getMagicWords( &$vars ) {
private static function getMagicWords() {
$requiredMagicWords = [
'redirect',
'img_right',
@ -220,7 +239,7 @@ class WikiEditorHooks {
$magicWords[$name] = MagicWord::get( $name )->getSynonym( 0 );
}
}
$vars['wgWikiEditorMagicWords'] = $magicWords;
return $magicWords;
}
/**

View file

@ -1,10 +0,0 @@
/*
* JavaScript for WikiEditor Dialogs
*/
$( function () {
// Replace icons
$.wikiEditor.modules.dialogs.config.replaceIcons( $( '#wpTextbox1' ) );
// Add dialogs module
$( '#wpTextbox1' ).wikiEditor( 'addModule', $.wikiEditor.modules.dialogs.config.getDefaultConfig() );
} );

View file

@ -12,6 +12,9 @@
},
trackdebug = !!mw.util.getParamValue( 'trackdebug' );
// This sets $.wikiEditor and $.fn.wikiEditor
require( './jquery.wikiEditor.js' );
function log() {
// mw.log is a no-op unless resource loader is in debug mode, so
// this allows trackdebug to work independently (T211698)
@ -127,7 +130,7 @@
var $textarea = $( '#wpTextbox1' ),
$editingSessionIdInput = $( '#editingStatsId' ),
origText = $textarea.val(),
submitting, onUnloadFallback;
submitting, onUnloadFallback, dialogsConfig;
if ( $editingSessionIdInput.length ) {
editingSessionId = $editingSessionIdInput.val();
@ -189,5 +192,19 @@
logAbort( true, unmodified );
} );
}
// The old toolbar is still in place and needs to be removed so there aren't two toolbars
$( '#toolbar' ).remove();
// Add toolbar module
// TODO: Implement .wikiEditor( 'remove' )
$textarea.wikiEditor(
'addModule', require( './jquery.wikiEditor.toolbar.config.js' )
);
dialogsConfig = require( './jquery.wikiEditor.dialogs.config.js' );
// Replace icons
dialogsConfig.replaceIcons( $textarea );
// Add dialogs module
$textarea.wikiEditor( 'addModule', dialogsConfig.getDefaultConfig() );
} );
}() );

View file

@ -1,12 +0,0 @@
/*
* JavaScript for WikiEditor Toolbar
*/
$( function () {
// The old toolbar is still in place and needs to be removed so there aren't two toolbars
$( '#toolbar' ).remove();
// Add toolbar module
// TODO: Implement .wikiEditor( 'remove' )
$( '#wpTextbox1' ).wikiEditor(
'addModule', $.wikiEditor.modules.toolbar.config.getDefaultConfig()
);
} );

View file

@ -3,9 +3,11 @@
*/
( function () {
var hasOwn = Object.prototype.hasOwnProperty;
var hasOwn = Object.prototype.hasOwnProperty,
toolbarModule = require( './jquery.wikiEditor.toolbar.js' ),
configData = require( './data.json' );
$.wikiEditor.modules.dialogs.config = {
module.exports = {
replaceIcons: function ( $textarea ) {
$textarea
@ -428,7 +430,7 @@
insertText = whitespace[ 0 ] + insertText + whitespace[ 1 ];
}
$( this ).dialog( 'close' );
$.wikiEditor.modules.toolbar.fn.doAction( $( this ).data( 'context' ), {
toolbarModule.fn.doAction( $( this ).data( 'context' ), {
type: 'replace',
options: {
pre: insertText
@ -539,7 +541,7 @@
id: 'wikieditor-toolbar-file-dialog',
htmlTemplate: 'dialogInsertFile.html',
init: function () {
var magicWordsI18N = mw.config.get( 'wgWikiEditorMagicWords' ),
var magicWordsI18N = configData.magicWords,
defaultMsg = mw.msg( 'wikieditor-toolbar-file-default' );
$( this ).find( '[data-i18n-magic]' )
.text( function () {
@ -565,7 +567,7 @@
var fileName, caption, fileFloat, fileFormat, fileSize, whitespace,
fileTitle, options, fileUse,
hasPxRgx = /.+px$/,
magicWordsI18N = mw.config.get( 'wgWikiEditorMagicWords' );
magicWordsI18N = configData.magicWords;
fileName = $( '#wikieditor-toolbar-file-target' ).val();
caption = $( '#wikieditor-toolbar-file-caption' ).val();
fileFloat = $( '#wikieditor-toolbar-file-float' ).val();
@ -594,7 +596,7 @@
}
fileUse = options.length === 0 ? fileName : ( fileName + '|' + options.join( '|' ) );
$( this ).dialog( 'close' );
$.wikiEditor.modules.toolbar.fn.doAction(
toolbarModule.fn.doAction(
$( this ).data( 'context' ),
{
type: 'replace',
@ -863,7 +865,7 @@
}
classStr = classes.length > 0 ? ' class="' + classes.join( ' ' ) + '"' : '';
$( this ).dialog( 'close' );
$.wikiEditor.modules.toolbar.fn.doAction(
toolbarModule.fn.doAction(
$( this ).data( 'context' ),
{
type: 'replace',

View file

@ -3,22 +3,22 @@
*/
( function () {
$.wikiEditor.modules.dialogs = {
var dialogsModule = {
/**
* API accessible functions
*/
api: {
addDialog: function ( context, data ) {
$.wikiEditor.modules.dialogs.fn.create( context, data );
dialogsModule.fn.create( context, data );
},
openDialog: function ( context, module ) {
var mod, $dialog;
if ( module in $.wikiEditor.modules.dialogs.modules ) {
mod = $.wikiEditor.modules.dialogs.modules[ module ];
if ( module in dialogsModule.modules ) {
mod = dialogsModule.modules[ module ];
$dialog = $( '#' + mod.id );
if ( $dialog.length === 0 ) {
$.wikiEditor.modules.dialogs.fn.reallyCreate( context, mod, module );
dialogsModule.fn.reallyCreate( context, mod, module );
$dialog = $( '#' + mod.id );
}
@ -31,8 +31,8 @@
}
},
closeDialog: function ( context, module ) {
if ( module in $.wikiEditor.modules.dialogs.modules ) {
$( '#' + $.wikiEditor.modules.dialogs.modules[ module ].id ).dialog( 'close' );
if ( module in dialogsModule.modules ) {
$( '#' + dialogsModule.modules[ module ].id ).dialog( 'close' );
}
}
},
@ -71,11 +71,11 @@
// Re-select from the DOM, we might have removed the dialog just now
$existingDialog = $( '#' + module.id );
if ( !filtered && $existingDialog.length === 0 ) {
$.wikiEditor.modules.dialogs.modules[ mod ] = module;
dialogsModule.modules[ mod ] = module;
context.$textarea.trigger( 'wikiEditor-dialogs-setup-' + mod );
// If this dialog requires immediate creation, create it now
if ( typeof module.immediateCreate !== 'undefined' && module.immediateCreate ) {
$.wikiEditor.modules.dialogs.fn.reallyCreate( context, module, mod );
dialogsModule.fn.reallyCreate( context, module, mod );
}
}
}
@ -86,7 +86,7 @@
*
* @param {Object} context Context object of editor dialog belongs to
* @param {Object} module Dialog module object
* @param {string} name Dialog name (key in $.wikiEditor.modules.dialogs.modules)
* @param {string} name Dialog name (key in dialogsModule.modules)
*/
reallyCreate: function ( context, module, name ) {
var msg, $dialogDiv, $content,
@ -123,14 +123,14 @@
.each( module.init )
.dialog( configuration );
// Set tabindexes on buttons added by .dialog()
$.wikiEditor.modules.dialogs.fn.setTabindexes( $dialogDiv.closest( '.ui-dialog' )
dialogsModule.fn.setTabindexes( $dialogDiv.closest( '.ui-dialog' )
.find( 'button' ).not( '[tabindex]' ) );
if ( !( 'resizeme' in module ) || module.resizeme ) {
$dialogDiv
.on( 'dialogopen', $.wikiEditor.modules.dialogs.fn.resize )
.on( 'dialogopen', dialogsModule.fn.resize )
.find( '.ui-tabs' ).on( 'tabsshow', function () {
$( this ).closest( '.ui-dialog-content' ).each(
$.wikiEditor.modules.dialogs.fn.resize );
dialogsModule.fn.resize );
} );
}
$dialogDiv.on( 'dialogclose', function () {
@ -212,4 +212,6 @@
};
module.exports = dialogsModule;
}() );

View file

@ -39,7 +39,10 @@
* module name. The existence of a module in this object only indicates the module is available. To check if a
* module is in use by a specific context check the context.modules object.
*/
modules: {},
modules: {
toolbar: require( './jquery.wikiEditor.toolbar.js' ),
dialogs: require( './jquery.wikiEditor.dialogs.js' )
},
/**
* A context can be extended, such as adding iframe support, on a per-wikiEditor instance basis.

File diff suppressed because it is too large Load diff

View file

@ -3,7 +3,7 @@
*/
( function () {
$.wikiEditor.modules.toolbar = {
var toolbarModule = {
/**
* API accessible functions
@ -23,7 +23,7 @@
if ( section === 'main' ) {
// Section
context.modules.toolbar.$toolbar.prepend(
$.wikiEditor.modules.toolbar.fn.buildSection(
toolbarModule.fn.buildSection(
context, section, data[ type ][ section ]
)
);
@ -31,11 +31,11 @@
}
// Section
$sections.append(
$.wikiEditor.modules.toolbar.fn.buildSection( context, section, data[ type ][ section ] )
toolbarModule.fn.buildSection( context, section, data[ type ][ section ] )
);
// Tab
$tabs.append(
$.wikiEditor.modules.toolbar.fn.buildTab( context, section, data[ type ][ section ] )
toolbarModule.fn.buildTab( context, section, data[ type ][ section ] )
);
}
break;
@ -47,7 +47,7 @@
for ( group in data[ type ] ) {
// Group
$section.append(
$.wikiEditor.modules.toolbar.fn.buildGroup( context, group, data[ type ][ group ] )
toolbarModule.fn.buildGroup( context, group, data[ type ][ group ] )
);
}
break;
@ -61,7 +61,7 @@
);
for ( tool in data[ type ] ) {
// Tool
$group.append( $.wikiEditor.modules.toolbar.fn.buildTool( context, tool, data[ type ][ tool ] ) );
$group.append( toolbarModule.fn.buildTool( context, tool, data[ type ][ tool ] ) );
}
if ( $group.children().length ) {
$group.removeClass( 'empty' );
@ -79,13 +79,13 @@
);
for ( page in data[ type ] ) {
// Page
$pages.append( $.wikiEditor.modules.toolbar.fn.buildPage( context, page, data[ type ][ page ] ) );
$pages.append( toolbarModule.fn.buildPage( context, page, data[ type ][ page ] ) );
// Index
$index.append(
$.wikiEditor.modules.toolbar.fn.buildBookmark( context, page, data[ type ][ page ] )
toolbarModule.fn.buildBookmark( context, page, data[ type ][ page ] )
);
}
$.wikiEditor.modules.toolbar.fn.updateBookletSelection( context, data.section, $pages, $index );
toolbarModule.fn.updateBookletSelection( context, data.section, $pages, $index );
break;
case 'rows':
if ( !( 'section' in data && 'page' in data ) ) {
@ -97,7 +97,7 @@
);
for ( i = 0; i < data.rows.length; i++ ) {
// Row
$table.append( $.wikiEditor.modules.toolbar.fn.buildRow( context, data.rows[ i ] ) );
$table.append( toolbarModule.fn.buildRow( context, data.rows[ i ] ) );
}
break;
case 'characters':
@ -112,14 +112,14 @@
for ( i = 0; i < data.characters.length; i++ ) {
// Character
$characters.append(
$( $.wikiEditor.modules.toolbar.fn.buildCharacter( data.characters[ i ], actions ) )
$( toolbarModule.fn.buildCharacter( data.characters[ i ], actions ) )
.on( 'mousedown', function ( e ) {
// No dragging!
e.preventDefault();
return false;
} )
.on( 'click', function ( e ) {
$.wikiEditor.modules.toolbar.fn.doAction( $( this ).parent().data( 'context' ),
toolbarModule.fn.doAction( $( this ).parent().data( 'context' ),
$( this ).parent().data( 'actions' )[ $( this ).attr( 'rel' ) ] );
e.preventDefault();
return false;
@ -160,7 +160,7 @@
} else {
// Just a page, remove the index too!
context.modules.toolbar.$toolbar.find( index ).remove();
$.wikiEditor.modules.toolbar.fn.updateBookletSelection(
toolbarModule.fn.updateBookletSelection(
context,
data.section,
context.modules.toolbar.$toolbar.find( target ),
@ -200,7 +200,7 @@
context.modules.toolbar.$toolbar = $( '<div>' )
.addClass( 'wikiEditor-ui-toolbar' )
.attr( 'id', 'wikiEditor-ui-toolbar' );
$.wikiEditor.modules.toolbar.fn.build( context, config );
toolbarModule.fn.build( context, config );
context.$ui.find( '.wikiEditor-ui-top' ).append( context.modules.toolbar.$toolbar );
},
/**
@ -262,7 +262,7 @@
empty = true;
if ( 'tools' in group ) {
for ( tool in group.tools ) {
tool = $.wikiEditor.modules.toolbar.fn.buildTool( context, tool, group.tools[ tool ] );
tool = toolbarModule.fn.buildTool( context, tool, group.tools[ tool ] );
if ( tool ) {
// Consider a group with only hidden tools empty as well
// .is( ':visible' ) always returns false because tool is not attached to the DOM yet
@ -342,13 +342,13 @@
} );
if ( $button.data( 'ooui' ) ) {
$button.data( 'ooui' ).on( 'click', function () {
$.wikiEditor.modules.toolbar.fn.doAction(
toolbarModule.fn.doAction(
context, tool.action
);
} );
} else {
$button.on( 'click', function ( e ) {
$.wikiEditor.modules.toolbar.fn.doAction(
toolbarModule.fn.doAction(
context, tool.action
);
e.preventDefault();
@ -374,7 +374,7 @@
return false;
} )
.on( 'click', function ( e ) {
$.wikiEditor.modules.toolbar.fn.doAction(
toolbarModule.fn.doAction(
$( this ).data( 'context' ), $( this ).data( 'action' ), $( this )
);
// Hide the dropdown
@ -445,10 +445,10 @@
} );
if ( deferLoad ) {
$page.one( 'loadPage', function () {
$.wikiEditor.modules.toolbar.fn.reallyBuildPage( context, id, page, $page );
toolbarModule.fn.reallyBuildPage( context, id, page, $page );
} );
} else {
$.wikiEditor.modules.toolbar.fn.reallyBuildPage( context, id, page, $page );
toolbarModule.fn.reallyBuildPage( context, id, page, $page );
}
return $page;
},
@ -460,11 +460,11 @@
html =
'<table class="table-' + id + '">';
if ( 'headings' in page ) {
html += $.wikiEditor.modules.toolbar.fn.buildHeading( context, page.headings );
html += toolbarModule.fn.buildHeading( context, page.headings );
}
if ( 'rows' in page ) {
for ( i = 0; i < page.rows.length; i++ ) {
html += $.wikiEditor.modules.toolbar.fn.buildRow( context, page.rows[ i ] );
html += toolbarModule.fn.buildRow( context, page.rows[ i ] );
}
}
$page.html( html + '</table>' );
@ -487,7 +487,7 @@
if ( 'characters' in page ) {
html = '';
for ( i = 0; i < page.characters.length; i++ ) {
html += $.wikiEditor.modules.toolbar.fn.buildCharacter( page.characters[ i ], actions );
html += toolbarModule.fn.buildCharacter( page.characters[ i ], actions );
}
$characters
.html( html )
@ -498,7 +498,7 @@
return false;
} )
.on( 'click', function ( e ) {
$.wikiEditor.modules.toolbar.fn.doAction(
toolbarModule.fn.doAction(
$( this ).parent().data( 'context' ),
$( this ).parent().data( 'actions' )[ $( this ).attr( 'rel' ) ],
$( this )
@ -650,7 +650,7 @@
selected = $.cookie( 'wikiEditor-' + context.instance + '-toolbar-section' );
show = selected === id;
$.wikiEditor.modules.toolbar.fn.reallyBuildSection( context, id, section, $section, section.deferLoad );
toolbarModule.fn.reallyBuildSection( context, id, section, $section, section.deferLoad );
// Show or hide section
if ( id !== 'main' ) {
@ -672,7 +672,7 @@
if ( 'groups' in section ) {
for ( group in section.groups ) {
$section.append(
$.wikiEditor.modules.toolbar.fn.buildGroup( context, group, section.groups[ group ] )
toolbarModule.fn.buildGroup( context, group, section.groups[ group ] )
);
}
}
@ -683,15 +683,15 @@
if ( 'pages' in section ) {
for ( page in section.pages ) {
$pages.append(
$.wikiEditor.modules.toolbar.fn.buildPage( context, page, section.pages[ page ], deferLoad )
toolbarModule.fn.buildPage( context, page, section.pages[ page ], deferLoad )
);
$index.append(
$.wikiEditor.modules.toolbar.fn.buildBookmark( context, page, section.pages[ page ] )
toolbarModule.fn.buildBookmark( context, page, section.pages[ page ] )
);
}
}
$section.append( $index ).append( $pages );
$.wikiEditor.modules.toolbar.fn.updateBookletSelection( context, id, $pages, $index );
toolbarModule.fn.updateBookletSelection( context, id, $pages, $index );
break;
}
},
@ -721,11 +721,11 @@
for ( section in config ) {
if ( section === 'main' ) {
context.modules.toolbar.$toolbar.prepend(
$.wikiEditor.modules.toolbar.fn.buildSection( context, section, config[ section ] )
toolbarModule.fn.buildSection( context, section, config[ section ] )
);
} else {
$sections.append( $.wikiEditor.modules.toolbar.fn.buildSection( context, section, config[ section ] ) );
$tabs.append( $.wikiEditor.modules.toolbar.fn.buildTab( context, section, config[ section ] ) );
$sections.append( toolbarModule.fn.buildSection( context, section, config[ section ] ) );
$tabs.append( toolbarModule.fn.buildTab( context, section, config[ section ] ) );
}
}
setTimeout( function () {
@ -736,4 +736,6 @@
};
module.exports = toolbarModule;
}() );