@onesy/utils
Version:
21 lines (17 loc) • 856 B
JavaScript
const startSelection = function (element) {
let start = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : undefined;
let end = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : undefined;
if (window.getSelection) {
var _element$childNodes;
const newRange = window.document.createRange();
const position = element === null || element === void 0 ? void 0 : (_element$childNodes = element.childNodes) === null || _element$childNodes === void 0 ? void 0 : _element$childNodes.length;
newRange.setStart(element, start !== undefined ? start : position);
newRange.setEnd(element, end !== undefined ? end : position);
const selection = window.getSelection();
if (selection) {
selection.removeAllRanges();
selection.addRange(newRange);
}
}
};
export default startSelection;