2023-08-22 01:00:20 +02:00
// eslint-disable-next-line no-restricted-imports
import { useClipboard } from '@vueuse/core' ;
2022-04-04 00:24:45 +02:00
import { useMessage } from 'naive-ui' ;
2023-08-22 01:00:20 +02:00
import type { MaybeRefOrGetter } from 'vue' ;
export function useCopy ( { source , text = 'Copied to the clipboard' , createToast = true } : { source? : MaybeRefOrGetter < string > ; text? : string ; createToast? : boolean } = { } ) {
const { copy , copied , . . . rest } = useClipboard ( {
source ,
legacy : true ,
} ) ;
2022-04-04 00:24:45 +02:00
const message = useMessage ( ) ;
return {
2023-08-22 01:00:20 +02:00
. . . rest ,
isJustCopied : copied ,
2023-08-08 09:19:43 +02:00
async copy ( content? : string , { notificationMessage } : { notificationMessage? : string } = { } ) {
2023-08-10 00:07:44 +02:00
if ( source ) {
await copy ( ) ;
}
else {
await copy ( content ) ;
}
2023-08-22 01:00:20 +02:00
if ( createToast ) {
message . success ( notificationMessage ? ? text ) ;
}
2022-04-04 00:24:45 +02:00
} ,
} ;
}