How to get the start index of a selected text within a multiline text of a div?

Assume I have an editable div which contains these lines:

enter image description here

How to retrieve the position of ‘w’ in the text ?

I can’t use:

window.getSelection().anchorOffset;

Because this gets the position in the line, but not in text, that is 0 instead of 5.

I prefer not to use:

element.innerText.indexOf(selectedText);

Because it may retrieve similar texts.