Content deleted Content added
Polygnotus (talk | contribs) No edit summary |
Polygnotus (talk | contribs) No edit summary |
||
Line 1:
// Add Templates section to 2010 wikitext editor toolbar
// Place this code in your common.js file
Line 5:
// Only run on edit pages with the 2010 wikitext editor during preview and submit
var action = mw.config.get('wgAction');
if ((action === 'edit' || action === 'submit') && $('#wpTextbox1').length && $('.wikiEditor-ui-toolbar').length) {
//
mw.hook('wikiEditor.toolbarReady').add(function() {
//
},
}
function addTemplatesSection() {
//
var
if ($tabs.length && !$('#wikiEditor-section-templates').length) {
//
var
.attr('rel', 'templates')
.html('<a class="skin-invert" tabindex="0" role="button" aria-expanded="false" aria-controls="wikiEditor-section-templates">Templates</a>');
//
var $templatesSection = $('<div>')
.addClass('toolbar section section-templates section-hidden')
.attr({
'rel': 'templates',
'id': 'wikiEditor-section-templates',
'aria-expanded': 'false'
});
//
var $templateGroup = $('<div>')
.addClass('group group-templates')
.attr('rel', 'templates');
//
$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')
.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();
}
}
}
});
|