2021-04-27 16:57:54 +00:00
|
|
|
/*
|
2022-04-26 00:29:29 +00:00
|
|
|
* Citizen
|
|
|
|
* Inline script used in includes/Hooks/SkinHooks.php
|
2021-04-27 16:57:54 +00:00
|
|
|
*
|
|
|
|
* https://starcitizen.tools
|
|
|
|
*
|
|
|
|
* Mangle using https://jscompress.com/
|
|
|
|
*/
|
|
|
|
window.applyPref = () => {
|
2022-04-26 00:29:29 +00:00
|
|
|
const prefix = 'skin-citizen-';
|
|
|
|
const cssProps = {
|
|
|
|
fontsize: 'font-size',
|
|
|
|
pagewidth: '--width-layout',
|
|
|
|
lineheight: '--line-height'
|
|
|
|
};
|
|
|
|
|
2021-04-27 16:57:54 +00:00
|
|
|
// Generates an array of prefix-(auto|dark|light) strings
|
2022-04-26 00:29:29 +00:00
|
|
|
const classNames = () => {
|
|
|
|
return [ 'auto', 'dark', 'light' ].map( ( themeType ) => {
|
2021-04-27 16:57:54 +00:00
|
|
|
return prefix + themeType;
|
2022-04-26 00:29:29 +00:00
|
|
|
} );
|
|
|
|
};
|
|
|
|
|
|
|
|
const injectStyles = ( css ) => {
|
|
|
|
const styleId = 'citizen-style';
|
|
|
|
let style = document.getElementById( styleId );
|
|
|
|
|
|
|
|
if ( style === null ) {
|
|
|
|
style = document.createElement( 'style' );
|
|
|
|
style.setAttribute( 'id', styleId );
|
|
|
|
document.head.appendChild( style );
|
|
|
|
}
|
|
|
|
style.textContent = `:root{${css}}`;
|
|
|
|
};
|
2021-04-27 16:57:54 +00:00
|
|
|
|
|
|
|
try {
|
2022-04-26 00:29:29 +00:00
|
|
|
const theme = window.localStorage.getItem( prefix + 'theme' );
|
|
|
|
|
|
|
|
let cssDeclaration = '';
|
|
|
|
|
|
|
|
// Apply pref by changing class
|
2021-04-27 16:57:54 +00:00
|
|
|
if ( theme !== null ) {
|
2022-04-26 00:29:29 +00:00
|
|
|
const htmlElement = document.documentElement;
|
|
|
|
// Remove all theme classes then add the right one
|
|
|
|
// The following classes are used here:
|
|
|
|
// * skin-citizen-auto
|
|
|
|
// * skin-citizen-light
|
|
|
|
// * skin-citizen-dark
|
|
|
|
htmlElement.classList.remove( ...classNames( prefix ) );
|
|
|
|
/* eslint-disable-next-line mediawiki/class-doc */
|
|
|
|
htmlElement.classList.add( prefix + theme );
|
2021-04-27 16:57:54 +00:00
|
|
|
}
|
2022-04-26 00:29:29 +00:00
|
|
|
|
|
|
|
// Apply pref by adding CSS to root
|
|
|
|
/* eslint-disable-next-line compat/compat */
|
|
|
|
for ( const [ key, property ] of Object.entries( cssProps ) ) {
|
|
|
|
const value = window.localStorage.getItem( prefix + key );
|
|
|
|
|
|
|
|
if ( value !== null ) {
|
|
|
|
cssDeclaration += `${property}:${value};`;
|
|
|
|
}
|
2021-04-27 16:57:54 +00:00
|
|
|
}
|
2022-04-26 00:29:29 +00:00
|
|
|
|
|
|
|
if ( cssDeclaration ) {
|
|
|
|
injectStyles( cssDeclaration );
|
2021-08-05 15:24:52 +00:00
|
|
|
}
|
2021-04-27 16:57:54 +00:00
|
|
|
} catch ( e ) {
|
|
|
|
}
|
2022-04-26 00:29:29 +00:00
|
|
|
};
|
2021-04-27 16:57:54 +00:00
|
|
|
|
2022-04-26 00:29:29 +00:00
|
|
|
( () => {
|
|
|
|
const themeId = 'skin-citizen-theme';
|
2022-04-19 22:17:53 +00:00
|
|
|
// Set up auto theme based on prefers-color-scheme
|
2022-04-26 00:29:29 +00:00
|
|
|
if ( window.localStorage.getItem( themeId ) === 'auto' ) {
|
2022-04-19 22:17:53 +00:00
|
|
|
const autoTheme = window.matchMedia( '(prefers-color-scheme: dark)' ) ? 'dark' : 'light';
|
|
|
|
// Set to the right theme temporarily
|
2022-04-26 00:29:29 +00:00
|
|
|
window.localStorage.setItem( themeId, autoTheme );
|
2022-04-19 22:17:53 +00:00
|
|
|
window.applyPref();
|
|
|
|
// Reset back to auto
|
2022-04-26 00:29:29 +00:00
|
|
|
window.localStorage.setItem( themeId, 'auto' );
|
2022-04-19 22:17:53 +00:00
|
|
|
} else {
|
|
|
|
window.applyPref();
|
|
|
|
}
|
2022-04-26 00:29:29 +00:00
|
|
|
} )();
|