MediaWiki:Common.js:修订间差异
小 修正尺寸切换逻辑 标签:已被回退 |
标签:撤销 |
||
| 第31行: | 第31行: | ||
// logo 切换逻辑 | // logo 切换逻辑 | ||
// Common.js - footer logo | // Common.js - footer logo 切换逻辑 | ||
$(function () { | $(function () { | ||
// ====================== | // ====================== | ||
| 第37行: | 第37行: | ||
// ====================== | // ====================== | ||
// | // 判断当前主题:dark / light | ||
function getEffectiveTheme() { | function getEffectiveTheme() { | ||
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 模式(小图) | ||
function isCompactMode() { | function isCompactMode() { | ||
return window.matchMedia && window.matchMedia('(max-width: 500px)').matches; | return window.matchMedia && window.matchMedia('(max-width: 500px)').matches; | ||
} | } | ||
// | // 如果元素存在就设置 src | ||
function | function setSrcIfExists(selector, src) { | ||
if ($ | var $el = $(selector); | ||
if ($el.length) { | |||
$el.attr('src', src); | |||
console.log('[logo-switch] set', selector, '->', src); | |||
} | |||
} | } | ||
| 第69行: | 第73行: | ||
// ====================== | // ====================== | ||
function updateLogos() { | function updateLogos() { | ||
var theme = getEffectiveTheme(); | |||
var compact = isCompactMode(); | |||
// | // 文件名后缀 | ||
var suffix = ''; | |||
if (compact) { | |||
suffix += '-compact'; | |||
} | |||
if (theme === 'dark') { | |||
suffix += '-dark'; | |||
} | |||
// 公共 logo(所有皮肤都显示) | |||
setSrcIfExists('#footer-copyrightico img', '/resources/assets/cc-by-nc-sa' + suffix + '.svg'); | |||
setSrcIfExists('#footer-poweredbyico img', '/resources/assets/poweredby-mediawiki' + suffix + '.svg'); | |||
setSrcIfExists('#footer-poweredbysmwico img', '/resources/assets/poweredby-smw' + suffix + '.svg'); | |||
// Citizen 专属(仅当元素存在时才切换) | |||
// Citizen | |||
if (document.body.classList.contains('skin-Citizen')) { | if (document.body.classList.contains('skin-Citizen')) { | ||
setSrcIfExists('#footer-poweredbycitizenico img', '/resources/assets/poweredby-citizen' + suffix + '.svg'); | |||
} | } | ||
} | } | ||
| 第140行: | 第99行: | ||
// 触发时机 | // 触发时机 | ||
// ====================== | // ====================== | ||
// 首次运行 | |||
updateLogos(); | updateLogos(); | ||
// 监听 <html> class 变化(主题切换) | // 监听 <html> class 变化(主题切换) | ||
new MutationObserver(() = | 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 | // 监听 body DOM 变动,防止 footer 异步插入 | ||
new MutationObserver(() | var bodyObserver = new MutationObserver(function () { | ||
updateLogos(); | |||
}); | |||
bodyObserver.observe(document.body, { childList: true, subtree: true }); | |||
// | // 监听系统主题变化 | ||
if (window.matchMedia) { | if (window.matchMedia) { | ||
var mqDark = window.matchMedia('(prefers-color-scheme: dark)'); | |||
if (typeof mqDark.addEventListener === 'function') { | |||
mqDark.addEventListener('change', updateLogos); | |||
} else if (typeof mqDark.addListener === 'function') { | |||
mqDark.addListener(updateLogos); | |||
} | |||
// 监听屏幕宽度变化(compact <-> normal) | |||
var mqCompact = window.matchMedia('(max-width: 500px)'); | |||
if (typeof mqCompact.addEventListener === 'function') { | |||
if (mqCompact.addEventListener) mqCompact.addEventListener('change', updateLogos); | mqCompact.addEventListener('change', updateLogos); | ||
else if (mqCompact.addListener) mqCompact.addListener(updateLogos); | } else if (typeof mqCompact.addListener === 'function') { | ||
mqCompact.addListener(updateLogos); | |||
} | |||
} | } | ||
// MediaWiki hook | // MediaWiki hook | ||
if (window.mw && mw.hook) mw.hook('user.preferencesSaved').add(updateLogos); | if (window.mw && mw.hook) { | ||
mw.hook('user.preferencesSaved').add(updateLogos); | |||
} | |||
// | // 兜底:页面 load 后、延迟再执行一次 | ||
$(window).on('load', updateLogos); | $(window).on('load', updateLogos); | ||
setTimeout(updateLogos, 1500); | setTimeout(updateLogos, 1500); | ||
}); | }); | ||
// 移除深色模式反馈提示 | // 移除深色模式反馈提示 | ||