Add DiscordContextMenu to the plugin API
This commit is contained in:
parent
237899c91a
commit
8cf28fcaba
|
@ -571,6 +571,37 @@ export default class PluginApi {
|
|||
return m => MonkeyPatch(this.plugin.id, m);
|
||||
}
|
||||
|
||||
/**
|
||||
* DiscordContextMenu
|
||||
*/
|
||||
|
||||
get discordContextMenus() {
|
||||
return this._discordContextMenus || (this._discordContextMenus = []);
|
||||
}
|
||||
addDiscordContextMenu(items, filter) {
|
||||
const menu = DiscordContextMenu.add(items, filter);
|
||||
this.discordContextMenus.push(menu);
|
||||
return menu;
|
||||
}
|
||||
removeDiscordContextMenu(menu) {
|
||||
DiscordContextMenu.remove(menu);
|
||||
Utils.removeFromArray(this.discordContextMenus, menu);
|
||||
}
|
||||
removeAllDiscordContextMenus() {
|
||||
for (let menu of this.discordContextMenus) {
|
||||
this.removeDiscordContextMenu(menu);
|
||||
}
|
||||
}
|
||||
get DiscordContextMenu() {
|
||||
return Object.defineProperty({
|
||||
add: this.addDiscordContextMenu.bind(this),
|
||||
remove: this.removeDiscordContextMenu.bind(this),
|
||||
removeAll: this.removeAllDiscordContextMenus.bind(this)
|
||||
}, 'menus', {
|
||||
get: () => this.discordContextMenus
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// Stop plugins from modifying the plugin API for all plugins
|
||||
|
|
|
@ -8,6 +8,7 @@
|
|||
* LICENSE file in the root directory of this source tree.
|
||||
*/
|
||||
|
||||
import { Utils } from 'common';
|
||||
import { ReactComponents, WebpackModules, MonkeyPatch } from 'modules';
|
||||
import { VueInjector, Toasts } from 'ui';
|
||||
import CMGroup from './components/contextmenu/Group.vue';
|
||||
|
@ -40,7 +41,13 @@ export class DiscordContextMenu {
|
|||
*/
|
||||
static add(items, filter) {
|
||||
if (!this.patched) this.patch();
|
||||
this.menus.push({ items, filter });
|
||||
const menu = { items, filter };
|
||||
this.menus.push(menu);
|
||||
return menu;
|
||||
}
|
||||
|
||||
static remove(menu) {
|
||||
Utils.removeFromArray(this.menus, menu);
|
||||
}
|
||||
|
||||
static get menus() {
|
||||
|
|
|
@ -1,20 +1,29 @@
|
|||
exports.main = (Plugin, { Logger, Settings, Modals, BdMenu: { BdMenuItems }, CommonComponents, Api }) => class extends Plugin {
|
||||
exports.main = (Plugin, { Logger, Settings, Modals, BdMenu: { BdMenuItems }, CommonComponents, DiscordContextMenu, Api }) => class extends Plugin {
|
||||
async onstart() {
|
||||
this.keybindEvent = this.keybindEvent.bind(this);
|
||||
|
||||
// Some array event examples
|
||||
/**
|
||||
* Array setting events.
|
||||
*/
|
||||
|
||||
const arraySetting = this.settings.getSetting('default', 'array-1');
|
||||
Logger.log('Array setting', arraySetting);
|
||||
arraySetting.on('item-added', event => Logger.log('Item', event.item, 'was added to the array setting'));
|
||||
arraySetting.on('item-updated', event => Logger.log('Item', event.item, 'of the array setting was updated', event));
|
||||
arraySetting.on('item-removed', event => Logger.log('Item', event.item, 'removed from the array setting'));
|
||||
|
||||
// Keybind setting examples
|
||||
/**
|
||||
* Keybind setting events.
|
||||
*/
|
||||
|
||||
const keybindSetting = this.settings.getSetting('default', 'keybind-1');
|
||||
Logger.log('Keybind setting', keybindSetting);
|
||||
keybindSetting.on('keybind-activated', this.keybindEvent);
|
||||
|
||||
// Create a new settings set and add it to the menu
|
||||
/**
|
||||
* Settings.
|
||||
*/
|
||||
|
||||
const set = Settings.createSet({
|
||||
text: this.name
|
||||
});
|
||||
|
@ -41,7 +50,7 @@ exports.main = (Plugin, { Logger, Settings, Modals, BdMenu: { BdMenuItems }, Com
|
|||
Logger.log('Updated settings', updatedSettings);
|
||||
await new Promise(resolve => setTimeout(resolve, 500));
|
||||
set.setSaved();
|
||||
})
|
||||
});
|
||||
|
||||
const setting2 = await category.addSetting({
|
||||
id: 'setting-2',
|
||||
|
@ -52,6 +61,10 @@ exports.main = (Plugin, { Logger, Settings, Modals, BdMenu: { BdMenuItems }, Com
|
|||
|
||||
setting2.on('setting-updated', event => Logger.log('Setting 2 was changed to', event.value));
|
||||
|
||||
/**
|
||||
* Menu items.
|
||||
*/
|
||||
|
||||
this.menuItem = BdMenuItems.addSettingsSet('Plugins', set, 'Plugin 4');
|
||||
|
||||
this.menuItem2 = BdMenuItems.addVueComponent('Plugins', 'Also Plugin 4', {
|
||||
|
@ -65,6 +78,17 @@ exports.main = (Plugin, { Logger, Settings, Modals, BdMenu: { BdMenuItems }, Com
|
|||
Api, plugin: Api.plugin
|
||||
}; }
|
||||
});
|
||||
|
||||
/**
|
||||
* Discord context menus.
|
||||
*/
|
||||
|
||||
this.contextMenu = DiscordContextMenu.add([
|
||||
{
|
||||
text: 'Test',
|
||||
onClick: () => Modals.basic('Test', 'Hello from Plugin 4')
|
||||
}
|
||||
]);
|
||||
}
|
||||
|
||||
onstop() {
|
||||
|
@ -72,6 +96,7 @@ exports.main = (Plugin, { Logger, Settings, Modals, BdMenu: { BdMenuItems }, Com
|
|||
keybindSetting.off('keybind-activated', this.keybindEvent);
|
||||
|
||||
BdMenuItems.removeAll();
|
||||
DiscordContextMenu.removeAll();
|
||||
}
|
||||
|
||||
keybindEvent(event) {
|
||||
|
|
Loading…
Reference in New Issue