From cff2c5397271afb237c353761da52eb041bcdde4 Mon Sep 17 00:00:00 2001 From: Vendicated Date: Thu, 21 Nov 2024 03:54:31 +0100 Subject: [PATCH] make plugin manager actually use fields --- src/api/MessagePopover.tsx | 10 ++-- src/plugins/hideAttachments/index.tsx | 6 +-- src/plugins/index.ts | 56 ++++++++++++++++++--- src/plugins/invisibleChat.desktop/index.tsx | 8 +-- src/plugins/previewMessage/index.tsx | 6 +-- src/plugins/quickMention/index.tsx | 6 +-- src/plugins/translate/index.tsx | 6 +-- src/plugins/viewRaw/index.tsx | 6 +-- 8 files changed, 73 insertions(+), 31 deletions(-) diff --git a/src/api/MessagePopover.tsx b/src/api/MessagePopover.tsx index 4009dfc66..717879546 100644 --- a/src/api/MessagePopover.tsx +++ b/src/api/MessagePopover.tsx @@ -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, @@ -33,23 +33,23 @@ export interface ButtonItem { onContextMenu?: MouseEventHandler; } -export type MessagePopoverButtonFactory = (message: Message) => ButtonItem | null; +export type MessagePopoverButtonFactory = (message: Message) => MessagePopoverButtonItem | null; export const buttons = new Map(); -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, + Component: React.ComponentType, message: Message ) { const items: React.ReactNode[] = []; diff --git a/src/plugins/hideAttachments/index.tsx b/src/plugins/hideAttachments/index.tsx index fe7f4ab92..253e3d3be 100644 --- a/src/plugins/hideAttachments/index.tsx +++ b/src/plugins/hideAttachments/index.tsx @@ -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() { diff --git a/src/plugins/index.ts b/src/plugins/index.ts index 129e42a0d..7777cb836 100644 --- a/src/plugins/index.ts +++ b/src/plugins/index.ts @@ -16,8 +16,14 @@ * along with this program. If not, see . */ +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(); + // 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}`); diff --git a/src/plugins/invisibleChat.desktop/index.tsx b/src/plugins/invisibleChat.desktop/index.tsx index 49b76f3b5..3862f18e4 100644 --- a/src/plugins/invisibleChat.desktop/index.tsx +++ b/src/plugins/invisibleChat.desktop/index.tsx @@ -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 diff --git a/src/plugins/previewMessage/index.tsx b/src/plugins/previewMessage/index.tsx index c7f39f052..7b03e31d7 100644 --- a/src/plugins/previewMessage/index.tsx +++ b/src/plugins/previewMessage/index.tsx @@ -16,7 +16,7 @@ * along with this program. If not, see . */ -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, }); diff --git a/src/plugins/quickMention/index.tsx b/src/plugins/quickMention/index.tsx index df86e9b70..2c7e7f6f3 100644 --- a/src/plugins/quickMention/index.tsx +++ b/src/plugins/quickMention/index.tsx @@ -16,7 +16,7 @@ * along with this program. If not, see . */ -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: () => ( diff --git a/src/plugins/translate/index.tsx b/src/plugins/translate/index.tsx index de61cef9c..2b0db104d 100644 --- a/src/plugins/translate/index.tsx +++ b/src/plugins/translate/index.tsx @@ -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"); }, }); diff --git a/src/plugins/viewRaw/index.tsx b/src/plugins/viewRaw/index.tsx index 8ee1ca8d7..b0764cd59 100644 --- a/src/plugins/viewRaw/index.tsx +++ b/src/plugins/viewRaw/index.tsx @@ -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"); } });