Jump to content

MediaWiki:Common.js: Difference between revisions

From IoT Colabs Wiki
No edit summary
No edit summary
 
Line 1: Line 1:
/* Any JavaScript here will be loaded for all users on every page load. */
/* Any JavaScript here will be loaded for all users on every page load. */
/* Add copy ICON to syntaxhighlight blocks */
/* Add STICKY copy icon to syntaxhighlight blocks */
mw.hook('wikipage.content').add(function($content) {
mw.hook('wikipage.content').add(function($content) {
    // Standard "Clipboard" SVG icon
     var copyIcon = '<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><rect x="9" y="9" width="13" height="13" rx="2" ry="2"></rect><path d="M5 15H4a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h9a2 2 0 0 1 2 2v1"></path></svg>';
     var copyIcon = '<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><rect x="9" y="9" width="13" height="13" rx="2" ry="2"></rect><path d="M5 15H4a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h9a2 2 0 0 1 2 2v1"></path></svg>';
     var checkIcon = '<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="#28a745" stroke-width="3" stroke-linecap="round" stroke-linejoin="round"><polyline points="20 6 9 17 4 12"></polyline></svg>';
     var checkIcon = '<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="#28a745" stroke-width="3" stroke-linecap="round" stroke-linejoin="round"><polyline points="20 6 9 17 4 12"></polyline></svg>';
Line 12: Line 11:
         var $button = $('<button>')
         var $button = $('<button>')
             .addClass('copy-button')
             .addClass('copy-button')
             .html(copyIcon) // Inject the SVG here
             .html(copyIcon)
             .attr('title', 'Copy to clipboard')
             .attr('title', 'Copy to clipboard')
             .on('click', function() {
             .on('click', function() {
                 var code = $block.find('pre').text();
                 var code = $block.find('pre').text();
                 navigator.clipboard.writeText(code).then(function() {
                 navigator.clipboard.writeText(code).then(function() {
                     $button.html(checkIcon); // Switch to checkmark on success
                     $button.html(checkIcon);
                     setTimeout(function() { $button.html(copyIcon); }, 2000);
                     setTimeout(function() { $button.html(copyIcon); }, 2000);
                 });
                 });
             });
             });


         $block.css('position', 'relative').prepend($button);
        // Prepend the button so it sits at the top of the container
         $block.prepend($button);
     });
     });
});
});

Latest revision as of 10:06, 6 May 2026

/* Any JavaScript here will be loaded for all users on every page load. */
/* Add STICKY copy icon to syntaxhighlight blocks */
mw.hook('wikipage.content').add(function($content) {
    var copyIcon = '<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><rect x="9" y="9" width="13" height="13" rx="2" ry="2"></rect><path d="M5 15H4a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h9a2 2 0 0 1 2 2v1"></path></svg>';
    var checkIcon = '<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="#28a745" stroke-width="3" stroke-linecap="round" stroke-linejoin="round"><polyline points="20 6 9 17 4 12"></polyline></svg>';

    $content.find('.mw-highlight').each(function() {
        var $block = $(this);
        if ($block.find('.copy-button').length > 0) return;

        var $button = $('<button>')
            .addClass('copy-button')
            .html(copyIcon)
            .attr('title', 'Copy to clipboard')
            .on('click', function() {
                var code = $block.find('pre').text();
                navigator.clipboard.writeText(code).then(function() {
                    $button.html(checkIcon);
                    setTimeout(function() { $button.html(copyIcon); }, 2000);
                });
            });

        // Prepend the button so it sits at the top of the container
        $block.prepend($button);
    });
});