From 08b791e547a4952dc738ac12ac2f9de5efa70f00 Mon Sep 17 00:00:00 2001 From: Samuel Elliott Date: Thu, 8 Feb 2018 20:19:26 +0000 Subject: [PATCH 1/2] Add theme configuration and saving --- client/src/modules/thememanager.js | 37 +++-- .../ui/components/bd/ThemeSettingsModal.vue | 134 ++++++++++++++++++ client/src/ui/components/bd/ThemesView.vue | 3 + tests/themes/Example/config.json | 35 ++++- 4 files changed, 199 insertions(+), 10 deletions(-) create mode 100644 client/src/ui/components/bd/ThemeSettingsModal.vue diff --git a/client/src/modules/thememanager.js b/client/src/modules/thememanager.js index 215846cc..16e8251a 100644 --- a/client/src/modules/thememanager.js +++ b/client/src/modules/thememanager.js @@ -16,6 +16,8 @@ class Theme { constructor(themeInternals) { this.__themeInternals = themeInternals; + this.hasSettings = this.themeConfig && this.themeConfig.length > 0; + this.saveSettings = this.saveSettings.bind(this); this.enable = this.enable.bind(this); this.disable = this.disable.bind(this); } @@ -37,13 +39,39 @@ class Theme { get css() { return this.__themeInternals.css } get id() { return this.name.toLowerCase().replace(/\s+/g, '-') } + async saveSettings(newSettings) { + if (newSettings) { + for (let category of newSettings) { + const oldCategory = this.themeConfig.find(c => c.category === category.category); + for (let setting of category.settings) { + const oldSetting = oldCategory.settings.find(s => s.id === setting.id); + if (oldSetting.value === setting.value) continue; + oldSetting.value = setting.value; + if (this.settingChanged) this.settingChanged(category.category, setting.id, setting.value); + } + } + } + + try { + await FileUtils.writeFile(`${this.themePath}/user.config.json`, JSON.stringify({ enabled: this.enabled, config: this.themeConfig })); + } catch (err) { + throw err; + } + + if (this.settingsChanged) this.settingsChanged(this.themeConfig); + + return this.pluginConfig; + } + enable() { this.userConfig.enabled = true; + this.saveSettings(); DOM.injectTheme(this.css, this.id); } disable() { this.userConfig.enabled = false; + this.saveSettings(); DOM.deleteTheme(this.id); } @@ -74,15 +102,6 @@ export default class extends ContentManager { } } - - static async loadPlugin(paths, configs, info, main) { - const plugin = window.require(paths.mainPath)(Plugin, {}, {}); - const instance = new plugin({ configs, info, main, paths: { contentPath: paths.contentPath, dirName: paths.dirName } }); - - if (instance.enabled) instance.start(); - return instance; - } - static enableTheme(theme) { theme.enable(); } diff --git a/client/src/ui/components/bd/ThemeSettingsModal.vue b/client/src/ui/components/bd/ThemeSettingsModal.vue new file mode 100644 index 00000000..31470091 --- /dev/null +++ b/client/src/ui/components/bd/ThemeSettingsModal.vue @@ -0,0 +1,134 @@ +/** + * BetterDiscord Theme Settings Modal Component + * 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. +*/ + + + diff --git a/client/src/ui/components/bd/ThemesView.vue b/client/src/ui/components/bd/ThemesView.vue index 13f79a1d..e4176e4f 100644 --- a/client/src/ui/components/bd/ThemesView.vue +++ b/client/src/ui/components/bd/ThemesView.vue @@ -32,6 +32,7 @@
+ @@ -40,6 +41,7 @@ import { ThemeManager } from 'modules'; import { SettingsWrapper } from './'; import { MiRefresh } from '../common'; + import ThemeSettingsModal from './ThemeSettingsModal.vue'; import ThemeCard from './ThemeCard.vue'; export default { @@ -52,6 +54,7 @@ }, components: { SettingsWrapper, ThemeCard, + ThemeSettingsModal, MiRefresh }, methods: { diff --git a/tests/themes/Example/config.json b/tests/themes/Example/config.json index fde991bd..364f03cb 100644 --- a/tests/themes/Example/config.json +++ b/tests/themes/Example/config.json @@ -6,5 +6,38 @@ "description": "Example Theme 1 Description", "icon": "data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0idXRmLTgiPz48IURPQ1RZUEUgc3ZnIFBVQkxJQyAiLS8vVzNDLy9EVEQgU1ZHIDEuMS8vRU4iICJodHRwOi8vd3d3LnczLm9yZy9HcmFwaGljcy9TVkcvMS4xL0RURC9zdmcxMS5kdGQiPjxzdmcgdmVyc2lvbj0iMS4xIiBpZD0iQ2FscXVlXzEiIHhtbG5z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lWNzE2LjdDMTkzMy41LDMxMi4yLDE2MDguNywxMzUuMiwxMjYyLjUsMTM1LjJ6Ii8+PC9nPjwvc3ZnPg==" }, - "main": "index.css" + "main": "index.css", + "defaultConfig": [ + { + "category": "default", + "settings": [ + { + "id": "default-0", + "type": "text", + "value": null, + "text": "Test setting #1", + "hint": "Just some test settings to test the settings panel for themes" + }, + { + "id": "default-1", + "type": "bool", + "value": true, + "text": "Work properly", + "hint": "Just some test settings to test the settings panel for themes" + }, + { + "id": "default-2", + "type": "radio", + "value": "opt1", + "text": "Test Radio Setting", + "hint": "Just some test settings to test the settings panel for themes", + "options": [ + { "value": 1, "text": "Option 1" }, + { "value": 2, "text": "Option 2" }, + { "value": 3, "text": "Option 3" } + ] + } + ] + } + ] } From d4c71452bca93a0fc796c0ad298e2888051a7fc3 Mon Sep 17 00:00:00 2001 From: Samuel Elliott Date: Thu, 8 Feb 2018 23:53:39 +0000 Subject: [PATCH 2/2] Enable theme on start if enabled --- client/src/modules/thememanager.js | 1 + 1 file changed, 1 insertion(+) diff --git a/client/src/modules/thememanager.js b/client/src/modules/thememanager.js index 16e8251a..32946afb 100644 --- a/client/src/modules/thememanager.js +++ b/client/src/modules/thememanager.js @@ -96,6 +96,7 @@ export default class extends ContentManager { try { const css = await FileUtils.readFile(paths.mainPath); const instance = new Theme({ configs, info, main, paths: { contentPath: paths.contentPath, dirName: paths.dirName }, css }); + if (instance.enabled) instance.enable(); return instance; } catch (err) { throw err;