2018-01-30 16:59:27 +01:00
|
|
|
/**
|
|
|
|
* BetterDiscord Theme Manager Module
|
|
|
|
* Copyright (c) 2015-present Jiiks/JsSucks - https://github.com/Jiiks / https://github.com/JsSucks
|
|
|
|
* All rights reserved.
|
|
|
|
* https://betterdiscord.net
|
|
|
|
*
|
|
|
|
* This source code is licensed under the MIT license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import ContentManager from './contentmanager';
|
2018-02-14 00:23:52 +01:00
|
|
|
import Theme from './theme';
|
2018-02-21 02:16:41 +01:00
|
|
|
import { FileUtils } from 'common';
|
2018-02-28 19:14:29 +01:00
|
|
|
import path from 'path';
|
2018-01-30 16:59:27 +01:00
|
|
|
|
2018-02-11 20:31:24 +01:00
|
|
|
export default class ThemeManager extends ContentManager {
|
2018-01-30 16:59:27 +01:00
|
|
|
|
|
|
|
static get localThemes() {
|
|
|
|
return this.localContent;
|
|
|
|
}
|
|
|
|
|
2018-02-13 17:57:05 +01:00
|
|
|
static get contentType() {
|
|
|
|
return 'theme';
|
|
|
|
}
|
|
|
|
|
|
|
|
static get moduleName() {
|
|
|
|
return 'Theme Manager';
|
|
|
|
}
|
|
|
|
|
2018-01-30 16:59:27 +01:00
|
|
|
static get pathId() {
|
|
|
|
return 'themes';
|
|
|
|
}
|
|
|
|
|
2018-02-21 18:46:27 +01:00
|
|
|
static get loadAllThemes() { return this.loadAllContent }
|
|
|
|
static get refreshThemes() { return this.refreshContent }
|
2018-01-30 16:59:27 +01:00
|
|
|
|
2018-02-05 15:19:24 +01:00
|
|
|
static get loadContent() { return this.loadTheme }
|
|
|
|
static async loadTheme(paths, configs, info, main) {
|
|
|
|
try {
|
2018-02-11 20:31:24 +01:00
|
|
|
const instance = new Theme({
|
|
|
|
configs, info, main,
|
|
|
|
paths: {
|
|
|
|
contentPath: paths.contentPath,
|
|
|
|
dirName: paths.dirName,
|
|
|
|
mainPath: paths.mainPath
|
|
|
|
}
|
|
|
|
});
|
2018-03-08 03:41:47 +01:00
|
|
|
if (instance.enabled) {
|
|
|
|
instance.userConfig.enabled = false;
|
|
|
|
instance.enable();
|
|
|
|
}
|
2018-02-05 15:19:24 +01:00
|
|
|
return instance;
|
|
|
|
} catch (err) {
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-21 18:46:27 +01:00
|
|
|
static get unloadTheme() { return this.unloadContent }
|
2018-02-22 17:19:35 +01:00
|
|
|
static async reloadTheme(theme) {
|
|
|
|
theme = await this.reloadContent(theme);
|
|
|
|
theme.recompile();
|
|
|
|
}
|
2018-02-21 18:46:27 +01:00
|
|
|
|
2018-02-05 17:48:03 +01:00
|
|
|
static enableTheme(theme) {
|
|
|
|
theme.enable();
|
|
|
|
}
|
|
|
|
|
|
|
|
static disableTheme(theme) {
|
|
|
|
theme.disable();
|
|
|
|
}
|
|
|
|
|
2018-02-21 18:46:27 +01:00
|
|
|
static get isTheme() { return this.isThisContent }
|
|
|
|
static isThisContent(theme) {
|
|
|
|
return theme instanceof Theme;
|
|
|
|
}
|
|
|
|
|
2018-03-02 20:42:17 +01:00
|
|
|
static async getConfigAsSCSS(settingsset) {
|
2018-02-11 20:31:24 +01:00
|
|
|
const variables = [];
|
|
|
|
|
2018-03-02 20:42:17 +01:00
|
|
|
for (let category of settingsset.categories) {
|
2018-02-11 20:31:24 +01:00
|
|
|
for (let setting of category.settings) {
|
2018-02-21 01:14:06 +01:00
|
|
|
const setting_scss = await this.parseSetting(setting);
|
2018-02-20 01:01:23 +01:00
|
|
|
if (setting_scss) variables.push(`$${setting_scss[0]}: ${setting_scss[1]};`);
|
2018-02-12 01:44:12 +01:00
|
|
|
}
|
|
|
|
}
|
2018-02-20 01:01:23 +01:00
|
|
|
|
2018-02-12 01:44:12 +01:00
|
|
|
return variables.join('\n');
|
|
|
|
}
|
2018-02-11 20:31:24 +01:00
|
|
|
|
2018-03-02 20:42:17 +01:00
|
|
|
static async getConfigAsSCSSMap(settingsset) {
|
2018-02-20 01:01:23 +01:00
|
|
|
const variables = [];
|
|
|
|
|
2018-03-02 20:42:17 +01:00
|
|
|
for (let category of settingsset.categories) {
|
2018-02-20 01:01:23 +01:00
|
|
|
for (let setting of category.settings) {
|
2018-02-21 01:14:06 +01:00
|
|
|
const setting_scss = await this.parseSetting(setting);
|
2018-02-20 01:01:23 +01:00
|
|
|
if (setting_scss) variables.push(`${setting_scss[0]}: (${setting_scss[1]})`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return '(' + variables.join(', ') + ')';
|
|
|
|
}
|
|
|
|
|
2018-02-21 01:14:06 +01:00
|
|
|
static async parseSetting(setting) {
|
2018-02-12 01:44:12 +01:00
|
|
|
const { type, id, value } = setting;
|
|
|
|
const name = id.replace(/[^a-zA-Z0-9-]/g, '-').replace(/--/g, '-');
|
2018-03-01 20:00:24 +01:00
|
|
|
const scss = await setting.toSCSS();
|
2018-02-11 20:31:24 +01:00
|
|
|
|
2018-03-01 20:00:24 +01:00
|
|
|
if (scss) return [name, scss];
|
2018-02-11 20:31:24 +01:00
|
|
|
}
|
|
|
|
|
2018-02-21 02:16:41 +01:00
|
|
|
static toSCSSString(value) {
|
|
|
|
if (typeof value !== 'string' && value.toString) value = value.toString();
|
|
|
|
return `'${typeof value === 'string' ? value.replace(/\\/g, '\\\\').replace(/'/g, '\\\'') : ''}'`;
|
|
|
|
}
|
|
|
|
|
2018-01-30 16:59:27 +01:00
|
|
|
}
|