it-tools/src/composable/copy.ts

21 lines
635 B
TypeScript
Raw Normal View History

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 } = {}) {
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
},
};
}