MediaWiki:Common.js:修订间差异

天明留言 | 贡献
修正尺寸切换逻辑
标签已被回退
天明留言 | 贡献
无编辑摘要
 
(未显示同一用户的3个中间版本)
第31行: 第31行:


// logo 切换逻辑
// logo 切换逻辑
// Common.js - footer logo 切换逻辑(支持 dark/light + compact/normal + Citizen)
$(function () {
$(function () {
     // ======================
     // 获取当前“实际使用”的主题:'dark' 或 'light'
    // 工具函数
    // ======================
 
    // 获取当前主题:dark / light
     function getEffectiveTheme() {
     function getEffectiveTheme() {
         const cls = document.documentElement.classList;
         var cls = document.documentElement.classList;
         if (cls.contains('skin-theme-clientpref-night')) return 'dark';
         if (cls.contains('skin-theme-clientpref-night')) return 'dark';
         if (cls.contains('skin-theme-clientpref-day')) return 'light';
         if (cls.contains('skin-theme-clientpref-day')) return 'light';
         if (cls.contains('skin-theme-clientpref-os')) {
         if (cls.contains('skin-theme-clientpref-os')) {
             if (window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches) return 'dark';
             if (window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches) {
                return 'dark';
            }
             return 'light';
             return 'light';
         }
         }
         if (window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches) return 'dark';
        // 兜底:根据系统偏好判断
         if (window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches) {
            return 'dark';
        }
         return 'light';
         return 'light';
     }
     }


     // 判断是否小屏(compact)
     // 如果元素存在就设置 src(并记录日志)
     function isCompactMode() {
     function setSrcIfExists(selector, src) {
         return window.matchMedia && window.matchMedia('(max-width: 500px)').matches;
         var $el = $(selector);
    }
         if ($el.length) {
 
            $el.attr('src', src);
    // 设置 <img> src
            console.log('[logo-switch] set', selector, '->', src);
    function setImgSrc($img, src) {
        } else {
         if ($img.length) $img.attr('src', src);
            console.log('[logo-switch] not found', selector);
    }
        }
 
    // 设置 <source> srcset
    function setSourceSrcset($source, srcset) {
        if ($source.length) $source.attr('srcset', srcset);
     }
     }


     // ======================
     // 主更新函数 —— 每次都重新 select 元素
    // 主更新逻辑
    // ======================
     function updateLogos() {
     function updateLogos() {
         const theme = getEffectiveTheme();
         var theme = getEffectiveTheme();
         const compact = isCompactMode();
         if (theme === 'dark') {
 
             setSrcIfExists('#footer-copyrightico img', '/resources/assets/cc-by-nc-sa-dark.svg');
        // 尺寸逻辑:大屏用 source 横版,宽度 88x31;小屏 fallback img 用 25x25
            setSrcIfExists('#footer-poweredbyico img', '/resources/assets/poweredby-mediawiki-dark.svg');
        // 注意:compact 状态下实际用 img,小屏时尺寸固定为 25x25
             setSrcIfExists('#footer-assistedbyico img', '/resources/assets/assistedby-ai-dark.svg');
        const sizeNormal = { width: 88, height: 31 };
             setSrcIfExists('#footer-poweredbysmwico img', '/resources/assets/poweredby-smw-dark.svg');  
        const sizeCompact = { width: 25, height: 25 };
        } else {
 
             setSrcIfExists('#footer-copyrightico img', '/resources/assets/cc-by-nc-sa.svg');
        // ----------------------
             setSrcIfExists('#footer-poweredbyico img', '/resources/assets/poweredby-mediawiki.svg');
        // 公共 Logo
            setSrcIfExists('#footer-assistedbyico img', '/resources/assets/assistedby-ai.svg');
        // ----------------------
            setSrcIfExists('#footer-poweredbysmwico img', '/resources/assets/poweredby-smw.svg');  
        ['copyright', 'poweredby', 'poweredbysmw'].forEach(id => {
             const $li = $(`#footer-${id}ico`);
            if (!$li.length) return;
 
            const $picture = $li.find('picture');
            if (!$picture.length) return;
 
            const $source = $picture.find('source');
            const $img = $picture.find('img');
 
            // 构造文件名
            let baseName = '';
            switch(id) {
                case 'copyright': baseName = 'cc-by-nc-sa'; break;
                case 'poweredby': baseName = 'poweredby-mediawiki'; break;
                case 'poweredbysmw': baseName = 'poweredby-smw'; break;
            }
            const normalFile = `/resources/assets/${baseName}${theme === 'dark' ? '-dark' : ''}.svg`;
             const compactFile = `/resources/assets/${baseName}-compact${theme === 'dark' ? '-dark' : ''}.svg`;
 
            // 设置 source -> 大屏横版
            setSourceSrcset($source, normalFile);
             $source.attr('width', sizeNormal.width);
            $source.attr('height', sizeNormal.height);
 
            // 设置 img -> 小屏紧凑版
            setImgSrc($img, compactFile);
            $img.attr('width', sizeCompact.width);
             $img.attr('height', sizeCompact.height);
        });
 
        // ----------------------
        // Citizen 专属
        // ----------------------
        if (document.body.classList.contains('skin-Citizen')) {
             const $li = $('#footer-poweredbycitizenico');
            if ($li.length) {
                const $picture = $li.find('picture');
                const $img = $picture.length ? $picture.find('img') : $li.find('img');
 
                const normalFile = `/resources/assets/poweredby-citizen${theme === 'dark' ? '-dark' : ''}.svg`;
                const compactFile = `/resources/assets/poweredby-citizen-compact${theme === 'dark' ? '-dark' : ''}.svg`;
 
                if ($picture.length) {
                    const $source = $picture.find('source');
                    setSourceSrcset($source, normalFile);
                    $source.attr('width', sizeNormal.width);
                    $source.attr('height', sizeNormal.height);
                }
 
                setImgSrc($img, compactFile);
                $img.attr('width', sizeCompact.width);
                $img.attr('height', sizeCompact.height);
            }
         }
         }
     }
     }


     // ======================
     // 首次运行
    // 触发时机
    // ======================
     updateLogos();
     updateLogos();


     // 监听 <html> class 变化(主题切换)
     // 监听 <html> class 变化(用户在 UI 切换浅/深/自动)
     new MutationObserver(() => updateLogos()).observe(document.documentElement, { attributes: true, attributeFilter: ['class'] });
     var htmlObserver = new MutationObserver(function (mutations) {
        for (var i = 0; i < mutations.length; i++) {
            if (mutations[i].attributeName === 'class') {
                updateLogos();
                break;
            }
        }
    });
    htmlObserver.observe(document.documentElement, { attributes: true, attributeFilter: ['class'] });


     // 监听 body DOM 变动(footer 异步插入)
     // 监听 body DOM 变动,防止 footer 异步插入或被替换
     new MutationObserver(() => updateLogos()).observe(document.body, { childList: true, subtree: true });
     var bodyObserver = new MutationObserver(function () {
        updateLogos();
    });
    bodyObserver.observe(document.body, { childList: true, subtree: true });


     // 监听系统主题 & 屏幕宽度变化
     // 监听系统主题变化(适用于 skin-theme-clientpref-os)
     if (window.matchMedia) {
     if (window.matchMedia) {
         const mqDark = window.matchMedia('(prefers-color-scheme: dark)');
         var mq = window.matchMedia('(prefers-color-scheme: dark)');
         const mqCompact = window.matchMedia('(max-width: 500px)');
         if (typeof mq.addEventListener === 'function') {
 
            mq.addEventListener('change', updateLogos);
        if (mqDark.addEventListener) mqDark.addEventListener('change', updateLogos);
         } else if (typeof mq.addListener === 'function') {
         else if (mqDark.addListener) mqDark.addListener(updateLogos);
            // 兼容旧浏览器
            mq.addListener(updateLogos);
        }
    }


        if (mqCompact.addEventListener) mqCompact.addEventListener('change', updateLogos);
    // 如果存在 MediaWiki 的 hook(例如偏好保存后),也触发一次
         else if (mqCompact.addListener) mqCompact.addListener(updateLogos);
    if (window.mw && mw.hook) {
         mw.hook('user.preferencesSaved').add(updateLogos);
     }
     }


     // MediaWiki hook
     // 兜底:页面 load 后、以及延迟 1.5s 再试一次
    if (window.mw && mw.hook) mw.hook('user.preferencesSaved').add(updateLogos);
 
    // 兜底
     $(window).on('load', updateLogos);
     $(window).on('load', updateLogos);
     setTimeout(updateLogos, 1500);
     setTimeout(updateLogos, 1500);
});
});


// 移除深色模式反馈提示
// 移除深色模式反馈提示