finish moving to collections

This commit is contained in:
Zack Rauen 2019-06-06 15:57:25 -04:00
parent c14d21eab4
commit 632d37f93e
18 changed files with 969 additions and 851 deletions

File diff suppressed because it is too large Load Diff

View File

@ -1,5 +1,4 @@
import Builtin, {onSettingChange} from "../structs/builtin"; import Builtin from "../structs/builtin";
import {SettingsCookie} from "data";
import {DiscordModules} from "modules"; import {DiscordModules} from "modules";
export default new class DeveloperMode extends Builtin { export default new class DeveloperMode extends Builtin {
@ -7,7 +6,7 @@ export default new class DeveloperMode extends Builtin {
get category() {return "developer";} get category() {return "developer";}
get id() {return "developerMode";} get id() {return "developerMode";}
get selectorModeID() {return "copySelector";} get selectorModeID() {return "copySelector";}
get selectorMode() {return SettingsCookie[this.selectorModeID];} get selectorMode() {return this.get(this.selectorModeID);}
constructor() { constructor() {
super(); super();
@ -23,7 +22,7 @@ export default new class DeveloperMode extends Builtin {
} }
}); });
if (this.selectorMode) this.enableSelectors(); if (this.selectorMode) this.enableSelectors();
this.selectorCancel = onSettingChange(this.category, this.selectorModeID, this.enableSelectors, this.disableSelectors); this.selectorCancel = this.registerSetting(this.selectorModeID, this.enableSelectors, this.disableSelectors);
} }
disabled() { disabled() {

View File

@ -1,5 +1,5 @@
import Builtin, {onSettingChange} from "../structs/builtin"; import Builtin from "../structs/builtin";
import {SettingsCookie, Emotes, State} from "data"; import {Emotes, State} from "data";
import {DataStore, Utilities, Events} from "modules"; import {DataStore, Utilities, Events} from "modules";
const headerHTML = `<div id="bda-qem"> const headerHTML = `<div id="bda-qem">
@ -44,7 +44,7 @@ export default new class EmoteMenu extends Builtin {
get category() {return "general";} get category() {return "general";}
get id() {return "emoteMenu";} get id() {return "emoteMenu";}
get hideEmojisID() {return "hideEmojiMenu";} get hideEmojisID() {return "hideEmojiMenu";}
get hideEmojis() {return SettingsCookie[this.hideEmojisID];} get hideEmojis() {return this.get(this.hideEmojisID);}
constructor() { constructor() {
super(); super();
@ -79,7 +79,7 @@ export default new class EmoteMenu extends Builtin {
childList: true, childList: true,
subtree: true subtree: true
}); });
this.hideEmojiCancel = onSettingChange(this.category, this.hideEmojisID, this.enableHideEmojis, this.disableHideEmojis); this.hideEmojiCancel = this.registerSetting(this.hideEmojisID, this.enableHideEmojis, this.disableHideEmojis);
if (this.hideEmojis) this.enableHideEmojis(); if (this.hideEmojis) this.enableHideEmojis();
// await this.waitForEmotes(); // await this.waitForEmotes();
// this.updateTwitchEmotes(); // this.updateTwitchEmotes();

View File

@ -6,24 +6,31 @@ import BDEmote from "../ui/emote";
import {Toasts} from "ui"; import {Toasts} from "ui";
const bdEmoteSettingIDs = { const bdEmoteSettingIDs = {
TwitchGlobal: "bda-es-7", TwitchGlobal: "twitch",
TwitchSubscriber: "bda-es-7", TwitchSubscriber: "twitch",
BTTV: "bda-es-2", BTTV: "bttv",
FrankerFaceZ: "bda-es-1", FrankerFaceZ: "ffz",
BTTV2: "bda-es-2" BTTV2: "bttv"
}; };
export default new class EmoteModule extends Builtin { export default new class EmoteModule extends Builtin {
get name() {return "Emotes";} get name() {return "Emotes";}
get collection() {return "emotes";} get collection() {return "settings";}
get category() {return "general";} get category() {return "general";}
get id() {return "";} get id() {return "emotes";}
get categories() { return Object.keys(bdEmoteSettingIDs).filter(k => SettingsCookie[bdEmoteSettingIDs[k]]); } get categories() { return Object.keys(bdEmoteSettingIDs).filter(k => this.isCategoryEnabled(bdEmoteSettingIDs[k])); }
get MessageContentComponent() {return WebpackModules.getModule(m => m.defaultProps && m.defaultProps.hasOwnProperty("disableButtons"));} get MessageContentComponent() {return WebpackModules.getModule(m => m.defaultProps && m.defaultProps.hasOwnProperty("disableButtons"));}
async initialize() { isCategoryEnabled(id) {
super.initialize(); return super.get("emotes", "categories", id);
}
get(id) {
return super.get("emotes", "general", id);
}
async enabled() {
// Disable emote module for now because it's annoying and slow // Disable emote module for now because it's annoying and slow
// await this.getBlacklist(); // await this.getBlacklist();
// await this.loadEmoteData(EmoteInfo); // await this.loadEmoteData(EmoteInfo);
@ -33,11 +40,16 @@ export default new class EmoteModule extends Builtin {
} }
disabled() { disabled() {
this.emptyEmotes();
if (this.cancelEmoteRender) return; if (this.cancelEmoteRender) return;
this.cancelEmoteRender(); this.cancelEmoteRender();
delete this.cancelEmoteRender; delete this.cancelEmoteRender;
} }
emptyEmotes() {
for (const cat in Emotes) Object.assign(Emotes, {[cat]: {}});
}
patchMessageContent() { patchMessageContent() {
if (this.cancelEmoteRender) return; if (this.cancelEmoteRender) return;
this.cancelEmoteRender = Utilities.monkeyPatch(this.MessageContentComponent.prototype, "render", {after: ({returnValue}) => { this.cancelEmoteRender = Utilities.monkeyPatch(this.MessageContentComponent.prototype, "render", {after: ({returnValue}) => {

View File

@ -1,12 +1,11 @@
import Builtin, {onSettingChange} from "../structs/builtin"; import Builtin from "../structs/builtin";
import {SettingsCookie} from "data";
export default new class MinimalMode extends Builtin { export default new class MinimalMode extends Builtin {
get name() {return "MinimalMode";} get name() {return "MinimalMode";}
get category() {return "appearance";} get category() {return "appearance";}
get id() {return "minimalMode";} get id() {return "minimalMode";}
get hideChannelsID() {return "hideChannels";} get hideChannelsID() {return "hideChannels";}
get hideChannels() {return SettingsCookie[this.hideChannelsID];} get hideChannels() {return this.get(this.hideChannelsID);}
constructor() { constructor() {
super(); super();
@ -17,7 +16,7 @@ export default new class MinimalMode extends Builtin {
enabled() { enabled() {
$("body").addClass("bd-minimal"); $("body").addClass("bd-minimal");
if (this.hideChannels) this.enableHideChannels(); if (this.hideChannels) this.enableHideChannels();
this.hideChannelCancel = onSettingChange(this.category, this.hideChannelsID, this.enableHideChannels, this.disableHideChannels); this.hideChannelCancel = this.registerSetting(this.hideChannelsID, this.enableHideChannels, this.disableHideChannels);
} }
disabled() { disabled() {

View File

@ -1,5 +1,4 @@
import Builtin from "../structs/builtin"; import Builtin from "../structs/builtin";
import {SettingsCookie} from "data";
import {BDV2, DiscordModules} from "modules"; import {BDV2, DiscordModules} from "modules";
import {PublicServers as PSComponents} from "ui"; import {PublicServers as PSComponents} from "ui";
@ -45,7 +44,7 @@ export default new class PublicServers extends Builtin {
render() { render() {
const root = this.root; const root = this.root;
if (!root) { if (!root) {
console.log("FAILED TO LOCATE ROOT: .layers"); this.error("FAILED TO LOCATE ROOT: .layers");
return; return;
} }
DiscordModules.ReactDOM.render(this.component, root); DiscordModules.ReactDOM.render(this.component, root);

View File

@ -12,9 +12,7 @@ import EmoteInfo from "./emotes/info";
import EmoteModifiers from "./emotes/modifiers"; import EmoteModifiers from "./emotes/modifiers";
import EmoteOverrides from "./emotes/overrides"; import EmoteOverrides from "./emotes/overrides";
import SettingsCollection from "./settings/config"; import SettingsConfig from "./settings/config";
import EmoteCollection from "./emotes/config"; import SettingsState from "./settings/state";
export const Collections = [SettingsCollection, EmoteCollection]; export {State, SettingsInfo, SettingsCookie, Config, PluginCookie, ThemeCookie, Themes, Plugins, Emotes, EmoteBlacklist, EmoteInfo, EmoteModifiers, EmoteOverrides, SettingsConfig, SettingsState};
export {State, SettingsInfo, SettingsCookie, Config, PluginCookie, ThemeCookie, Themes, Plugins, Emotes, EmoteBlacklist, EmoteInfo, EmoteModifiers, EmoteOverrides};

View File

@ -1,89 +0,0 @@
export default [
{
type: "category",
id: "general",
name: "General",
collapsible: true,
settings: [
{
type: "switch",
id: "download",
name: "Download Emotes",
note: "Download emotes once a week to stay up to date",
value: true
},
{
type: "switch",
id: "emoteMenu",
name: "Emote Menu",
note: "Show Twitch/Favourite emotes in emote menu",
value: true
},
{
type: "switch",
id: "hideEmojiMenu",
name: "Hide Emoji Menu",
note: "Hides Discord's emoji menu when using emote menu",
value: true,
enableWith: "emoteMenu"
},
{
type: "switch",
id: "autoCaps",
name: "Emote Autocapitalization",
note: "Autocapitalize emote commands",
value: false
},
{
type: "switch",
id: "showNames",
name: "Show Names",
note: "Show emote names on hover",
value: false
},
{
type: "switch",
id: "modifiers",
name: "Show Emote Modifiers",
note: "Enable emote mods (flip, spin, pulse, spin2, spin3, 1spin, 2spin, 3spin, tr, bl, br, shake, shake2, shake3, flap)",
value: false
},
{
type: "switch",
id: "animateOnHover",
name: "Animate On Hover",
note: "Only animate the emote modifiers on hover",
value: false
}
]
},
{
type: "category",
id: "categories",
name: "Categories",
collapsible: true,
settings: [
{
type: "switch",
id: "twitch",
name: "Twitch",
note: "Show Twitch global & subscriber emotes",
value: false
},
{
type: "switch",
id: "ffz",
name: "FrankerFaceZ",
note: "Show emotes from FFZ",
value: true
},
{
type: "switch",
id: "bttv",
name: "BetterTTV",
note: "Show emotes from BTTV",
value: true
}
]
}
];

View File

@ -1 +0,0 @@
export default {};

View File

@ -1,4 +1,9 @@
export default [ export default [
{
type: "collection",
id: "settings",
name: "Settings",
settings: [
{ {
type: "category", type: "category",
id: "general", id: "general",
@ -170,4 +175,101 @@ export default [
} }
] ]
} }
]
},
{
type: "collection",
id: "emotes",
name: "Emotes",
enableWith: "settings.general.emotes",
settings: [
{
type: "category",
id: "general",
name: "General",
collapsible: true,
settings: [
{
type: "switch",
id: "download",
name: "Download Emotes",
note: "Download emotes once a week to stay up to date",
value: true
},
{
type: "switch",
id: "emoteMenu",
name: "Emote Menu",
note: "Show Twitch/Favourite emotes in emote menu",
value: true
},
{
type: "switch",
id: "hideEmojiMenu",
name: "Hide Emoji Menu",
note: "Hides Discord's emoji menu when using emote menu",
value: false,
enableWith: "emoteMenu"
},
{
type: "switch",
id: "autoCaps",
name: "Emote Autocapitalization",
note: "Autocapitalize emote commands",
value: false
},
{
type: "switch",
id: "showNames",
name: "Show Names",
note: "Show emote names on hover",
value: true
},
{
type: "switch",
id: "modifiers",
name: "Show Emote Modifiers",
note: "Enable emote mods (flip, spin, pulse, spin2, spin3, 1spin, 2spin, 3spin, tr, bl, br, shake, shake2, shake3, flap)",
value: true
},
{
type: "switch",
id: "animateOnHover",
name: "Animate On Hover",
note: "Only animate the emote modifiers on hover",
value: false
}
]
},
{
type: "category",
id: "categories",
name: "Categories",
collapsible: true,
settings: [
{
type: "switch",
id: "twitch",
name: "Twitch",
note: "Show Twitch global & subscriber emotes",
value: true
},
{
type: "switch",
id: "ffz",
name: "FrankerFaceZ",
note: "Show emotes from FFZ",
value: true
},
{
type: "switch",
id: "bttv",
name: "BetterTTV",
note: "Show emotes from BTTV",
value: true
}
]
}
]
}
]; ];

View File

@ -5,7 +5,7 @@ import {Config} from "data";
// import QuickEmoteMenu from "../builtins/emotemenu"; // import QuickEmoteMenu from "../builtins/emotemenu";
import PluginManager from "./pluginmanager"; import PluginManager from "./pluginmanager";
import ThemeManager from "./thememanager"; import ThemeManager from "./thememanager";
import SettingsPanel from "./settingsmanager"; import Settings from "./settingsmanager";
import * as Builtins from "builtins"; import * as Builtins from "builtins";
import {Modals} from "ui"; import {Modals} from "ui";
@ -31,7 +31,7 @@ Core.prototype.init = async function() {
} }
Utilities.log("Startup", "Initializing Settings"); Utilities.log("Startup", "Initializing Settings");
SettingsPanel.initialize(); Settings.initialize();
Utilities.log("Startup", "Initializing EmoteModule"); Utilities.log("Startup", "Initializing EmoteModule");
// window.emotePromise = EmoteModule.init().then(() => { // window.emotePromise = EmoteModule.init().then(() => {
// EmoteModule.initialized = true; // EmoteModule.initialized = true;
@ -45,7 +45,6 @@ Core.prototype.init = async function() {
await this.checkForGuilds(); await this.checkForGuilds();
BDV2.initialize(); BDV2.initialize();
Utilities.log("Startup", "Updating Settings"); Utilities.log("Startup", "Updating Settings");
SettingsPanel.initializeSettings();
for (const module in Builtins) Builtins[module].initialize(); for (const module in Builtins) Builtins[module].initialize();
Utilities.log("Startup", "Loading Plugins"); Utilities.log("Startup", "Loading Plugins");
@ -107,7 +106,7 @@ Core.prototype.initObserver = function () {
// if (node.getElementsByClassName("socialLinks-3jqNFy").length) { // if (node.getElementsByClassName("socialLinks-3jqNFy").length) {
// node.setAttribute("layer-id", "user-settings"); // node.setAttribute("layer-id", "user-settings");
// node.setAttribute("id", "user-settings"); // node.setAttribute("id", "user-settings");
// if (!document.getElementById("bd-settings-sidebar")) SettingsPanel.renderSidebar(); // if (!document.getElementById("bd-settings-sidebar")) Settings.renderSidebar();
// } // }
} }
} }

View File

@ -1,39 +1,54 @@
import {Config} from "data"; import {Config} from "data";
const fs = require("fs"); const fs = require("fs");
const path = require("path"); const path = require("path");
const releaseChannel = DiscordNative.globals.releaseChannel; const releaseChannel = DiscordNative.globals.releaseChannel;
// Schema 1
// =======================
// %appdata%\BetterDiscord
// -> data\
// -> [releaseChannel].json (stable/canary/ptb)
// Schema 2
// =======================
// %appdata%\BetterDiscord
// -> data
// -> [releaseChannel]\ (stable/canary/ptb)
// -> settings.json
// -> plugins.json
// -> themes.json
export default new class DataStore { export default new class DataStore {
constructor() { constructor() {
this.data = {settings: {stable: {}, canary: {}, ptb: {}}}; this.data = {};
this.pluginData = {}; this.pluginData = {};
} }
initialize() { initialize() {
if (!fs.existsSync(path.resolve(this.BDFile, ".."))) fs.mkdirSync(path.resolve(this.BDFile, ".."));
if (!fs.existsSync(this.BDFile)) fs.writeFileSync(this.BDFile, JSON.stringify(this.data, null, 4)); if (!fs.existsSync(this.BDFile)) fs.writeFileSync(this.BDFile, JSON.stringify(this.data, null, 4));
const data = __non_webpack_require__(this.BDFile); this.data = __non_webpack_require__(this.BDFile);
if (data.hasOwnProperty("settings")) this.data = data; // if (data.hasOwnProperty("settings")) this.data = data;
if (!fs.existsSync(this.settingsFile)) return; // if (!fs.existsSync(this.settingsFile)) return;
let settings = __non_webpack_require__(this.settingsFile); // let settings = __non_webpack_require__(this.settingsFile);
fs.unlinkSync(this.settingsFile); // fs.unlinkSync(this.settingsFile);
if (settings.hasOwnProperty("settings")) settings = Object.assign({stable: {}, canary: {}, ptb: {}}, {[releaseChannel]: settings}); // if (settings.hasOwnProperty("settings")) settings = Object.assign({stable: {}, canary: {}, ptb: {}}, {[releaseChannel]: settings});
else settings = Object.assign({stable: {}, canary: {}, ptb: {}}, settings); // else settings = Object.assign({stable: {}, canary: {}, ptb: {}}, settings);
this.setBDData("settings", settings); // this.setBDData("settings", settings);
} }
get BDFile() {return this._BDFile || (this._BDFile = path.resolve(Config.dataPath, "bdstorage.json"));} get BDFile() {return this._BDFile || (this._BDFile = path.resolve(Config.dataPath, "data", `${releaseChannel}.json`));}
get settingsFile() {return this._settingsFile || (this._settingsFile = path.resolve(Config.dataPath, "bdsettings.json"));} // get settingsFile() {return this._settingsFile || (this._settingsFile = path.resolve(Config.dataPath, "bdsettings.json"));}
getPluginFile(pluginName) {return path.resolve(Config.dataPath, "plugins", pluginName + ".config.json");} getPluginFile(pluginName) {return path.resolve(Config.dataPath, "plugins", pluginName + ".config.json");}
getSettingGroup(key) { // getSettingGroup(key) {
return this.data.settings[releaseChannel][key] || null; // return this.data.settings[key] || null;
} // }
setSettingGroup(key, data) { // setSettingGroup(key, data) {
this.data.settings[releaseChannel][key] = data; // this.data.settings[key] = data;
fs.writeFileSync(this.BDFile, JSON.stringify(this.data, null, 4)); // fs.writeFileSync(this.BDFile, JSON.stringify(this.data, null, 4));
} // }
getBDData(key) { getBDData(key) {
return this.data[key] || ""; return this.data[key] || "";
@ -44,6 +59,15 @@ export default new class DataStore {
fs.writeFileSync(this.BDFile, JSON.stringify(this.data, null, 4)); fs.writeFileSync(this.BDFile, JSON.stringify(this.data, null, 4));
} }
getData(key) {
return this.data[key] || "";
}
setData(key, value) {
this.data[key] = value;
fs.writeFileSync(this.BDFile, JSON.stringify(this.data, null, 4));
}
getPluginData(pluginName, key) { getPluginData(pluginName, key) {
if (this.pluginData[pluginName] !== undefined) return this.pluginData[pluginName][key] || undefined; if (this.pluginData[pluginName] !== undefined) return this.pluginData[pluginName][key] || undefined;
if (!fs.existsSync(this.getPluginFile(pluginName))) return undefined; if (!fs.existsSync(this.getPluginFile(pluginName))) return undefined;

View File

@ -150,13 +150,13 @@ PluginModule.prototype.updatePluginList = function() {
}; };
PluginModule.prototype.loadPluginData = function () { PluginModule.prototype.loadPluginData = function () {
const saved = DataStore.getSettingGroup("plugins"); const saved = DataStore.getData("plugins");
if (!saved) return; if (!saved) return;
Object.assign(PluginCookie, saved); Object.assign(PluginCookie, saved);
}; };
PluginModule.prototype.savePluginData = function () { PluginModule.prototype.savePluginData = function () {
DataStore.setSettingGroup("plugins", PluginCookie); DataStore.setData("plugins", PluginCookie);
}; };
PluginModule.prototype.newMessage = function () { PluginModule.prototype.newMessage = function () {

View File

@ -1,8 +1,7 @@
import {SettingsCookie, Collections} from "data"; import {SettingsConfig, SettingsState} from "data";
import DataStore from "./datastore"; import DataStore from "./datastore";
import ContentManager from "./contentmanager"; import ContentManager from "./contentmanager";
import BdApi from "./pluginapi"; import BdApi from "./pluginapi";
// import EmoteModule from "./emotes";
import Events from "./emitter"; import Events from "./emitter";
import WebpackModules from "./webpackmodules"; import WebpackModules from "./webpackmodules";
@ -10,59 +9,75 @@ import {SettingsPanel as SettingsRenderer} from "ui";
import Utilities from "./utilities"; import Utilities from "./utilities";
import {Toasts} from "ui"; import {Toasts} from "ui";
import EmoteSettings from "../data/emotes/config";
import EmoteState from "../data/emotes/state";
import TheSettings from "../data/settings/config";
import SettingsState from "../data/settings/state";
//WebpackModules.getModule(m => m.getSection && m.getProps && !m.getGuildId && !m.getChannel)
//WebpackModules.getByProps("getGuildId", "getSection")
export default new class SettingsManager { export default new class SettingsManager {
constructor() { constructor() {
this.renderer = new SettingsRenderer({onChange: this.updateSettings.bind(this)}); this.renderer = new SettingsRenderer();
this.updateSettings = this.updateSettings.bind(this); this.config = SettingsConfig;
console.log(Collections); this.state = SettingsState;
this.setup(SettingsConfig, SettingsState);
} }
initialize() { initialize() {
DataStore.initialize(); DataStore.initialize();
if (!DataStore.getSettingGroup("settings")) return this.saveSettings(); // if (!DataStore.getSettingGroup("settings")) return this.saveSettings();
const savedSettings = this.loadSettings(); // const savedSettings = this.loadSettings();
$("<style id=\"customcss\">").text(atob(DataStore.getBDData("bdcustomcss"))).appendTo(document.head); // $("<style id=\"customcss\">").text(atob(DataStore.getBDData("bdcustomcss"))).appendTo(document.head);
for (const setting in savedSettings) { // for (const setting in savedSettings) {
if (savedSettings[setting] !== undefined) SettingsCookie[setting] = savedSettings[setting]; // if (savedSettings[setting] !== undefined) SettingsCookie[setting] = savedSettings[setting];
} // }
this.saveSettings(); // this.saveSettings();
this.loadSettings();
this.patchSections(); this.patchSections();
this.initializeConfig(TheSettings, SettingsState); // Object.assign(this.state, this.defaultState);
this.initializeConfig(EmoteSettings, EmoteState); // this.initializeConfig(EmoteSettings, EmoteState);
} }
initializeConfig(defaultConfig, state) { getPath(path, collectionId = "", categoryId = "") {
const collection = path.length == 3 ? path[0] : collectionId;
const category = path.length == 3 ? path[1] : path.length == 2 ? path[0] : categoryId;
const setting = path[path.length - 1];
return {collection, category, setting};
}
setup(collections, state) {
const config = {}; const config = {};
for (let s = 0; s < defaultConfig.length; s++) { for (let c = 0; c < collections.length; c++) {
const current = defaultConfig[s]; const collection = collections[c];
if (current.type != "category") {config[current.id] = current.value;} const categories = collections[c].settings;
config[collection.id] = {};
for (let s = 0; s < categories.length; s++) {
const category = categories[s];
if (category.type != "category") {config[collection.id][category.id] = category.value;}
else { else {
config[current.id] = {}; config[collection.id][category.id] = {};
for (let s = 0; s < current.settings.length; s++) { for (let s = 0; s < category.settings.length; s++) {
const subCurrent = current.settings[s]; const setting = category.settings[s];
config[current.id][subCurrent.id] = subCurrent.value; config[collection.id][category.id][setting.id] = setting.value;
if (subCurrent.enableWith) { if (setting.enableWith) {
Object.defineProperty(subCurrent, "disabled", { const path = this.getPath(setting.enableWith.split("."), collection.id, category.id);
Object.defineProperty(setting, "disabled", {
get: () => { get: () => {
return !state[current.id][subCurrent.enableWith]; return !state[path.collection][path.category][path.setting];
} }
}); });
} }
} }
} }
} }
console.log(defaultConfig); if (collection.enableWith) {
console.log(config); const path = this.getPath(collection.enableWith.split("."));
Object.assign(state, config); Object.defineProperty(collection, "disabled", {
get: () => {
return !state[path.collection][path.category][path.setting];
}
});
}
}
this.defaultState = config;
Object.assign(this.state, this.defaultState);
} }
buildSettingsPanel(title, config, state, onChange) { buildSettingsPanel(title, config, state, onChange) {
@ -73,7 +88,7 @@ export default new class SettingsManager {
} }
async patchSections() { async patchSections() {
const UserSettings = await this.getUserSettings(); // data.returnValue.type; const UserSettings = await this.getUserSettings();
Utilities.monkeyPatch(UserSettings.prototype, "generateSections", {after: (data) => { Utilities.monkeyPatch(UserSettings.prototype, "generateSections", {after: (data) => {
let location = data.returnValue.findIndex(s => s.section.toLowerCase() == "linux") + 1; let location = data.returnValue.findIndex(s => s.section.toLowerCase() == "linux") + 1;
const insert = (section) => { const insert = (section) => {
@ -83,9 +98,14 @@ export default new class SettingsManager {
console.log(data); /* eslint-disable-line no-console */ console.log(data); /* eslint-disable-line no-console */
insert({section: "DIVIDER"}); insert({section: "DIVIDER"});
insert({section: "HEADER", label: "BandagedBD"}); insert({section: "HEADER", label: "BandagedBD"});
// insert({section: "BBD Settings", label: "Settings", element: () => this.renderer.core2}); for (const collection of this.config) {
insert({section: "BBD Settings", label: "Settings", element: () => this.buildSettingsPanel("Settings", TheSettings, SettingsState, this.updateSettings.bind(this, SettingsState))}); if (collection.disabled) continue;
if (SettingsState.general.emotes) insert({section: "BBD Emotes", label: "Emotes", element: () => this.buildSettingsPanel("Emote Settings", EmoteSettings, EmoteState, this.updateSettings.bind(this, EmoteState))}); insert({
section: collection.name,
label: collection.name,
element: () => this.buildSettingsPanel(collection.name, collection.settings, SettingsState[collection.id], this.onSettingChange.bind(this, collection.id))
});
}
insert({section: "BBD Test", label: "Test Tab", onClick: function() {Toasts.success("This can just be a click listener!", {forceShow: true});}}); insert({section: "BBD Test", label: "Test Tab", onClick: function() {Toasts.success("This can just be a click listener!", {forceShow: true});}});
insert({section: "CUSTOM", element: () => this.renderer.attribution}); insert({section: "CUSTOM", element: () => this.renderer.attribution});
}}); }});
@ -109,46 +129,55 @@ export default new class SettingsManager {
} }
saveSettings() { saveSettings() {
DataStore.setSettingGroup("settings", SettingsCookie); DataStore.setData("settings", this.state);
} }
loadSettings() { loadSettings() {
return DataStore.getSettingGroup("settings"); const previousState = DataStore.getData("settings");
if (!previousState) return this.saveSettings();
for (const collection in this.defaultState) {
if (!previousState[collection]) Object.assign(previousState, {[collection]: this.defaultState[collection]});
for (const category in this.defaultState[collection]) {
if (!previousState[collection][category]) Object.assign(previousState[collection][category], {[category]: this.defaultState[collection][category]});
for (const setting in this.defaultState[collection][category]) {
if (previousState[collection][category][setting] == undefined) continue;
this.state[collection][category][setting] = previousState[collection][category][setting];
}
}
} }
onSettingChange(collection, category, id, enabled) { this.saveSettings(); // in case new things were added
collection[category][id] = enabled;
Events.dispatch("setting-updated", category, id, enabled);
// console.log(collection);
if (id == "emotes") this.forceUpdate();
} }
getSetting(category, id) { onSettingChange(collection, category, id, value) {
if (arguments.length == 2) return SettingsState[category][id]; const before = this.config.filter(c => c.disabled).length;
const collection = arguments[0] == "emotes" ? EmoteState : SettingsState; this.state[collection][category][id] = value;
return collection && collection[arguments[1]][arguments[2]]; Events.dispatch("setting-updated", collection, category, id, value);
const after = this.config.filter(c => c.disabled).length;
this.saveSettings();
if (before != after) this.forceUpdate();
} }
get(category, id) { getSetting(collection, category, id) {
if (arguments.length == 2) return SettingsState[category][id]; if (arguments.length == 2) return this.config[0].find(c => c.id == arguments[0]).settings.find(s => s.id == arguments[1]);
const collection = arguments[0] == "emotes" ? EmoteState : SettingsState; return this.config.find(c => c.id == collection).find(c => c.id == category).settings.find(s => s.id == id);
return collection && collection[arguments[1]][arguments[2]]; }
get(collection, category, id) {
if (arguments.length == 2) return this.state[this.config[0].id][arguments[0]][arguments[1]];
return this.state[collection][category][id];
}
on(collection, category, identifier, callback) {
const handler = (col, cat, id, value) => {
if (col !== collection || cat !== category || id !== identifier) return;
callback(value);
};
Events.on("setting-updated", handler);
return () => {Events.off("setting-updated", handler);};
} }
updateSettings(collection, category, id, enabled) { updateSettings(collection, category, id, enabled) {
// console.log("Updating ", collection);
// console.log(category, id, enabled);
collection[category][id] = enabled;
Events.dispatch("setting-updated", category, id, enabled);
// console.log(collection);
if (id == "emotes") this.forceUpdate();
// SettingsCookie[id] = enabled;
// if (id == "bda-es-4") {
// if (enabled) EmoteModule.autoCapitalize();
// else EmoteModule.disableAutoCapitalize();
// }
if (id == "fork-ps-5") { if (id == "fork-ps-5") {
if (enabled) { if (enabled) {
@ -173,10 +202,10 @@ export default new class SettingsManager {
initializeSettings() { initializeSettings() {
// if (SettingsCookie["bda-es-4"]) EmoteModule.autoCapitalize(); // if (SettingsCookie["bda-es-4"]) EmoteModule.autoCapitalize();
if (SettingsCookie["fork-ps-5"]) { // if (SettingsCookie["fork-ps-5"]) {
ContentManager.watchContent("plugin"); // ContentManager.watchContent("plugin");
ContentManager.watchContent("theme"); // ContentManager.watchContent("theme");
} // }
this.saveSettings(); this.saveSettings();
} }

View File

@ -99,13 +99,13 @@ ThemeModule.prototype.updateThemeList = function() {
}; };
ThemeModule.prototype.loadThemeData = function() { ThemeModule.prototype.loadThemeData = function() {
const saved = DataStore.getSettingGroup("themes"); const saved = DataStore.getData("themes");
if (!saved) return; if (!saved) return;
Object.assign(ThemeCookie, saved); Object.assign(ThemeCookie, saved);
}; };
ThemeModule.prototype.saveThemeData = function () { ThemeModule.prototype.saveThemeData = function () {
DataStore.setSettingGroup("themes", ThemeCookie); DataStore.setData("themes", ThemeCookie);
}; };
export default new ThemeModule(); export default new ThemeModule();

View File

@ -1,18 +1,7 @@
import {SettingsCookie} from "data"; import {SettingsState} from "data";
import SettingState from "../data/settings/state";
import EmoteState from "../data/emotes/state";
import Utilities from "../modules/utilities"; import Utilities from "../modules/utilities";
import Events from "../modules/emitter"; import Events from "../modules/emitter";
import Settings from "../modules/settingsmanager";
export function onSettingChange(category, identifier, onEnable, onDisable) {
const handler = (cat, id, enabled) => {
if (category !== cat || id !== identifier) return;
if (enabled) onEnable();
else onDisable();
};
Events.on("setting-updated", handler);
return () => {Events.off("setting-updated", handler);};
}
export default class BuiltinModule { export default class BuiltinModule {
@ -22,15 +11,49 @@ export default class BuiltinModule {
get id() {return "None";} get id() {return "None";}
async initialize() { async initialize() {
const state = this.collection == "settings" ? SettingState : EmoteState; if (SettingsState[this.collection][this.category][this.id]) await this.enable();
if (state[this.category][this.id]) await this.enable(); Events.on("setting-updated", (collection, category, id, enabled) => {
Events.on("setting-updated", (category, id, enabled) => { if (collection != this.collection || category !== this.category || id !== this.id) return;
if (category !== this.category || id !== this.id) return;
if (enabled) this.enable(); if (enabled) this.enable();
else this.disable(); else this.disable();
}); });
} }
registerSetting(collection, category, id, onEnable, onDisable) {
if (arguments.length == 4) {
collection = this.collection;
category = arguments[0];
id = arguments[1];
onEnable = arguments[2];
onDisable = arguments[3];
}
else if (arguments.length == 3) {
collection = this.collection;
category = this.category;
id = arguments[0];
onEnable = arguments[1];
onDisable = arguments[2];
}
return Settings.on(collection, category, id, (value) => {
if (value) onEnable();
else onDisable();
});
}
get(collection, category, id) {
if (arguments.length == 2) {
collection = this.collection;
category = arguments[0];
id = arguments[1];
}
else if (arguments.length == 1) {
collection = this.collection;
category = this.category;
id = arguments[0];
}
return Settings.get(collection, category, id);
}
async enable() { async enable() {
this.log("Enabled"); this.log("Enabled");
await this.enabled(); await this.enabled();

View File

@ -49,12 +49,9 @@ export default class Group extends React.Component {
return <div className={groupClass}> return <div className={groupClass}>
<Title text={this.props.name} collapsible={this.props.collapsible} onClick={() => this.toggleCollapse()} button={this.props.button} isGroup={true} /> <Title text={this.props.name} collapsible={this.props.collapsible} onClick={() => this.toggleCollapse()} button={this.props.button} isGroup={true} />
<div className="bd-settings-container" ref={this.container}> <div className="bd-settings-container" ref={this.container}>
{settings.filter(s => !s.hidden).map((setting) => { {settings.filter(s => !s.hidden).map((setting) =>
// console.log(setting); <Switch disabled={setting.disabled} id={setting.id} key={setting.id} name={setting.name} note={setting.note} checked={setting.value} onChange={this.onChange} />
const item = <Switch disabled={setting.disabled} id={setting.id} key={setting.id} name={setting.name} note={setting.note} checked={setting.value} onChange={this.onChange} />; )}
const shouldHide = setting.shouldHide ? setting.shouldHide() : false;
if (!shouldHide) return item;
})}
</div> </div>
{this.props.showDivider && <Divider />} {this.props.showDivider && <Divider />}
</div>; </div>;

View File

@ -21,10 +21,6 @@ import SettingsTitle from "./title";
export default class V2_SettingsPanel { export default class V2_SettingsPanel {
constructor({onChange}) {
this.onChange = onChange;
}
get coreSettings() { get coreSettings() {
const settings = this.getSettings("core"); const settings = this.getSettings("core");
const categories = [...new Set(settings.map(s => s.category))]; const categories = [...new Set(settings.map(s => s.category))];