You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
42 lines
915 B
42 lines
915 B
// copy from https://github.com/sudodoki/toggle-selection
|
|
// refactor to esm
|
|
const deselectCurrent = (): (() => void) => {
|
|
const selection = document.getSelection();
|
|
if (!selection.rangeCount) {
|
|
return function () {};
|
|
}
|
|
let active = document.activeElement as any;
|
|
|
|
const ranges = [];
|
|
for (let i = 0; i < selection.rangeCount; i++) {
|
|
ranges.push(selection.getRangeAt(i));
|
|
}
|
|
|
|
switch (
|
|
active.tagName.toUpperCase() // .toUpperCase handles XHTML
|
|
) {
|
|
case 'INPUT':
|
|
case 'TEXTAREA':
|
|
active.blur();
|
|
break;
|
|
|
|
default:
|
|
active = null;
|
|
break;
|
|
}
|
|
|
|
selection.removeAllRanges();
|
|
return function () {
|
|
selection.type === 'Caret' && selection.removeAllRanges();
|
|
|
|
if (!selection.rangeCount) {
|
|
ranges.forEach(function (range) {
|
|
selection.addRange(range);
|
|
});
|
|
}
|
|
|
|
active && active.focus();
|
|
};
|
|
};
|
|
export default deselectCurrent;
|