web-highlighter
Version:
✨A no-runtime dependency lib for text highlighting & persistence on any website ✨🖍️
23 lines (17 loc) • 559 B
text/typescript
/**
* Something about the Selection/Range API in browsers.
* If you want to use Highlighter in some old browsers, you may use a polyfill.
* https://caniuse.com/#search=selection
*/
export const getDomRange = (): Range => {
const selection = window.getSelection();
if (selection.isCollapsed) {
// eslint-disable-next-line no-console
console.debug('no text selected');
return null;
}
return selection.getRangeAt(0);
};
export const removeSelection = (): void => {
window.getSelection().removeAllRanges();
};