Closed YOUR1 closed 1 year ago
I think the rangy can tell us the current text of selection.
But the determining of '#3' needs to use other libraries.
I solved it relative simply by using the Search addon;
getTextAndIndex: function( selection, wrap ) {
wrap = typeof wrap === "undefined" ? true : wrap;
var baseEl = getNodeRoot(),
selectionPos = rangy.serializeSelection( selection ),
searchFor = selection.toString(),
range = rangy.createRange(),
searchScopeRange = rangy.createRange(),
i = 0,
res = {
text: searchFor,
index: i
};
range.selectNodeContents( baseEl );
searchScopeRange.selectNodeContents( baseEl );
var options = {
caseSensitive: true,
withinRange: searchScopeRange
};
if ( searchFor !== "" ) {
while( range.findText( searchFor, options ) ) {
if ( rangy.serializeRange( range ) === selectionPos ) {
res.index = i;
}
range.collapse(false);
++i;
}
}
return res;
},
Hi;
Let say we have the following text:
As you can see; we have multiple 'at'-text nodes, 3 to be exact. Is it possible to determine what number is selected?
For example if I select the 'at' in the last line, I want to have something like 'at#3'.
Is this possible with the Rangy library?
Thanks in advance.