forked from Q42/meteor-autoform-markdown
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.selection.js
executable file
·354 lines (314 loc) · 12.6 KB
/
jquery.selection.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
/*!
* jQuery.selection - jQuery Plugin
*
* Copyright (c) 2010-2014 IWASAKI Koji (@madapaja).
* http://blog.madapaja.net/
* Under The MIT License
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
(function($, win, doc) {
/**
* get caret status of the selection of the element
*
* @param {Element} element target DOM element
* @return {Object} return
* @return {String} return.text selected text
* @return {Number} return.start start position of the selection
* @return {Number} return.end end position of the selection
*/
var _getCaretInfo = function(element){
var res = {
text: '',
start: 0,
end: 0
};
if (!element.value) {
/* no value or empty string */
return res;
}
try {
if (win.getSelection) {
/* except IE */
res.start = element.selectionStart;
res.end = element.selectionEnd;
res.text = element.value.slice(res.start, res.end);
} else if (doc.selection) {
/* for IE */
element.focus();
var range = doc.selection.createRange(),
range2 = doc.body.createTextRange();
res.text = range.text;
try {
range2.moveToElementText(element);
range2.setEndPoint('StartToStart', range);
} catch (e) {
range2 = element.createTextRange();
range2.setEndPoint('StartToStart', range);
}
res.start = element.value.length - range2.text.length;
res.end = res.start + range.text.length;
}
} catch (e) {
/* give up */
}
return res;
};
/**
* caret operation for the element
* @type {Object}
*/
var _CaretOperation = {
/**
* get caret position
*
* @param {Element} element target element
* @return {Object} return
* @return {Number} return.start start position for the selection
* @return {Number} return.end end position for the selection
*/
getPos: function(element) {
var tmp = _getCaretInfo(element);
return {start: tmp.start, end: tmp.end};
},
/**
* set caret position
*
* @param {Element} element target element
* @param {Object} toRange caret position
* @param {Number} toRange.start start position for the selection
* @param {Number} toRange.end end position for the selection
* @param {String} caret caret mode: any of the following: "keep" | "start" | "end"
*/
setPos: function(element, toRange, caret) {
caret = this._caretMode(caret);
if (caret === 'start') {
toRange.end = toRange.start;
} else if (caret === 'end') {
toRange.start = toRange.end;
}
element.focus();
try {
if (element.createTextRange) {
var range = element.createTextRange();
if (win.navigator.userAgent.toLowerCase().indexOf("msie") >= 0) {
toRange.start = element.value.substr(0, toRange.start).replace(/\r/g, '').length;
toRange.end = element.value.substr(0, toRange.end).replace(/\r/g, '').length;
}
range.collapse(true);
range.moveStart('character', toRange.start);
range.moveEnd('character', toRange.end - toRange.start);
range.select();
} else if (element.setSelectionRange) {
element.setSelectionRange(toRange.start, toRange.end);
}
} catch (e) {
/* give up */
}
},
/**
* get selected text
*
* @param {Element} element target element
* @return {String} return selected text
*/
getText: function(element) {
return _getCaretInfo(element).text;
},
/**
* get caret mode
*
* @param {String} caret caret mode
* @return {String} return any of the following: "keep" | "start" | "end"
*/
_caretMode: function(caret) {
caret = caret || "keep";
if (caret === false) {
caret = 'end';
}
switch (caret) {
case 'keep':
case 'start':
case 'end':
break;
default:
caret = 'keep';
}
return caret;
},
/**
* replace selected text
*
* @param {Element} element target element
* @param {String} text replacement text
* @param {String} caret caret mode: any of the following: "keep" | "start" | "end"
*/
replace: function(element, text, caret) {
var tmp = _getCaretInfo(element),
orig = element.value,
pos = $(element).scrollTop(),
range = {start: tmp.start, end: tmp.start + text.length};
element.value = orig.substr(0, tmp.start) + text + orig.substr(tmp.end);
$(element).scrollTop(pos);
this.setPos(element, range, caret);
},
/**
* insert before the selected text
*
* @param {Element} element target element
* @param {String} text insertion text
* @param {String} caret caret mode: any of the following: "keep" | "start" | "end"
*/
insertBefore: function(element, text, caret) {
var tmp = _getCaretInfo(element),
orig = element.value,
pos = $(element).scrollTop(),
range = {start: tmp.start + text.length, end: tmp.end + text.length};
element.value = orig.substr(0, tmp.start) + text + orig.substr(tmp.start);
$(element).scrollTop(pos);
this.setPos(element, range, caret);
},
/**
* insert after the selected text
*
* @param {Element} element target element
* @param {String} text insertion text
* @param {String} caret caret mode: any of the following: "keep" | "start" | "end"
*/
insertAfter: function(element, text, caret) {
var tmp = _getCaretInfo(element),
orig = element.value,
pos = $(element).scrollTop(),
range = {start: tmp.start, end: tmp.end};
element.value = orig.substr(0, tmp.end) + text + orig.substr(tmp.end);
$(element).scrollTop(pos);
this.setPos(element, range, caret);
}
};
/* add jQuery.selection */
$.extend({
/**
* get selected text on the window
*
* @param {String} mode selection mode: any of the following: "text" | "html"
* @return {String} return
*/
selection: function(mode) {
var getText = ((mode || 'text').toLowerCase() === 'text');
try {
if (win.getSelection) {
if (getText) {
// get text
return win.getSelection().toString();
} else {
// get html
var sel = win.getSelection(), range;
if (sel.getRangeAt) {
range = sel.getRangeAt(0);
} else {
range = doc.createRange();
range.setStart(sel.anchorNode, sel.anchorOffset);
range.setEnd(sel.focusNode, sel.focusOffset);
}
return $('<div></div>').append(range.cloneContents()).html();
}
} else if (doc.selection) {
if (getText) {
// get text
return doc.selection.createRange().text;
} else {
// get html
return doc.selection.createRange().htmlText;
}
}
} catch (e) {
/* give up */
}
return '';
}
});
/* add selection */
$.fn.extend({
selection: function(mode, opts) {
opts = opts || {};
switch (mode) {
/**
* selection('getPos')
* get caret position
*
* @return {Object} return
* @return {Number} return.start start position for the selection
* @return {Number} return.end end position for the selection
*/
case 'getPos':
return _CaretOperation.getPos(this[0]);
/**
* selection('setPos', opts)
* set caret position
*
* @param {Number} opts.start start position for the selection
* @param {Number} opts.end end position for the selection
*/
case 'setPos':
return this.each(function() {
_CaretOperation.setPos(this, opts);
});
/**
* selection('replace', opts)
* replace the selected text
*
* @param {String} opts.text replacement text
* @param {String} opts.caret caret mode: any of the following: "keep" | "start" | "end"
*/
case 'replace':
return this.each(function() {
_CaretOperation.replace(this, opts.text, opts.caret);
});
/**
* selection('insert', opts)
* insert before/after the selected text
*
* @param {String} opts.text insertion text
* @param {String} opts.caret caret mode: any of the following: "keep" | "start" | "end"
* @param {String} opts.mode insertion mode: any of the following: "before" | "after"
*/
case 'insert':
return this.each(function() {
if (opts.mode === 'before') {
_CaretOperation.insertBefore(this, opts.text, opts.caret);
} else {
_CaretOperation.insertAfter(this, opts.text, opts.caret);
}
});
/**
* selection('get')
* get selected text
*
* @return {String} return
*/
case 'get':
/* falls through */
default:
return _CaretOperation.getText(this[0]);
}
return this;
}
});
})(jQuery, window, window.document);