feat:paste image

pr675
黄云飞 4 years ago
parent 3c272e12b4
commit ede0a43f7b

@ -1,3 +1,4 @@
import { ElementType } from '../../dataset/enum/Element'
import { debounce } from '../../utils'
import { getElementListByHTML } from '../../utils/clipboard'
import { Draw } from '../draw/Draw'
@ -56,7 +57,7 @@ export class CursorAgent {
}
for (let i = 0; i < clipboardData.items.length; i++) {
const item = clipboardData.items[i]
if (item.kind !== 'string') continue
if (item.kind === 'string') {
if (item.type === 'text/plain' && !isHTML) {
item.getAsString(plainText => {
const elementList = plainText.split('').map(value => ({
@ -71,6 +72,29 @@ export class CursorAgent {
this.draw.insertElementList(elementList)
})
}
} else if (item.kind === 'file') {
if (item.type.includes('image')) {
const file = item.getAsFile()
if (file) {
const fileReader = new FileReader()
fileReader.readAsDataURL(file)
fileReader.onload = () => {
// 计算宽高
const image = new Image()
const value = fileReader.result as string
image.src = value
image.onload = () => {
this.draw.insertElementList([{
value,
type: ElementType.IMAGE,
width: image.width,
height: image.height
}])
}
}
}
}
}
}
evt.preventDefault()
}

Loading…
Cancel
Save