From 5c92a46569d796fad119807b1a996288f5947d6a Mon Sep 17 00:00:00 2001 From: Fine0830 Date: Sun, 24 Nov 2024 17:25:13 +0800 Subject: [PATCH] Refactor copy util with Web API (#432) --- src/utils/copy.ts | 32 ++++++++++++++++++-------------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/src/utils/copy.ts b/src/utils/copy.ts index 3baddf0d..d593e79f 100644 --- a/src/utils/copy.ts +++ b/src/utils/copy.ts @@ -16,18 +16,22 @@ */ import { ElNotification } from "element-plus"; -export default (value: string): void => { - const input = document.createElement("input"); - input.value = value; - document.body.appendChild(input); - input.select(); - if (document.execCommand("Copy")) { - document.execCommand("Copy"); - } - input.remove(); - ElNotification({ - title: "Success", - message: "Copied", - type: "success", - }); + +export default (text: string): void => { + navigator.clipboard + .writeText(text) + .then(() => { + ElNotification({ + title: "Success", + message: "Copied", + type: "success", + }); + }) + .catch((err) => { + ElNotification({ + title: "Error", + message: err, + type: "warning", + }); + }); };