From dab3c5ec2dc3cc7943e5655c4a6ca830326c7452 Mon Sep 17 00:00:00 2001 From: DCloud_JSON Date: Mon, 22 Apr 2024 21:46:05 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=20=E5=AF=8C=E6=96=87?= =?UTF-8?q?=E6=9C=AC=E5=86=85=E5=AE=B9=E5=8F=B3=E9=94=AE=E5=A4=8D=E5=88=B6?= =?UTF-8?q?=E5=86=85=E5=AE=B9=E5=90=8E=E7=B2=98=E8=B4=B4=E5=88=B0=E9=92=89?= =?UTF-8?q?=E9=92=89=E4=B8=8D=E5=8F=AF=E7=94=A8=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- components/uni-im-msg/popup-control.vue | 77 ++++++++++--------------- 1 file changed, 32 insertions(+), 45 deletions(-) diff --git a/components/uni-im-msg/popup-control.vue b/components/uni-im-msg/popup-control.vue index 9b52bf1..3dd5e73 100644 --- a/components/uni-im-msg/popup-control.vue +++ b/components/uni-im-msg/popup-control.vue @@ -212,52 +212,10 @@ this.opacity = 1 }) }, - copyContent(){ + async copyContent(){ // console.log('setClipboardData'); // console.log('this.controlData',this.controlData); - let data = this.controlData.msg.body - switch (this.controlData.msg.type){ - case 'userinfo-card': - data = location.origin + '/#/?user_id='+this.controlData.msg.body.user_id - break; - case 'rich-text': - data = JSON.stringify({"uni-im-rich-text-data":data}) - break; - default: - break; - } - // #ifdef H5 - if (this.controlData.msg.type === 'rich-text') { - // 如果富文本消息是纯图片,则直接把图片内容放入剪贴板 - let [n1, n2, n3] = this.controlData.msg.body - if ( - n1?.type === 'text' && n1?.text === '' && - n2?.name === 'img' && n2?.attrs?.src?.startsWith('data:image/') && - n3?.type === 'text' && n3?.text === '' - ) { - // data:image/png;base64,iVBORw0K... - let dataurl = n2.attrs.src - let m = /^data:(image\/.*);base64,(.*)$/.exec(dataurl) - if (m) { - let [_, mime, base64] = m - let byteCharacters = atob(base64) - let byteNumbers = new Array(byteCharacters.length) - for (let i = 0; i < byteCharacters.length; i++) { - byteNumbers[i] = byteCharacters.charCodeAt(i) - } - let byteArray = new Uint8Array(byteNumbers) - let blob = new Blob([byteArray], { type: mime }) - let file = new File([blob], mime.replace('/', '.'), { type: mime }) - let clipboardItem = new ClipboardItem({ [mime]: file }) - navigator.clipboard.write([clipboardItem]) - uni.hideToast() - - return - } - } - } - if (this.controlData.msg.type === 'image') { let { name, url } = this.controlData.msg.body fetch(url).then(response => response.blob()).then(blob => { @@ -266,11 +224,40 @@ let clipboardItem = new ClipboardItem({ [mime]: file }) navigator.clipboard.write([clipboardItem]) }) - uni.hideToast() - return } // #endif + + let data = this.controlData.msg.body + switch (this.controlData.msg.type){ + case 'userinfo-card': + data = location.origin + '/#/?user_id='+this.controlData.msg.body.user_id + break; + case 'rich-text': + /* + let html = data.map(i=>i.name == 'img' ? `` : i.text).join(' ') + // 把data写到剪切板 + // 将HTML字符串转换为ArrayBuffer,确保UTF-8编码 + const encoder = new TextEncoder(); + const htmlArrayBuffer = encoder.encode(html); + // 创建一个包含HTML内容的ClipboardItem + const clipboardItem = new ClipboardItem({ + 'text/html': new Blob([htmlArrayBuffer], { type: 'text/html' }), + }); + // 尝试将ClipboardItem写入剪切板 + try { + await navigator.clipboard.write([clipboardItem]); + console.log('HTML内容已写入剪切板'); + } catch (err) { + console.error('写入剪切板失败: ', err); + } + return + */ + data = data.map(i=>i.name == 'img' ? '[图片]' : i.text).join(' ') + break; + default: + break; + } uni.setClipboardData({ data, -- GitLab