From 3c6e5700338698a4f8b8184124f3199206334809 Mon Sep 17 00:00:00 2001 From: vishnyanetchereshnya <151846235+vishnyanetchereshnya@users.noreply.github.com> Date: Sun, 9 Jun 2024 20:13:01 +0300 Subject: [PATCH] added feature to copy emojies from messages and message reactions --- src/plugins/copyEmojiMarkdown/index.tsx | 146 ++++++++++++++++++++---- 1 file changed, 126 insertions(+), 20 deletions(-) diff --git a/src/plugins/copyEmojiMarkdown/index.tsx b/src/plugins/copyEmojiMarkdown/index.tsx index a9c018a91..2abe2033a 100644 --- a/src/plugins/copyEmojiMarkdown/index.tsx +++ b/src/plugins/copyEmojiMarkdown/index.tsx @@ -4,14 +4,18 @@ * SPDX-License-Identifier: GPL-3.0-or-later */ +import { findGroupChildrenByChildId } from "@api/ContextMenu"; import { definePluginSettings } from "@api/Settings"; import { Devs } from "@utils/constants"; import { copyWithToast } from "@utils/misc"; import definePlugin, { OptionType } from "@utils/types"; import { findByPropsLazy } from "@webpack"; import { Menu } from "@webpack/common"; +import { Message } from "discord-types/general"; +import { ReactElement } from "react"; -const { convertNameToSurrogate } = findByPropsLazy("convertNameToSurrogate"); +const { convertNameToSurrogate, hasSurrogates, convertSurrogateToName } = + findByPropsLazy("convertNameToSurrogate"); interface Emoji { type: string; @@ -37,39 +41,141 @@ function getEmojiMarkdown(target: Target, copyUnicode: boolean): string { /https:\/\/cdn\.discordapp\.com\/emojis\/\d+\.(\w+)/ )?.[1]; - return `<${extension === "gif" ? "a" : ""}:${emojiName.replace(/~\d+$/, "")}:${emojiId}>`; + return `<${extension === "gif" ? "a" : ""}:${emojiName.replace( + /~\d+$/, + "" + )}:${emojiId}>`; } +const patchExpressionPickerContextMenu = ( + children: Array, + { target }: { target: Target } +) => { + if (target.dataset.type !== "emoji") return; + + children.push( + { + copyWithToast( + getEmojiMarkdown(target, settings.store.copyUnicode), + "Success! Copied emoji markdown." + ); + }} + /> + ); +}; + +const addCopyButton = ( + children: Array, + emojiMarkdown: string +) => { + findGroupChildrenByChildId("copy-link", children)?.push( + { + copyWithToast(emojiMarkdown, "Success! Copied emoji markdown."); + }} + /> + ); +}; + +const patchMessageContextMenu = ( + children: Array, + props: { + favoriteableId: string | null; + favoriteableName: string | null; + favoriteableType: string | null; + message: Message; + } | null +) => { + if (!props) return; + + if (props.favoriteableType !== "emoji") return; + + const emojiName = props.favoriteableName; + + if (hasSurrogates(emojiName)) { + addCopyButton( + children, + settings.store.copyUnicode + ? emojiName + : convertSurrogateToName(emojiName) + ); + return; + } + + const emojiId = props.favoriteableId; + + if (!emojiId) { + if (emojiName) { + const emojiMarkdown = settings.store.copyUnicode + ? convertNameToSurrogate(emojiName.replace(/(^:|:$)/g, "")) + : emojiName; + + addCopyButton( + children, + emojiMarkdown !== "" ? emojiMarkdown : emojiName + ); + } + return; + } + + const messageEmojiMarkdown = props.message.content.match( + RegExp(`()`) + )?.[1]; + + if (messageEmojiMarkdown) { + addCopyButton(children, messageEmojiMarkdown.replace(/~\d+/, "")); + return; + } + + const reactionEmojiObject = props.message.reactions.find( + ({ emoji: { id } }) => id === emojiId + )?.emoji; + + if (!reactionEmojiObject) return; + + const reactionEmojiName = reactionEmojiObject.name; + + if (hasSurrogates(reactionEmojiName)) { + addCopyButton( + children, + settings.store.copyUnicode + ? reactionEmojiName + : convertSurrogateToName(reactionEmojiName) + ); + return; + } + + addCopyButton( + children, + `<${ + reactionEmojiObject.animated ? "a" : "" + }:${reactionEmojiName}:${emojiId}>` + ); +}; + const settings = definePluginSettings({ copyUnicode: { type: OptionType.BOOLEAN, - description: "Copy the raw unicode character instead of :name: for default emojis (👽)", + description: + "Copy the raw unicode character instead of :name: for default emojis (👽)", default: true, }, }); export default definePlugin({ name: "CopyEmojiMarkdown", - description: "Allows you to copy emojis as formatted string (<:blobcatcozy:1026533070955872337>)", + description: + "Allows you to copy emojis as formatted string (<:blobcatcozy:1026533070955872337>)", authors: [Devs.HappyEnderman, Devs.Vishnya], settings, contextMenus: { - "expression-picker"(children, { target }: { target: Target }) { - if (target.dataset.type !== "emoji") return; - - children.push( - { - copyWithToast( - getEmojiMarkdown(target, settings.store.copyUnicode), - "Success! Copied emoji markdown." - ); - }} - /> - ); - }, + "expression-picker": patchExpressionPickerContextMenu, + "message": patchMessageContextMenu, }, });