From e2b622c76bd3f25f674c4c3ff899bef756cdf6ca Mon Sep 17 00:00:00 2001 From: Cynthia Foxwell Date: Fri, 21 Oct 2022 16:17:18 -0600 Subject: feat(api): Message Accessories API (#131) --- src/api/MessageAccessories.ts | 41 ++++++++++++++++++++++++++++++++++++ src/api/index.ts | 14 ++++++------ src/plugins/apiMessageAccessories.ts | 18 ++++++++++++++++ 3 files changed, 66 insertions(+), 7 deletions(-) create mode 100644 src/api/MessageAccessories.ts create mode 100644 src/plugins/apiMessageAccessories.ts diff --git a/src/api/MessageAccessories.ts b/src/api/MessageAccessories.ts new file mode 100644 index 0000000..3a46828 --- /dev/null +++ b/src/api/MessageAccessories.ts @@ -0,0 +1,41 @@ +export type AccessoryCallback = (props: Record) => JSX.Element; +export type Accessory = { + callback: AccessoryCallback; + position?: number; +}; + +export const accessories = new Map(); + +export function addAccessory( + identifier: string, + callback: AccessoryCallback, + position?: number +) { + accessories.set(identifier, { + callback, + position, + }); +} + +export function removeAccessory(identifier: string) { + accessories.delete(identifier); +} + +export function _modifyAccessories( + elements: JSX.Element[], + props: Record +) { + for (const accessory of accessories.values()) { + elements.splice( + accessory.position != null + ? accessory.position < 0 + ? elements.length + accessory.position + : accessory.position + : elements.length, + 0, + accessory.callback(props) + ); + } + + return elements; +} diff --git a/src/api/index.ts b/src/api/index.ts index 2ee5883..a80ea3f 100644 --- a/src/api/index.ts +++ b/src/api/index.ts @@ -6,6 +6,7 @@ import * as $MessageEventsAPI from "./MessageEvents"; import * as $Notices from "./Notices"; import * as $Commands from "./Commands"; import * as $DataStore from "./DataStore"; +import * as $MessageAccessories from "./MessageAccessories"; /** * An API allowing you to listen to Message Clicks or run your own logic @@ -36,12 +37,11 @@ const Commands = $Commands; * * This is actually just idb-keyval, so if you're familiar with that, you're golden! * @see {@link https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Structured_clone_algorithm#supported_types} -*/ + */ const DataStore = $DataStore; +/** + * An API allowing you to add custom components as message accessories + */ +const MessageAccessories = $MessageAccessories; -export { - DataStore, - MessageEvents, - Notices, - Commands -}; +export { DataStore, MessageAccessories, MessageEvents, Notices, Commands }; diff --git a/src/plugins/apiMessageAccessories.ts b/src/plugins/apiMessageAccessories.ts new file mode 100644 index 0000000..a2e8e70 --- /dev/null +++ b/src/plugins/apiMessageAccessories.ts @@ -0,0 +1,18 @@ +import { Devs } from "../utils/constants"; +import definePlugin from "../utils/types"; + +export default definePlugin({ + name: "MessageAccessoriesAPI", + description: "API to add message accessories.", + authors: [Devs.Cyn], + patches: [ + { + find: "_messageAttachmentToEmbedMedia", + replacement: { + match: /\(\)\.container\)},(.+?)\)};return/, + replace: (_, accessories) => + `().container)},Vencord.Api.MessageAccessories._modifyAccessories([${accessories}],this.props))};return`, + }, + }, + ], +}); -- cgit