From 955a947411a8858446871acd088bf6a2c20f8cb0 Mon Sep 17 00:00:00 2001 From: jacky Date: Thu, 9 May 2024 18:26:58 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A4=84=E7=90=86=E4=BB=8Eword=E7=B2=98?= =?UTF-8?q?=E8=B4=B4=E5=9B=BE=E7=89=87=E4=B8=8D=E6=98=BE=E7=A4=BA=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/components/richtext/rich-edit.vue | 40 +++++++++++++++------------ 1 file changed, 22 insertions(+), 18 deletions(-) diff --git a/src/components/richtext/rich-edit.vue b/src/components/richtext/rich-edit.vue index 7425c0d..7487941 100644 --- a/src/components/richtext/rich-edit.vue +++ b/src/components/richtext/rich-edit.vue @@ -75,29 +75,33 @@ const handleCreated = (editor) => { const customPaste = (editor, event, callback) => { let htmlData = event.clipboardData.getData('text/html') // 获取粘贴的 html - console.log(htmlData) + // console.log(htmlData) let rtfData = event.clipboardData.getData('text/rtf') // 获取粘贴的 html - console.log('-------------- rtfData --------------', rtfData) + // console.log('-------------- rtfData --------------', rtfData) htmlData = htmlData.replace(/<\/?span[^>]*>/g, ''); - htmlData = htmlData.replace(']*src=['"]([^'"]+)[^>]*>/g); - // 如果有 - if (imgSrcs && Array.isArray(imgSrcs) && imgSrcs.length) { - console.log('imgSrcs', imgSrcs) - // 从rtf内容中查找图片数据 - const rtfImageData = extractImageDataFromRtf(rtfData); - console.log('rtfImageData', rtfImageData) - // 如果找到 - if (rtfImageData.length) { - // TODO:此处可以将图片上传到自己的服务器上, - // 执行替换:将html内容中的img标签的src替换成ref中的图片数据,如果上面上传了则为图片路径 - htmlData = replaceImageFile(htmlData, imgSrcs, rtfImageData) - console.log('+++++++++++++++++++ replace after +++++++++++++++++++', htmlData) + if (rtfData !== "") { + // 处理 word 内图片 + // 从html内容中查找粘贴内容中是否有图片元素,并返回img标签的属性src值的集合 + const imgSrcs = htmlData.match(/]*src=['"]([^'"]+)[^>]*>/g); + // 如果有 + if (imgSrcs && Array.isArray(imgSrcs) && imgSrcs.length) { + // console.log('imgSrcs', imgSrcs) + // 从rtf内容中查找图片数据 + const rtfImageData = extractImageDataFromRtf(rtfData); + // console.log('rtfImageData', rtfImageData) + // 如果找到 + if (rtfImageData.length) { + // TODO:此处可以将图片上传到自己的服务器上, + + // 执行替换:将html内容中的img标签的src替换成ref中的图片数据,如果上面上传了则为图片路径 + htmlData = replaceImageFile(htmlData, imgSrcs, rtfImageData) + // console.log('22222222222222222 replace after 22222222222222222', htmlData) + } } }