User:Polygnotus/Scripts/2010.js

This is an old revision of this page, as edited by Polygnotus (talk | contribs) at 14:59, 9 July 2025. The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
// Add Templates section to 2010 wikitext editor toolbar
// Place this code in your common.js file

$(document).ready(function() {
    // 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) {
        
        // Wait for WikiEditor to be fully loaded
        mw.hook('wikiEditor.toolbarReady').add(function() {
            addTemplatesSection();
        });
        
        // Fallback if hook doesn't fire
        setTimeout(function() {
            if (!$('#wikiEditor-section-templates').length) {
                addTemplatesSection();
            }
        }, 1000);
    }
    
    function addTemplatesSection() {
        // Add Templates tab to the toolbar
        var $tabs = $('.wikiEditor-ui-toolbar .tabs');
        if ($tabs.length && !$('#wikiEditor-section-templates').length) {
            
            // Create the Templates tab
            var $templatesTab = $('<span>')
                .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>');
            
            // Add tab to the toolbar
            $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();
            }
        }
    }
});