Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function setSelectionRange(input,selectionStart,selectionEnd){if(input.setSelectionRange){input.focus();input.setSelectionRange(selectionStart,selectionEnd);}else if(input.createTextRange){var range=input.createTextRange();range.collapse(true);range.moveEnd('character',selectionEnd);range.moveStart('character',selectionStart);range.select();}}
- function replaceSelection(input, replaceString, shift) {
- var ie = false;
- var value = input.value;
- if (input.setSelectionRange) {
- var selectionStart = input.selectionStart;
- var selectionEnd = input.selectionEnd;
- var old = value.substring(selectionStart, selectionEnd);
- } else if(document.selection) {
- ie = true;
- var range=document.selection.createRange();
- if (range.parentElement() == input) {
- var selectionStart = input.selectionStart;
- var selectionEnd = input.selectionEnd;
- var old = range.text;
- } else return;
- }
- var isCollapsed = old == '';
- if (!isCollapsed && replaceString == '\t' && (old.indexOf('\n') != -1 || ((!selectionStart || value.charCodeAt(selectionStart - 1) == 10) && (selectionEnd == value.length || value.charCodeAt(selectionEnd) == 10)))) {
- if (selectionStart) selectionStart = value.substring(0, selectionStart + 2).lastIndexOf('\n') + 1 || selectionStart;
- selectionEnd = (value.indexOf('\n', selectionEnd) + 1 || value.length + 1) - 1;
- old = value.substring(selectionStart, selectionEnd).split("\n");
- var i = old.length, a;
- if (!shift) while (i--) {
- a = old[i];
- if (a) old[i] = '\t' + a;
- }
- else while (i--) {
- a = old[i];
- if (a) old[i] = (a.charCodeAt(0) == 9 ? a.substr(1) : a);
- }
- replaceString = old.join('\n');
- }
- if (ie) {
- range.text = replaceString;
- if (isCollapsed) return;
- } else {
- input.value = value.substring(0, selectionStart) + replaceString + value.substr(selectionEnd);
- }
- var end = selectionStart + replaceString.length;
- setSelectionRange(input, (selectionStart != selectionEnd) ? selectionStart : end, end);
- }
- function stopEvent(e){if(e.preventDefault){e.preventDefault();}if(e.stopPropagation){e.stopPropagation();}e.stopped=true;}
- function catchTab(item,e){if(!e)e=window.event;var c=e.keyCode||e.which;if(c==9){var scrollCurrent=item.scrollTop;replaceSelection(item,String.fromCharCode(9),e.shiftKey);stopEvent(e);item.scrollTop=scrollCurrent;return false;}}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement