Plugin

CopyFileContents

Adds a button to text file attachments to copy their contents

Utility
index.tsx
Download

Source

src/plugins/copyFileContents/index.tsx
1import "./style.css";
2
3import ErrorBoundary from "@components/ErrorBoundary";
4import { CopyIcon, NoEntrySignIcon } from "@components/Icons";
5import { Devs } from "@utils/constants";
6import { copyWithToast } from "@utils/discord";
7import definePlugin from "@utils/types";
8import { Tooltip, useState } from "@webpack/common";
9
10const CheckMarkIcon = () => {
11 return <svg width="18" height="18" viewBox="0 0 24 24">
12 <path fill="currentColor" d="M21.7 5.3a1 1 0 0 1 0 1.4l-12 12a1 1 0 0 1-1.4 0l-6-6a1 1 0 1 1 1.4-1.4L9 16.58l11.3-11.3a1 1 0 0 1 1.4 0Z"></path>
13 </svg>;
14};
15
16export default definePlugin({
17 name: "CopyFileContents",
18 description: "Adds a button to text file attachments to copy their contents",
19 tags: ["Utility"],
20 authors: [Devs.Obsidian, Devs.Nuckyz],
21 patches: [
22 {
23 find: "#{intl::PREVIEW_BYTES_LEFT}",
24 replacement: [
25 // Inline preview
26 {
27 match: /fileContents:(\i),bytesLeft:(\i)\}\):null,/,
28 replace: "$&$self.addCopyButton({fileContents:$1,bytesLeft:$2}),"
29 },
30 // Modal
31 {
32 match: /align:"\i"\}\),(?=\(0,\i\.jsx\)\(\i,\{wordWrap:\i,setWordWrap:\i)/,
33 replace: "$&$self.addCopyButton(arguments[0]),"
34 }
35 ]
36 }
37 ],
38
39 addCopyButton: ErrorBoundary.wrap(({ fileContents, bytesLeft }: { fileContents: string, bytesLeft: number; }) => {
40 const [recentlyCopied, setRecentlyCopied] = useState(false);
41
42 return (
43 <Tooltip text={recentlyCopied ? "Copied!" : bytesLeft > 0 ? "File too large to copy" : "Copy File Contents"}>
44 {tooltipProps => (
45 <div
46 {...tooltipProps}
47 className="vc-cfc-button"
48 role="button"
49 onClick={() => {
50 if (!recentlyCopied && bytesLeft <= 0) {
51 copyWithToast(fileContents);
52 setRecentlyCopied(true);
53 setTimeout(() => setRecentlyCopied(false), 2000);
54 }
55 }}
56 >
57 {recentlyCopied
58 ? <CheckMarkIcon />
59 : bytesLeft > 0
60 ? <NoEntrySignIcon width={18} height={18} color="var(--channel-icon)" />
61 : <CopyIcon width={18} height={18} />
62 }
63 </div>
64 )}
65 </Tooltip>
66 );
67 }, { noop: true }),
68});
69