feat(修复某些状态下无法正常工作的问题): 还是选择span完全包裹,简化处理难度

This commit is contained in:
anime
2024-12-30 16:15:57 +08:00
parent 5ace2d1e40
commit 06c41ae8f0

View File

@@ -1,6 +1,7 @@
function convertTextToSpoilerSpan(node, className, spoilerTag) {
const regex = new RegExp(`${spoilerTag}(.*?)${spoilerTag}`, 'g')
const wholeText = node.wholeText
let outerSpan = document.createElement('span')
const fragments = []
let lastIndex = 0
let match
@@ -8,7 +9,7 @@ function convertTextToSpoilerSpan(node, className, spoilerTag) {
console.log('符合要求的文字' + wholeText)
// 添加前面未匹配的部分
if (match.index > lastIndex) {
fragments.push(
outerSpan.appendChild(
document.createTextNode(wholeText.slice(lastIndex, match.index))
)
}
@@ -19,21 +20,16 @@ function convertTextToSpoilerSpan(node, className, spoilerTag) {
if (className) {
span.className = className
}
fragments.push(span)
outerSpan.appendChild(span)
// 设置lastIndex
lastIndex = regex.lastIndex
}
if (fragments.length) {
if (outerSpan.childNodes.length) {
// 添加剩余未匹配的部分
if (lastIndex < wholeText.length) {
fragments.push(document.createTextNode(wholeText.slice(lastIndex)))
outerSpan.appendChild(document.createTextNode(wholeText.slice(lastIndex)))
}
// 替换原节点
fragments.forEach(fragment => {
node.parentNode.appendChild(fragment)
})
node.remove()
node.replaceWith(outerSpan)
}
}