|
| 1 | +import type { MaybeRefOrGetter } from '@vueuse/shared' |
| 2 | +import { toValue, useTimeoutFn } from '@vueuse/shared' |
| 3 | +import type { ComputedRef, Ref } from 'vue-demi' |
| 4 | +import { ref } from 'vue-demi' |
| 5 | +import { useEventListener } from '../useEventListener' |
| 6 | +import { useSupported } from '../useSupported' |
| 7 | +import type { ConfigurableNavigator } from '../_configurable' |
| 8 | +import { defaultNavigator } from '../_configurable' |
| 9 | + |
| 10 | +export interface UseClipboardItemsOptions<Source> extends ConfigurableNavigator { |
| 11 | + /** |
| 12 | + * Enabled reading for clipboard |
| 13 | + * |
| 14 | + * @default false |
| 15 | + */ |
| 16 | + read?: boolean |
| 17 | + |
| 18 | + /** |
| 19 | + * Copy source |
| 20 | + */ |
| 21 | + source?: Source |
| 22 | + |
| 23 | + /** |
| 24 | + * Milliseconds to reset state of `copied` ref |
| 25 | + * |
| 26 | + * @default 1500 |
| 27 | + */ |
| 28 | + copiedDuring?: number |
| 29 | +} |
| 30 | + |
| 31 | +export interface UseClipboardItemsReturn<Optional> { |
| 32 | + isSupported: Ref<boolean> |
| 33 | + content: ComputedRef<ClipboardItems> |
| 34 | + copied: ComputedRef<boolean> |
| 35 | + copy: Optional extends true ? (content?: ClipboardItems) => Promise<void> : (text: ClipboardItems) => Promise<void> |
| 36 | +} |
| 37 | + |
| 38 | +/** |
| 39 | + * Reactive Clipboard API. |
| 40 | + * |
| 41 | + * @see https://vueuse.org/useClipboardItems |
| 42 | + * @param options |
| 43 | + */ |
| 44 | +export function useClipboardItems(options?: UseClipboardItemsOptions<undefined>): UseClipboardItemsReturn<false> |
| 45 | +export function useClipboardItems(options: UseClipboardItemsOptions<MaybeRefOrGetter<ClipboardItems>>): UseClipboardItemsReturn<true> |
| 46 | +export function useClipboardItems(options: UseClipboardItemsOptions<MaybeRefOrGetter<ClipboardItems> | undefined> = {}): UseClipboardItemsReturn<boolean> { |
| 47 | + const { |
| 48 | + navigator = defaultNavigator, |
| 49 | + read = false, |
| 50 | + source, |
| 51 | + copiedDuring = 1500, |
| 52 | + } = options |
| 53 | + |
| 54 | + const isSupported = useSupported(() => (navigator && 'clipboard' in navigator)) |
| 55 | + const content = ref<ClipboardItems>([]) |
| 56 | + const copied = ref(false) |
| 57 | + const timeout = useTimeoutFn(() => copied.value = false, copiedDuring) |
| 58 | + |
| 59 | + function updateContent() { |
| 60 | + if (isSupported.value) { |
| 61 | + navigator!.clipboard.read().then((items) => { |
| 62 | + content.value = items |
| 63 | + }) |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + if (isSupported.value && read) |
| 68 | + useEventListener(['copy', 'cut'], updateContent) |
| 69 | + |
| 70 | + async function copy(value = toValue(source)) { |
| 71 | + if (isSupported.value && value != null) { |
| 72 | + await navigator!.clipboard.write(value) |
| 73 | + |
| 74 | + content.value = value |
| 75 | + copied.value = true |
| 76 | + timeout.start() |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + return { |
| 81 | + isSupported, |
| 82 | + content: content as ComputedRef<ClipboardItems>, |
| 83 | + copied: copied as ComputedRef<boolean>, |
| 84 | + copy, |
| 85 | + } |
| 86 | +} |
0 commit comments