1
0
Fork 1
mirror of https://github.com/Vendicated/Vencord.git synced 2025-01-09 09:26:22 +00:00

make plugin manager actually use fields

This commit is contained in:
Vendicated 2024-11-21 03:54:31 +01:00
parent bd1dd23ba3
commit cff2c53972
No known key found for this signature in database
GPG key ID: D66986BAF75ECF18
8 changed files with 73 additions and 31 deletions

View file

@ -23,7 +23,7 @@ import type { ComponentType, MouseEventHandler } from "react";
const logger = new Logger("MessagePopover");
export interface ButtonItem {
export interface MessagePopoverButtonItem {
key?: string,
label: string,
icon: ComponentType<any>,
@ -33,23 +33,23 @@ export interface ButtonItem {
onContextMenu?: MouseEventHandler<HTMLButtonElement>;
}
export type MessagePopoverButtonFactory = (message: Message) => ButtonItem | null;
export type MessagePopoverButtonFactory = (message: Message) => MessagePopoverButtonItem | null;
export const buttons = new Map<string, MessagePopoverButtonFactory>();
export function addButton(
export function addMessagePopoverButton(
identifier: string,
item: MessagePopoverButtonFactory,
) {
buttons.set(identifier, item);
}
export function removeButton(identifier: string) {
export function removeMessagePopoverButton(identifier: string) {
buttons.delete(identifier);
}
export function _buildPopoverElements(
Component: React.ComponentType<ButtonItem>,
Component: React.ComponentType<MessagePopoverButtonItem>,
message: Message
) {
const items: React.ReactNode[] = [];

View file

@ -17,7 +17,7 @@
*/
import { get, set } from "@api/DataStore";
import { addButton, removeButton } from "@api/MessagePopover";
import { addMessagePopoverButton, removeMessagePopoverButton } from "@api/MessagePopover";
import { ImageInvisible, ImageVisible } from "@components/Icons";
import { Devs } from "@utils/constants";
import definePlugin from "@utils/types";
@ -48,7 +48,7 @@ export default definePlugin({
await getHiddenMessages();
await this.buildCss();
addButton("HideAttachments", msg => {
addMessagePopoverButton("HideAttachments", msg => {
if (!msg.attachments.length && !msg.embeds.length && !msg.stickerItems.length) return null;
const isHidden = hiddenMessages.has(msg.id);
@ -66,7 +66,7 @@ export default definePlugin({
stop() {
style.remove();
hiddenMessages.clear();
removeButton("HideAttachments");
removeMessagePopoverButton("HideAttachments");
},
async buildCss() {

View file

@ -16,8 +16,14 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import { addChatBarButton, removeChatBarButton } from "@api/ChatButtons";
import { registerCommand, unregisterCommand } from "@api/Commands";
import { addContextMenuPatch, removeContextMenuPatch } from "@api/ContextMenu";
import { addDecorator, removeDecorator } from "@api/MemberListDecorators";
import { addAccessory, removeAccessory } from "@api/MessageAccessories";
import { addDecoration, removeDecoration } from "@api/MessageDecorations";
import { addClickListener, addPreEditListener, addPreSendListener, removeClickListener, removePreEditListener, removePreSendListener } from "@api/MessageEvents";
import { addMessagePopoverButton, removeMessagePopoverButton } from "@api/MessagePopover";
import { Settings } from "@api/Settings";
import { Logger } from "@utils/Logger";
import { canonicalizeFind } from "@utils/patches";
@ -83,6 +89,8 @@ function isReporterTestable(p: Plugin, part: ReporterTestable) {
: (p.reporterTestable & part) === part;
}
const neededApiPlugins = new Set<string>();
// First round-trip to mark and force enable dependencies
//
// FIXME: might need to revisit this if there's ever nested (dependencies of dependencies) dependencies since this only
@ -106,10 +114,18 @@ for (const p of pluginsValues) if (isPluginEnabled(p.name)) {
dep.isDependency = true;
});
if (p.commands?.length) {
Plugins.CommandsAPI.isDependency = true;
settings.CommandsAPI.enabled = true;
}
if (p.commands?.length) neededApiPlugins.add("CommandsAPI");
if (p.onBeforeMessageEdit || p.onBeforeMessageSend || p.onMessageClick) neededApiPlugins.add("MessageEventsAPI");
if (p.renderChatBarButton) neededApiPlugins.add("ChatInputButtonAPI");
if (p.renderMemberListDecorator) neededApiPlugins.add("MemberListDecoratorsAPI");
if (p.renderMessageAccessory) neededApiPlugins.add("MessageAccessoriesAPI");
if (p.renderMessageDecoration) neededApiPlugins.add("MessageDecorationsAPI");
if (p.renderMessagePopoverButton) neededApiPlugins.add("MessagePopoverAPI");
}
for (const p of neededApiPlugins) {
Plugins[p].isDependency = true;
settings[p].enabled = true;
}
for (const p of pluginsValues) {
@ -215,7 +231,11 @@ export function subscribeAllPluginsFluxEvents(fluxDispatcher: typeof FluxDispatc
}
export const startPlugin = traceFunction("startPlugin", function startPlugin(p: Plugin) {
const { name, commands, contextMenus } = p;
const {
name, commands, contextMenus,
onBeforeMessageEdit, onBeforeMessageSend, onMessageClick,
renderChatBarButton, renderMemberListDecorator, renderMessageAccessory, renderMessageDecoration, renderMessagePopoverButton
} = p;
if (p.start) {
logger.info("Starting plugin", name);
@ -257,11 +277,25 @@ export const startPlugin = traceFunction("startPlugin", function startPlugin(p:
}
}
if (onBeforeMessageEdit) addPreEditListener(onBeforeMessageEdit);
if (onBeforeMessageSend) addPreSendListener(onBeforeMessageSend);
if (onMessageClick) addClickListener(onMessageClick);
if (renderChatBarButton) addChatBarButton(name, renderChatBarButton);
if (renderMemberListDecorator) addDecorator(name, renderMemberListDecorator);
if (renderMessageDecoration) addDecoration(name, renderMessageDecoration);
if (renderMessageAccessory) addAccessory(name, renderMessageAccessory);
if (renderMessagePopoverButton) addMessagePopoverButton(name, renderMessagePopoverButton);
return true;
}, p => `startPlugin ${p.name}`);
export const stopPlugin = traceFunction("stopPlugin", function stopPlugin(p: Plugin) {
const { name, commands, contextMenus } = p;
const {
name, commands, contextMenus,
onBeforeMessageEdit, onBeforeMessageSend, onMessageClick,
renderChatBarButton, renderMemberListDecorator, renderMessageAccessory, renderMessageDecoration, renderMessagePopoverButton
} = p;
if (p.stop) {
logger.info("Stopping plugin", name);
@ -300,5 +334,15 @@ export const stopPlugin = traceFunction("stopPlugin", function stopPlugin(p: Plu
}
}
if (onBeforeMessageEdit) removePreEditListener(onBeforeMessageEdit);
if (onBeforeMessageSend) removePreSendListener(onBeforeMessageSend);
if (onMessageClick) removeClickListener(onMessageClick);
if (renderChatBarButton) removeChatBarButton(name);
if (renderMemberListDecorator) removeDecorator(name);
if (renderMessageDecoration) removeDecoration(name);
if (renderMessageAccessory) removeAccessory(name);
if (renderMessagePopoverButton) removeMessagePopoverButton(name);
return true;
}, p => `stopPlugin ${p.name}`);

View file

@ -17,7 +17,7 @@
*/
import { addChatBarButton, ChatBarButton, ChatBarButtonFactory } from "@api/ChatButtons";
import { addButton, removeButton } from "@api/MessagePopover";
import { addMessagePopoverButton, removeMessagePopoverButton } from "@api/MessagePopover";
import { updateMessage } from "@api/MessageUpdater";
import { definePluginSettings } from "@api/Settings";
import ErrorBoundary from "@components/ErrorBoundary";
@ -125,7 +125,7 @@ export default definePlugin({
/(http(s)?:\/\/.)?(www\.)?[-a-zA-Z0-9@:%._+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_+.~#?&//=]*)/,
),
async start() {
addButton("InvisibleChat", message => {
addMessagePopoverButton("InvisibleChat", message => {
return this.INV_REGEX.test(message?.content)
? {
label: "Decrypt Message",
@ -151,8 +151,8 @@ export default definePlugin({
},
stop() {
removeButton("InvisibleChat");
removeButton("InvisibleChat");
removeMessagePopoverButton("InvisibleChat");
removeMessagePopoverButton("InvisibleChat");
},
// Gets the Embed of a Link

View file

@ -16,7 +16,7 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import { addChatBarButton, ChatBarButton, ChatBarButtonFactory, removeChatBarButton } from "@api/ChatButtons";
import { ChatBarButton, ChatBarButtonFactory } from "@api/ChatButtons";
import { generateId, sendBotMessage } from "@api/Commands";
import { Devs } from "@utils/constants";
import definePlugin, { StartAt } from "@utils/types";
@ -121,11 +121,9 @@ export default definePlugin({
name: "PreviewMessage",
description: "Lets you preview your message before sending it.",
authors: [Devs.Aria],
dependencies: ["ChatInputButtonAPI"],
// start early to ensure we're the first plugin to add our button
// This makes the popping in less awkward
startAt: StartAt.Init,
start: () => addChatBarButton("previewMessage", PreviewButton),
stop: () => removeChatBarButton("previewMessage"),
renderChatBarButton: PreviewButton,
});

View file

@ -16,7 +16,7 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import { addButton, removeButton } from "@api/MessagePopover";
import { addMessagePopoverButton, removeMessagePopoverButton } from "@api/MessagePopover";
import { Devs } from "@utils/constants";
import { insertTextIntoChatInputBox } from "@utils/discord";
import definePlugin from "@utils/types";
@ -29,7 +29,7 @@ export default definePlugin({
dependencies: ["MessagePopoverAPI"],
start() {
addButton("QuickMention", msg => {
addMessagePopoverButton("QuickMention", msg => {
const channel = ChannelStore.getChannel(msg.channel_id);
if (channel.guild_id && !PermissionStore.can(PermissionsBits.SEND_MESSAGES, channel)) return null;
@ -43,7 +43,7 @@ export default definePlugin({
});
},
stop() {
removeButton("QuickMention");
removeMessagePopoverButton("QuickMention");
},
Icon: () => (

View file

@ -22,7 +22,7 @@ import { addChatBarButton, removeChatBarButton } from "@api/ChatButtons";
import { findGroupChildrenByChildId, NavContextMenuPatchCallback } from "@api/ContextMenu";
import { addAccessory, removeAccessory } from "@api/MessageAccessories";
import { addPreSendListener, removePreSendListener } from "@api/MessageEvents";
import { addButton, removeButton } from "@api/MessagePopover";
import { addMessagePopoverButton, removeMessagePopoverButton } from "@api/MessagePopover";
import { Devs } from "@utils/constants";
import definePlugin from "@utils/types";
import { ChannelStore, Menu } from "@webpack/common";
@ -68,7 +68,7 @@ export default definePlugin({
addChatBarButton("vc-translate", TranslateChatBarIcon);
addButton("vc-translate", message => {
addMessagePopoverButton("vc-translate", message => {
if (!message.content) return null;
return {
@ -101,7 +101,7 @@ export default definePlugin({
stop() {
removePreSendListener(this.preSend);
removeChatBarButton("vc-translate");
removeButton("vc-translate");
removeMessagePopoverButton("vc-translate");
removeAccessory("vc-translation");
},
});

View file

@ -17,7 +17,7 @@
*/
import { NavContextMenuPatchCallback } from "@api/ContextMenu";
import { addButton, removeButton } from "@api/MessagePopover";
import { addMessagePopoverButton, removeMessagePopoverButton } from "@api/MessagePopover";
import { definePluginSettings } from "@api/Settings";
import { CodeBlock } from "@components/CodeBlock";
import ErrorBoundary from "@components/ErrorBoundary";
@ -160,7 +160,7 @@ export default definePlugin({
},
start() {
addButton("ViewRaw", msg => {
addMessagePopoverButton("ViewRaw", msg => {
const handleClick = () => {
if (settings.store.clickMethod === "Right") {
copyWithToast(msg.content);
@ -197,6 +197,6 @@ export default definePlugin({
},
stop() {
removeButton("ViewRaw");
removeMessagePopoverButton("ViewRaw");
}
});