User:Polygnotus/Scripts/2010.js

This is an old revision of this page, as edited by Polygnotus (talk | contribs) at 14:57, 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
// 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) {
        
        // Create the Templates section
        var templatesSection = $('<div>')
            .attr('id', 'templates-section')
            .css({
                'border': '1px solid #a2a9b1',
                'margin': '10px 0',
                'padding': '10px',
                'background-color': '#f8f9fa'
            });
        
        // Add section title
        var sectionTitle = $('<h3>')
            .text('Templates')
            .css({
                'margin': '0 0 10px 0',
                'font-size': '14px',
                'font-weight': 'bold'
            });
        
        // Create the uw-3rr button
        var uw3rrButton = $('<button>')
            .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}}';
            
            // Get current cursor position
            var startPos = textArea.selectionStart;
            var endPos = textArea.selectionEnd;
            
            // Insert template at cursor position
            var textBefore = textArea.value.substring(0, startPos);
            var textAfter = textArea.value.substring(endPos);
            
            textArea.value = textBefore + template + textAfter;
            
            // Set cursor position after the inserted template
            textArea.selectionStart = textArea.selectionEnd = startPos + template.length;
            
            // Focus back on the text area
            textArea.focus();
        });
        
        // Assemble the section
        templatesSection.append(sectionTitle);
        templatesSection.append(uw3rrButton);
        
        // Insert the section above the edit form
        $('#editform').before(templatesSection);
    }
});