-
Notifications
You must be signed in to change notification settings - Fork 3
/
libcursor.js
executable file
·33 lines (32 loc) · 6.98 KB
/
libcursor.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
function Cursor(){}
function unicodeLength(str){var count=0;for(var i=0;i<str.length;i++){if((str.charCodeAt(i)&0xF800)==0xD800)
i++;count++;}
return count;}
if(!HTMLTextAreaElement.prototype.setRangeText){HTMLTextAreaElement.prototype.setRangeText=function(text){var old_start=this.selectionStart;this.value=(this.value.substring(0,this.selectionStart)
+text
+this.value.substring(this.selectionEnd,this.value.length));this.setSelectionRange(old_start,old_start+text.length);};}
if(!HTMLInputElement.prototype.setRangeText){HTMLInputElement.prototype.setRangeText=function(text){var old_start=this.selectionStart;this.value=(this.value.substring(0,this.selectionStart)
+text
+this.value.substring(this.selectionEnd,this.value.length));this.setSelectionRange(old_start,old_start+text.length);};}
Cursor.new=function(DomElement,position){if(arguments.length==0){if((document.activeElement.nodeName=="TEXTAREA")||((document.activeElement.nodeName=="INPUT")&&(document.activeElement.getAttribute("type")=="text"))){return new TextAreaCursor();}
else if(document.activeElement.isContentEditable){return new ContentEditableCursor();}
else{}}
else if(arguments.length==1){if((DomElement.nodeName=="TEXTAREA")||((DomElement.nodeName=="INPUT")&&(DomElement.getAttribute("type")=="text"))){return new TextAreaCursor(DomElement);}
else if(DomElement.isContentEditable){return new ContentEditableCursor(DomElement);}
else{}}
else if(arguments.length==2){if((DomElement.nodeName=="TEXTAREA")||((DomElement.nodeName=="INPUT")&&(DomElement.getAttribute("type")=="text"))){return new TextAreaCursor(DomElement,position);}
else if(document.activeElement.isContentEditable){return new ContentEditableCursor(DomElement,position);}
else{}}
return null;};function TextAreaCursor(Textarea,position){if(arguments.length==0){this.textarea=document.activeElement;}
else if(arguments.length==1){this.textarea=Textarea;this.textarea.setSelectionRange(this.textarea.value.length,this.textarea.value.length);}
else if(arguments.length==2){this.textarea=Textarea;this.textarea.setSelectionRange(position,position);}}
function countTextNodes(elem){var count=0;for(var i=0;i<elem.childNodes.length;i++){if(elem.childNodes[i].nodeType==Node.TEXT_NODE)count++;}
return count;}
function ContentEditableCursor(DomElement,position){if(arguments.length==0){this.selection=document.getSelection();}
else if(arguments.length==1){DomElement.focus();this.selection=document.getSelection();this.selection.extend(DomElement,DomElement.children.length+countTextNodes(DomElement));this.selection.collapseToEnd();}
else if(arguments.length==2){DomElement.focus();this.selection=document.getSelection();this.selection.extend(DomElement,0);this.selection.collapseToStart();for(var i=0;i<position;i++){this.selection.modify('move','forward','character');}}}
Cursor.prototype.insert=function(text){return this.insertBefore(text);};TextAreaCursor.prototype.insert=Cursor.prototype.insert;ContentEditableCursor.prototype.insert=Cursor.prototype.insert;Cursor.prototype.insertBefore=function(text){};TextAreaCursor.prototype.insertBefore=function(text){this.textarea.setRangeText(text);this.textarea.setSelectionRange(this.textarea.selectionStart+text.length,this.textarea.selectionStart+text.length);return this;};ContentEditableCursor.prototype.insertBefore=function(text){this.insertAfter(text);this.move(unicodeLength(text));return this;};Cursor.prototype.insertAfter=function(text){};TextAreaCursor.prototype.insertAfter=function(text){this.textarea.setRangeText(text);this.textarea.setSelectionRange(this.textarea.selectionStart,this.textarea.selectionStart);return this;};ContentEditableCursor.prototype.insertAfter=function(text){var range=this.selection.getRangeAt(0);range.deleteContents();range.insertNode(document.createTextNode(text));this.selection.collapseToStart();return this;};Cursor.prototype.move=function(offset){if(offset>0)return this.moveForward(offset);else return this.moveBackward(-offset);};TextAreaCursor.prototype.move=Cursor.prototype.move;ContentEditableCursor.prototype.move=Cursor.prototype.move;Cursor.prototype.moveForward=function(offset){};TextAreaCursor.prototype.moveForward=function(offset){this.textarea.setSelectionRange(this.textarea.selectionEnd+offset,this.textarea.selectionEnd+offset);return this;};ContentEditableCursor.prototype.moveForward=function(offset){this.selection.collapseToEnd();while(offset--){this.selection.modify('move','forward','character');}
return this;};Cursor.prototype.moveBackward=function(offset){};TextAreaCursor.prototype.moveBackward=function(offset){this.textarea.setSelectionRange(this.textarea.selectionStart-offset,this.textarea.selectionStart-offset);return this;};ContentEditableCursor.prototype.moveBackward=function(offset){this.selection.collapseToStart();while(offset--){this.selection.modify('move','backward','character');}
return this;};Cursor.prototype.delete=function(amount){if(amount>0)return this.deleteForward(amount);else return this.deleteBackward(-amount);};TextAreaCursor.prototype.delete=Cursor.prototype.delete;ContentEditableCursor.prototype.delete=Cursor.prototype.delete;Cursor.prototype.deleteForward=function(amount){};TextAreaCursor.prototype.deleteForward=function(amount){this.textarea.setSelectionRange(this.textarea.selectionEnd,this.textarea.selectionEnd+amount);this.textarea.setRangeText("");return this;};ContentEditableCursor.prototype.deleteForward=function(amount){this.selection.collapseToEnd();while(amount--){this.selection.modify('extend','forward','character');}
this.selection.getRangeAt(0).deleteContents();return this;};Cursor.prototype.deleteBackward=function(amount){};TextAreaCursor.prototype.deleteBackward=function(amount){this.textarea.setSelectionRange(this.textarea.selectionStart-amount,this.textarea.selectionStart);this.textarea.setRangeText("");return this;};ContentEditableCursor.prototype.deleteBackward=function(amount){this.selection.collapseToStart();while(amount--){this.selection.modify('extend','backward','character');}
this.selection.getRangeAt(0).deleteContents();return this;};Cursor.prototype.getText=function(start,end){};TextAreaCursor.prototype.getText=function(start,end){var new_start=this.textarea.selectionStart+start;var new_end=this.textarea.selectionEnd+end;return this.textarea.value.substring(new_start,new_end);};ContentEditableCursor.prototype.getText=function(start,end){var range=this.selection.getRangeAt(0);return this.selection.anchorNode.textContent.substring(range.startOffset+start,range.endOffset+end);};Cursor.prototype.getTextBefore=function(len){var bigstr=this.getText(-len,0);var smallstr=this.getText(0,0);return bigstr.substring(0,bigstr.length-smallstr.length);};TextAreaCursor.prototype.getTextBefore=Cursor.prototype.getTextBefore;ContentEditableCursor.prototype.getTextBefore=Cursor.prototype.getTextBefore;Cursor.prototype.getTextAfter=function(len){var bigstr=this.getText(0,len);var smallstr=this.getText(0,0);return bigstr.substring(smallstr.length,bigstr.length);};TextAreaCursor.prototype.getTextAfter=Cursor.prototype.getTextAfter;ContentEditableCursor.prototype.getTextAfter=Cursor.prototype.getTextAfter;