Jump to content

MediaWiki:Common.js: Difference between revisions

From FC1
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. */
(function () {
/* Animation script for MediaWiki templates */
     function applyTextEffects($content) {
(function() {
         $content.find('.text-animation-container').each(function () {
     function applyEffects(el) {
            const $el = $(this);
         // Only process if it has one of our target classes
            if ($el.data('animated')) return;  
        const hasEffect = ['wavy', 'rainbow', 'jolt', 'force-glow', 'force-shadow'].some(cls => el.classList.contains(cls));
        if (!hasEffect || el.getAttribute('data-animated') === 'true') return;


            const text = $el.text();
        const chars = el.textContent.split('');
            const chars = text.split('');
        const isRainbow = el.classList.contains('rainbow');
            const isWavy = $el.hasClass('wavy');
        const isWavy = el.classList.contains('wavy');
            const isRainbow = $el.hasClass('rainbow');
        const isJolt = el.classList.contains('jolt');
            const isJolt = $el.hasClass('jolt'); // Added Jolt check


            const nodes = chars.map((char, i) => {
        // Prevent the container itself from moving
                const $span = $('<span>').text(char === ' ' ? '\u00A0' : char);
        el.style.setProperty('animation', 'none', 'important');
                let delays = [];


                if (isWavy) delays.push((i * -0.15).toFixed(2) + 's');
        const newHTML = chars.map((c, i) => {
                if (isRainbow) delays.push((i * -0.15).toFixed(2) + 's');
            let delays = [];
               
           
                 // Randomized delay for Jolt to make it look "glitchy"
            if (isWavy) {
                 if (isJolt) {
                delays.push((i * -0.15).toFixed(2) + 's');
                    delays.push((Math.random() * -0.5).toFixed(2) + 's');
            }
                }
            if (isRainbow) {
                delays.push((i * -0.15).toFixed(2) + 's');
            }
            if (isJolt) {
                 // Random delay makes the "jolt" look chaotic rather than a uniform slide
                 delays.push((Math.random() * -0.5).toFixed(2) + 's');
            }


                if (delays.length) {
            const content = (c === ' ') ? '&nbsp;' : c;
                    $span.css('animation-delay', delays.join(', '));
            const delayStyle = delays.length > 0 ? `animation-delay: ${delays.join(', ')} !important;` : '';
                }
           
                return $span;
            // Return the span as a string
            });
            return `<span style="${delayStyle}">${content}</span>`;
        }).join('');


            $el.empty().append(nodes).data('animated', true);
        el.innerHTML = newHTML;
        });
        el.setAttribute('data-animated', 'true'); // Mark as done to prevent double-processing
     }
     }


     mw.hook('wikipage.content').add(applyTextEffects);
    // This hook ensures it runs on initial load AND when clicking "Preview" or "Show changes"
}());
     mw.hook('wikipage.content').add(function($content) {
        $content.find('.text-animation-container').each(function() {
            applyEffects(this);
        });
    });
})();

Revision as of 19:15, 17 March 2026

/* Any JavaScript here will be loaded for all users on every page load. */
/* Animation script for MediaWiki templates */
(function() {
    function applyEffects(el) {
        // Only process if it has one of our target classes
        const hasEffect = ['wavy', 'rainbow', 'jolt', 'force-glow', 'force-shadow'].some(cls => el.classList.contains(cls));
        if (!hasEffect || el.getAttribute('data-animated') === 'true') return;

        const chars = el.textContent.split('');
        const isRainbow = el.classList.contains('rainbow');
        const isWavy = el.classList.contains('wavy');
        const isJolt = el.classList.contains('jolt');

        // Prevent the container itself from moving
        el.style.setProperty('animation', 'none', 'important');

        const newHTML = chars.map((c, i) => {
            let delays = [];
            
            if (isWavy) {
                delays.push((i * -0.15).toFixed(2) + 's');
            }
            if (isRainbow) {
                delays.push((i * -0.15).toFixed(2) + 's');
            }
            if (isJolt) {
                // Random delay makes the "jolt" look chaotic rather than a uniform slide
                delays.push((Math.random() * -0.5).toFixed(2) + 's');
            }

            const content = (c === ' ') ? '&nbsp;' : c;
            const delayStyle = delays.length > 0 ? `animation-delay: ${delays.join(', ')} !important;` : '';
            
            // Return the span as a string
            return `<span style="${delayStyle}">${content}</span>`;
        }).join('');

        el.innerHTML = newHTML;
        el.setAttribute('data-animated', 'true'); // Mark as done to prevent double-processing
    }

    // This hook ensures it runs on initial load AND when clicking "Preview" or "Show changes"
    mw.hook('wikipage.content').add(function($content) {
        $content.find('.text-animation-container').each(function() {
            applyEffects(this);
        });
    });
})();