Change how plugin config works and set z index for modals

This commit is contained in:
Jiiks 2018-02-03 11:25:34 +02:00
parent 7ce0ecbf62
commit 39da2ffdb8
6 changed files with 143 additions and 115 deletions

View File

@ -16,6 +16,7 @@ import { ClientLogger as Logger } from 'common';
class BetterDiscord { class BetterDiscord {
constructor() { constructor() {
window.pm = PluginManager;
window.events = Events; window.events = Events;
window.wpm = WebpackModules; window.wpm = WebpackModules;
DOM.injectStyle(BdCss, 'bdmain'); DOM.injectStyle(BdCss, 'bdmain');

View File

@ -98,11 +98,14 @@ export default class {
try { try {
const readUserConfig = await this.readUserConfig(contentPath); const readUserConfig = await this.readUserConfig(contentPath);
userConfig.config = userConfig.defaultConfig.map(config => { userConfig.enabled = readUserConfig.enabled || false;
userConfig.config = readConfig.defaultConfig.map(config => {
const userSet = readUserConfig.config.find(c => c.id === config.id); const userSet = readUserConfig.config.find(c => c.id === config.id);
return userSet || config; return userSet || config;
}); });
} catch (err) {/*We don't care if this fails it either means that user config doesn't exist or there's something wrong with it so we revert to default config*/ } } catch (err) { /*We don't care if this fails it either means that user config doesn't exist or there's something wrong with it so we revert to default config*/
}
const configs = { const configs = {
defaultConfig: readConfig.defaultConfig, defaultConfig: readConfig.defaultConfig,

View File

@ -8,6 +8,8 @@
* LICENSE file in the root directory of this source tree. * LICENSE file in the root directory of this source tree.
*/ */
import { FileUtils } from 'common';
export default class { export default class {
constructor(pluginInternals) { constructor(pluginInternals) {
@ -37,6 +39,7 @@ export default class {
} }
saveSettings(newSettings) { saveSettings(newSettings) {
console.log(this);
let changed = false; let changed = false;
for (let newSetting of newSettings) { for (let newSetting of newSettings) {
const setting = this.pluginConfig.find(s => s.id === newSetting.id && s.value !== newSetting.value); const setting = this.pluginConfig.find(s => s.id === newSetting.id && s.value !== newSetting.value);
@ -46,6 +49,7 @@ export default class {
changed = true; changed = true;
} }
if (changed && this.settingsSaved) this.settingsSaved(this.pluginConfig); if (changed && this.settingsSaved) this.settingsSaved(this.pluginConfig);
FileUtils.writeFile(`${this.pluginPath}/user.config.json`, JSON.stringify({enabled: this.enabled, config: this.pluginConfig}));
} }
start() { start() {

View File

@ -7,6 +7,7 @@
background: #000; background: #000;
opacity: .85; opacity: .85;
padding: 20px; padding: 20px;
z-index: 9000;
} }
.bd-modal { .bd-modal {
@ -29,6 +30,7 @@
align-items: center; align-items: center;
box-sizing: border-box; box-sizing: border-box;
pointer-events: none; pointer-events: none;
z-index: 9001;
} }
.bd-modal .bd-modal-inner { .bd-modal .bd-modal-inner {

View File

@ -13,14 +13,20 @@
<div class="bd-backdrop" @click="attemptToClose"></div> <div class="bd-backdrop" @click="attemptToClose"></div>
<Modal :headerText="plugin.name + ' Settings'" :close="attemptToClose"> <Modal :headerText="plugin.name + ' Settings'" :close="attemptToClose">
<div slot="body" class="bd-plugin-settings-body"> <div slot="body" class="bd-plugin-settings-body">
<template v-for="category in configCache">
<template v-for="category in categories"> <div v-if="category.category === 'default' || !category.type">
<template v-if="category === 'default'"> <PluginSetting v-for="setting in category.settings" :key="setting.id" :setting="setting" :change="settingChange" />
<PluginSetting v-for="setting in configCache" v-if="!setting.category || setting.category === 'default'" :key="setting.id" :setting="setting" :change="settingChange" /> </div>
</template> <div v-else-if="category.type === 'static'">
<div v-else class="bd-category-container"> {{category.category}} static with header
<span>{{category}}</span> <PluginSetting v-for="setting in category.settings" :key="setting.id" :setting="setting" :change="settingChange" />
<PluginSetting v-for="setting in configCache" v-if="setting.category === category" :key="setting.id" :setting="setting" :change="settingChange" /> </div>
<div v-else-if="category.type === 'drawer'">
{{category.category}} drawer
<PluginSetting v-for="setting in category.settings" :key="setting.id" :setting="setting" :change="settingChange" />
</div>
<div v-else>
<PluginSetting v-for="setting in category.settings" :key="setting.id" :setting="setting" :change="settingChange" />
</div> </div>
</template> </template>
@ -44,8 +50,7 @@
return { return {
changed: false, changed: false,
warnclose: false, warnclose: false,
configCache: [], configCache: []
categories: ['default']
} }
}, },
components: { components: {
@ -54,29 +59,35 @@
}, },
methods: { methods: {
checkForChanges() { checkForChanges() {
for (let cachedSetting of this.configCache) { for (let category of this.configCache) {
if (this.plugin.pluginConfig.find(s => s.id === cachedSetting.id && s.value !== cachedSetting.value)) { const cat = this.plugin.pluginConfig.find(c => c.category === category.category);
return true; for (let setting of category.settings) {
if (cat.settings.find(s => s.id === setting.id).value !== setting.value) return true;
} }
} }
return false; return false;
}, },
textInputKd(settingId) {
},
settingChange(settingId, newValue) { settingChange(settingId, newValue) {
this.configCache.find(s => s.id === settingId).value = newValue; for (let category of this.configCache) {
const found = category.settings.find(s => s.id === settingId);
if (found) {
found.value = newValue;
break;
}
}
this.changed = this.checkForChanges(); this.changed = this.checkForChanges();
}, },
saveSettings() { saveSettings() {
this.plugin.saveSettings(this.configCache); //this.plugin.saveSettings(this.configCache);
this.configCache = JSON.parse(JSON.stringify(this.plugin.pluginConfig)); //this.configCache = JSON.parse(JSON.stringify(this.plugin.pluginConfig));
// TODO later
this.changed = false; this.changed = false;
}, },
resetSettings() { resetSettings() {
this.configCache = JSON.parse(JSON.stringify(this.plugin.pluginConfig)); this.configCache = JSON.parse(JSON.stringify(this.plugin.pluginConfig));
this.changed = false; this.changed = false;
}, },
attemptToClose() { attemptToClose(e) {
if (!this.changed) return this.close(); if (!this.changed) return this.close();
this.warnclose = true; this.warnclose = true;
setTimeout(() => { setTimeout(() => {
@ -86,11 +97,7 @@
}, },
beforeMount() { beforeMount() {
this.configCache = JSON.parse(JSON.stringify(this.plugin.pluginConfig)); this.configCache = JSON.parse(JSON.stringify(this.plugin.pluginConfig));
this.configCache.forEach(s => { console.log(this.configCache);
if (!s.category || s.category === 'default') return;
if (this.categories.includes(s.category)) return;
this.categories.push(s.category);
});
this.changed = this.checkForChanges(); this.changed = this.checkForChanges();
} }
} }

View File

@ -8,99 +8,110 @@
"main": "index.js", "main": "index.js",
"defaultConfig": [ "defaultConfig": [
{ {
"id": "test-setting-1", "category_default_comment": "default category has no header and is always displayed first",
"category": "test category", "category": "default",
"type": "bool", "settings": [
"value": false, {
"text": "Bool Test Setting", "id": "default-0",
"hint": "Bool Test Setting Hint" "type": "bool",
"value": false,
"text": "Bool Test Setting 3",
"hint": "Bool Test Setting Hint 3"
},
{
"id": "default-1",
"type": "text",
"value": "defaultValue",
"text": "Text Test Setting",
"hint": "Text Test Setting Hint"
},
{
"id": "default-2",
"type": "bool",
"value": false,
"text": "Bool Test Setting 3",
"hint": "Bool Test Setting Hint 3"
},
{
"id": "default-3",
"type": "file",
"value": null,
"text": "Test File Setting 1",
"hint": "File selector with the default options."
},
{
"id": "default-4",
"type": "file",
"value": null,
"text": "Test File Setting 2",
"hint": "File selector with custom button text and the ability to open directories and multiple items.",
"dialogOptions": {
"buttonLabel": "Select",
"properties": [
"openFile",
"openDirectory",
"multiSelections"
]
}
}
]
}, },
{ {
"id": "test-setting-2", "category_header_comment": "Setting a category other than default has a header with category name as the text",
"category": "test category 2", "category": "Test Category",
"type": "text", "drawer_type_comment": "// Drawer type will create an expandable drawer for the settings",
"value": "defaultValue", "type": "drawer",
"text": "Text Test Setting", "settings": [
"hint": "Text Test Setting Hint" {
"id": "drawer-0",
"type": "bool",
"value": false,
"text": "Bool Test Setting 3",
"hint": "Bool Test Setting Hint 3"
},
{
"id": "drawer-1",
"type": "text",
"value": "defaultValue",
"text": "Text Test Setting",
"hint": "Text Test Setting Hint"
},
{
"id": "drawer-2",
"type": "bool",
"value": false,
"text": "Bool Test Setting 3",
"hint": "Bool Test Setting Hint 3"
}
]
}, },
{ {
"id": "test-setting-3", "category": "Test Category 2",
"type": "bool", "static_type_comment": "Static type will behave like default but will have a header",
"value": false, "type": "static",
"text": "Bool Test Setting 2", "settings": [
"hint": "Bool Test Setting Hint 2" {
}, "id": "static-0",
{ "type": "bool",
"id": "test-setting-4", "value": false,
"type": "bool", "text": "Bool Test Setting 3",
"value": false, "hint": "Bool Test Setting Hint 3"
"text": "Bool Test Setting 3", },
"hint": "Bool Test Setting Hint 3" {
}, "id": "static-1",
{ "type": "text",
"id": "test-setting-5", "value": "defaultValue",
"category": "test category", "text": "Text Test Setting",
"type": "bool", "hint": "Text Test Setting Hint"
"value": false, },
"text": "Bool Test Setting 4", {
"hint": "Bool Test Setting Hint 4" "id": "static-2",
}, "type": "bool",
{ "value": false,
"id": "test-setting-6", "text": "Bool Test Setting 3",
"type": "bool", "hint": "Bool Test Setting Hint 3"
"value": false, }
"text": "Bool Test Setting 5", ]
"hint": "Bool Test Setting Hint 5"
},
{
"id": "test-setting-7",
"type": "bool",
"value": false,
"text": "Bool Test Setting 6",
"hint": "Bool Test Setting Hint 6"
},
{
"id": "test-setting-8",
"type": "bool",
"value": false,
"text": "Bool Test Setting 7",
"hint": "Bool Test Setting Hint 7"
},
{
"id": "test-setting-9",
"type": "bool",
"value": false,
"text": "Bool Test Setting 8",
"hint": "Bool Test Setting Hint 8"
},
{
"id": "test-setting-10",
"type": "bool",
"value": false,
"text": "Bool Test Setting 9",
"hint": "Bool Test Setting Hint 9"
},
{
"id": "test-setting-file-1",
"type": "file",
"value": null,
"text": "Test File Setting 1",
"hint": "File selector with the default options."
},
{
"id": "test-setting-file-2",
"type": "file",
"value": null,
"text": "Test File Setting 2",
"hint": "File selector with custom button text and the ability to open directories and multiple items.",
"dialogOptions": {
"buttonLabel": "Select",
"properties": [
"openFile",
"openDirectory",
"multiSelections"
]
}
} }
] ]
} }