I have some content generated by a ML model in 100s of paragraphs, each para has some highlighted content.
Sometimes, highlighted data is incorrect, we want the user to select the text and get it highlighted again to correct it.
<p> Contrary to popular belief, <span class='highlighted'>Lorem</span>Ipsum is not simply random text. It has roots in a piece of popular classical Latin literature from 45 <span class='highlighted'>BC</span>, making it over 2000 years old.</p>
In this example, the user might want to select:
- Lorem Ipsum
- Latin
- 45 BC
- 2000 years
Below code works for #2 and #4, but I am not able to do it for #1 and #3 as its already highlighted.
I am using this function getSelectionText() to select the text.
$('div.content>p').on('mouseup', function(e){
var toselected = getSelectionText();
var para = $(this).html();
var start = para.indexOf(toselected);
var end = start + toselected.length;
var html = para.replace(toselected, '<span class="highlighted">' toselected + '</span>');
var obj = {"content": $(this).text(), "indices_range":[[start, end]]}
$.post('/update/<content_id>', data:tojson(obj), function(){ $(e.target).html(html);})
});
Also wondering, how can I get the start, end indices if same text occurs twice or multiple times eg. popular
.?