Содержимое удалено Содержимое добавлено
hook legacy.toolbar.ready
перенос и некоторая адаптация кода из MediaWiki:Gadget-mediawiki.toolbar.js, MediaWiki:ToolbarOld.js, MediaWiki:Gadget-common-action-edit.js, MediaWiki:Gadget-common-action-edit.css и популярных гаджетов
Строка 1:
/**
function addOldToolbarButtons () {
* Interface for the classic edit toolbar.
mw.toolbar.addButtons(
*
* Adapted from MediaWiki Core, before it was removed from it on 2018-10-17
*/
( function () {
var toolbar, isReady, $toolbar, queue, slice, $currentFocused;
 
/**
* Internal helper that does the actual insertion of the button into the toolbar.
*
* For backwards-compatibility, passing `imageFile`, `speedTip`, `tagOpen`, `tagClose`,
* `sampleText` and `imageId` as separate arguments (in this order) is also supported.
*
* @private
*
* @param {Object} button Object with the following properties.
* You are required to provide *either* the `onClick` parameter, or the three parameters
* `tagOpen`, `tagClose` and `sampleText`, but not both (they're mutually exclusive).
* @param {string} [button.imageFile] Image to use for the button.
* @param {string} button.speedTip Tooltip displayed when user mouses over the button.
* @param {Function} [button.onClick] Function to be executed when the button is clicked.
* @param {string} [button.tagOpen]
* @param {string} [button.tagClose]
* @param {string} [button.sampleText] Alternative to `onClick`. `tagOpen`, `tagClose` and
* `sampleText` together provide the markup that should be inserted into page text at
* current cursor position.
* @param {string} [button.imageId] `id` attribute of the button HTML element. Can be
* used to define the image with CSS if it's not provided as `imageFile`.
* @param {string} [speedTip]
* @param {string} [tagOpen]
* @param {string} [tagClose]
* @param {string} [sampleText]
* @param {string} [imageId]
*/
function insertButton( button, speedTip, tagOpen, tagClose, sampleText, imageId ) {
var $button;
 
// Backwards compatibility
if ( typeof button !== 'object' ) {
button = {
imageFile: button,
speedTip: speedTip,
tagOpen: tagOpen,
tagClose: tagClose,
sampleText: sampleText,
imageId: imageId
};
}
 
if ( button.imageFile ) {
$button = $( '<img>' ).attr( {
src: button.imageFile,
alt: button.speedTip,
title: button.speedTip,
id: button.imageId || undefined,
'class': 'mw-toolbar-editbutton'
} );
} else {
$button = $( '<div>' ).attr( {
title: button.speedTip,
id: button.imageId || undefined,
'class': 'mw-toolbar-editbutton'
} );
}
 
$button.click( function ( e ) {
if ( button.onClick !== undefined ) {
button.onClick( e );
} else {
toolbar.insertTags( button.tagOpen, button.tagClose, button.sampleText );
}
 
return false;
} );
 
$toolbar.append( $button );
}
 
isReady = false;
$toolbar = false;
 
/**
* @private
* @property {Array}
* Contains button objects (and for backwards compatibility, it can
* also contains an arguments array for insertButton).
*/
queue = [];
slice = queue.slice;
 
toolbar = {
 
/**
* Add buttons to the toolbar.
*
* Takes care of race conditions and time-based dependencies by placing buttons in a queue if
* this method is called before the toolbar is created.
*
* For backwards-compatibility, passing `imageFile`, `speedTip`, `tagOpen`, `tagClose`,
* `sampleText` and `imageId` as separate arguments (in this order) is also supported.
*
* @inheritdoc #insertButton
*/
addButton: function () {
if ( isReady ) {
insertButton.apply( toolbar, arguments );
} else {
// Convert arguments list to array
queue.push( slice.call( arguments ) );
}
},
 
/**
* Add multiple buttons to the toolbar (see also #addButton).
*
* Example usage:
*
* addButtons( [ { .. }, { .. }, { .. } ] );
* addButtons( { .. }, { .. } );
*
* @param {...Object|Array} [buttons] An array of button objects or the first
* button object in a list of variadic arguments.
*/
addButtons: function ( buttons ) {
if ( !Array.isArray( buttons ) ) {
buttons = slice.call( arguments );
}
if ( isReady ) {
buttons.forEach( function ( button ) {
insertButton( button );
} );
} else {
// Push each button into the queue
queue.push.apply( queue, buttons );
}
},
 
/**
* Apply tagOpen/tagClose to selection in currently focused textarea.
*
* Uses `sampleText` if selection is empty.
*
* @param {string} tagOpen
* @param {string} tagClose
* @param {string} sampleText
*/
insertTags: function ( tagOpen, tagClose, sampleText ) {
if ( $currentFocused && $currentFocused.length ) {
$currentFocused.textSelection(
'encapsulateSelection', {
pre: tagOpen,
peri: sampleText,
post: tagClose
}
);
}
}
};
 
// For backwards compatibility. Used to be called from EditPage.php, maybe other places as well.
toolbar.init = $.noop;
 
// Expose API publicly
mw.toolbar = toolbar;
 
$( function () {
var $textBox, i, button;
 
// Used to determine where to insert tags
$currentFocused = $( '#wpTextbox1' );
 
if ( mw.loader.getState( 'ext.wikiEditor' ) === 'ready' ) {
// do nothing if wikiEditor 2010 is ready
} else {
 
// Populate the selector cache for $toolbar
$toolbar = $( '#toolbar' );
 
if ( $toolbar.length === 0 ) {
$textBox = $( '#wpTextbox1' );
if ( $textBox.length === 0 ) {
return;
}
$toolbar = $( '<div>' ).attr( { id: 'toolbar' } );
$toolbar.css( 'clear', 'none' );
$toolbar.insertBefore( $textBox );
}
 
for ( i = 0; i < queue.length; i++ ) {
button = queue[ i ];
if ( Array.isArray( button ) ) {
// Forwarded arguments array from mw.toolbar.addButton
insertButton.apply( toolbar, button );
} else {
// Raw object from mw.toolbar.addButtons
insertButton( button );
}
}
}
// Clear queue
queue.length = 0;
 
// This causes further calls to addButton to go to insertion directly
// instead of to the queue.
// It is important that this is after the one and only loop through
// the queue
isReady = true;
 
// Apply to dynamically created textboxes as well as normal ones
$( document ).on( 'focus', 'textarea, input:text', function () {
$currentFocused = $( this );
} );
} );
 
}() );
 
( function () {
 
function addOldToolbarButtons() {
mw.toolbar.addButtons( {
imageFile: '//upload.wikimedia.org/wikipedia/commons/e/e2/Button_bold.png',
speedTip: mw.msg( 'bold_tip' ),
tagOpen: '\'\'\'',
tagClose: '\'\'\'',
sampleText: mw.msg( 'bold_sample' ),
imageId: 'mw-editbutton-bold'
},
{
imageFile: "'//upload.wikimedia.org/wikipedia/commons/e1/e21d/Button_boldButton_italic.png"',
speedTip: mw.messagemsg( 'bold_tipitalic_tip').text( ),
tagOpen: "'\'\''",
tagClose: "'\'\''",
sampleText: mw.messagemsg( 'bold_sampleitalic_sample').text( ),
imageId: "'mw-editbutton-bold"italic'
},
{
imageFile: "'//upload.wikimedia.org/wikipedia/commons/1c/1dc0/Button_italicButton_link.png"',
speedTip: mw.messagemsg( 'italic_tiplink_tip').text( ),
tagOpen: "'[['",
tagClose: "']]'",
sampleText: mw.messagemsg( 'italic_samplelink_sample').text( ),
imageId: "mw-editbutton-italiclink"
},
{
imageFile: "'//upload.wikimedia.org/wikipedia/commons/ce/c0ec/Button_linkButton_extlink.png"',
speedTip: mw.messagemsg( 'link_tipextlink_tip').text( ),
tagOpen: "['["',
tagClose: "]']"',
sampleText: mw.messagemsg( 'link_sampleextlink_sample').text( ),
imageId: "'mw-editbutton-link"extlink'
},
{
imageFile: "'//upload.wikimedia.org/wikipedia/commons/e7/ec78/Button_extlinkButton_head_A2.png"',
speedTip: mw.messagemsg( 'extlink_tipheadline_tip').text( ),
tagOpen: "["'== ',
tagClose: "]"' ==',
sampleText: mw.messagemsg( 'extlink_sampleheadline_sample').text( ),
imageId: "'mw-editbutton-extlink"headline'
},
{
imageFile: "'//upload.wikimedia.org/wikipedia/commons/7d/78de/Button_head_A2Button_image.png"',
speedTip: mw.messagemsg( 'headline_tipimage_tip').text( ),
tagOpen: "== "'[[Файл:',
tagClose: " ='|thumb|ссылка="]]',
sampleText: mw.messagemsg( 'headline_sampleimage_sample').text( ),
imageId: "'mw-editbutton-headline"image'
},
{
imageFile: "'//upload.wikimedia.org/wikipedia/commons/d1/de19/Button_imageButton_media.png"',
speedTip: mw.messagemsg( 'image_tipmedia_tip').text( ),
tagOpen: "'[[Файл:"',
tagClose: "'|thumb|ссылка=Описание]]"',
sampleText: mw.messagemsg( 'image_samplemedia_sample').text( ),
imageId: "'mw-editbutton-image"media'
},
{
imageFile: "'//upload.wikimedia.org/wikipedia/commons/18/1982/Button_mediaNowiki_icon.png"',
speedTip: mw.messagemsg( 'media_tipnowiki_tip').text( ),
tagOpen: "[[Файл:"'<nowiki\>',
tagClose: "|thumb|Описание]]"'</\nowiki>',
sampleText: mw.messagemsg( 'media_samplenowiki_sample').text( ),
imageId: "'mw-editbutton-media"nowiki'
},
{
imageFile: "'//upload.wikimedia.org/wikipedia/commons/86/826d/Nowiki_iconButton_sig.png"',
speedTip: mw.messagemsg( 'nowiki_tipsig_tip').text( ),
tagOpen: "<nowiki"+">"'-- ~~\~~',
tagClose: "</"+"nowiki>"'',
sampleText: mw.message( 'nowiki_sample').text(),
imageId: "'mw-editbutton-nowiki"signature'
},
{
imageFile: "'//upload.wikimedia.org/wikipedia/commons/60/6d0d/Button_sigButton_hr.png"',
speedTip: mw.messagemsg( 'sig_tiphr_tip').text( ),
tagOpen: "'-- ~~"+"~~"\--',
tagClose: ""'',
sampleText: ""'',
imageId: "'mw-editbutton-signature"hr'
},
{
speedTip: 'Перенаправление',
imageFile: "//upload.wikimedia.org/wikipedia/commons/0/0d/Button_hr.png",
imageFile: '//upload.wikimedia.org/wikipedia/ru/1/1d/Button_redirect_rus.png',
speedTip: mw.message( 'hr_tip').text(),
tagOpen: "--"+"--"'#REDIRECT [[',
sampleText: 'Название целевой страницы',
tagClose: "",
tagClose: ']]',
sampleText: "",
imageId: "'mw-editbutton-hr"redirect'
},
{
speedTip: 'Категория',
imageFile: '//upload.wikimedia.org/wikipedia/commons/3/3c/Button_cat_ru.png',
tagOpen: '[\[Категория:',
sampleText: '',
tagClose: ']]\n',
imageId: 'mw-editbutton-category'
},
{
speedTip: 'Комментарий',
imageFile: '//upload.wikimedia.org/wikipedia/en/3/34/Button_hide_comment.png',
tagOpen: '<!-- ',
sampleText: 'Комментарий',
tagClose: ' -->',
imageId: 'mw-editbutton-comment'
},
{
speedTip: 'Развёрнутая цитата',
imageFile: '//upload.wikimedia.org/wikipedia/en/f/fd/Button_blockquote.png',
tagOpen: '<blockquote>\n',
sampleText: 'Развёрнутая цитата одним абзацем',
tagClose: '\n</blockquote>',
imageId: 'mw-editbutton-blockquote'
},
{
speedTip: 'Вставить таблицу',
imageFile: '//upload.wikimedia.org/wikipedia/en/6/60/Button_insert_table.png',
tagOpen: '{| class="wikitable"\n|',
sampleText: '-\n! заголовок 1\n! заголовок 2\n! заголовок 3\n|-\n| строка 1, ячейка 1\n| строка 1, ячейка 2\n| строка 1, ячейка 3\n|-\n| строка 2, ячейка 1\n| строка 2, ячейка 2\n| строка 2, ячейка 3',
tagClose: '\n|}',
imageId: 'mw-editbutton-insert_table'
},
{
speedTip: 'Сноска',
imageFile: '//upload.wikimedia.org/wikipedia/commons/7/79/Button_reflink.png',
tagOpen: '<ref\>',
sampleText: 'Вставьте сюда текст сноски',
tagClose: '</ref>',
imageId: 'mw-editbutton-reflink'
} );
 
$( '#mw-editbutton-image' )
.off( 'click' )
.click( function ( e ) {
e.preventDefault();
mw.toolbar.insertTags( '[[Файл:', '|thumb]]', 'Example.jpg' );
} );
 
// Кнопки популярных гаджетов
var $gadgetToolbar = $( '<div>' )
.attr( 'id', 'gadget-toolbar' )
.prependTo( '#toolbar' );
 
mw.loader.using( 'mediawiki.util' ).done( function () {
mw.util.addCSS( '\
#gadget-toolbar {\
float: left;\
}\
' );
} );
 
function addOldToolbarButton( button ) {
$( '<div>' )
.addClass( 'mw-toolbar-editbutton' )
.attr( 'alt', button.alt )
.attr( 'title', button.title )
.css( button.css )
.appendTo( $gadgetToolbar )
.click( button.callback );
}
 
var gadgets = {
wikificator: {
alt: 'Викификатор',
title: 'Викификатор — автоматический обработчик текста',
css: {
width: '69px',
backgroundImage: 'url(//upload.wikimedia.org/wikipedia/commons/3/38/Button_wikify.png)',
},
callback: Wikify,
},
urldecoder: {
alt: 'Раскодировщик URL',
title: 'Раскодировать URL перед курсором или все URL в выделенном тексте',
css: { backgroundImage: 'url(//upload.wikimedia.org/wikipedia/commons/6/63/Link_go_toolbar.png)' },
callback: urlDecoderRun,
},
Wikilinker: {
alt: 'Вики-ссыльщик',
title: 'Вики-ссыльщик — подбирает вики-ссылку для выделенного слова или словосочетания',
css: { backgroundImage: 'url(//upload.wikimedia.org/wikipedia/commons/a/ad/Wikilinker_toolbar.png)' },
callback: WikiLinker,
},
};
 
for ( var name in gadgets ) {
if ( mw.user.options.get( 'gadget-' + name ) || name === 'wikificator' ) {
mw.loader.using( [ 'ext.gadget.' + name ] ).done( function () {
addOldToolbarButton( gadgets[ name ] );
} );
}
}
 
mw.hook( 'legacy.toolbar.ready' ).fire();
}
 
if ( $[ 'edit', 'submit' ].inArrayindexOf( mw.config.get( 'wgAction' ), [ 'edit', 'submit' ] ) !== -1 ) {
mw.loader.using( [ 'user.options', 'jquery.textSelection' ], function () {
if ( mw.user.options.get( 'usebetatoolbar' ) != 1 ) {
$.when( mw.loader.using( [ 'mediawiki.api', 'ext.gadget.mediawiki.toolbar' ] ) )
.then( function() {
return new mw.Api().loadMessagesIfMissing( [
'bold_sample', 'bold_tip',
[
'bold_sampleitalic_sample', 'bold_tipitalic_tip',
'italic_samplelink_sample', 'italic_tiplink_tip',
'link_sampleextlink_sample', 'link_tipextlink_tip',
'extlink_sampleheadline_sample', 'extlink_tipheadline_tip',
'headline_sampleimage_sample', 'headline_tipimage_tip',
'image_samplemedia_sample', 'image_tipmedia_tip',
'media_samplenowiki_sample', 'media_tipnowiki_tip',
'nowiki_samplesig_tip', 'nowiki_tiphr_tip',
], { amlang: mw.config.get( 'wgUserLanguage' ) } );
'sig_tip', 'hr_tip'
} )
], { amlang: mw.config.get( 'wgUserLanguage' ) } );
.then( function () {
} )
$( addOldToolbarButtons );
// Then: Do stuff with them
} );
.then( function () {
$( addOldToolbarButtons );
} );
}
} );
}
 
}() );