refactor: copy btn

This commit is contained in:
Lazy 2025-02-17 16:56:56 +08:00
parent e4abb18302
commit 02d986d25e
3 changed files with 32 additions and 21 deletions

View File

@ -1,39 +1,21 @@
<script setup lang="ts">
import { t } from "@/lang/i18n";
import { CopyOutlined } from "@ant-design/icons-vue";
import { Modal, message } from "ant-design-vue";
import { reportErrorMsg } from "@/tools/validator";
import type { ButtonType } from "ant-design-vue/es/button";
import type { SizeType } from "ant-design-vue/es/config-provider";
import { h } from "vue";
import { toCopy } from "@/tools/copy";
const props = defineProps<{
size?: string;
type?: string;
value: string;
}>();
const copy = async () => {
if (!navigator.clipboard) {
Modal.warning({
title: t("TXT_CODE_ca07c84c"),
content: [h("span", t("TXT_CODE_2452016e")), h("br"), h("span", props.value)]
});
return;
}
try {
await navigator.clipboard.writeText(props.value);
message.success(t("TXT_CODE_b858d78a"));
} catch (error: any) {
reportErrorMsg(t("TXT_CODE_81b9b599") + error);
}
};
</script>
<template>
<a-tooltip>
<template #title>{{ t("TXT_CODE_13ae6a93") }}</template>
<a-button :type="<ButtonType>type" :size="<SizeType>size" @click="copy">
<a-button :type="<ButtonType>type" :size="<SizeType>size" @click="toCopy(props.value)">
<template #icon>
<CopyOutlined />
</template>

View File

@ -10,6 +10,7 @@ import type { FormInstance } from "ant-design-vue";
import CopyButton from "@/components/CopyButton.vue";
import { bind2FA } from "../services/apis/user";
import { PERMISSION_MAP } from "@/config/const";
import { toCopy } from "@/tools/copy";
const { state, updateUserInfo } = useAppStateStore();
const { state: tools } = useAppToolsStore();
@ -176,7 +177,6 @@ const disable2FACode = async () => {
</a-button>
</div>
</a-form-item>
<a-form-item label="APIKEY">
<a-typography-paragraph>
{{ t("TXT_CODE_b2dbf778") }}

View File

@ -0,0 +1,29 @@
import { t } from "@/lang/i18n";
import { useClipboard } from "@vueuse/core";
import { message, Modal } from "ant-design-vue";
import { h } from "vue";
const { copy, copied, isSupported } = useClipboard();
export const toCopy = async (sth: string | number) => {
try {
if (!isSupported.value) {
const input = document.createElement("input");
input.setAttribute("value", String(sth));
document.body.appendChild(input);
input.select();
document.execCommand("copy");
document.body.removeChild(input);
return message.success(t("TXT_CODE_b858d78a"));
} else {
await copy(String(sth));
if (copied.value) return message.success(t("TXT_CODE_b858d78a"));
}
} catch (err: any) {
console.error(err);
Modal.warning({
title: t("TXT_CODE_ca07c84c"),
content: [h("span", t("TXT_CODE_2452016e")), h("br"), h("span", sth)]
});
}
};