User:Polygnotus/Scripts/2010.js: Difference between revisions

Content deleted Content added
No edit summary
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) {
// CreateWait thefor TemplatesWikiEditor sectionto be fully loaded
mw.hook('wikiEditor.toolbarReady').add(function() {
var templatesSection = $('<div>')
.attraddTemplatesSection('id', 'templates-section');
.css({});
'border': '1px solid #a2a9b1',
'margin': '10px 0',
'padding': '10px',
'background-color': '#f8f9fa'
});
// AddFallback sectionif titlehook doesn't fire
var sectionTitle = $setTimeout(function('<h3>') {
.textif (!$('Templates#wikiEditor-section-templates').length) {
.css addTemplatesSection({);
'margin': '0 0 10px 0',}
}, 'font-size': '14px',1000);
}
'font-weight': 'bold'
});
function addTemplatesSection() {
// CreateAdd theTemplates uw-3rrtab to the buttontoolbar
var uw3rrButton$tabs = $('<button>.wikiEditor-ui-toolbar .tabs');
if ($tabs.length && !$('#wikiEditor-section-templates').length) {
.attr('type', 'button')
.text('{{uw-3rr}}')
.css({
'margin': '5px',
'padding': '5px 10px',
'background-color': '#0645ad',
'color': 'white',
'border': 'none',
'border-radius': '3px',
'cursor': 'pointer'
})
.hover(
function() { $(this).css('background-color', '#0b0080'); },
function() { $(this).css('background-color', '#0645ad'); }
);
// Add click handler to insert template
uw3rrButton.click(function() {
var textArea = $('#wpTextbox1')[0];
var template = '{{uw-3rr}}';
// GetCreate currentthe cursorTemplates positiontab
var startPos$templatesTab = textArea.selectionStart;$('<span>')
var endPos = textArea .selectionEnd;addClass('tab tab-templates')
.attr('rel', 'templates')
.html('<a class="skin-invert" tabindex="0" role="button" aria-expanded="false" aria-controls="wikiEditor-section-templates">Templates</a>');
// InsertAdd templatetab atto cursorthe positiontoolbar
var textBefore = textArea$tabs.value.substringappend(0, startPos$templatesTab);
var textAfter = textArea.value.substring(endPos);
textArea.value// =Create textBeforethe +Templates templatesection + textAfter;content
var $templatesSection = $('<div>')
.addClass('toolbar section section-templates section-hidden')
.attr({
'rel': 'templates',
'id': 'wikiEditor-section-templates',
'aria-expanded': 'false'
});
// SetCreate cursortemplate positionbuttons after the inserted templategroup
var $templateGroup = $('<div>')
textArea.selectionStart = textArea.selectionEnd = startPos + template.length;
.addClass('group group-templates')
.attr('rel', 'templates');
// FocusAdd backgroup on the text arealabel
$templateGroup.append('<span class="label">Warning templates</span>');
textArea.focus();
});
// Create uw-3rr button
var $uw3rrButton = $('<span>')
// Assemble the section
.addClass('tool oo-ui-widget oo-ui-widget-enabled oo-ui-buttonElement oo-ui-buttonElement-frameless oo-ui-iconElement oo-ui-buttonWidget')
templatesSection.append(sectionTitle);
templatesSection .appendattr(uw3rrButton'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>' +
// Insert the section above the edit form
'<span class="oo-ui-labelElement-label">{{uw-3rr}}</span>' +
$('#editform').before(templatesSection);
'<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();
}
}
}
});