gpt4 book ai didi

javascript - 突出显示文档中的文本重复添加

转载 作者:行者123 更新时间:2023-11-28 01:50:52 25 4
gpt4 key购买 nike

我是 angular js 的新手。在这里,我有一个字符串

Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged

现在,我在这里尝试突出显示文档中的文本现在,问题是 ->

在本文中,我通过添加一些跨度类来突出显示 Lorem Ipsum。现在,对于下一次交互,如果 startoffset 和 endoffset 的字符串只是 Ipsum 已成为行业标准。现在,这两个重叠,然后突出显示重叠。因此,我无法获得确切的文本,因为偏移量发生了变化。

现在,为此我尝试了以下解决方案 ->

const str = 'Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry\'s standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged';

const highlights = [{startOffset: 2, endOffset: 16}, {startOffset: 68, endOffset: 75}, {startOffset: 80, endOffset: 92}];

const result = [];
let currentIndex = 0;

highlights.forEach(h => {
result.push(str.substring(currentIndex, h.startOffset));
result.push(`<span class="mark">${str.substring(h.startOffset, h.endOffset)}</span>`);
currentIndex = h.endOffset;
});

result.push(str.substring(currentIndex, str.length));

document.getElementById('root').innerHTML = result.join('')

现在,在这里,它正在解决我的问题,但是如果它重叠,那么 Here A duplicate text is getting added in my string 。如果有一个文本相互重叠,那么它就是将文本分开。但我不想要那种行为。有人可以帮我解决这个问题吗?

最佳答案

我不确定,如果我理解正确你的问题,但你的问题发生了,如果突出显示的区域重叠,像这样?

var highlights = [{startOffset: 2, endOffset: 16}, 
{startOffset: 85, endOffset: 100},
{startOffset: 80, endOffset: 92}];

要实现这一点,您必须首先生成不同的区域。

highlights.sort(function (a, b) {return a.startOffset - b.startOffset; }); //first order by the start offset
// highlights looks like this
//[{startOffset: 2, endOffset: 16},
// {startOffset: 80, endOffset: 92},
// {startOffset: 85, endOffset: 100}]

// next merge all overlapping areas

for(var i = 0; i < highlights.length - 1; i++) { //each element but the last, because it can't overlap with the next
if(highlights[i].endOffset > highlights[i + 1].startOffset) { //if it overlaps with the next
highlights[i].endOffset = highlights[i].endOffset > highlights[i + 1].endOffset ?
highlights[i].endOffset : highlights[i + 1].endOffset; //take the higher end offset of those two as new offset

highlights.splice(i + 1, 1); //remove next element in list, since it is no longer useful
i--; //check current element again
}
}
//output
//[{startOffset: 2, endOffset: 16},
// {startOffset: 80, endOffset: 100}]

在那之后,您的代码应该可以工作了。

关于javascript - 突出显示文档中的文本重复添加,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49937396/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com