Hi All! Help wanted I’m trying to find a way to get word on click(tap) from veeeery long string in ion-text… is it possible without indexing - cause otherwise it will have poor performance. I’m trying to achieve it by using elementFromPoint (elements) - but it seems that it is not the correct way…
getWordSelection(e) {
const selection = window.getSelection();
if (!selection || selection.rangeCount < 1) {return;}
const node = selection.anchorNode;
const range = selection.getRangeAt(0);
const text = selection.anchorNode.textContent;
let startIndex; let endIndex;
startIndex = endIndex = selection.anchorOffset;
const expected = /[A-ZА-Я]*/i;
const testSlice = () => {
const slice = text.slice(startIndex, endIndex);
return slice === slice.match(expected)[0];
};
while(startIndex > 0 && testSlice()) {
startIndex -= 1;
}
startIndex += 1;
while(endIndex < text.length && testSlice()){
endIndex += 1;
}
endIndex -= 1;
range.setStart(node, startIndex);
range.setEnd(node, endIndex);
const word = range.toString();
console.log(word);
return word;
}
Found such solution - hope it helps to someone