From d5eaae9d5137d4eb2c77517080e1ee554ab2f5ba Mon Sep 17 00:00:00 2001 From: Obsidian <108832807+Obsidianninja11@users.noreply.github.com> Date: Sat, 31 Aug 2024 18:02:27 -0800 Subject: [PATCH] new plugin CopyFileContents ~Easily copy text file attachments contents (#2790) --- src/components/Icons.tsx | 8 ++-- src/plugins/CopyFileContents/README.md | 5 +++ src/plugins/CopyFileContents/index.tsx | 60 ++++++++++++++++++++++++++ src/plugins/CopyFileContents/style.css | 8 ++++ src/utils/constants.ts | 4 ++ 5 files changed, 81 insertions(+), 4 deletions(-) create mode 100644 src/plugins/CopyFileContents/README.md create mode 100644 src/plugins/CopyFileContents/index.tsx create mode 100644 src/plugins/CopyFileContents/style.css diff --git a/src/components/Icons.tsx b/src/components/Icons.tsx index 7ba078d3..fa142a18 100644 --- a/src/components/Icons.tsx +++ b/src/components/Icons.tsx @@ -65,8 +65,7 @@ export function LinkIcon({ height = 24, width = 24, className }: IconProps) { } /** - * Discord's copy icon, as seen in the user popout right of the username when clicking - * your own username in the bottom left user panel + * Discord's copy icon, as seen in the user panel popout on the right of the username and in large code blocks */ export function CopyIcon(props: IconProps) { return ( @@ -76,8 +75,9 @@ export function CopyIcon(props: IconProps) { viewBox="0 0 24 24" > - - + + + ); diff --git a/src/plugins/CopyFileContents/README.md b/src/plugins/CopyFileContents/README.md new file mode 100644 index 00000000..18dc2d3d --- /dev/null +++ b/src/plugins/CopyFileContents/README.md @@ -0,0 +1,5 @@ +# CopyFileContents + +Adds a button to text file attachments to copy their contents. + +![](https://github.com/user-attachments/assets/b1a0f6f4-106f-4953-94d9-4c5ef5810bca) diff --git a/src/plugins/CopyFileContents/index.tsx b/src/plugins/CopyFileContents/index.tsx new file mode 100644 index 00000000..13b64917 --- /dev/null +++ b/src/plugins/CopyFileContents/index.tsx @@ -0,0 +1,60 @@ +/* + * Vencord, a Discord client mod + * Copyright (c) 2024 Vendicated and contributors + * SPDX-License-Identifier: GPL-3.0-or-later + */ + +import "./style.css"; + +import ErrorBoundary from "@components/ErrorBoundary"; +import { CopyIcon, NoEntrySignIcon } from "@components/Icons"; +import { Devs } from "@utils/constants"; +import { copyWithToast } from "@utils/misc"; +import definePlugin from "@utils/types"; +import { Tooltip, useState } from "@webpack/common"; + +const CheckMarkIcon = () => { + return + + ; +}; + +export default definePlugin({ + name: "CopyFileContents", + description: "Adds a button to text file attachments to copy their contents", + authors: [Devs.Obsidian, Devs.Nuckyz], + patches: [ + { + find: ".Messages.PREVIEW_BYTES_LEFT.format(", + replacement: { + match: /\.footerGap.+?url:\i,fileName:\i,fileSize:\i}\),(?<=fileContents:(\i),bytesLeft:(\i).+?)/g, + replace: "$&$self.addCopyButton({fileContents:$1,bytesLeft:$2})," + } + } + ], + + addCopyButton: ErrorBoundary.wrap(({ fileContents, bytesLeft }: { fileContents: string, bytesLeft: number; }) => { + const [recentlyCopied, setRecentlyCopied] = useState(false); + + return ( + 0 ? "File too large to copy" : "Copy File Contents"}> + {tooltipProps => ( +
{ + if (!recentlyCopied && bytesLeft <= 0) { + copyWithToast(fileContents); + setRecentlyCopied(true); + setTimeout(() => setRecentlyCopied(false), 2000); + } + }} + > + {recentlyCopied ? : bytesLeft > 0 ? : } +
+ )} +
+ ); + }, { noop: true }), +}); diff --git a/src/plugins/CopyFileContents/style.css b/src/plugins/CopyFileContents/style.css new file mode 100644 index 00000000..c643cf0f --- /dev/null +++ b/src/plugins/CopyFileContents/style.css @@ -0,0 +1,8 @@ +.vc-cfc-button { + color: var(--interactive-normal); + cursor: pointer; +} + +.vc-cfc-button:hover { + color: var(--interactive-hover); +} diff --git a/src/utils/constants.ts b/src/utils/constants.ts index ae1943b0..60c20325 100644 --- a/src/utils/constants.ts +++ b/src/utils/constants.ts @@ -550,6 +550,10 @@ export const Devs = /* #__PURE__*/ Object.freeze({ name: "Lumap", id: 585278686291427338n, }, + Obsidian: { + name: "Obsidian", + id: 683171006717755446n, + }, SerStars: { name: "SerStars", id: 861631850681729045n,