-
Notifications
You must be signed in to change notification settings - Fork 0
/
iSelection.js
executable file
·129 lines (105 loc) · 4.23 KB
/
iSelection.js
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/**
* @author Knyazevich Denis
*/
(function (window, document, root) {
"use strict";
var iSelection = {
/**
*
* @param input {HTMLInputElement|HTMLTextAreaElement}
* @param position {Number}
* @returns {HTMLInputElement|HTMLTextAreaElement}
*/
moveSelectionTo: function (input, position) {
var end = isNaN(position) ? input.value.length : position;
if (input.setSelectionRange && input.selectionStart === null) {
input.focus();
input.setSelectionRange(end, end);
} else if (input.createTextRange) {
input.range = input.createTextRange();
input.range.moveStart('character', -end);
input.range.moveStart('character', end);
input.range.moveEnd('character', 0);
input.range.collapse(true);
input.range.select();
}
return input;
},
/**
*
* @param inputObject {HTMLInputElement|HTMLTextAreaElement}
* @returns {*}
*/
getSelection: function (inputObject) {
if (inputObject == null) {
return null;
}
if (inputObject.selectionStart != null) {
return {
start: inputObject.selectionStart,
end: inputObject.selectionEnd
};
}
if (document.selection) {
var start = 0,
end = 0,
normalizedValue = '',
textInputRange = null,
endRange = null,
range = document.selection.createRange();
if (range && range.parentElement() == inputObject) {
var len = inputObject.value.length;
normalizedValue = inputObject.value.replace(/\r\n/g, "\n");
textInputRange = inputObject.createTextRange();
textInputRange.moveToBookmark(range.getBookmark());
endRange = inputObject.createTextRange();
endRange.collapse(false);
if (textInputRange.compareEndPoints("StartToEnd", endRange) > -1) {
start = len;
end = len;
} else {
start = -textInputRange.moveStart("character", -len);
start += normalizedValue.slice(0, start).split("\n").length - 1;
if (textInputRange.compareEndPoints("EndToEnd", endRange) > -1) {
end = len;
} else {
end = -textInputRange.moveEnd("character", -len);
end += normalizedValue.slice(0, end).split("\n").length - 1;
}
}
return {
start: start,
end: end
};
}
return null;
}
return null;
},
/**
*
* @param input {HTMLInputElement|HTMLTextAreaElement}
* @param start
* @param end
* @returns {HTMLInputElement|HTMLTextAreaElement}
*/
setSelection: function (input, start, end) {
var length = input.value.length;
start = isNaN(start) ? length : start;
end = isNaN(end) ? length : end;
if (input.selectionStart != null) {
input.focus();
input.setSelectionRange(start, end);
} else if (input.createTextRange) {
input.range = input.createTextRange();
input.range.moveStart('character', -start);
input.range.moveStart('character', start);
input.range.moveEnd('character', end);
input.range.collapse(true);
input.range.select();
}
return input;
}
};
(root || window).iSelection = iSelection;
}(window, document, this));