【问题标题】:Highlighting a text from the document getting duplicate adds突出显示文档中的文本得到重复添加
【发布时间】:2018-09-30 22:05:27
【问题描述】:

我是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

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

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

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

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('')

现在,在这,它正在解决我的问题,但是如果它重叠,那么在我的字符串中添加了一个重复的文本。如果有一个文本相互重叠,那么它就是分隔文本。但我不想要那种行为。有人能帮我吗?

【问题讨论】:

  • 您是否正在尝试创建类似于富文本编辑器的东西?
  • 不,不是那样的。就像,我从该文本中提取了一些文本,以突出显示从后端传给我的文本的某些部分。然后如果需要突出显示的某些部分也是已经突出显示的另一个文本的一部分。

标签: javascript css angularjs html


【解决方案1】:

我不确定我是否正确理解了您的问题,但是如果突出显示的区域像这样重叠,就会出现您的问题?

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}]

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

【讨论】:

  • 基本思想是将高亮数组减少到应突出显示的不同区域。这是通过找到重叠区域来完成的(如果一个区域在下一次开始之后结束,它会重叠)如果重叠,最后取第一个起始值和最后一个结束值(条件赋值),删除现在未使用的突出显示区域(它并入第一)
  • 但是在这里,我也想强调一下重叠之类的东西。
  • 我试过了,但它现在不适用于重叠的东西
猜你喜欢
  • 2018-08-29
  • 2018-08-31
  • 1970-01-01
  • 1970-01-01
  • 2013-10-17
  • 1970-01-01
  • 2012-05-23
  • 1970-01-01
  • 2014-03-28
相关资源
最近更新 更多