mirror of
https://github.com/mue/mue.git
synced 2026-06-12 03:28:46 +02:00
refactor: imports etc
This commit is contained in:
@@ -1,18 +1,16 @@
|
||||
export default class Date {
|
||||
static nth(d) {
|
||||
if (d > 3 && d < 21) {
|
||||
return d + 'th';
|
||||
}
|
||||
export default function nth(d) {
|
||||
if (d > 3 && d < 21) {
|
||||
return d + 'th';
|
||||
}
|
||||
|
||||
switch (d % 10) {
|
||||
case 1:
|
||||
return d + 'st';
|
||||
case 2:
|
||||
return d + 'nd';
|
||||
case 3:
|
||||
return d + 'rd';
|
||||
default:
|
||||
return d + 'th';
|
||||
}
|
||||
switch (d % 10) {
|
||||
case 1:
|
||||
return d + 'st';
|
||||
case 2:
|
||||
return d + 'nd';
|
||||
case 3:
|
||||
return d + 'rd';
|
||||
default:
|
||||
return d + 'th';
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,101 +1,99 @@
|
||||
import EventBus from './eventbus';
|
||||
|
||||
export default class MarketplaceFunctions {
|
||||
// based on https://stackoverflow.com/questions/37684/how-to-replace-plain-urls-with-links
|
||||
static urlParser(input) {
|
||||
const urlPattern = /https?:\/\/(www\.)?[-a-zA-Z0-9@:%._+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()!@:%_+.~#?&//=]*)/;
|
||||
return input.replace(urlPattern, '<a href="$&" target="_blank">$&</a>');
|
||||
}
|
||||
|
||||
static uninstall(type, name) {
|
||||
switch (type) {
|
||||
case 'settings':
|
||||
const oldSettings = JSON.parse(localStorage.getItem('backup_settings'));
|
||||
localStorage.clear();
|
||||
oldSettings.forEach((item) => {
|
||||
localStorage.setItem(item.name, item.value);
|
||||
});
|
||||
break;
|
||||
case 'quotes':
|
||||
localStorage.removeItem('quote_packs');
|
||||
localStorage.removeItem('quoteAPI');
|
||||
localStorage.setItem('quoteType', localStorage.getItem('oldQuoteType'));
|
||||
localStorage.removeItem('oldQuoteType');
|
||||
EventBus.dispatch('refresh', 'marketplacequoteuninstall');
|
||||
break;
|
||||
case 'photos':
|
||||
localStorage.removeItem('photo_packs');
|
||||
localStorage.setItem('backgroundType', localStorage.getItem('oldBackgroundType'));
|
||||
localStorage.removeItem('oldBackgroundType');
|
||||
EventBus.dispatch('refresh', 'marketplacebackgrounduninstall');
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
let installed = JSON.parse(localStorage.getItem('installed'));
|
||||
for (let i = 0; i < installed.length; i++) {
|
||||
if (installed[i].name === name) {
|
||||
installed.splice(i, 1);
|
||||
break;
|
||||
}
|
||||
}
|
||||
localStorage.setItem('installed', JSON.stringify(installed));
|
||||
}
|
||||
|
||||
static install(type, input, sideload) {
|
||||
switch (type) {
|
||||
case 'settings':
|
||||
localStorage.removeItem('backup_settings');
|
||||
|
||||
let oldSettings = [];
|
||||
Object.keys(localStorage).forEach((key) => {
|
||||
oldSettings.push({
|
||||
name: key,
|
||||
value: localStorage.getItem(key)
|
||||
});
|
||||
});
|
||||
|
||||
localStorage.setItem('backup_settings', JSON.stringify(oldSettings));
|
||||
Object.keys(input.settings).forEach((key) => {
|
||||
localStorage.setItem(key, input.settings[key]);
|
||||
});
|
||||
break;
|
||||
|
||||
case 'photos':
|
||||
localStorage.setItem('photo_packs', JSON.stringify(input.photos));
|
||||
localStorage.setItem('oldBackgroundType', localStorage.getItem('backgroundType'));
|
||||
localStorage.setItem('backgroundType', 'photo_pack');
|
||||
EventBus.dispatch('refresh', 'background');
|
||||
break;
|
||||
|
||||
case 'quotes':
|
||||
if (input.quote_api) {
|
||||
localStorage.setItem('quoteAPI', JSON.stringify(input.quote_api));
|
||||
}
|
||||
|
||||
localStorage.setItem('quote_packs', JSON.stringify(input.quotes));
|
||||
localStorage.setItem('oldQuoteType', localStorage.getItem('quoteType'));
|
||||
localStorage.setItem('quoteType', 'quote_pack');
|
||||
EventBus.dispatch('refresh', 'quote');
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
const installed = JSON.parse(localStorage.getItem('installed'));
|
||||
|
||||
if (sideload) {
|
||||
installed.push({
|
||||
content: {
|
||||
updated: 'Unpublished',
|
||||
data: input
|
||||
}
|
||||
});
|
||||
} else {
|
||||
installed.push(input);
|
||||
}
|
||||
|
||||
localStorage.setItem('installed', JSON.stringify(installed));
|
||||
}
|
||||
// based on https://stackoverflow.com/questions/37684/how-to-replace-plain-urls-with-links
|
||||
export function urlParser(input) {
|
||||
const urlPattern = /https?:\/\/(www\.)?[-a-zA-Z0-9@:%._+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()!@:%_+.~#?&//=]*)/;
|
||||
return input.replace(urlPattern, '<a href="$&" target="_blank">$&</a>');
|
||||
}
|
||||
|
||||
export function uninstall(type, name) {
|
||||
switch (type) {
|
||||
case 'settings':
|
||||
const oldSettings = JSON.parse(localStorage.getItem('backup_settings'));
|
||||
localStorage.clear();
|
||||
oldSettings.forEach((item) => {
|
||||
localStorage.setItem(item.name, item.value);
|
||||
});
|
||||
break;
|
||||
case 'quotes':
|
||||
localStorage.removeItem('quote_packs');
|
||||
localStorage.removeItem('quoteAPI');
|
||||
localStorage.setItem('quoteType', localStorage.getItem('oldQuoteType'));
|
||||
localStorage.removeItem('oldQuoteType');
|
||||
EventBus.dispatch('refresh', 'marketplacequoteuninstall');
|
||||
break;
|
||||
case 'photos':
|
||||
localStorage.removeItem('photo_packs');
|
||||
localStorage.setItem('backgroundType', localStorage.getItem('oldBackgroundType'));
|
||||
localStorage.removeItem('oldBackgroundType');
|
||||
EventBus.dispatch('refresh', 'marketplacebackgrounduninstall');
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
let installed = JSON.parse(localStorage.getItem('installed'));
|
||||
for (let i = 0; i < installed.length; i++) {
|
||||
if (installed[i].name === name) {
|
||||
installed.splice(i, 1);
|
||||
break;
|
||||
}
|
||||
}
|
||||
localStorage.setItem('installed', JSON.stringify(installed));
|
||||
}
|
||||
|
||||
export function install(type, input, sideload) {
|
||||
switch (type) {
|
||||
case 'settings':
|
||||
localStorage.removeItem('backup_settings');
|
||||
|
||||
let oldSettings = [];
|
||||
Object.keys(localStorage).forEach((key) => {
|
||||
oldSettings.push({
|
||||
name: key,
|
||||
value: localStorage.getItem(key)
|
||||
});
|
||||
});
|
||||
|
||||
localStorage.setItem('backup_settings', JSON.stringify(oldSettings));
|
||||
Object.keys(input.settings).forEach((key) => {
|
||||
localStorage.setItem(key, input.settings[key]);
|
||||
});
|
||||
break;
|
||||
|
||||
case 'photos':
|
||||
localStorage.setItem('photo_packs', JSON.stringify(input.photos));
|
||||
localStorage.setItem('oldBackgroundType', localStorage.getItem('backgroundType'));
|
||||
localStorage.setItem('backgroundType', 'photo_pack');
|
||||
EventBus.dispatch('refresh', 'background');
|
||||
break;
|
||||
|
||||
case 'quotes':
|
||||
if (input.quote_api) {
|
||||
localStorage.setItem('quoteAPI', JSON.stringify(input.quote_api));
|
||||
}
|
||||
|
||||
localStorage.setItem('quote_packs', JSON.stringify(input.quotes));
|
||||
localStorage.setItem('oldQuoteType', localStorage.getItem('quoteType'));
|
||||
localStorage.setItem('quoteType', 'quote_pack');
|
||||
EventBus.dispatch('refresh', 'quote');
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
const installed = JSON.parse(localStorage.getItem('installed'));
|
||||
|
||||
if (sideload) {
|
||||
installed.push({
|
||||
content: {
|
||||
updated: 'Unpublished',
|
||||
data: input
|
||||
}
|
||||
});
|
||||
} else {
|
||||
installed.push(input);
|
||||
}
|
||||
|
||||
localStorage.setItem('installed', JSON.stringify(installed));
|
||||
}
|
||||
|
||||
@@ -3,144 +3,142 @@ import experimentalInit from '../experimental';
|
||||
const defaultSettings = require('../../default_settings.json');
|
||||
const languages = require('../../languages.json');
|
||||
|
||||
export default class SettingsFunctions {
|
||||
static setDefaultSettings(reset) {
|
||||
localStorage.clear();
|
||||
defaultSettings.forEach((element) => localStorage.setItem(element.name, element.value));
|
||||
export function setDefaultSettings(reset) {
|
||||
localStorage.clear();
|
||||
defaultSettings.forEach((element) => localStorage.setItem(element.name, element.value));
|
||||
|
||||
// Languages
|
||||
const languageCodes = languages.map(({ value }) => value);
|
||||
const browserLanguage = (navigator.languages && navigator.languages.find((lang) => lang.replace('-', '_') && languageCodes.includes(lang))) || navigator.language.replace('-', '_');
|
||||
// Languages
|
||||
const languageCodes = languages.map(({ value }) => value);
|
||||
const browserLanguage = (navigator.languages && navigator.languages.find((lang) => lang.replace('-', '_') && languageCodes.includes(lang))) || navigator.language.replace('-', '_');
|
||||
|
||||
if (languageCodes.includes(browserLanguage)) {
|
||||
localStorage.setItem('language', browserLanguage);
|
||||
} else {
|
||||
localStorage.setItem('language', 'en_GB');
|
||||
}
|
||||
|
||||
localStorage.setItem('tabName', window.language.tabname);
|
||||
|
||||
if (reset) {
|
||||
localStorage.setItem('showWelcome', false);
|
||||
}
|
||||
|
||||
// Finally we set this to true so it doesn't run the function on every load
|
||||
localStorage.setItem('firstRun', true);
|
||||
if (languageCodes.includes(browserLanguage)) {
|
||||
localStorage.setItem('language', browserLanguage);
|
||||
} else {
|
||||
localStorage.setItem('language', 'en_GB');
|
||||
}
|
||||
|
||||
static loadSettings(hotreload) {
|
||||
document.getElementById('widgets').style.zoom = localStorage.getItem('widgetzoom') + '%';
|
||||
localStorage.setItem('tabName', window.language.tabname);
|
||||
|
||||
const theme = localStorage.getItem('theme');
|
||||
switch (theme) {
|
||||
case 'dark':
|
||||
document.body.classList.add('dark');
|
||||
break;
|
||||
case 'auto':
|
||||
if (window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches) {
|
||||
document.body.classList.add('dark');
|
||||
} else {
|
||||
document.body.classList.remove('dark');
|
||||
}
|
||||
break;
|
||||
default:
|
||||
document.body.classList.remove('dark');
|
||||
}
|
||||
|
||||
const tabName = localStorage.getItem('tabName') || window.language.tabname;
|
||||
document.title = tabName;
|
||||
|
||||
if (hotreload === true) {
|
||||
const custom = ['customcss', 'customjs', 'customfont'];
|
||||
custom.forEach((element) => {
|
||||
try {
|
||||
document.head.removeChild(document.getElementById(element));
|
||||
} catch (e) {
|
||||
// Disregard exception
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
const css = localStorage.getItem('customcss');
|
||||
if (css) {
|
||||
document.head.insertAdjacentHTML('beforeend', '<style id="customcss">' + css + '</style>');
|
||||
}
|
||||
|
||||
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');`;
|
||||
}
|
||||
|
||||
document.head.insertAdjacentHTML('beforeend', `
|
||||
<style id='customfont'>
|
||||
${url}
|
||||
* {
|
||||
font-family: '${font}', 'Lexend Deca', 'Montserrat', sans-serif !important;
|
||||
font-weight: ${localStorage.getItem('fontweight')};
|
||||
font-style: ${localStorage.getItem('fontstyle')};
|
||||
}
|
||||
</style>
|
||||
`);
|
||||
}
|
||||
|
||||
// everything below this either doesn't support hot reload (custom js) or shouldn't run on a hot reload event
|
||||
if (hotreload === true) {
|
||||
return;
|
||||
}
|
||||
|
||||
const js = localStorage.getItem('customjs');
|
||||
if (js) {
|
||||
try {
|
||||
// eslint-disable-next-line no-eval
|
||||
eval(js);
|
||||
} catch (e) {
|
||||
console.error('Failed to run custom JS: ', e);
|
||||
}
|
||||
}
|
||||
|
||||
if (localStorage.getItem('experimental') === 'true') {
|
||||
experimentalInit();
|
||||
}
|
||||
|
||||
// easter egg
|
||||
console.log(`
|
||||
█████████████████████████████████████████████████████████████
|
||||
██ ██
|
||||
██ ███ ███ ██ ██ ███████ ██
|
||||
██ ████ ████ ██ ██ ██ ██
|
||||
██ ██ ████ ██ ██ ██ █████ ██
|
||||
██ ██ ██ ██ ██ ██ ██ ██
|
||||
██ ██ ██ ██████ ███████ ██
|
||||
██ ██
|
||||
██ ██
|
||||
██ Copyright 2018-${new Date().getFullYear()} The Mue Authors ██
|
||||
██ GitHub: https://github.com/mue/mue ██
|
||||
██ ██
|
||||
██ Thank you for using Mue! ██
|
||||
██ Feedback: hello@muetab.com ██
|
||||
█████████████████████████████████████████████████████████████
|
||||
`);
|
||||
if (reset) {
|
||||
localStorage.setItem('showWelcome', false);
|
||||
}
|
||||
|
||||
// in a nutshell, this function saves all of the current settings, resets them, sets the defaults and then overrides
|
||||
// the new settings with the old saved messages where they exist
|
||||
static moveSettings() {
|
||||
if (Object.keys(localStorage).length === 0) {
|
||||
return this.setDefaultSettings();
|
||||
}
|
||||
|
||||
let settings = {};
|
||||
Object.keys(localStorage).forEach((key) => {
|
||||
settings[key] = localStorage.getItem(key);
|
||||
});
|
||||
|
||||
localStorage.clear();
|
||||
|
||||
this.setDefaultSettings();
|
||||
Object.keys(settings).forEach((key) => {
|
||||
localStorage.setItem(key, settings[key]);
|
||||
});
|
||||
}
|
||||
// Finally we set this to true so it doesn't run the function on every load
|
||||
localStorage.setItem('firstRun', true);
|
||||
}
|
||||
|
||||
export function loadSettings(hotreload) {
|
||||
document.getElementById('widgets').style.zoom = localStorage.getItem('widgetzoom') + '%';
|
||||
|
||||
const theme = localStorage.getItem('theme');
|
||||
switch (theme) {
|
||||
case 'dark':
|
||||
document.body.classList.add('dark');
|
||||
break;
|
||||
case 'auto':
|
||||
if (window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches) {
|
||||
document.body.classList.add('dark');
|
||||
} else {
|
||||
document.body.classList.remove('dark');
|
||||
}
|
||||
break;
|
||||
default:
|
||||
document.body.classList.remove('dark');
|
||||
}
|
||||
|
||||
const tabName = localStorage.getItem('tabName') || window.language.tabname;
|
||||
document.title = tabName;
|
||||
|
||||
if (hotreload === true) {
|
||||
const custom = ['customcss', 'customjs', 'customfont'];
|
||||
custom.forEach((element) => {
|
||||
try {
|
||||
document.head.removeChild(document.getElementById(element));
|
||||
} catch (e) {
|
||||
// Disregard exception
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
const css = localStorage.getItem('customcss');
|
||||
if (css) {
|
||||
document.head.insertAdjacentHTML('beforeend', '<style id="customcss">' + css + '</style>');
|
||||
}
|
||||
|
||||
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');`;
|
||||
}
|
||||
|
||||
document.head.insertAdjacentHTML('beforeend', `
|
||||
<style id='customfont'>
|
||||
${url}
|
||||
* {
|
||||
font-family: '${font}', 'Lexend Deca', 'Montserrat', sans-serif !important;
|
||||
font-weight: ${localStorage.getItem('fontweight')};
|
||||
font-style: ${localStorage.getItem('fontstyle')};
|
||||
}
|
||||
</style>
|
||||
`);
|
||||
}
|
||||
|
||||
// everything below this either doesn't support hot reload (custom js) or shouldn't run on a hot reload event
|
||||
if (hotreload === true) {
|
||||
return;
|
||||
}
|
||||
|
||||
const js = localStorage.getItem('customjs');
|
||||
if (js) {
|
||||
try {
|
||||
// eslint-disable-next-line no-eval
|
||||
eval(js);
|
||||
} catch (e) {
|
||||
console.error('Failed to run custom JS: ', e);
|
||||
}
|
||||
}
|
||||
|
||||
if (localStorage.getItem('experimental') === 'true') {
|
||||
experimentalInit();
|
||||
}
|
||||
|
||||
// easter egg
|
||||
console.log(`
|
||||
█████████████████████████████████████████████████████████████
|
||||
██ ██
|
||||
██ ███ ███ ██ ██ ███████ ██
|
||||
██ ████ ████ ██ ██ ██ ██
|
||||
██ ██ ████ ██ ██ ██ █████ ██
|
||||
██ ██ ██ ██ ██ ██ ██ ██
|
||||
██ ██ ██ ██████ ███████ ██
|
||||
██ ██
|
||||
██ ██
|
||||
██ Copyright 2018-${new Date().getFullYear()} The Mue Authors ██
|
||||
██ GitHub: https://github.com/mue/mue ██
|
||||
██ ██
|
||||
██ Thank you for using Mue! ██
|
||||
██ Feedback: hello@muetab.com ██
|
||||
█████████████████████████████████████████████████████████████
|
||||
`);
|
||||
}
|
||||
|
||||
// in a nutshell, this function saves all of the current settings, resets them, sets the defaults and then overrides
|
||||
// the new settings with the old saved messages where they exist
|
||||
export function moveSettings() {
|
||||
if (Object.keys(localStorage).length === 0) {
|
||||
return this.setDefaultSettings();
|
||||
}
|
||||
|
||||
let settings = {};
|
||||
Object.keys(localStorage).forEach((key) => {
|
||||
settings[key] = localStorage.getItem(key);
|
||||
});
|
||||
|
||||
localStorage.clear();
|
||||
|
||||
setDefaultSettings();
|
||||
Object.keys(settings).forEach((key) => {
|
||||
localStorage.setItem(key, settings[key]);
|
||||
});
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
import { toast } from 'react-toastify';
|
||||
|
||||
const saveFile = (data, filename = 'file') => {
|
||||
export function saveFile(data, filename = 'file') {
|
||||
if (typeof data === 'object') {
|
||||
data = JSON.stringify(data, undefined, 4);
|
||||
}
|
||||
@@ -17,28 +17,22 @@ const saveFile = (data, filename = 'file') => {
|
||||
a.dispatchEvent(e);
|
||||
};
|
||||
|
||||
export default class ModalSettingsFunctions {
|
||||
static exportSettings() {
|
||||
let settings = {};
|
||||
Object.keys(localStorage).forEach((key) => {
|
||||
settings[key] = localStorage.getItem(key);
|
||||
});
|
||||
saveFile(settings, 'mue-settings.json');
|
||||
window.stats.postEvent('tab', 'Settings exported');
|
||||
}
|
||||
export function exportSettings() {
|
||||
let settings = {};
|
||||
Object.keys(localStorage).forEach((key) => {
|
||||
settings[key] = localStorage.getItem(key);
|
||||
});
|
||||
saveFile(settings, 'mue-settings.json');
|
||||
window.stats.postEvent('tab', 'Settings exported');
|
||||
}
|
||||
|
||||
static saveFile(data, filename) {
|
||||
saveFile(data, filename);
|
||||
}
|
||||
export function importSettings(e) {
|
||||
const content = JSON.parse(e.target.result);
|
||||
|
||||
static importSettings(e) {
|
||||
const content = JSON.parse(e.target.result);
|
||||
Object.keys(content).forEach((key) => {
|
||||
localStorage.setItem(key, content[key]);
|
||||
});
|
||||
|
||||
Object.keys(content).forEach((key) => {
|
||||
localStorage.setItem(key, content[key]);
|
||||
});
|
||||
|
||||
toast(window.language.toasts.imported);
|
||||
window.stats.postEvent('tab', 'Settings imported');
|
||||
}
|
||||
}
|
||||
toast(window.language.toasts.imported);
|
||||
window.stats.postEvent('tab', 'Settings imported');
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user