Plugin
CopyFileContents
Adds a button to text file attachments to copy their contents
1
import "./style.css";2
3
import ErrorBoundary from "@components/ErrorBoundary";4
import { CopyIcon, NoEntrySignIcon } from "@components/Icons";5
import { Devs } from "@utils/constants";6
import { copyWithToast } from "@utils/discord";7
import definePlugin from "@utils/types";8
import { Tooltip, useState } from "@webpack/common";9
10
const 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
16
export 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 preview26
{27
match: /fileContents:(\i),bytesLeft:(\i)\}\):null,/,28
replace: "$&$self.addCopyButton({fileContents:$1,bytesLeft:$2}),"29
},30
// Modal31
{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
<div46
{...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
{recentlyCopied58
? <CheckMarkIcon />59
: bytesLeft > 060
? <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