当前需求
插件传送门 cross-text-highlight
当前需求,是后端返回了全文内容(** 带样式标签的 **) ,以及大模型生成的匹配的纯文本,然后需要将其 一一对应 并做上对应标注如下图
vue-search-hightlight 传送门 : vue-search-hightlight
自定义 searchHighLight.vue组件
主要针对他的定位进行了更改,文本匹配没有更改,以及新增了标注点
当前版本是基于vue2的,有需要vue3的自行更改下即可
<!--
用法案例
content 整个文本内容
keyword 搜索的关键词
<div ref="htmlContent" id="htmlContent">
<search-highlight
class="search-highlight"
ref="search"
@current-change="currentChange"
:content="text"
:keyword="keyword"
highlightStyle="background: rgb(224, 234, 250)"
currentStyle="{background: rgb(224, 234, 250)}"
></search-highlight>
</div>
-->
<template>
<div class="search-highlight" v-html="contentShow">
</div>
</template>
<script>
const PLUGIN_FLAG = 'search-hightlight'
export default {
name: 'searchHighLight',
props: {
content: {
type: String,
default: ''
},
keyword: {
type: String,
default: ''
},
highlightStyle: {
type: String,
default: 'background: #ffff00'
},
currentStyle: {
type: String,
default: 'background: #ff9632'
},
regExp: {
type: Boolean,
default: false
}
},
data() {
return {
lightIndex: 0,
matchCount: 0,
contentShow: '',
random: `${Math.random()}`.slice(2)
}
},
computed: {
watchString() {
return [this.content, this.keyword]
},
watchStyle() {
return [this.lightIndex, this.highlightStyle, this.currentStyle]
},
flag() {
return `${PLUGIN_FLAG}${this.random}`
},
styleSelector() {
return `style[${this.flag}]`
},
},
watch: {
watchString: {
immediate: true,
handler() {
this.replaceKeywords()
}
},
watchStyle: {
immediate: true,
handler() {
this.setStyle()
}
},
lightIndex: {
immediate: true,
handler() {
this.$emit('current-change', this.lightIndex)
}
},
matchCount: {
immediate: true,
handler() {
this.$emit('match-count-change', this.matchCount)
}
}
},
beforeDestroy() {
this.clearStyle()
},
methods: {
getTextNodeList(dom) {
const nodeList = [...dom.childNodes]
const textNodes = []
while (nodeList.length) {
const node = nodeList.shift()
if (node.nodeType === node.TEXT_NODE) {
node.wholeText && textNodes.push(node)
} else {
nodeList.unshift(...node.childNodes)
}
}
return textNodes
},
getTextInfoList(textNodes) {
let length = 0
return textNodes.map(node => {
let startIdx = length, endIdx = length + node.wholeText.length
length = endIdx
return {
text: node.wholeText,
startIdx,
endIdx
}
})
},
getMatchList(content, keyword) {
if (!this.regExp) {
const characters = [...'\\[](){}?.+*^$:|'].reduce((r, c) => (r[c] = true, r), {})
keyword = keyword.split('').map(s => characters[s] ? `\\${s}` : s).join('[\\s\\n]*')
}
const reg = new RegExp(keyword, 'gmi')
const matchList = []
let match = reg.exec(content)
while (match) {
matchList.push(match)
match = reg.exec(content)
}
return matchList
},
replaceMatchResult(textNodes, textList, matchList) {
// 对于每一个匹配结果,可能分散在多个标签中,找出这些标签,截取匹配片段并用font标签替换出
for (let i = matchList.length - 1; i >= 0; i--) {
const match = matchList[i]
const matchStart = match.index, matchEnd = matchStart + match[0].length // 匹配结果在拼接字符串中的起止索引
// 遍历文本信息列表,查找匹配的文本节点
for (let textIdx = 0; textIdx < textList.length; textIdx++) {
const {text, startIdx, endIdx} = textList[textIdx] // 文本内容、文本在拼接串中开始、结束索引
if (endIdx < matchStart) continue // 匹配的文本节点还在后面
if (startIdx >= matchEnd) break // 匹配文本节点已经处理完了
let textNode = textNodes[textIdx] // 这个节点中的部分或全部内容匹配到了关键词,将匹配部分截取出来进行替换
const nodeMatchStartIdx = Math.max(0, matchStart - startIdx) // 匹配内容在文本节点内容中的开始索引
const nodeMatchLength = Math.min(endIdx, matchEnd) - startIdx - nodeMatchStartIdx // 文本节点内容匹配关键词的长度
if (nodeMatchStartIdx > 0) textNode = textNode.splitText(nodeMatchStartIdx) // textNode取后半部分
if (nodeMatchLength < textNode.wholeText.length) textNode.splitText(nodeMatchLength)
const font = document.createElement('font')
font.setAttribute(this.flag, i + 1)
font.innerText = text.substr(nodeMatchStartIdx, nodeMatchLength)
textNode.parentNode.replaceChild(font, textNode)
}
}
},
replaceKeywords() {
let errFlag = false
if (this.regExp) {
try {
const reg = new RegExp(this.keyword)
if (reg.test('')) errFlag = true
} catch (err) {
errFlag = true
}
}
if (errFlag || !this.keyword) {
this.matchCount = 0;
this.lightIndex = 0;
this.contentShow = this.content
return
}
const div = document.createElement('div')
div.innerHTML = this.content
const textNodes = this.getTextNodeList(div)
const textList = this.getTextInfoList(textNodes)
const content = textList.map(({text}) => text).join('')
const matchList = this.getMatchList(content, this.keyword)
this.matchCount = matchList.length
this.lightIndex = this.matchCount ? 1 : 0
this.replaceMatchResult(textNodes, textList, matchList)
this.contentShow = div.innerHTML
},
/**
* 滚动方法
* @param index 当前序号
* @param pEle 父元素
* @param cEle 子元素也是文本内容元素
*/
scrollTo(index, pEle,cEle) {
this.$nextTick(() => {
let node = this.$el.querySelector(`font[${this.flag}='${index}']`)
console.dir(node)
if (node) {
this.lightIndex = index
// node.scrollIntoView()
// 外层包裹的盒子
const contentElement2 = document.getElementById(pEle);
console.log('contentElement2--', contentElement2)
setTimeout(() => {
const contentDiv = cEle; // 获取 ref 为 "content" 的 DOM 元素
if (contentDiv) {
const contentRect = contentDiv.$el.getBoundingClientRect(); // 获取 contentDiv 的位置
// 假设你有一个方法来获取 class 为 "choose-highlight" 的元素
const highlightRect = node.getBoundingClientRect(); // 获取 highlightedElement 的位置
// 计算相对于 contentDiv 的位置
const relativeTop = highlightRect.top - contentRect.top;
contentElement2.scrollTo({top: relativeTop - 200, behavior: 'smooth'});
}
}, 100)
}
})
},
searchNext() {
this.$nextTick(() => {
let idx = this.lightIndex >= this.matchCount ? 1 : this.lightIndex + 1
this.scrollTo(idx)
})
},
searchLast() {
this.$nextTick(() => {
let idx = this.lightIndex <= 1 ? this.matchCount : this.lightIndex - 1
this.scrollTo(idx)
})
},
setStyle() {
let style = document.head.querySelector(this.styleSelector)
if (!style) {
style = document.createElement('style')
style.setAttribute(this.flag, 1)
}
style.innerText = `font[${this.flag}]{${this.highlightStyle}}font[${this.flag}='${this.lightIndex}']{${this.currentStyle}}`
document.head.appendChild(style)
},
clearStyle() {
let style = document.head.querySelector(this.styleSelector)
style && document.head.removeChild(style)
}
}
}
</script>
如何使用:
组件 1:
<el-card class="box-card">
<div ref="htmlContent" id="htmlContent">
<search-highlight
class="search-highlight"
ref="search"
@current-change="currentChange"
:content="text"
:keyword="keyword"
highlightStyle="background: rgb(224, 234, 250)"
currentStyle="{background: rgb(224, 234, 250)}"
></search-highlight>
</div>
</el-card>
这里有还有两个比较重要的地方:
定位方法跟画标签
getPosition(index){
this.$refs.search.scrollTo(1,'htmlContent',this.$refs.search)
this.fixPositionRisk()
this.$nextTick(()=>{
let parent = document.getElementById('htmlContent');
let arr = []
// 获取所有font标签
let fonts = parent.getElementsByTagName('font');
for (let i = 0; i < fonts.length; i++) {
if(fonts[i].attributes.length > 0){
arr.push(fonts[i])
}
}
if(!arr[0]){
this.$message.error('暂未找到匹配的节点')
return
}
let canvas = document.createElement('canvas')
canvas.width = 200 // 设置canvas宽度
canvas.height = 200 // 设置canvas高度
let ctx = canvas.getContext('2d')
ctx.clearRect(0,0,canvas.width,canvas.height)
ctx.font = '150px Arial' // 设置文本字体大小和样式
ctx.fillStyle = 'white' // 设置文本颜色
ctx.textAlign = 'center' // 设置文本水平居中
ctx.textBaseline = 'middle' // 设置文本垂直居中
ctx.fillText(!index ? 1 : index+ 1, 95, 110) // 在canvas上绘制文本
let img = new Image()
img.src = canvas.toDataURL()
let imageElement = document.createElement('img')
imageElement.src = img.src
// imageElement.style.zIndex = `${index}`
imageElement.className = `custom-marker`
// 清除之前节点
document.querySelectorAll(".custom-marker").forEach(element => {
element.remove();
})
// 把之前节点去掉
arr[0].insertBefore(imageElement, arr[0].firstChild);
})
},
fixPositionRisk() {
this.$nextTick(()=>{
const contentElement2 = document.getElementById('test1');
const contentDiv = this.$refs.test1; // 获取 ref 为 "content" 的 DOM 元素
const highlightedElement = contentDiv.querySelector('.risk-active');
const scrollTopDistance = highlightedElement.getBoundingClientRect().top - contentElement2.getBoundingClientRect().top + contentElement2.scrollTop;
console.log('scrollTopDistance', scrollTopDistance)
contentElement2.scrollTop = scrollTopDistance - 30
})
},
如方法中的 test1 , .risk-active 提供如下布局
<div class="risk-list" id="test1" ref="test1" v-loading="showRisk">
<template v-if="riskList.length && !showRisk">
<div :class="[activeRisk !== index ? 'risk' : 'risk-active']" v-for="(item, index) in riskList"
:key="index"
@click="changeRisk(item, index)">
<div class="risk-sign">
{{ index + 1 }}
</div>
<div class="risk-title">涉嫌违反条目</div>
<div class="risk-content">{{ item.violationTypeName }}</div>
<div class="risk-title">AI解读</div>
<div class="risk-content">{{ item.reason }}</div>
</div>
</template>
<div class="no-risk" v-else-if="!showRisk">{{ riskText }}</div>
</div>