// Add Templates section to WikiEditor 2010 wikitext editor toolbar
// Place this code in your common.js file
// Check if we're editing a page
$(document).ready(function() {
if ( [ 'edit', 'submit' ].indexOf( mw.config.get( 'wgAction' ) ) !== -1 ) {
// Only run on edit pages with the 2010 wikitext editor during preview and submit
// Add a hook handler
var action = mw.config.get('wgAction');
mw.hook( 'wikiEditor.toolbarReady' ).add( function ( $textarea ) {
if ((action === 'edit' || action === 'submit') && $('#wpTextbox1').length && $('.wikiEditor-ui-toolbar').length) {
// WaitAdd forthe WikiEditorTemplates totoolbar be fully loadedsection
$textarea.wikiEditor( 'addToToolbar', {
mw.hook('wikiEditor.toolbarReady').add(function() {
addTemplatesSection();sections: {
}); templates: {
type: 'toolbar',
label: 'Templates'
}
}
} );
// FallbackAdd ifa hookgroup doesn'tfor firewarning templates
setTimeout$textarea.wikiEditor(function() 'addToToolbar', {
ifsection: (!$('#wikiEditor-section-templates').length) {,
groups: addTemplatesSection();{
warnings: {
label: 'Warning templates'
}
}
}, 1000);
}
// Add the uw-3rr button
$textarea.wikiEditor( 'addToToolbar', {
function addTemplatesSection() {
// Add Templates tab tosection: the toolbar'templates',
group: 'warnings',
var $tabs = $('.wikiEditor-ui-toolbar .tabs');
tools: {
if ($tabs.length && !$('#wikiEditor-section-templates').length) {
'uw-3rr': {
// Create the Templates tab label: '{{uw-3rr}} - 3RR warning',
var $templatesTab = $( type: '<span>button'),
.addClass('tab tab-templates oouiIcon: 'alert'),
.attr('rel', 'templates') action: {
type: 'encapsulate',
.html('<a class="skin-invert" tabindex="0" role="button" aria-expanded="false" aria-controls="wikiEditor-section-templates">Templates</a>');
options: {
// Add tab to the toolbar pre: '{{uw-3rr}}'
$tabs.append($templatesTab); }
}
// Create the Templates section content}
var $templatesSection = $('<div>')
.addClass('toolbar section section-templates section-hidden')
.attr({
'rel': 'templates',
'id': 'wikiEditor-section-templates',
'aria-expanded': 'false'
});
// Create template buttons group
var $templateGroup = $('<div>')
.addClass('group group-templates')
.attr('rel', 'templates');
// Add group label
$templateGroup.append('<span class="label">Warning templates</span>');
// Create uw-3rr button
var $uw3rrButton = $('<span>')
.addClass('tool oo-ui-widget oo-ui-widget-enabled oo-ui-buttonElement oo-ui-buttonElement-frameless oo-ui-iconElement oo-ui-buttonWidget')
.attr('rel', 'uw-3rr')
.html('<a class="oo-ui-buttonElement-button" role="button" title="3RR warning template" tabindex="0" rel="nofollow">' +
'<span class="oo-ui-iconElement-icon oo-ui-icon-alert"></span>' +
'<span class="oo-ui-labelElement-label">{{uw-3rr}}</span>' +
'<span class="oo-ui-indicatorElement-indicator oo-ui-indicatorElement-noIndicator"></span>' +
'</a>');
// Add click handler for the button
$uw3rrButton.on('click', function(e) {
e.preventDefault();
insertTemplate('{{uw-3rr}}');
});
// Assemble the section
$templateGroup.append($uw3rrButton);
$templatesSection.append($templateGroup);
// Add section to the toolbar sections container
$('.wikiEditor-ui-toolbar .sections').append($templatesSection);
// Add tab click handler
$templatesTab.on('click', function(e) {
e.preventDefault();
// Hide all other sections and remove current class from tabs
$('.wikiEditor-ui-toolbar .section').removeClass('section-visible').addClass('section-hidden').attr('aria-expanded', 'false');
$('.wikiEditor-ui-toolbar .tab a').removeClass('current');
// Show templates section and mark tab as current
$templatesSection.removeClass('section-hidden').addClass('section-visible').attr('aria-expanded', 'true');
$(this).find('a').addClass('current');
});
// Add handlers for other tabs to hide templates section
$('.wikiEditor-ui-toolbar .tab').not($templatesTab).on('click', function() {
$templatesSection.removeClass('section-visible').addClass('section-hidden').attr('aria-expanded', 'false');
$templatesTab.find('a').removeClass('current');
});
}
}
function insertTemplate(template) {
// Check if CodeMirror is active
if ($('.cm-editor').length && $('.cm-content').length) {
// CodeMirror is active - use CodeMirror API
var cmEditor = $('.cm-editor')[0];
if (cmEditor && cmEditor.CodeMirror) {
var cm = cmEditor.CodeMirror;
var cursor = cm.getCursor();
cm.replaceRange(template, cursor);
cm.setCursor(cursor.line, cursor.ch + template.length);
cm.focus();
}
} else {);
// Fallback to regular textarea
} );
var textArea = $('#wpTextbox1')[0];
}
if (textArea) {
var startPos = textArea.selectionStart;
var endPos = textArea.selectionEnd;
var textBefore = textArea.value.substring(0, startPos);
var textAfter = textArea.value.substring(endPos);
textArea.value = textBefore + template + textAfter;
textArea.selectionStart = textArea.selectionEnd = startPos + template.length;
textArea.focus();
}
}
}
});
|