Allow tool.element to be a function

This adds support for the new 'element' tool type to be a function
which returns the HTML for the toolbar widget. It means that it's
possible for the element to access the WikiEditor context object.

Bug: T286759
Change-Id: I6af12075b96d480639f2fc0d97333a6142480c02
This commit is contained in:
Sam Wilson 2022-03-02 10:08:54 +08:00
parent fc768ba3c4
commit fff7cd6ba1

View file

@ -429,11 +429,13 @@
$select.append( $( '<div>' ).addClass( 'menu' ).append( $options ) );
return $select;
case 'element':
// A raw 'element' type can be {htmlString|Element|Text|Array|jQuery|OO.ui.HTMLSnippet}.
// A raw 'element' type can be {htmlString|Element|Text|Array|jQuery|OO.ui.HTMLSnippet|function}.
var $element = $( '<div>' )
.attr( { rel: id, class: 'tool tool-element' } );
if ( tool.element instanceof OO.ui.HtmlSnippet ) {
$element.append( tool.element.toString() );
} else if ( typeof tool.element === 'function' ) {
$element.append( tool.element( context ) );
} else {
$element.append( tool.element );
}