mediawiki-extensions-Visual.../modules/ve-mw/ui/ve.ui.MWWikitextCommandRegistry.js
James D. Forrester a05f9e4bf4 Update VE core submodule to master (0b17a137b)
New changes:
d4cb2f03e Update OOUI to v0.34.0
b0b481693 jsduck: Stop listing OOjs in upstream, it's using JSDoc now
0b17a137b Update OOjs to v3.0.0

Change-Id: If29a97ce689523763431face5a13929a461735b4
2019-09-05 13:03:41 -07:00

242 lines
7.5 KiB
JavaScript

/*!
* VisualEditor Wikitext command registry
*
* @copyright 2011-2019 VisualEditor Team and others; see http://ve.mit-license.org
*/
/**
* Command registry.
*
* @class
* @extends ve.ui.CommandRegistry
* @constructor
* @param {ve.ui.CommandRegistry} fallbackRegistry Fallback registry
*/
ve.ui.MWWikitextCommandRegistry = function VeUiMwWikitextCommandRegistry( fallbackRegistry ) {
// Parent constructor
ve.ui.MWWikitextCommandRegistry.super.apply( this, arguments );
this.fallbackRegistry = fallbackRegistry;
};
/* Inheritance */
OO.inheritClass( ve.ui.MWWikitextCommandRegistry, ve.ui.CommandRegistry );
/* Methods */
/**
* Get data for a given symbolic name.
*
* See https://doc.wikimedia.org/oojs/master/OO.Registry.html
*
* @param {string} name Symbolic name
* @return {Mixed|undefined} Data associated with symbolic name
*/
ve.ui.MWWikitextCommandRegistry.prototype.lookup = function ( name ) {
// Parent method
var data = ve.ui.MWWikitextCommandRegistry.super.prototype.lookup.call( this, name );
if ( data !== undefined ) {
return data;
}
// Fall through to original command registry if not overridden.
return this.fallbackRegistry.lookup( name );
};
/**
* @inheritdoc
*/
ve.ui.MWWikitextCommandRegistry.prototype.getNames = function () {
return OO.simpleArrayUnion(
// Parent method
ve.ui.MWWikitextCommandRegistry.super.prototype.getNames.call( this ),
this.fallbackRegistry.getNames()
);
};
/* Initialization */
ve.ui.wikitextCommandRegistry = new ve.ui.MWWikitextCommandRegistry( ve.ui.commandRegistry );
/* Registrations */
ve.ui.wikitextCommandRegistry.register(
new ve.ui.Command(
'bold', 'mwWikitext', 'toggleWrapSelection',
{ args: [ '\'\'\'', '\'\'\'', OO.ui.deferMsg( 'visualeditor-annotationbutton-bold-tooltip' ) ], supportedSelections: [ 'linear' ] }
)
);
ve.ui.wikitextCommandRegistry.register(
new ve.ui.Command(
'italic', 'mwWikitext', 'toggleWrapSelection',
{
args: [
'\'\'', '\'\'',
OO.ui.deferMsg( 'visualeditor-annotationbutton-italic-tooltip' ),
/* expandOffsetsCallback */
function ( textBefore, textAfter ) {
var matches, lengthBefore, lengthAfter;
if ( ( matches = textBefore.match( /('+)$/ ) ) ) {
lengthBefore = -matches[ 1 ].length;
}
if ( ( matches = textAfter.match( /^('+)/ ) ) ) {
lengthAfter = matches[ 1 ].length;
}
return lengthBefore || lengthAfter ? [ lengthBefore, lengthAfter ] : null;
},
/* unwrapOffsetsCallback */
function ( text ) {
/* Text is only italic if there are 2 or 5+ apostrophes */
var matches = text.match( /^(''([^'].*|.*[^'])''|'{5,}([^'].*|.*[^'])'{5,})$/ );
return matches ? [ 2, 2 ] : null;
}
],
supportedSelections: [ 'linear' ] }
)
);
ve.ui.wikitextCommandRegistry.register(
new ve.ui.Command(
'code', 'mwWikitext', 'toggleWrapSelection',
{ args: [ '<code>', '</code>', OO.ui.deferMsg( 'visualeditor-annotationbutton-code-tooltip' ) ], supportedSelections: [ 'linear' ] }
)
);
ve.ui.wikitextCommandRegistry.register(
new ve.ui.Command(
'strikethrough', 'mwWikitext', 'toggleWrapSelection',
{ args: [ '<s>', '</s>', OO.ui.deferMsg( 'visualeditor-annotationbutton-strikethrough-tooltip' ) ], supportedSelections: [ 'linear' ] }
)
);
ve.ui.wikitextCommandRegistry.register(
new ve.ui.Command(
'underline', 'mwWikitext', 'toggleWrapSelection',
{ args: [ '<u>', '</u>', OO.ui.deferMsg( 'visualeditor-annotationbutton-underline-tooltip' ) ], supportedSelections: [ 'linear' ] }
)
);
ve.ui.wikitextCommandRegistry.register(
new ve.ui.Command(
'subscript', 'mwWikitext', 'toggleWrapSelection',
{ args: [ '<sub>', '</sub>', OO.ui.deferMsg( 'visualeditor-annotationbutton-subscript-tooltip' ) ], supportedSelections: [ 'linear' ] }
)
);
ve.ui.wikitextCommandRegistry.register(
new ve.ui.Command(
'superscript', 'mwWikitext', 'toggleWrapSelection',
{ args: [ '<sup>', '</sup>', OO.ui.deferMsg( 'visualeditor-annotationbutton-superscript-tooltip' ) ], supportedSelections: [ 'linear' ] }
)
);
ve.ui.wikitextCommandRegistry.register(
new ve.ui.Command(
'big', 'mwWikitext', 'toggleWrapSelection',
{ args: [ '<big>', '</big>', OO.ui.deferMsg( 'visualeditor-annotationbutton-big-tooltip' ) ], supportedSelections: [ 'linear' ] }
)
);
ve.ui.wikitextCommandRegistry.register(
new ve.ui.Command(
'small', 'mwWikitext', 'toggleWrapSelection',
{ args: [ '<small>', '</small>', OO.ui.deferMsg( 'visualeditor-annotationbutton-small-tooltip' ) ], supportedSelections: [ 'linear' ] }
)
);
( function () {
var i, heading = '';
function unformat( text ) {
/* Use lazy .+? in the middle so whitespace is matched to wrappers */
var headings, pre;
if ( ( headings = text.match( /^((={1,6})\s*).+?(\s*\2\s*)$/ ) ) ) {
return [ headings[ 1 ].length, headings[ 3 ].length ];
}
if ( ( pre = text.match( /^ +/ ) ) ) {
return [ pre[ 0 ].length, 0 ];
}
}
for ( i = 1; i <= 6; i++ ) {
heading += '=';
ve.ui.wikitextCommandRegistry.register(
new ve.ui.Command(
'heading' + i, 'mwWikitext', 'wrapLine',
{
args: [
heading + ' ',
' ' + heading,
OO.ui.deferMsg( 'visualeditor-formatdropdown-format-heading' + i ),
unformat
],
supportedSelections: [ 'linear' ]
}
)
);
}
ve.ui.wikitextCommandRegistry.register(
new ve.ui.Command(
'paragraph', 'mwWikitext', 'wrapLine',
{ args: [ '', '', '', unformat ], supportedSelections: [ 'linear' ] }
)
);
ve.ui.wikitextCommandRegistry.register(
new ve.ui.Command(
'preformatted', 'mwWikitext', 'wrapLine',
{ args: [ ' ', '', OO.ui.deferMsg( 'visualeditor-formatdropdown-format-preformatted' ), unformat ], supportedSelections: [ 'linear' ] }
)
);
ve.ui.wikitextCommandRegistry.register(
new ve.ui.Command(
'blockquote', 'mwWikitext', 'toggleWrapSelection',
{ args: [ '<blockquote>', '</blockquote>', OO.ui.deferMsg( 'visualeditor-formatdropdown-format-blockquote' ) ], supportedSelections: [ 'linear' ] }
)
);
function unlist( keepType, text ) {
var matches;
if ( ( matches = text.match( /^[*#] */ ) ) && text.slice( 0, 1 ) !== keepType ) {
return [ matches[ 0 ].length, 0 ];
}
}
ve.ui.wikitextCommandRegistry.register(
new ve.ui.Command(
'number', 'mwWikitext', 'wrapLine',
{ args: [ '# ', '', OO.ui.deferMsg( 'visualeditor-listbutton-number-tooltip' ), unlist.bind( this, '#' ) ], supportedSelections: [ 'linear' ] }
)
);
ve.ui.wikitextCommandRegistry.register(
new ve.ui.Command(
'bullet', 'mwWikitext', 'wrapLine',
{ args: [ '* ', '', OO.ui.deferMsg( 'visualeditor-listbutton-bullet-tooltip' ), unlist.bind( this, '*' ) ], supportedSelections: [ 'linear' ] }
)
);
}() );
ve.ui.wikitextCommandRegistry.register(
new ve.ui.Command(
'comment', 'mwWikitext', 'toggleWrapSelection',
{ args: [ '<!-- ', ' -->', OO.ui.deferMsg( 'visualeditor-commentinspector-title' ) ], supportedSelections: [ 'linear' ] }
)
);
ve.ui.wikitextCommandRegistry.register(
new ve.ui.Command(
'insertTable', 'mwWikitext', 'toggleWrapSelection',
{
args: [
[ { type: 'paragraph' } ].concat( '{| class="wikitable"'.split( '' ) ).concat( { type: '/paragraph' } ),
[ { type: 'paragraph' } ].concat( '|}'.split( '' ) ).concat( { type: '/paragraph' } ),
function () {
return '' +
'|+ ' + ve.msg( 'visualeditor-table-caption' ) +
'\n' +
'! ' + ve.msg( 'visualeditor-table-format-header' ) + ' !! ' + ve.msg( 'visualeditor-table-format-header' ) +
'\n' +
'|-' +
'\n' +
'| ' + ve.msg( 'visualeditor-table-format-data' ) + ' || ' + ve.msg( 'visualeditor-table-format-data' );
}
],
supportedSelections: [ 'linear' ]
}
)
);