diff --git a/.eslintrc b/.eslintrc index 1a64e4a4..0173af86 100644 --- a/.eslintrc +++ b/.eslintrc @@ -4,7 +4,7 @@ "node": true }, "parserOptions": { - "ecmaVersion": 2020, + "ecmaVersion": 2022, "sourceType": "module", "ecmaFeatures": { "jsx": true diff --git a/package.json b/package.json index 57770b99..98da3e90 100644 --- a/package.json +++ b/package.json @@ -14,7 +14,7 @@ "lint": "eslint --ext .js common/ && npm run lint --prefix injector && npm run lint --prefix renderer", "test": "mocha --require @babel/register --recursive \"./tests/renderer/*.js\"", "dist": "npm run build-prod && node scripts/pack.js", - "api": "jsdoc -X renderer/src/modules/pluginapi.js > jsdoc-ast.json" + "api": "jsdoc -X -r renderer/src/modules/api/ > jsdoc-ast.json" }, "devDependencies": { "asar": "^3.0.3", diff --git a/renderer/src/index.js b/renderer/src/index.js index 9921da4a..25e99dfe 100644 --- a/renderer/src/index.js +++ b/renderer/src/index.js @@ -2,7 +2,7 @@ import secure from "./secure"; import patchModuleLoad from "./moduleloader"; import LoadingIcon from "./loadingicon"; import BetterDiscord from "./modules/core"; -import BdApi from "./modules/pluginapi"; +import BdApi from "./modules/api/index"; // Perform some setup secure(); diff --git a/renderer/src/moduleloader.js b/renderer/src/moduleloader.js index 14db4e8a..0b2351f9 100644 --- a/renderer/src/moduleloader.js +++ b/renderer/src/moduleloader.js @@ -1,4 +1,4 @@ -import BdApi from "./modules/pluginapi"; +import BdApi from "./modules/api/index"; export default function() { const namespace = "betterdiscord"; diff --git a/renderer/src/modules/api/addonapi.js b/renderer/src/modules/api/addonapi.js new file mode 100644 index 00000000..96b92568 --- /dev/null +++ b/renderer/src/modules/api/addonapi.js @@ -0,0 +1,64 @@ +/** + * `AddonAPI` is a utility class for working with plugins and themes. Instances are accessible through the {@link BdApi}. + * @name AddonAPI + */ +class AddonAPI { + #manager; + + constructor(manager) {this.#manager = manager;} + + /** + * The path to the addon folder. + * @type string + */ + get folder() {return this.#manager.addonFolder;} + + /** + * Determines if a particular adon is enabled. + * @param {string} idOrFile Addon id or filename. + * @returns {boolean} + */ + isEnabled(idOrFile) {return this.#manager.isEnabled(idOrFile);} + + /** + * Enables the given addon. + * @param {string} idOrFile Addon id or filename. + */ + enable(idOrAddon) {return this.#manager.enableAddon(idOrAddon);} + + /** + * Disables the given addon. + * @param {string} idOrFile Addon id or filename. + */ + disable(idOrAddon) {return this.#manager.disableAddon(idOrAddon);} + + /** + * Toggles if a particular addon is enabled. + * @param {string} idOrFile Addon id or filename. + */ + toggle(idOrAddon) {return this.#manager.toggleAddon(idOrAddon);} + + /** + * Reloads if a particular addon is enabled. + * @param {string} idOrFile Addon id or filename. + */ + reload(idOrFileOrAddon) {return this.#manager.reloadAddon(idOrFileOrAddon);} + + /** + * Gets a particular addon. + * @param {string} idOrFile Addon id or filename. + * @returns {object} Addon instance + */ + get(idOrFile) {return this.#manager.getAddon(idOrFile);} + + /** + * Gets all addons of this type. + * @returns {Array} Array of all addon instances + */ + getAll() {return this.#manager.addonList.map(a => this.#manager.getAddon(a.id));} +} + +Object.freeze(AddonAPI); +Object.freeze(AddonAPI.prototype); + +export default AddonAPI; \ No newline at end of file diff --git a/renderer/src/modules/api/data.js b/renderer/src/modules/api/data.js new file mode 100644 index 00000000..b054b47f --- /dev/null +++ b/renderer/src/modules/api/data.js @@ -0,0 +1,56 @@ +import DataStore from "../datastore"; + +/** + * `Data` is a simple utility class for the management of plugin data. An instance is available on {@link BdApi}. + * @type Data + * @summary {@link Data} is a simple utility class for the management of plugin data. + * @name Data + */ +class Data { + + constructor(callerName) { + if (!callerName) return; + this.save = this.save.bind(this, callerName); + this.load = this.load.bind(this, callerName); + this.delete = this.delete.bind(this, callerName); + } + + /** + * Saves JSON-serializable data. + * + * @param {string} pluginName Name of the plugin saving data + * @param {string} key Which piece of data to store + * @param {any} data The data to be saved + * @returns + */ + save(pluginName, key, data) { + return DataStore.setPluginData(pluginName, key, data); + } + + /** + * Loads previously stored data. + * + * @param {string} pluginName Name of the plugin loading data + * @param {string} key Which piece of data to load + * @returns {any} The stored data + */ + load(pluginName, key, data) { + return DataStore.setPluginData(pluginName, key, data); + } + + /** + * Deletes a piece of stored data, this is different than saving as null or undefined. + * + * @param {string} pluginName Name of the plugin deleting data + * @param {string} key Which piece of data to delete + */ + delete(pluginName, key, data) { + return DataStore.setPluginData(pluginName, key, data); + } + +} + +Object.freeze(Data); +Object.freeze(Data.prototype); + +export default Data; \ No newline at end of file diff --git a/renderer/src/modules/api/dom.js b/renderer/src/modules/api/dom.js new file mode 100644 index 00000000..6a6ad286 --- /dev/null +++ b/renderer/src/modules/api/dom.js @@ -0,0 +1,58 @@ +import DOMManager from "../dommanager"; +import Utilities from "../utilities"; + +/** + * `DOM` is a simple utility class for dom manipulation. An instance is available on {@link BdApi}. + * @type DOM + * @summary {@link DOM} is a simple utility class for dom manipulation. + * @name DOM + */ +class DOM { + + constructor(callerName) { + if (!callerName) return; + this.addStyle = this.addStyle.bind(this, callerName); + this.removeStyle = this.removeStyle.bind(this, callerName); + } + + /** + * Adds a `