wujingjing
2025-04-07 457cc6cf166d3b6c22be4f78c1db8802a7fbb4c7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
<script setup lang="ts">
import { CheckOne, Copy, Loading } from '@icon-park/vue-next';
import type { Theme } from '@icon-park/vue-next/lib/runtime';
import { ref } from 'vue';
 
const porps = defineProps<{ content: string }>();
const btnConfig: {
    size: number;
    fill: string;
    theme: Theme;
} = {
    size: 14,
    fill: '#999',
    theme: 'outline',
};
const btnTips = {
    copy: '复制全文',
    loading: '',
    success: '已复制到剪贴板!',
    error: '复制失败!',
};
const btnStatus = ref<'copy' | 'loading' | 'success' | 'error'>('copy');
 
const copyToClipboard = (content: string = porps.content) => {
    btnStatus.value = 'loading';
    navigator.clipboard
        .writeText(content)
        .then(() => setTimeout(() => (btnStatus.value = 'success'), 150))
        .catch(() => (btnStatus.value = 'error'))
        .finally(() => setTimeout(() => (btnStatus.value = 'copy'), 1500));
};
</script>
 
<template>
    <div class="flex items-center cursor-pointer" @click="copyToClipboard()">
        <copy v-show="btnStatus === 'copy'" :theme="btnConfig.theme" :size="btnConfig.size" :fill="btnConfig.fill" />
        <loading class="rotate" v-show="btnStatus === 'loading'" :theme="btnConfig.theme" :size="btnConfig.size" :fill="btnConfig.fill" />
        <check-one v-show="btnStatus === 'success'" :theme="btnConfig.theme" :size="btnConfig.size" :fill="btnConfig.fill" />
        <close-one v-show="btnStatus === 'error'" :theme="btnConfig.theme" :size="btnConfig.size" :fill="btnConfig.fill" />
        <span class="ml-0.5 text-gray-500 leading-none">{{ btnTips[btnStatus] }}</span>
    </div>
</template>
 
<style scoped>
@keyframes spin {
    0% {
        transform: rotate(0deg);
    }
    100% {
        transform: rotate(360deg);
    }
}
 
.rotate {
    animation: spin 2s linear infinite;
}
</style>