blob: 2593a5f262557e4210f5ea72315ec52fdc216f50 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
module.exports.releaseDocumentFocus = () => {
const element = document.createElement('span');
document.body.appendChild(element);
const range = document.createRange();
range.setStart(element, 0);
const selection = window.getSelection();
selection.removeAllRanges();
selection.addRange(range);
selection.removeAllRanges();
document.body.removeChild(element);
};
module.exports.claimDocumentFocus = () => {
const { activeElement } = document;
const selection = window.getSelection();
let selectionStart;
let selectionEnd;
let range;
if (activeElement) ({ selectionStart, selectionEnd } = activeElement);
if (selection.rangeCount) range = selection.getRangeAt(0);
const restoreOriginalSelection = () => {
if (selectionStart >= 0 && selectionEnd >= 0) {
activeElement.selectionStart = selectionStart;
activeElement.selectionEnd = selectionEnd;
} else if (range) {
selection.addRange(range);
}
};
exports.releaseDocumentFocus();
window.requestAnimationFrame(restoreOriginalSelection);
};
|