Skip to content

Commit 5c0c358

Browse files
committed
fix: copy to clipboard
1 parent eca9920 commit 5c0c358

File tree

6 files changed

+42
-7
lines changed

6 files changed

+42
-7
lines changed

package-lock.json

Lines changed: 16 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
"class-variance-authority": "^0.7.0",
3333
"clsx": "^2.1.1",
3434
"cmdk": "^1.0.0",
35+
"copy-to-clipboard": "^3.3.3",
3536
"i18next": "^24.0.2",
3637
"jotai-zustand": "^0.6.0",
3738
"lucide-react": "^0.454.0",

src/components/fm.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import useWebSocket from "react-use-websocket"
1414
import { toast } from "sonner"
1515
import { ColumnDef } from "@tanstack/react-table"
1616
import { Folder, File } from "lucide-react"
17-
import { fm, formatPath, fmWorker as worker } from "@/lib/utils"
17+
import { copyToClipboard, fm, formatPath, fmWorker as worker } from "@/lib/utils"
1818
import {
1919
AlertDialog,
2020
AlertDialogContent,
@@ -293,7 +293,7 @@ const FMComponent: React.FC<FMProps & JSX.IntrinsicElements["div"]> = ({ wsUrl,
293293
<DropdownMenuItem onClick={listFile}>{t('Refresh')}</DropdownMenuItem>
294294
<DropdownMenuItem onClick={
295295
async () => {
296-
await navigator.clipboard.writeText(formatPath(currentPath));
296+
await copyToClipboard(formatPath(currentPath));
297297
}
298298
}>{t("CopyPath")}</DropdownMenuItem>
299299
<AlertDialogTrigger asChild>

src/components/install-commands.tsx

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { Check, Clipboard } from "lucide-react"
1212
import { toast } from "sonner"
1313

1414
import { useTranslation } from "react-i18next"
15+
import { copyToClipboard } from "@/lib/utils"
1516

1617
enum OSTypes {
1718
Linux = 1,
@@ -29,7 +30,8 @@ export const InstallCommandsMenu = forwardRef<HTMLButtonElement, ButtonProps>((p
2930
try {
3031
setCopy(true);
3132
if (settings)
32-
await navigator.clipboard.writeText(generateCommand(type, settings) || '');
33+
await copyToClipboard(generateCommand(type, settings) || '');
34+
3335
} catch (e) {
3436
console.error(e);
3537
toast(t("Error"), {
@@ -52,9 +54,9 @@ export const InstallCommandsMenu = forwardRef<HTMLButtonElement, ButtonProps>((p
5254
</Button>
5355
</DropdownMenuTrigger>
5456
<DropdownMenuContent>
55-
<DropdownMenuItem onClick={async () => { switchState(OSTypes.Linux) }}>Linux</DropdownMenuItem>
56-
<DropdownMenuItem onClick={async () => { switchState(OSTypes.macOS) }}>macOS</DropdownMenuItem>
57-
<DropdownMenuItem onClick={async () => { switchState(OSTypes.Windows) }}>Windows</DropdownMenuItem>
57+
<DropdownMenuItem className="nezha-copy" onClick={async () => { switchState(OSTypes.Linux) }}>Linux</DropdownMenuItem>
58+
<DropdownMenuItem className="nezha-copy" onClick={async () => { switchState(OSTypes.macOS) }}>macOS</DropdownMenuItem>
59+
<DropdownMenuItem className="nezha-copy" onClick={async () => { switchState(OSTypes.Windows) }}>Windows</DropdownMenuItem>
5860
</DropdownMenuContent>
5961
</DropdownMenu>
6062
);

src/components/note-menu.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { IconButton } from "./xui/icon-button"
1010
import { toast } from "sonner";
1111

1212
import { useTranslation } from "react-i18next";
13+
import { copyToClipboard } from "@/lib/utils";
1314

1415
interface NoteMenuProps extends ButtonProps {
1516
note: { private?: string, public?: string };
@@ -29,7 +30,7 @@ export const NoteMenu = forwardRef<HTMLButtonElement, NoteMenuProps>((props, ref
2930

3031
if (!copy) {
3132
setCopy(true);
32-
await navigator.clipboard.writeText(text);
33+
await copyToClipboard(text);
3334
setTimeout(() => {
3435
setCopy(false);
3536
}, 2 * 1000);

src/lib/utils.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { twMerge } from "tailwind-merge"
33
import { z } from "zod"
44
import { FMEntry, FMOpcode, ModelIP } from "@/types"
55
import FMWorker from "./fm?worker"
6+
import copy from "copy-to-clipboard"
67

78
export function cn(...inputs: ClassValue[]) {
89
return twMerge(clsx(inputs))
@@ -165,3 +166,17 @@ function ipv6BinaryToString(binary: Uint8Array) {
165166

166167
return ipv6;
167168
}
169+
170+
export async function copyToClipboard(text: string) {
171+
try {
172+
return await navigator.clipboard.writeText(text);
173+
} catch (error) {
174+
console.error('navigator', error);
175+
}
176+
try {
177+
return copy(text)
178+
} catch (error) {
179+
console.error('copy', error);
180+
}
181+
throw new Error('Failed to copy text to clipboard');
182+
}

0 commit comments

Comments
 (0)