BetterDiscordAddons/Plugins/ThemeSettings/ThemeSettings.plugin.js

269 lines
12 KiB
JavaScript
Raw Normal View History

2020-10-20 23:25:34 +02:00
/**
* @name ThemeSettings
2021-03-05 13:26:41 +01:00
* @author DevilBro
2020-10-20 23:25:34 +02:00
* @authorId 278543574059057154
2022-09-06 19:55:02 +02:00
* @version 1.3.5
* @description Allows you to change Theme Variables within Discord
2020-10-20 23:25:34 +02:00
* @invite Jx3TjNS
* @donate https://www.paypal.me/MircoWittrien
* @patreon https://www.patreon.com/MircoWittrien
2021-03-09 15:10:55 +01:00
* @website https://mwittrien.github.io/
* @source https://github.com/mwittrien/BetterDiscordAddons/tree/master/Plugins/ThemeSettings/
2021-03-10 09:17:37 +01:00
* @updateUrl https://mwittrien.github.io/BetterDiscordAddons/Plugins/ThemeSettings/ThemeSettings.plugin.js
2020-10-20 23:25:34 +02:00
*/
2018-12-11 19:43:51 +01:00
2020-09-19 20:49:33 +02:00
module.exports = (_ => {
2022-09-01 14:40:11 +02:00
const changeLog = {
2022-09-06 19:55:02 +02:00
improved: {
"Changed Location": "Due to the new Plugin Guidelines, which forbid changes to BDs Plugin/Themes Pages, the option to change the variables for Themes are now inside the Plugin Settings of 'ThemeSettings'"
}
2020-09-19 20:49:33 +02:00
};
2020-11-13 19:47:44 +01:00
2022-02-05 21:14:17 +01:00
return !window.BDFDB_Global || (!window.BDFDB_Global.loaded && !window.BDFDB_Global.started) ? class {
2022-09-01 14:55:22 +02:00
constructor (meta) {for (let key in meta) this[key] = meta[key];}
getName () {return this.name;}
getAuthor () {return this.author;}
getVersion () {return this.version;}
getDescription () {return `The Library Plugin needed for ${this.name} is missing. Open the Plugin Settings to download it. \n\n${this.description}`;}
2021-02-01 17:13:13 +01:00
downloadLibrary () {
require("request").get("https://mwittrien.github.io/BetterDiscordAddons/Library/0BDFDB.plugin.js", (e, r, b) => {
2021-03-05 13:14:18 +01:00
if (!e && b && r.statusCode == 200) require("fs").writeFile(require("path").join(BdApi.Plugins.folder, "0BDFDB.plugin.js"), b, _ => BdApi.showToast("Finished downloading BDFDB Library", {type: "success"}));
2021-03-06 14:59:48 +01:00
else BdApi.alert("Error", "Could not download BDFDB Library Plugin. Try again later or download it manually from GitHub: https://mwittrien.github.io/downloader/?library");
2021-02-01 17:13:13 +01:00
});
}
2020-05-22 21:17:02 +02:00
2021-01-06 12:38:36 +01:00
load () {
2020-11-19 16:51:14 +01:00
if (!window.BDFDB_Global || !Array.isArray(window.BDFDB_Global.pluginQueue)) window.BDFDB_Global = Object.assign({}, window.BDFDB_Global, {pluginQueue: []});
2020-09-19 20:49:33 +02:00
if (!window.BDFDB_Global.downloadModal) {
window.BDFDB_Global.downloadModal = true;
2022-09-01 14:55:22 +02:00
BdApi.showConfirmationModal("Library Missing", `The Library Plugin needed for ${this.name} is missing. Please click "Download Now" to install it.`, {
2020-09-19 20:49:33 +02:00
confirmText: "Download Now",
cancelText: "Cancel",
onCancel: _ => {delete window.BDFDB_Global.downloadModal;},
2020-09-20 08:15:13 +02:00
onConfirm: _ => {
delete window.BDFDB_Global.downloadModal;
2021-02-01 17:13:13 +01:00
this.downloadLibrary();
2020-09-20 08:15:13 +02:00
}
2020-09-19 20:49:33 +02:00
});
2020-03-28 07:55:39 +01:00
}
2022-09-01 14:55:22 +02:00
if (!window.BDFDB_Global.pluginQueue.includes(this.name)) window.BDFDB_Global.pluginQueue.push(this.name);
2020-10-09 21:09:35 +02:00
}
2021-01-06 12:38:36 +01:00
start () {this.load();}
stop () {}
getSettingsPanel () {
2020-11-28 23:12:09 +01:00
let template = document.createElement("template");
2022-09-01 14:55:22 +02:00
template.innerHTML = `<div style="color: var(--header-primary); font-size: 16px; font-weight: 300; white-space: pre; line-height: 22px;">The Library Plugin needed for ${this.name} is missing.\nPlease click <a style="font-weight: 500;">Download Now</a> to install it.</div>`;
2021-02-01 17:13:13 +01:00
template.content.firstElementChild.querySelector("a").addEventListener("click", this.downloadLibrary);
2020-11-28 23:12:09 +01:00
return template.content.firstElementChild;
}
2021-05-12 15:30:56 +02:00
} : (([Plugin, BDFDB]) => {
2020-09-19 20:49:33 +02:00
var dir;
2020-10-09 21:09:35 +02:00
return class ThemeSettings extends Plugin {
2021-01-06 12:38:36 +01:00
onLoad () {
2020-09-19 20:49:33 +02:00
dir = BDFDB.BDUtils.getThemesFolder();
2022-09-06 19:55:02 +02:00
this.css = `
${BDFDB.dotCN._themesettingsgrid} {
display: grid;
grid-template-columns: 50% auto;
2021-02-09 12:57:45 +01:00
}
2022-09-06 19:55:02 +02:00
${BDFDB.dotCN._themesettingscard} {
display: flex;
align-items: center;
padding: 10px;
}
${BDFDB.dotCN._themesettingscardname} {
flex: 1 1 auto;
margin-right: 10px;
}
`;
2020-03-28 07:55:39 +01:00
}
2020-09-19 20:49:33 +02:00
2022-09-06 19:55:02 +02:00
onStart () {}
2021-02-09 12:57:45 +01:00
2022-09-06 19:55:02 +02:00
onStop () {}
2021-02-09 12:57:45 +01:00
2022-09-06 19:55:02 +02:00
getSettingsPanel () {
let themes = window.BdApi && BdApi.Themes && BdApi.Themes.getAll && BdApi.Themes.getAll().map(theme => {
let css = theme.css.replace(/\r/g, "");
2021-05-12 15:30:56 +02:00
let imports = css.split("\n@import url(").splice(1).map(n => [n.split(");")[0], true]).concat(css.split("\n/* @import url(").splice(1).map(n => [n.split("); */")[0], false]));
let vars = css.split(":root");
if (vars.length > 1) {
vars = vars[1].replace(/\t\(/g, " (").replace(/\t| {2,}/g, "").replace(/\/\*\n*((?!\/\*|\*\/).|\n)*\n+((?!\/\*|\*\/).|\n)*\n*\*\//g, "").replace(/\n\/\*.*?\*\//g, "").replace(/\n/g, "");
vars = vars.split("{");
vars.shift();
vars = vars.join("{").replace(/\s*(:|;|--|\*)\s*/g, "$1");
vars = vars.split("}")[0];
vars = (vars.endsWith(";") ? vars.slice(0, -1) : vars).slice(2).split(/;--|\*\/--/);
}
else vars = [];
2022-09-06 19:55:02 +02:00
if (imports.length || vars.length) return {data: theme, imports, vars};
}).filter(n => n);
return themes && themes.length && BDFDB.ReactUtils.createElement("div", {
className: BDFDB.disCN._themesettingsgrid,
children: themes.map(theme => BDFDB.ReactUtils.createElement("div", {
className: BDFDB.disCN._themesettingscard,
children: [
BDFDB.ReactUtils.createElement("div", {
className: BDFDB.disCN._themesettingscardname,
children: theme.data.name
}),
BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.Button, {
size: BDFDB.LibraryComponents.Button.Sizes.SMALL,
children: BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.Flex, {
align: BDFDB.LibraryComponents.Flex.Align.CENTER,
children: [
BDFDB.ReactUtils.createElement("div", {
children: "Edit"
}),
BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.SvgIcon, {
className: BDFDB.disCN.marginleft4,
name: BDFDB.LibraryComponents.SvgIcon.Names.PENCIL
})
]
}),
onClick: _ => {
let importInstances = {}, inputInstances = {};
BDFDB.ModalUtils.open(this, {
header: `${theme.data.name} ${BDFDB.LanguageUtils.LanguageStrings.SETTINGS}`,
subHeader: "",
size: "MEDIUM",
children: BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.SettingsPanel, {
addon: theme.data,
children: _ => {
let settingsItems = [];
let varInputs = [];
for (let i in theme.vars) {
let varStr = theme.vars[i].split(":");
let varName = varStr.shift().trim();
varStr = varStr.join(":").split(/;[^A-z0-9]|\/\*/);
let varValue = varStr.shift().trim();
if (varValue) {
let childType = "text", childMode = "";
let isColor = BDFDB.ColorUtils.getType(varValue);
let isComp = !isColor && /^[0-9 ]+,[0-9 ]+,[0-9 ]+$/g.test(varValue);
if (isColor || isComp) {
childType = "color";
childMode = isComp && "comp";
}
else {
let isUrlFile = /url\(.+\)/gi.test(varValue);
let isFile = !isUrlFile && /(http(s)?):\/\/[(www\.)?a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)/.test(varValue);
if (isFile || isUrlFile) {
childType = "file";
childMode = isUrlFile && "url";
}
}
let varDescription = varStr.join("").replace(/\*\/|\/\*/g, "").replace(/:/g, ": ").replace(/: \//g, ":/").replace(/--/g, " --").replace(/\( --/g, "(--").trim();
varInputs.push(BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.SettingsItem, {
type: "TextInput",
margin: 8,
childProps: {
type: childType,
mode: childMode,
filter: childType == "file" && "image",
ref: instance => {if (instance) inputInstances[i] = instance;}
},
2022-09-27 11:53:04 +02:00
label: varName.split("-").map(BDFDB.StringUtils.upperCaseFirstChar).join(" "),
2022-09-06 19:55:02 +02:00
note: varDescription && varDescription.indexOf("*") == 0 ? varDescription.slice(1) : varDescription,
basis: "70%",
name: varName,
value: varValue,
oldValue: varValue,
defaultValue: varValue,
placeholder: varValue
}));
2021-05-12 15:30:56 +02:00
}
2022-09-06 19:55:02 +02:00
};
if (theme.imports.length) settingsItems.push(BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.SettingsPanelList, {
title: "Imports:",
dividerBottom: varInputs.length,
children: theme.imports.map((impo, i) => {
let name = impo[0].split("/").pop().replace(/"/g, "");
return BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.SettingsItem, {
type: "Switch",
margin: 8,
childProps: {ref: instance => {if (instance) importInstances[i] = instance;}},
label: name[0].toUpperCase() + name.slice(1),
note: impo[0].replace(/"/g, ""),
name: impo[0],
value: impo[1],
oldValue: impo[1].toString(),
defaultValue: impo[1].toString()
});
})
}));
if (varInputs.length) settingsItems.push(BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.SettingsPanelList, {
title: "Variables:",
children: varInputs
2021-05-12 15:30:56 +02:00
}));
2022-09-06 19:55:02 +02:00
return settingsItems;
2021-05-12 15:30:56 +02:00
}
2022-09-06 19:55:02 +02:00
}),
buttons: [{
contents: BDFDB.LanguageUtils.LanguageStrings.SAVE,
color: "BRAND",
onClick: _ => this.updateTheme(theme.data, {imports: importInstances, inputs: inputInstances}, false)
}, {
contents: BDFDB.LanguageUtils.LanguageStrings.RESET,
look: "LINK",
onClick: _ => this.updateTheme(theme.data, {imports: importInstances, inputs: inputInstances}, true)
}]
});
}
})
]
}))
});
2018-12-11 19:43:51 +01:00
}
2020-11-06 16:14:31 +01:00
2022-09-06 19:55:02 +02:00
updateTheme (theme, instances, reset) {
let path = BDFDB.LibraryRequires.path.join(dir, theme.filename);
2020-11-06 16:14:31 +01:00
let css = BDFDB.LibraryRequires.fs.readFileSync(path).toString();
if (css) {
let amount = 0;
2021-05-12 15:30:56 +02:00
for (let i in instances.imports) {
let input = instances.imports[i];
2021-01-29 20:57:25 +01:00
let oldValue = input.props.oldValue;
2021-05-12 15:30:56 +02:00
let newValue = reset ? input.props.defaultValue : input.props.value;
2020-12-19 19:53:11 +01:00
if (newValue.toString() != oldValue.toString()) {
2021-05-12 15:30:56 +02:00
let trueValue = typeof newValue == "string" ? newValue == "true" : newValue;
2021-01-29 20:57:25 +01:00
let importUrl = input.props.name;
2021-05-12 15:30:56 +02:00
if (trueValue) css = css.replace(new RegExp(`\\n${BDFDB.StringUtils.regEscape("/* @import url(" + importUrl + "); */")}`, "g"), `\n@import url(${importUrl});`);
2020-12-19 19:53:11 +01:00
else css = css.replace(new RegExp(`\\n${BDFDB.StringUtils.regEscape("@import url(" + importUrl + ");")}`, "g"), `\n/* @import url(${importUrl}); */`);
2021-05-12 15:30:56 +02:00
input.props.value = trueValue;
2021-01-29 20:57:25 +01:00
input.props.oldValue = newValue;
2020-12-19 19:53:11 +01:00
amount++;
}
}
2021-05-12 15:30:56 +02:00
for (let i in instances.inputs) {
let input = instances.inputs[i];
2021-01-29 20:57:25 +01:00
let oldValue = input.props.oldValue;
2021-05-12 15:30:56 +02:00
let newValue = reset ? input.props.defaultValue : input.props.value;
2020-11-06 16:14:31 +01:00
if (newValue && newValue.trim() && newValue != oldValue) {
2021-01-29 20:57:25 +01:00
let varName = input.props.name;
2020-12-19 19:53:11 +01:00
css = css.replace(new RegExp(`--${BDFDB.StringUtils.regEscape(varName)}(\\s*):(\\s*)${BDFDB.StringUtils.regEscape(oldValue)}`,"g"), `--${varName}$1: $2${newValue}`);
2021-05-12 15:30:56 +02:00
input.props.value = newValue;
2021-01-29 20:57:25 +01:00
input.props.oldValue = newValue;
input.props.placeholder = newValue;
2020-11-06 16:14:31 +01:00
amount++;
}
}
if (amount > 0) {
2021-05-12 15:30:56 +02:00
BDFDB.ReactUtils.forceUpdate(BDFDB.ObjectUtils.toArray(instances.imports), BDFDB.ObjectUtils.toArray(instances.inputs));
2020-11-06 16:14:31 +01:00
BDFDB.LibraryRequires.fs.writeFileSync(path, css);
2022-09-06 19:55:02 +02:00
BDFDB.NotificationUtils.toast(`Updated ${amount} Variable${amount == 1 ? "" : "s"} in '${theme.filename}'`, {type: "success"});
2020-11-06 16:14:31 +01:00
}
2022-09-06 19:55:02 +02:00
else BDFDB.NotificationUtils.toast(`There are no changed Variables to be updated in '${theme.filename}'`, {type: "warning"});
2020-11-06 16:14:31 +01:00
}
2022-09-06 19:55:02 +02:00
else BDFDB.NotificationUtils.toast(`Could not find Theme File '${theme.filename}'`, {type: "danger"});
2020-11-06 16:14:31 +01:00
}
2020-09-19 20:49:33 +02:00
};
2022-09-01 14:40:11 +02:00
})(window.BDFDB_Global.PluginUtils.buildPlugin(changeLog));
2020-09-19 20:49:33 +02:00
})();