created
This commit is contained in:
parent
a25fb939b3
commit
d25d121293
1 changed files with 48 additions and 0 deletions
48
src/components/CopyToClipboard.vue
Normal file
48
src/components/CopyToClipboard.vue
Normal file
|
|
@ -0,0 +1,48 @@
|
||||||
|
<script setup>
|
||||||
|
import { useMessage } from 'naive-ui'
|
||||||
|
|
||||||
|
const message = useMessage()
|
||||||
|
|
||||||
|
const props = defineProps({
|
||||||
|
text: {
|
||||||
|
type: String,
|
||||||
|
required: true,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
const copyToClipboard = async () => {
|
||||||
|
if (!props.text || props.text.trim() === '') {
|
||||||
|
message.error('Nothing to copy to clipboard')
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
await navigator.clipboard.writeText(props.text)
|
||||||
|
copySuccess()
|
||||||
|
} catch (err) {
|
||||||
|
console.error('Error copy to clipboard', err)
|
||||||
|
copyError()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function copySuccess() {
|
||||||
|
const displayText = props.text.length > 50
|
||||||
|
? props.text.substring(0, 50) + '...'
|
||||||
|
: props.text
|
||||||
|
message.success(`Copied to clipboard: ${displayText}`)
|
||||||
|
}
|
||||||
|
|
||||||
|
function copyError() {
|
||||||
|
message.error('Nothing to copy to clipboard')
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<template>
|
||||||
|
<span @click="copyToClipboard">
|
||||||
|
<slot />
|
||||||
|
</span>
|
||||||
|
</template>
|
||||||
|
|
||||||
|
<style scoped></style>
|
||||||
Loading…
Add table
Add a link
Reference in a new issue