I am trying to create an auto-indexing text area, and so far it works with the following code. The problem I am facing is that I currently prevent the default action of pressing the enter key in order to calculate the tabs in a row and then insert a new row later.
This works, but I want to have a default action for the text field, because if you hold the enter key down, it does not scroll the text field until the carriage falls into the bottom row and then the scroll bar remains with the carriage in the bottom row. The current code below keeps the carriage in view if used anywhere in the text field, but this causes the contents above the carriage to scroll up, which is a compromise since the carriage no longer disappears. If there is no other solution, this will work fine, but I hope there are other ideas.
var start = this.selectionStart-1;
var end = this.selectionEnd;
var sT = this.scrollTop;
var sH = this.scrollHeight;
var vH = $(this).height();
var x = (sH - sT) - vH;
if (this.value.lastIndexOf('\n',start)==-1)
var startSubstr = 0;
else
var startSubstr = this.value.lastIndexOf('\n',start)+1;
var endFirst = end - startSubstr;
var valueToScan = this.value.substr(startSubstr,endFirst);
var count = 0;
while (valueToScan.indexOf('\t')!=-1) {
if (valueToScan.indexOf('\t')!=0)
break;
count++;
valueToScan = valueToScan.substr(valueToScan.indexOf('\t')+1);
}
$(this).insertAtCaret('\n');
for (var i=0;i<count;count--) {
$(this).insertAtCaret('\t');
}
var sH = this.scrollHeight;
this.scrollTop = (sH - x) - vH;
EDIT
As an update, in order to eliminate any confusion, I am trying to create an IDE style text box, such as in most IDEs, if you type the following
function example(whatever) {
Example Stuff
, , " ", . , , , , . , , . , , , , , . ?
EDIT 2 PHP
, . , , , , , lastIndexOf('\n')-1 \n \t. , , , . .
3. , , , ( , , ) , , , , . http://stackoverflow.com/users/15066/matyr' > matyr, , , .
if (this.value.lastIndexOf('\n',start-2)==-1) {
var startSubstr = 0;
} else {
var startSubstr = this.value.lastIndexOf('\n',start-1)+1;
}
var valueToScan = this.value.substr(startSubstr,start);