-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathselectRange.ts
92 lines (75 loc) · 2.98 KB
/
selectRange.ts
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
class selectRange {
private element: HTMLTextAreaElement;
public constructor(inputs: HTMLTextAreaElement) {
this.element = inputs;
}
public static of(e: HTMLTextAreaElement): selectRange {
return e ? new selectRange(e) : <any>{};
}
public getCurPos(): number {
var curPos: number = 0;
var input = this.element;
if ((<any>document).selection) {
input.focus();
var range: any = (<any>document).selection.createRange();
range.moveStart("character", -input.value.length);
curPos = range.text.length;
} else {
(input.selectionStart || 0 == input.selectionStart) && (curPos = input.selectionStart);
}
return curPos;
}
public setCurPos(position: number) {
var t;
var input = this.element;
input.setSelectionRange ? (input.focus(),
input.setSelectionRange(position, position)) : (<any>input).createTextRange && ((t = (<any>input).createTextRange()).collapse(!0),
t.moveEnd("character", position),
t.moveStart("character", position),
t.select());
}
public getSelectText(): string {
var range;
var text: string = "";
if (window.getSelection) {
range = window.getSelection();
} else {
(<any>document).selection && (range = (<any>document).selection.createRange());
}
(text = range.text) || (text = range);
return text;
}
public setSelectText(e: number, t: number) {
var n: number;
var c;
var input = this.element;
(n = input.value.length) && (e || (e = 0),
t || (t = n),
n < e && (e = n),
n < t && (t = n),
e < 0 && (e = n + e),
t < 0 && (t = n + t),
(<any>input).createTextRange ? ((c = (<any>input).createTextRange()).moveStart("character", -n),
c.moveEnd("character", -n),
c.moveStart("character", e),
c.moveEnd("character", t),
c.select()) : (input.setSelectionRange(e, t),
input.focus()));
}
public insertAfterText(e: string) {
var t: number;
var n: number;
var c: number;
var input: HTMLTextAreaElement = this.element;
(<any>document).selection ?
(input.focus(), (<any>document).selection.createRange().text = e, input.focus()) : input.selectionStart || 0 == input.selectionStart ?
(t = input.selectionStart,
n = input.selectionEnd,
c = input.scrollTop,
input.value = input.value.substring(0, t) + e + input.value.substring(n, input.value.length),
input.focus(),
input.selectionStart = t + e.length,
input.selectionEnd = t + e.length,
input.scrollTop = c) : (input.value += e, input.focus());
}
}