From 5c0c358255a43a04030297197d4151768e93cd7b Mon Sep 17 00:00:00 2001 From: naiba Date: Sat, 30 Nov 2024 17:08:47 +0800 Subject: [PATCH] fix: copy to clipboard --- package-lock.json | 16 ++++++++++++++++ package.json | 1 + src/components/fm.tsx | 4 ++-- src/components/install-commands.tsx | 10 ++++++---- src/components/note-menu.tsx | 3 ++- src/lib/utils.ts | 15 +++++++++++++++ 6 files changed, 42 insertions(+), 7 deletions(-) diff --git a/package-lock.json b/package-lock.json index 33f70e0..0096cd4 100644 --- a/package-lock.json +++ b/package-lock.json @@ -29,6 +29,7 @@ "class-variance-authority": "^0.7.0", "clsx": "^2.1.1", "cmdk": "^1.0.0", + "copy-to-clipboard": "^3.3.3", "i18next": "^24.0.2", "jotai-zustand": "^0.6.0", "lucide-react": "^0.454.0", @@ -3627,6 +3628,15 @@ "dev": true, "license": "MIT" }, + "node_modules/copy-to-clipboard": { + "version": "3.3.3", + "resolved": "https://registry.npmjs.org/copy-to-clipboard/-/copy-to-clipboard-3.3.3.tgz", + "integrity": "sha512-2KV8NhB5JqC3ky0r9PMCAZKbUHSwtEo4CwCs0KXgruG43gX5PMqDEBbVU4OUzw2MuAWUfsuFmWvEKG5QRfSnJA==", + "license": "MIT", + "dependencies": { + "toggle-selection": "^1.0.6" + } + }, "node_modules/cosmiconfig": { "version": "9.0.0", "resolved": "https://registry.npmjs.org/cosmiconfig/-/cosmiconfig-9.0.0.tgz", @@ -6231,6 +6241,12 @@ "node": ">=8.0" } }, + "node_modules/toggle-selection": { + "version": "1.0.6", + "resolved": "https://registry.npmjs.org/toggle-selection/-/toggle-selection-1.0.6.tgz", + "integrity": "sha512-BiZS+C1OS8g/q2RRbJmy59xpyghNBqrr6k5L/uKBGRsTfxmu3ffiRnd8mlGPUVayg8pvfi5urfnu8TU7DVOkLQ==", + "license": "MIT" + }, "node_modules/tr46": { "version": "0.0.3", "resolved": "https://registry.npmjs.org/tr46/-/tr46-0.0.3.tgz", diff --git a/package.json b/package.json index c90ae67..c3e9ca0 100644 --- a/package.json +++ b/package.json @@ -32,6 +32,7 @@ "class-variance-authority": "^0.7.0", "clsx": "^2.1.1", "cmdk": "^1.0.0", + "copy-to-clipboard": "^3.3.3", "i18next": "^24.0.2", "jotai-zustand": "^0.6.0", "lucide-react": "^0.454.0", diff --git a/src/components/fm.tsx b/src/components/fm.tsx index b37dbf9..35c7035 100644 --- a/src/components/fm.tsx +++ b/src/components/fm.tsx @@ -14,7 +14,7 @@ import useWebSocket from "react-use-websocket" import { toast } from "sonner" import { ColumnDef } from "@tanstack/react-table" import { Folder, File } from "lucide-react" -import { fm, formatPath, fmWorker as worker } from "@/lib/utils" +import { copyToClipboard, fm, formatPath, fmWorker as worker } from "@/lib/utils" import { AlertDialog, AlertDialogContent, @@ -293,7 +293,7 @@ const FMComponent: React.FC = ({ wsUrl, {t('Refresh')} { - await navigator.clipboard.writeText(formatPath(currentPath)); + await copyToClipboard(formatPath(currentPath)); } }>{t("CopyPath")} diff --git a/src/components/install-commands.tsx b/src/components/install-commands.tsx index 5b77e6c..c5dfe1a 100644 --- a/src/components/install-commands.tsx +++ b/src/components/install-commands.tsx @@ -12,6 +12,7 @@ import { Check, Clipboard } from "lucide-react" import { toast } from "sonner" import { useTranslation } from "react-i18next" +import { copyToClipboard } from "@/lib/utils" enum OSTypes { Linux = 1, @@ -29,7 +30,8 @@ export const InstallCommandsMenu = forwardRef((p try { setCopy(true); if (settings) - await navigator.clipboard.writeText(generateCommand(type, settings) || ''); + await copyToClipboard(generateCommand(type, settings) || ''); + } catch (e) { console.error(e); toast(t("Error"), { @@ -52,9 +54,9 @@ export const InstallCommandsMenu = forwardRef((p - { switchState(OSTypes.Linux) }}>Linux - { switchState(OSTypes.macOS) }}>macOS - { switchState(OSTypes.Windows) }}>Windows + { switchState(OSTypes.Linux) }}>Linux + { switchState(OSTypes.macOS) }}>macOS + { switchState(OSTypes.Windows) }}>Windows ); diff --git a/src/components/note-menu.tsx b/src/components/note-menu.tsx index 161adc2..a45b40b 100644 --- a/src/components/note-menu.tsx +++ b/src/components/note-menu.tsx @@ -10,6 +10,7 @@ import { IconButton } from "./xui/icon-button" import { toast } from "sonner"; import { useTranslation } from "react-i18next"; +import { copyToClipboard } from "@/lib/utils"; interface NoteMenuProps extends ButtonProps { note: { private?: string, public?: string }; @@ -29,7 +30,7 @@ export const NoteMenu = forwardRef((props, ref if (!copy) { setCopy(true); - await navigator.clipboard.writeText(text); + await copyToClipboard(text); setTimeout(() => { setCopy(false); }, 2 * 1000); diff --git a/src/lib/utils.ts b/src/lib/utils.ts index 10a957e..20537ee 100644 --- a/src/lib/utils.ts +++ b/src/lib/utils.ts @@ -3,6 +3,7 @@ import { twMerge } from "tailwind-merge" import { z } from "zod" import { FMEntry, FMOpcode, ModelIP } from "@/types" import FMWorker from "./fm?worker" +import copy from "copy-to-clipboard" export function cn(...inputs: ClassValue[]) { return twMerge(clsx(inputs)) @@ -165,3 +166,17 @@ function ipv6BinaryToString(binary: Uint8Array) { return ipv6; } + +export async function copyToClipboard(text: string) { + try { + return await navigator.clipboard.writeText(text); + } catch (error) { + console.error('navigator', error); + } + try { + return copy(text) + } catch (error) { + console.error('copy', error); + } + throw new Error('Failed to copy text to clipboard'); +} \ No newline at end of file