-
Notifications
You must be signed in to change notification settings - Fork 126
/
SelectionHelpers.tsx
48 lines (46 loc) · 1.29 KB
/
SelectionHelpers.tsx
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
39
40
41
42
43
44
45
46
47
48
export const selectNodeContents = (node: HTMLElement | null, start?: number, end?: number) => {
if (!node) {
return;
}
if ('createRange' in document) {
try {
const selection = window.getSelection();
const range = window.document.createRange();
if (start !== undefined && end !== undefined) {
range.setStart(node, start);
range.setEnd(node, end);
} else {
range.selectNodeContents(node);
}
if (selection !== null) {
// Fix IE from issue not working (https://github.com/skbkontur/retail-ui/issues/1205)
selection.removeAllRanges();
selection.addRange(range);
return;
}
} catch (e) {
// empty block
}
}
// @ts-ignore (IE specific api)
if (typeof document.body.createTextRange === 'function') {
// @ts-ignore
const range = document.body.createTextRange();
range.moveToElementText(node);
if (typeof range.select === 'function') {
range.select();
}
return;
}
};
export const removeAllSelections = () => {
const selection = window.getSelection();
if (selection !== null) {
try {
// Fix IE from issue not working (https://github.com/skbkontur/retail-ui/issues/1205)
selection.removeAllRanges();
} catch (e) {
// empty block
}
}
};