UNPKG

@onesy/utils

Version:
24 lines (19 loc) 992 B
const clearSelection = function () { let end = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : true; if (window.getSelection) { const selection = window.getSelection(); if (!selection) return; const previousRange = selection.getRangeAt(0); const newRange = window.document.createRange(); if (window.getSelection) { if (window.getSelection().empty) window.getSelection().empty();else if (window.getSelection().removeAllRanges) window.getSelection().removeAllRanges(); } else if (window.document.selection) window.document.selection.empty(); if (end) { var _window$getSelection; newRange.setStart(previousRange.endContainer, previousRange.endOffset); newRange.setEnd(previousRange.endContainer, previousRange.endOffset); (_window$getSelection = window.getSelection()) === null || _window$getSelection === void 0 ? void 0 : _window$getSelection.addRange(newRange); } } }; export default clearSelection;