feat(copy): support legacy copy to clipboard for older browser (#581)

This commit is contained in:
Corentin THOMASSET 2023-08-22 01:00:20 +02:00 committed by GitHub
parent 76b2761d62
commit 6f93cba3da
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 47 additions and 53 deletions

View file

@ -1,11 +1,19 @@
import { type MaybeRef, get, useClipboard } from '@vueuse/core';
// 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,
});
export function useCopy({ source, text = 'Copied to the clipboard' }: { source?: MaybeRef<unknown>; text?: string } = {}) {
const { copy } = useClipboard(source ? { source: computed(() => String(get(source))) } : {});
const message = useMessage();
return {
...rest,
isJustCopied: copied,
async copy(content?: string, { notificationMessage }: { notificationMessage?: string } = {}) {
if (source) {
await copy();
@ -14,7 +22,9 @@ export function useCopy({ source, text = 'Copied to the clipboard' }: { source?:
await copy(content);
}
message.success(notificationMessage ?? text);
if (createToast) {
message.success(notificationMessage ?? text);
}
},
};
}