translate: Add context menu item; fix MLE compatibility

This commit is contained in:
V 2023-07-08 03:30:16 +02:00
parent 2a318e390e
commit 7cd1d4c60f
No known key found for this signature in database
GPG Key ID: A1DC0CFB5615D905
2 changed files with 27 additions and 1 deletions

@ -44,6 +44,9 @@ export function TranslationAccessory({ message }: { message: Message; }) {
const [translation, setTranslation] = useState<TranslationValue>(); const [translation, setTranslation] = useState<TranslationValue>();
useEffect(() => { useEffect(() => {
// Ignore MessageLinkEmbeds messages
if ((message as any).vencordEmbeddedBy) return;
TranslationSetters.set(message.id, setTranslation); TranslationSetters.set(message.id, setTranslation);
return () => void TranslationSetters.delete(message.id); return () => void TranslationSetters.delete(message.id);

@ -18,19 +18,39 @@
import "./styles.css"; import "./styles.css";
import { addContextMenuPatch, findGroupChildrenByChildId, NavContextMenuPatchCallback, removeContextMenuPatch } from "@api/ContextMenu";
import { addAccessory, removeAccessory } from "@api/MessageAccessories"; import { addAccessory, removeAccessory } from "@api/MessageAccessories";
import { addPreSendListener, removePreSendListener } from "@api/MessageEvents"; import { addPreSendListener, removePreSendListener } from "@api/MessageEvents";
import { addButton, removeButton } from "@api/MessagePopover"; import { addButton, removeButton } from "@api/MessagePopover";
import ErrorBoundary from "@components/ErrorBoundary"; import ErrorBoundary from "@components/ErrorBoundary";
import { Devs } from "@utils/constants"; import { Devs } from "@utils/constants";
import definePlugin from "@utils/types"; import definePlugin from "@utils/types";
import { ChannelStore } from "@webpack/common"; import { ChannelStore, Menu } from "@webpack/common";
import { settings } from "./settings"; import { settings } from "./settings";
import { TranslateChatBarIcon, TranslateIcon } from "./TranslateIcon"; import { TranslateChatBarIcon, TranslateIcon } from "./TranslateIcon";
import { handleTranslate, TranslationAccessory } from "./TranslationAccessory"; import { handleTranslate, TranslationAccessory } from "./TranslationAccessory";
import { translate } from "./utils"; 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, (
<Menu.MenuItem
id="vc-trans"
label="Translate"
icon={TranslateIcon}
action={async () => {
const trans = await translate("received", message.content);
handleTranslate(message.id, trans);
}}
/>
));
};
export default definePlugin({ export default definePlugin({
name: "Translate", name: "Translate",
description: "Translate messages with Google Translate", description: "Translate messages with Google Translate",
@ -53,6 +73,8 @@ export default definePlugin({
start() { start() {
addAccessory("vc-translation", props => <TranslationAccessory message={props.message} />); addAccessory("vc-translation", props => <TranslationAccessory message={props.message} />);
addContextMenuPatch("message", messageCtxPatch);
addButton("vc-translate", message => { addButton("vc-translate", message => {
if (!message.content) return null; if (!message.content) return null;
@ -78,6 +100,7 @@ export default definePlugin({
stop() { stop() {
removePreSendListener(this.preSend); removePreSendListener(this.preSend);
removeContextMenuPatch("message", messageCtxPatch);
removeButton("vc-translate"); removeButton("vc-translate");
removeAccessory("vc-translation"); removeAccessory("vc-translation");
}, },