feat: 优化聊天室功能,修复复制等问题

This commit is contained in:
橙子
2024-10-04 00:37:36 +08:00
parent 10e1fad7f3
commit 13120712b1

View File

@@ -46,9 +46,10 @@ const startCountTip = () => {
} }
}, 1000); }, 1000);
}; };
let codeCopyDic=[];
//当前聊天框显示的消息 //当前聊天框显示的消息
const currentMsgContext = computed(() => { const currentMsgContext = computed(() => {
if (selectIsAll()) { if (selectIsAll()) {
return chatStore.allMsgContext; return chatStore.allMsgContext;
} }
@@ -57,6 +58,7 @@ const currentMsgContext = computed(() => {
// console.log(chatStore.aiMsgContext, "chatStore.aiMsgContext"); // console.log(chatStore.aiMsgContext, "chatStore.aiMsgContext");
// return chatStore.aiMsgContext; // return chatStore.aiMsgContext;
let tempHtml = []; let tempHtml = [];
codeCopyDic=[];
chatStore.aiMsgContext.forEach(element => { chatStore.aiMsgContext.forEach(element => {
tempHtml.push({ content: toMarkDownHtml(element.content), messageType: 'Ai', sendUserId: element.sendUserId, sendUserInfo: element.sendUserInfo }); tempHtml.push({ content: toMarkDownHtml(element.content), messageType: 'Ai', sendUserId: element.sendUserId, sendUserInfo: element.sendUserInfo });
@@ -88,7 +90,6 @@ const toMarkDownHtml = (text) => {
tables: true,//支持表格 tables: true,//支持表格
breaks: true, breaks: true,
sanitize: false, sanitize: false,
smartLists: true,
smartypants: false, smartypants: false,
xhtml: false, xhtml: false,
smartLists: true, smartLists: true,
@@ -102,7 +103,7 @@ const toMarkDownHtml = (text) => {
return soureHtml; return soureHtml;
} }
let codeCopyDic=[];
//code部分处理、高亮 //code部分处理、高亮
const codeHandler = (code, language) => { const codeHandler = (code, language) => {
const codeIndex = parseInt(Date.now() + "") + Math.floor(Math.random() * 10000000); const codeIndex = parseInt(Date.now() + "") + Math.floor(Math.random() * 10000000);
@@ -204,20 +205,25 @@ onUnmounted(() => {
}) })
const clickCopyEvent=async function(event) {
const spanId=event.target.id;
console.log(codeCopyDic,"codeCopyDic")
console.log(spanId,"spanId")
await navigator.clipboard.writeText(codeCopyDic.filter(x=>x.id==spanId)[0].code);
ElMessage({
message: "代码块复制成功",
type: "success",
duration: 2000,
});
}
//代码copy事件 //代码copy事件
const addCopyEvent=()=>{ const addCopyEvent=()=>{
const copySpans = document.querySelectorAll('.copy'); const copySpans = document.querySelectorAll('.copy');
// 为每个 copy span 元素添加点击事件 // 为每个 copy span 元素添加点击事件
copySpans.forEach(span => { copySpans.forEach(span => {
//先移除,再新增
span.addEventListener('click', async function() { span.removeEventListener('click',clickCopyEvent );
await navigator.clipboard.writeText(codeCopyDic.filter(x=>x.id==span.id)[0].code ); span.addEventListener('click', clickCopyEvent);
ElMessage({
message: "代码块复制成功",
type: "success",
duration: 2000,
});
});
}); });
} }
@@ -412,7 +418,7 @@ const getLastMessage = ((receiveId, itemType) => {
<template> <template>
<div style="position: absolute; top: 0;left: 0;" v-show="isShowTipNumber>0"> <div style="position: absolute; top: 0;left: 0;" v-show="isShowTipNumber>0">
<p>当前版本1.5.2</p> <p>当前版本1.6.2</p>
<p>tip:官方学习交流群每次发送消息消耗 1 钱钱</p> <p>tip:官方学习交流群每次发送消息消耗 1 钱钱</p>
<p>tip:点击聊天窗口右上角X可退出</p> <p>tip:点击聊天窗口右上角X可退出</p>
<p>tip:多人同时在聊天室时左侧可显示其他成员</p> <p>tip:多人同时在聊天室时左侧可显示其他成员</p>