From ede0a43f7b5362f2dc8f77b580528ecdb36d2e80 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E4=BA=91=E9=A3=9E?= Date: Thu, 12 May 2022 17:57:22 +0800 Subject: [PATCH] feat:paste image --- src/editor/core/cursor/CursorAgent.ts | 52 +++++++++++++++++++-------- 1 file changed, 38 insertions(+), 14 deletions(-) diff --git a/src/editor/core/cursor/CursorAgent.ts b/src/editor/core/cursor/CursorAgent.ts index 0c76478..91c2be3 100644 --- a/src/editor/core/cursor/CursorAgent.ts +++ b/src/editor/core/cursor/CursorAgent.ts @@ -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,20 +57,43 @@ export class CursorAgent { } for (let i = 0; i < clipboardData.items.length; i++) { const item = clipboardData.items[i] - if (item.kind !== 'string') continue - if (item.type === 'text/plain' && !isHTML) { - item.getAsString(plainText => { - const elementList = plainText.split('').map(value => ({ - value - })) - this.draw.insertElementList(elementList) - }) - } - if (item.type === 'text/html' && isHTML) { - item.getAsString(htmlText => { - const elementList = getElementListByHTML(htmlText) - this.draw.insertElementList(elementList) - }) + if (item.kind === 'string') { + if (item.type === 'text/plain' && !isHTML) { + item.getAsString(plainText => { + const elementList = plainText.split('').map(value => ({ + value + })) + this.draw.insertElementList(elementList) + }) + } + if (item.type === 'text/html' && isHTML) { + item.getAsString(htmlText => { + const elementList = getElementListByHTML(htmlText) + 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()