2023-05-28 23:13:24 +02:00
|
|
|
import { type MaybeRef, get, useClipboard } from '@vueuse/core';
|
2022-04-04 00:24:45 +02:00
|
|
|
import { useMessage } from 'naive-ui';
|
|
|
|
|
2023-08-08 09:19:43 +02:00
|
|
|
export function useCopy({ source, text = 'Copied to the clipboard' }: { source?: MaybeRef<unknown>; text?: string } = {}) {
|
|
|
|
const { copy } = useClipboard(source ? { source: computed(() => String(get(source))) } : {});
|
2022-04-04 00:24:45 +02:00
|
|
|
const message = useMessage();
|
|
|
|
|
|
|
|
return {
|
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-08 09:19:43 +02:00
|
|
|
message.success(notificationMessage ?? text);
|
2022-04-04 00:24:45 +02:00
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|