diff --git a/im-web/src/components/chat/ChatBox.vue b/im-web/src/components/chat/ChatBox.vue index 7b1212e..87d96e2 100644 --- a/im-web/src/components/chat/ChatBox.vue +++ b/im-web/src/components/chat/ChatBox.vue @@ -122,7 +122,7 @@ 翻译 { + for (let i = 0; i < nodes.length; i++) { + let node = nodes[i]; + if (!node) { + continue; + } + // 文本节点 + if (node.nodeType === 3) { + continue; + } + let nodeName = node.nodeName.toLowerCase(); + // 忽略script标签 + if (nodeName === 'script') { + continue; + } + // 图片节点(包括emoji) + if (nodeName === 'img') { + hasNonTextContent = true; + return; + } + // 文件节点 + if (nodeName === 'div' && node.dataset.fileId) { + hasNonTextContent = true; + return; + } + // @用户节点 + if (nodeName === 'span' && node.dataset.id) { + hasNonTextContent = true; + return; + } + // 递归检查子节点 + if (node.childNodes && node.childNodes.length > 0) { + each(node.childNodes); + if (hasNonTextContent) { + return; + } + } + } + }; + each(nodes); + return !hasNonTextContent; + }, + // 获取纯文本内容 + getTextContent() { + let nodes = this.$refs.content.childNodes; + let textContent = ''; + let each = (nodes) => { + for (let i = 0; i < nodes.length; i++) { + let node = nodes[i]; + if (!node) { + continue; + } + // 文本节点 + if (node.nodeType === 3) { + textContent += node.textContent; + continue; + } + let nodeName = node.nodeName.toLowerCase(); + // 忽略script标签 + if (nodeName === 'script') { + continue; + } + // 忽略div节点(换行) + if (nodeName === 'div' && !node.dataset.fileId) { + textContent += '\n'; + each(node.childNodes); + continue; + } + // 递归检查子节点 + if (node.childNodes && node.childNodes.length > 0) { + each(node.childNodes); + } + } + }; + each(nodes); + return textContent.trim(); + }, + // 设置纯文本内容 + setTextContent(text) { + // 清空内容 + this.$refs.content.innerHTML = ""; + // 添加文本节点到content中 + let textNode = document.createTextNode(text); + this.$refs.content.appendChild(textNode); + // 将光标定位到文本末尾 + this.selectElement(textNode, text.length); } }