2021-09-16 11:47:53 +00:00
|
|
|
import variables from 'modules/variables';
|
2021-07-02 20:29:33 +00:00
|
|
|
import experimentalInit from '../experimental';
|
2021-03-31 20:54:16 +00:00
|
|
|
|
2022-04-13 14:25:50 +00:00
|
|
|
import * as defaultSettings from 'modules/default_settings.json';
|
|
|
|
import * as languages from 'modules/languages.json';
|
2020-08-10 18:49:46 +00:00
|
|
|
|
2021-09-23 14:56:41 +00:00
|
|
|
const getMessage = (text) => variables.language.getMessage(variables.languagecode, text);
|
2021-09-16 11:47:53 +00:00
|
|
|
|
2021-08-14 19:10:48 +00:00
|
|
|
export function setDefaultSettings(reset) {
|
|
|
|
localStorage.clear();
|
|
|
|
defaultSettings.forEach((element) => localStorage.setItem(element.name, element.value));
|
|
|
|
|
|
|
|
// Languages
|
|
|
|
const languageCodes = languages.map(({ value }) => value);
|
2022-04-11 21:57:07 +00:00
|
|
|
const browserLanguage =
|
|
|
|
(navigator.languages &&
|
|
|
|
navigator.languages.find((lang) => lang.replace('-', '_') && languageCodes.includes(lang))) ||
|
|
|
|
navigator.language.replace('-', '_');
|
2021-08-14 19:10:48 +00:00
|
|
|
|
|
|
|
if (languageCodes.includes(browserLanguage)) {
|
|
|
|
localStorage.setItem('language', browserLanguage);
|
|
|
|
} else {
|
|
|
|
localStorage.setItem('language', 'en_GB');
|
|
|
|
}
|
2020-08-10 18:49:46 +00:00
|
|
|
|
2021-09-23 14:56:41 +00:00
|
|
|
localStorage.setItem('tabName', getMessage('tabname'));
|
2021-03-30 21:29:42 +00:00
|
|
|
|
2021-08-14 19:10:48 +00:00
|
|
|
if (reset) {
|
|
|
|
localStorage.setItem('showWelcome', false);
|
2021-03-17 13:01:53 +00:00
|
|
|
}
|
|
|
|
|
2021-08-14 19:10:48 +00:00
|
|
|
// Finally we set this to true so it doesn't run the function on every load
|
|
|
|
localStorage.setItem('firstRun', true);
|
2021-08-21 10:11:00 +00:00
|
|
|
}
|
2021-08-14 19:10:48 +00:00
|
|
|
|
|
|
|
export function loadSettings(hotreload) {
|
2021-08-22 21:12:47 +00:00
|
|
|
switch (localStorage.getItem('theme')) {
|
2021-08-14 19:10:48 +00:00
|
|
|
case 'dark':
|
|
|
|
document.body.classList.add('dark');
|
2022-04-08 13:48:36 +00:00
|
|
|
document.body.classList.remove('light');
|
2021-08-14 19:10:48 +00:00
|
|
|
break;
|
|
|
|
case 'auto':
|
|
|
|
if (window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches) {
|
2021-04-13 09:51:36 +00:00
|
|
|
document.body.classList.add('dark');
|
2021-08-14 19:10:48 +00:00
|
|
|
} else {
|
2021-04-13 09:51:36 +00:00
|
|
|
document.body.classList.remove('dark');
|
2022-04-08 13:48:36 +00:00
|
|
|
document.body.classList.add('light');
|
2021-03-17 13:01:53 +00:00
|
|
|
}
|
2021-08-14 19:10:48 +00:00
|
|
|
break;
|
|
|
|
default:
|
2022-04-08 13:48:36 +00:00
|
|
|
document.body.classList.add('light');
|
2021-08-14 19:10:48 +00:00
|
|
|
document.body.classList.remove('dark');
|
|
|
|
}
|
2021-03-17 13:01:53 +00:00
|
|
|
|
2021-09-23 14:56:41 +00:00
|
|
|
document.title = localStorage.getItem('tabName') || getMessage('tabname');
|
2021-05-02 11:11:07 +00:00
|
|
|
|
2021-08-14 19:10:48 +00:00
|
|
|
if (hotreload === true) {
|
2021-08-22 21:12:47 +00:00
|
|
|
// remove old custom stuff and add new
|
|
|
|
const custom = ['customcss', 'customfont'];
|
2021-08-14 19:10:48 +00:00
|
|
|
custom.forEach((element) => {
|
2021-07-04 11:28:28 +00:00
|
|
|
try {
|
2021-08-14 19:10:48 +00:00
|
|
|
document.head.removeChild(document.getElementById(element));
|
2021-07-04 11:28:28 +00:00
|
|
|
} catch (e) {
|
2021-08-22 21:12:47 +00:00
|
|
|
// Disregard exception if custom stuff doesn't exist
|
2021-07-04 11:28:28 +00:00
|
|
|
}
|
2021-08-14 19:10:48 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-04-11 21:57:07 +00:00
|
|
|
if (localStorage.getItem('animations') === 'false') {
|
2021-08-25 11:01:48 +00:00
|
|
|
document.body.classList.add('no-animations');
|
|
|
|
} else {
|
|
|
|
document.body.classList.remove('no-animations');
|
|
|
|
}
|
|
|
|
|
2022-04-08 13:48:36 +00:00
|
|
|
// technically, this is text SHADOW, and not BORDER
|
|
|
|
// however it's a mess and we'll just leave it at this for now
|
|
|
|
const textBorder = localStorage.getItem('textBorder');
|
|
|
|
// enable/disable old text border from before redesign
|
|
|
|
if (textBorder === 'true') {
|
2021-08-25 12:28:41 +00:00
|
|
|
const elements = ['greeting', 'clock', 'quote', 'quoteauthor', 'date'];
|
2021-08-25 11:01:48 +00:00
|
|
|
elements.forEach((element) => {
|
|
|
|
try {
|
2021-08-25 12:28:41 +00:00
|
|
|
document.querySelector('.' + element).classList.add('textBorder');
|
|
|
|
} catch (e) {
|
2022-04-11 21:57:07 +00:00
|
|
|
// Disregard exception
|
2021-08-25 12:28:41 +00:00
|
|
|
}
|
2021-08-25 11:01:48 +00:00
|
|
|
});
|
|
|
|
} else {
|
2021-08-25 12:28:41 +00:00
|
|
|
const elements = ['greeting', 'clock', 'quote', 'quoteauthor', 'date'];
|
2021-08-25 11:01:48 +00:00
|
|
|
elements.forEach((element) => {
|
|
|
|
try {
|
2021-08-25 12:28:41 +00:00
|
|
|
document.querySelector('.' + element).classList.remove('textBorder');
|
|
|
|
} catch (e) {
|
2022-04-11 21:57:07 +00:00
|
|
|
// Disregard exception
|
2021-08-25 12:28:41 +00:00
|
|
|
}
|
2021-08-25 11:01:48 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-04-08 13:48:36 +00:00
|
|
|
// remove actual default shadow
|
|
|
|
if (textBorder === 'none') {
|
|
|
|
document.getElementById('center').classList.add('no-textBorder');
|
|
|
|
} else {
|
|
|
|
document.getElementById('center').classList.remove('no-textBorder');
|
|
|
|
}
|
|
|
|
|
2021-08-14 19:10:48 +00:00
|
|
|
const css = localStorage.getItem('customcss');
|
|
|
|
if (css) {
|
|
|
|
document.head.insertAdjacentHTML('beforeend', '<style id="customcss">' + css + '</style>');
|
|
|
|
}
|
2021-06-13 12:47:18 +00:00
|
|
|
|
2021-08-14 19:10:48 +00:00
|
|
|
const font = localStorage.getItem('font');
|
|
|
|
if (font) {
|
|
|
|
let url = '';
|
|
|
|
if (localStorage.getItem('fontGoogle') === 'true') {
|
|
|
|
url = `@import url('https://fonts.googleapis.com/css2?family=${font}&display=swap');`;
|
2021-03-31 20:54:16 +00:00
|
|
|
}
|
|
|
|
|
2022-04-11 21:57:07 +00:00
|
|
|
document.head.insertAdjacentHTML(
|
|
|
|
'beforeend',
|
|
|
|
`
|
2021-08-14 19:10:48 +00:00
|
|
|
<style id='customfont'>
|
|
|
|
${url}
|
|
|
|
* {
|
|
|
|
font-family: '${font}', 'Lexend Deca', 'Montserrat', sans-serif !important;
|
|
|
|
font-weight: ${localStorage.getItem('fontweight')};
|
|
|
|
font-style: ${localStorage.getItem('fontstyle')};
|
|
|
|
}
|
|
|
|
</style>
|
2022-04-11 21:57:07 +00:00
|
|
|
`,
|
|
|
|
);
|
2021-03-17 13:01:53 +00:00
|
|
|
}
|
2021-04-16 18:09:56 +00:00
|
|
|
|
2022-03-06 17:28:25 +00:00
|
|
|
// everything below this shouldn't run on a hot reload event
|
2021-08-14 19:10:48 +00:00
|
|
|
if (hotreload === true) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (localStorage.getItem('experimental') === 'true') {
|
|
|
|
experimentalInit();
|
|
|
|
}
|
2021-04-17 20:38:16 +00:00
|
|
|
|
2021-08-14 19:10:48 +00:00
|
|
|
// easter egg
|
|
|
|
console.log(`
|
|
|
|
█████████████████████████████████████████████████████████████
|
|
|
|
██ ██
|
2021-11-17 21:50:34 +00:00
|
|
|
██ ███ ███ ██ ██ ███████ ██
|
|
|
|
██ ████ ████ ██ ██ ██ ██
|
|
|
|
██ ██ ████ ██ ██ ██ █████ ██
|
|
|
|
██ ██ ██ ██ ██ ██ ██ ██
|
|
|
|
██ ██ ██ ██████ ███████ ██
|
2021-08-14 19:10:48 +00:00
|
|
|
██ ██
|
|
|
|
██ ██
|
|
|
|
██ Copyright 2018-${new Date().getFullYear()} The Mue Authors ██
|
|
|
|
██ GitHub: https://github.com/mue/mue ██
|
|
|
|
██ ██
|
|
|
|
██ Thank you for using Mue! ██
|
|
|
|
██ Feedback: hello@muetab.com ██
|
|
|
|
█████████████████████████████████████████████████████████████
|
|
|
|
`);
|
2021-08-27 17:27:11 +00:00
|
|
|
}
|
2021-04-16 18:09:56 +00:00
|
|
|
|
2022-04-11 21:57:07 +00:00
|
|
|
// in a nutshell, this function saves all of the current settings, resets them, sets the defaults and then overrides
|
2021-08-14 19:10:48 +00:00
|
|
|
// the new settings with the old saved messages where they exist
|
|
|
|
export function moveSettings() {
|
2021-08-22 21:12:47 +00:00
|
|
|
const currentSettings = Object.keys(localStorage);
|
|
|
|
if (currentSettings.length === 0) {
|
2021-08-14 19:10:48 +00:00
|
|
|
return this.setDefaultSettings();
|
2021-04-16 18:09:56 +00:00
|
|
|
}
|
2021-08-14 19:10:48 +00:00
|
|
|
|
2021-08-22 21:12:47 +00:00
|
|
|
const settings = {};
|
|
|
|
currentSettings.forEach((key) => {
|
2021-08-14 19:10:48 +00:00
|
|
|
settings[key] = localStorage.getItem(key);
|
|
|
|
});
|
|
|
|
|
|
|
|
localStorage.clear();
|
|
|
|
setDefaultSettings();
|
2021-08-22 21:12:47 +00:00
|
|
|
|
2021-08-14 19:10:48 +00:00
|
|
|
Object.keys(settings).forEach((key) => {
|
|
|
|
localStorage.setItem(key, settings[key]);
|
|
|
|
});
|
2021-08-27 18:42:40 +00:00
|
|
|
}
|