From 207ed0e42fc8530f1d7be95f0df6429efec62652 Mon Sep 17 00:00:00 2001 From: doctorwu Date: Mon, 16 Oct 2023 17:57:01 +0800 Subject: [PATCH] feat(useClipboard): support customize support to pass a parameter |transform2ClipboardItems| to customize clipboardItems fix #3450 --- packages/core/useClipboard/index.ts | 35 ++++++++++++++++++++++++++--- 1 file changed, 32 insertions(+), 3 deletions(-) diff --git a/packages/core/useClipboard/index.ts b/packages/core/useClipboard/index.ts index d2a7b59eeb6..e7c1361f13a 100644 --- a/packages/core/useClipboard/index.ts +++ b/packages/core/useClipboard/index.ts @@ -35,6 +35,25 @@ export interface UseClipboardOptions extends ConfigurableNavigator { * @default false */ legacy?: boolean + + /** + * This function will be used to transform the source to ClipboardItems, + * This way you can customize the ClipboardItem, such as specifying the mime type. + * + * @param source Copy source + * @returns Array of ClipboardItems + * @default + ``` + (source: string) => { + const type = "text/plain"; + const blob = new Blob([toValue(source)], { type }); + return [ + new ClipboardItem({ [type]: blob }), + ] + } + ``` + */ + transform2ClipboardItems?: (source: string) => ClipboardItem[] } export interface UseClipboardReturn { @@ -59,6 +78,13 @@ export function useClipboard(options: UseClipboardOptions { + const type = 'text/plain' + const blob = new Blob([toValue(source)], { type }) + return [ + new ClipboardItem({ [type]: blob }), + ] + }, } = options const isClipboardApiSupported = useSupported(() => (navigator && 'clipboard' in navigator)) @@ -83,10 +109,13 @@ export function useClipboard(options: UseClipboardOptions