MediaWiki:Gadget-dark-mode-toggle.js: Difference between revisions
Content deleted Content added
MusikAnimal (talk | contribs) add note that messages must match the ::before content in the CSS |
sync requested change from User:Nardog/dark-mode-toggle.js |
||
Line 5:
*/
// 'Dark mode' and 'Light mode' messages must match the ::before content in
// [[MediaWiki:Gadget-dark-mode-toggle-pagestyles.css]] and [[MediaWiki:Gadget-dark-mode.css]], respectively. mw.messages.set({
'darkmode-turn-on-label': 'Dark mode',
'darkmode-turn-on-tooltip': 'Turn
'darkmode-turn-off-label': 'Light mode',
'darkmode-turn-off-tooltip': 'Turn
});
$.when($.ready, mw.loader.using(['mediawiki.util', 'mediawiki.api', 'mediawiki.Uri', 'mediawiki.storage', 'es6-polyfills'])).then(function() {
var
if (isOn) {
var onOrOff = inDarkMode ? 'off' : 'on';▼
// CSS class for externally styling elements in dark mode via TemplateStyles (or CSS from other gadgets or common.css)
// A brief flash of the original styles will occur, so this is only suitable for style changes for which flashes are tolerable.
// For others, update Gadget-dark-mode.css directly which is loaded without FOUCs
document.documentElement.classList.add('client-dark-mode');
}
var label = mw.msg('darkmode-turn-' + onOrOff + '-label');
var tooltip = mw.msg('darkmode-turn-' + onOrOff + '-tooltip');
var nextnode = mw.config.get('skin')
var portletLink = mw.util.addPortletLink('p-personal', '#', label, 'pt-darkmode', tooltip, '', nextnode);
function toggleMode() {
var
new mw.Api().saveOption('gadget-dark-mode',
mw.user.options.set('gadget-dark-mode',
$(document.documentElement).toggleClass('client-dark-mode', !!newState);
// In case the user navigates to another page too quickly
var onOrOff = darkMode ? 'off' : 'on';▼
// Toggle portlet link label and tooltip
Line 36 ⟶ 48:
// Modify the <link> element on the page to include/exclude dark-mode styles
// We can't use mw.loader as it doesn't work both ways (see talk page)
var scriptPath = mw.util.wikiScript('load');
var gadgetsLinkElement = $('link[rel="stylesheet"][href^="' + scriptPath + '?"][href*="ext.gadget."]')
if (gadgetsLinkElement) {
var uri = new mw.Uri(gadgetsLinkElement.href);
if (
uri.query.modules += ',dark-mode';
} else {
Line 48 ⟶ 61:
gadgetsLinkElement.href = uri.getRelativePath();
} else {
// No
$('<link>').attr(
rel '&modules=ext.gadget.dark-mode&only=styles&skin=' + mw.config.get('skin')
}).appendTo( }
}
$(
e.preventDefault();
toggleMode();
});
// Recover state if the navigation was too quick
var storageState = mw.storage.session.get('dark-mode-toggled');
if (storageState && Number(storageState) !== Number(isOn)) {
toggleMode();
}
if (window.wpDarkModeAutoToggle) {
var toggleBasedOnSystemColourScheme = function () {
var systemSchemeNow = matchMedia('(prefers-color-scheme: dark)').matches
var systemSchemeLast = mw.storage.get('dark-mode-system-scheme')
var wpSchemeNow = !!mw.user.options.get('gadget-dark-mode')
if (systemSchemeNow !== systemSchemeLast)
if (systemSchemeNow !== wpSchemeNow) { toggleMode();
}
mw.requestIdleCallback(function() {
mw.storage.set('dark-mode-system-scheme', Number(systemSchemeNow));
});
}
▲ mw.storage.set('dark-mode-system-scheme', systemSchemeNow);
};
Line 83 ⟶ 108:
}
}
});
|