I am creating a range based on a user selection using the following method:
var selection = window.getSelection();
var beginsAt = selection.getRangeAt(0).startOffset;
var endsAt = selection.getRangeAt(0).endOffset;
return [beginsAt,endsAt];
The component that calls this function does the following:
let before = text.substring(0, range[0]);
let highlighted = text.substring(range[0], range[1]);
let after = text.substring(range[1], text.length);
return(
<div>
{before}
<span style={{
backgroundColor: 'yellow'
}}>
{highlighted}
</span>
{after}
</div>
)
Whenever I select my text, the range is always relative to where the highlighted span ends. So if my text is on a range between 0 and 50 and I select the range [25,26] then select the range [26,27], rather than highlighting the range [26,27] it highlights the range from [0,1] because the range was [0,1] relative to the text generated by {after}.
As long as I highlight any text rendered by {before} I have no issues, its only when I highlight something rendered in the {after} section.
How can I make these range values relative to the root of the entire text rather than relative to the blocks that they are rendered in? Is this a limitation of react or my implementation?