|
|
@ -450,6 +450,97 @@ export default { |
|
|
}, |
|
|
}, |
|
|
focus() { |
|
|
focus() { |
|
|
this.$refs.content.focus(); |
|
|
this.$refs.content.focus(); |
|
|
|
|
|
}, |
|
|
|
|
|
// 检查输入框是否只包含纯文本 |
|
|
|
|
|
isPureText() { |
|
|
|
|
|
let nodes = this.$refs.content.childNodes; |
|
|
|
|
|
let hasNonTextContent = false; |
|
|
|
|
|
let each = (nodes) => { |
|
|
|
|
|
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); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|
|