|
import { onMounted, onUnmounted } from 'vue' |
|
import { storeToRefs } from 'pinia' |
|
import { useMainStore } from '@/store' |
|
import { getImageDataURL } from '@/utils/image' |
|
import usePasteTextClipboardData from './usePasteTextClipboardData' |
|
import useCreateElement from './useCreateElement' |
|
|
|
export default () => { |
|
const { editorAreaFocus, thumbnailsFocus, disableHotkeys } = storeToRefs(useMainStore()) |
|
|
|
const { pasteTextClipboardData } = usePasteTextClipboardData() |
|
const { createImageElement } = useCreateElement() |
|
|
|
|
|
const pasteImageFile = (imageFile: File) => { |
|
getImageDataURL(imageFile).then(dataURL => createImageElement(dataURL)) |
|
} |
|
|
|
|
|
|
|
|
|
|
|
const pasteListener = (e: ClipboardEvent) => { |
|
if (!editorAreaFocus.value && !thumbnailsFocus.value) return |
|
if (disableHotkeys.value) return |
|
|
|
if (!e.clipboardData) return |
|
|
|
const clipboardDataItems = e.clipboardData.items |
|
const clipboardDataFirstItem = clipboardDataItems[0] |
|
|
|
if (!clipboardDataFirstItem) return |
|
|
|
|
|
let isImage = false |
|
for (const item of clipboardDataItems) { |
|
if (item.kind === 'file' && item.type.indexOf('image') !== -1) { |
|
const imageFile = item.getAsFile() |
|
if (imageFile) pasteImageFile(imageFile) |
|
isImage = true |
|
} |
|
} |
|
|
|
if (isImage) return |
|
|
|
|
|
if (clipboardDataFirstItem.kind === 'string' && clipboardDataFirstItem.type === 'text/plain') { |
|
clipboardDataFirstItem.getAsString(text => pasteTextClipboardData(text)) |
|
} |
|
} |
|
|
|
onMounted(() => { |
|
document.addEventListener('paste', pasteListener) |
|
}) |
|
onUnmounted(() => { |
|
document.removeEventListener('paste', pasteListener) |
|
}) |
|
} |