提交 dab3c5ec 编写于 作者: DCloud_JSON's avatar DCloud_JSON

修复 富文本内容右键复制内容后粘贴到钉钉不可用的问题

上级 cb55e287
...@@ -212,52 +212,10 @@ ...@@ -212,52 +212,10 @@
this.opacity = 1 this.opacity = 1
}) })
}, },
copyContent(){ async copyContent(){
// console.log('setClipboardData'); // console.log('setClipboardData');
// console.log('this.controlData',this.controlData); // 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 // #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') { if (this.controlData.msg.type === 'image') {
let { name, url } = this.controlData.msg.body let { name, url } = this.controlData.msg.body
fetch(url).then(response => response.blob()).then(blob => { fetch(url).then(response => response.blob()).then(blob => {
...@@ -266,12 +224,41 @@ ...@@ -266,12 +224,41 @@
let clipboardItem = new ClipboardItem({ [mime]: file }) let clipboardItem = new ClipboardItem({ [mime]: file })
navigator.clipboard.write([clipboardItem]) navigator.clipboard.write([clipboardItem])
}) })
uni.hideToast()
return return
} }
// #endif // #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' ? `<img src="${i.attrs.src}">` : 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({ uni.setClipboardData({
data, data,
complete:(e)=> { complete:(e)=> {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册