From 7cd1d4c60f417f47a7badbae698f9f3ed4cfbf41 Mon Sep 17 00:00:00 2001 From: V Date: Sat, 8 Jul 2023 03:30:16 +0200 Subject: [PATCH] translate: Add context menu item; fix MLE compatibility --- .../translate/TranslationAccessory.tsx | 3 +++ src/plugins/translate/index.tsx | 25 ++++++++++++++++++- 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/src/plugins/translate/TranslationAccessory.tsx b/src/plugins/translate/TranslationAccessory.tsx index 4f46e4b..72b3594 100644 --- a/src/plugins/translate/TranslationAccessory.tsx +++ b/src/plugins/translate/TranslationAccessory.tsx @@ -44,6 +44,9 @@ export function TranslationAccessory({ message }: { message: Message; }) { const [translation, setTranslation] = useState(); useEffect(() => { + // Ignore MessageLinkEmbeds messages + if ((message as any).vencordEmbeddedBy) return; + TranslationSetters.set(message.id, setTranslation); return () => void TranslationSetters.delete(message.id); diff --git a/src/plugins/translate/index.tsx b/src/plugins/translate/index.tsx index d71b2da..3007032 100644 --- a/src/plugins/translate/index.tsx +++ b/src/plugins/translate/index.tsx @@ -18,19 +18,39 @@ import "./styles.css"; +import { addContextMenuPatch, findGroupChildrenByChildId, NavContextMenuPatchCallback, removeContextMenuPatch } from "@api/ContextMenu"; import { addAccessory, removeAccessory } from "@api/MessageAccessories"; import { addPreSendListener, removePreSendListener } from "@api/MessageEvents"; import { addButton, removeButton } from "@api/MessagePopover"; import ErrorBoundary from "@components/ErrorBoundary"; import { Devs } from "@utils/constants"; import definePlugin from "@utils/types"; -import { ChannelStore } from "@webpack/common"; +import { ChannelStore, Menu } from "@webpack/common"; import { settings } from "./settings"; import { TranslateChatBarIcon, TranslateIcon } from "./TranslateIcon"; import { handleTranslate, TranslationAccessory } from "./TranslationAccessory"; import { translate } from "./utils"; +const messageCtxPatch: NavContextMenuPatchCallback = (children, { message }) => () => { + if (!message.content) return; + + const group = findGroupChildrenByChildId("copy-text", children); + if (!group) return; + + group.splice(group.findIndex(c => c?.props?.id === "copy-text") + 1, 0, ( + { + const trans = await translate("received", message.content); + handleTranslate(message.id, trans); + }} + /> + )); +}; + export default definePlugin({ name: "Translate", description: "Translate messages with Google Translate", @@ -53,6 +73,8 @@ export default definePlugin({ start() { addAccessory("vc-translation", props => ); + addContextMenuPatch("message", messageCtxPatch); + addButton("vc-translate", message => { if (!message.content) return null; @@ -78,6 +100,7 @@ export default definePlugin({ stop() { removePreSendListener(this.preSend); + removeContextMenuPatch("message", messageCtxPatch); removeButton("vc-translate"); removeAccessory("vc-translation"); },