Achieving this with javascript is definitely possible.
The key element required for selection manipulation is the Range object. By defining a range, we can then define a selection. To establish a range, the essential properties needed are startContainer
, startOffset
, endContainer
, and endOffset
.
The concept behind creating an "Android" selection involves tracking user interaction when clicking with the mouse: noting down the container and offset at which the click occurred. This can be swiftly achieved using document.caretPositionFromPoint
or document.caretRangeFromPoint
, depending on the browser. As the mouse moves or is released, the second container and offset are recorded. This ensures that there are always values for both the start and end points, allowing us to form a selection:
var range = document.createRange();
range.setStart(start.container, start.offset);
range.setEnd(end.container, end.offset);
var selection = document.getSelection();
selection.addRange(range);
If the user clicks again while the mouse is down, we can determine whether they clicked at the start or end of the selection. Based on this information, we can choose to 'freeze' one point and adjust the other, essentially reconstructing the selection range.
This approach has been implemented in a working example found here: https://jsfiddle.net/uvaf36gh/. Hopefully, this provides some clarity and assistance!