mirror of
https://github.com/CorentinTh/it-tools.git
synced 2025-04-25 09:16:15 -04:00
30 lines
826 B
TypeScript
30 lines
826 B
TypeScript
// eslint-disable-next-line no-restricted-imports
|
|
import { useClipboard } from '@vueuse/core';
|
|
import { useMessage } from 'naive-ui';
|
|
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,
|
|
});
|
|
|
|
const message = useMessage();
|
|
|
|
return {
|
|
...rest,
|
|
isJustCopied: copied,
|
|
async copy(content?: string, { notificationMessage }: { notificationMessage?: string } = {}) {
|
|
if (source) {
|
|
await copy();
|
|
}
|
|
else {
|
|
await copy(content);
|
|
}
|
|
|
|
if (createToast) {
|
|
message.success(notificationMessage ?? text);
|
|
}
|
|
},
|
|
};
|
|
}
|