|
import { storeToRefs } from 'pinia' |
|
import { useKeyboardStore } from '@/store' |
|
import { pasteCustomClipboardString } from '@/utils/clipboard' |
|
import { parseText2Paragraphs } from '@/utils/textParser' |
|
import { getImageDataURL, isSVGString, svg2File } from '@/utils/image' |
|
import { isValidURL } from '@/utils/common' |
|
import useCreateElement from '@/hooks/useCreateElement' |
|
import useAddSlidesOrElements from '@/hooks/useAddSlidesOrElements' |
|
|
|
interface PasteTextClipboardDataOptions { |
|
onlySlide?: boolean |
|
onlyElements?: boolean |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const isValidImgURL = (url: string) => { |
|
const pexels = /^https?:\/\/(?:[a-zA-Z0-9-]+\.)*pexels\.com\/[^\s]+\.(?:jpg|jpeg|png|svg|webp)(?:\?.*)?$/i.test(url) |
|
const pptist = /^https?:\/\/(?:[a-zA-Z0-9-]+\.)*pptist\.cn\/[^\s]+\.(?:jpg|jpeg|png|svg|webp)(?:\?.*)?$/i.test(url) |
|
return pexels || pptist |
|
} |
|
|
|
export default () => { |
|
const { shiftKeyState } = storeToRefs(useKeyboardStore()) |
|
|
|
const { createTextElement, createImageElement } = useCreateElement() |
|
const { addElementsFromData, addSlidesFromData } = useAddSlidesOrElements() |
|
|
|
|
|
|
|
|
|
|
|
const createTextElementFromClipboard = (text: string) => { |
|
createTextElement({ |
|
left: 0, |
|
top: 0, |
|
width: 600, |
|
height: 50, |
|
}, { content: text }) |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
const pasteTextClipboardData = (text: string, options?: PasteTextClipboardDataOptions) => { |
|
const onlySlide = options?.onlySlide || false |
|
const onlyElements = options?.onlyElements || false |
|
|
|
const clipboardData = pasteCustomClipboardString(text) |
|
|
|
|
|
if (typeof clipboardData === 'object') { |
|
const { type, data } = clipboardData |
|
|
|
if (type === 'elements' && !onlySlide) addElementsFromData(data) |
|
else if (type === 'slides' && !onlyElements) addSlidesFromData(data) |
|
} |
|
|
|
|
|
else if (!onlyElements && !onlySlide) { |
|
|
|
if (shiftKeyState.value) { |
|
const string = parseText2Paragraphs(clipboardData) |
|
createTextElementFromClipboard(string) |
|
} |
|
else { |
|
|
|
if (isValidImgURL(clipboardData)) { |
|
createImageElement(clipboardData) |
|
} |
|
|
|
else if (isValidURL(clipboardData)) { |
|
createTextElementFromClipboard(`<a href="${clipboardData}" title="${clipboardData}" target="_blank">${clipboardData}</a>`) |
|
} |
|
|
|
else if (isSVGString(clipboardData)) { |
|
const file = svg2File(clipboardData) |
|
getImageDataURL(file).then(dataURL => createImageElement(dataURL)) |
|
} |
|
|
|
else { |
|
const string = parseText2Paragraphs(clipboardData) |
|
createTextElementFromClipboard(string) |
|
} |
|
} |
|
} |
|
} |
|
|
|
return { |
|
pasteTextClipboardData, |
|
} |
|
} |