it-tools/src/composable/copy.ts

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