Smart Text Indent

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;

    // Check if hitting enter on the first line as no newline will be found
    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;

    // Count only \t at the beginning of the line, none in the code   
    while (valueToScan.indexOf('\t')!=-1) {
     if (valueToScan.indexOf('\t')!=0)
      break;
     count++;
     valueToScan = valueToScan.substr(valueToScan.indexOf('\t')+1);
    }

    // Command to isert the newline, and then add the \t found in previously  
    $(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 // And then hit enter here

, , " ", . , , , , . , , . , , , , , . ?

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);
+5
4

, textarea

keyup keydown/keypress ?

​​

, scrollTop.

<!DOCTYPE html>
<title>autoindent example</title>
<textarea id="TA" rows="8"></textarea>
<script>
document.getElementById('TA').addEventListener('keyup', function(v){
  if(v.keyCode != 13 || v.shiftKey || v.ctrlKey || v.altKey || v.metaKey)
    return;
  var val = this.value, pos = this.selectionStart;
  var line = val.slice(val.lastIndexOf('\n', pos - 2) + 1, pos - 1);
  var indent = /^\s*/.exec(line)[0];
  if(!indent) return;
  var st = this.scrollTop;
  this.value = val.slice(0, pos) + indent + val.slice(this.selectionEnd);
  this.selectionStart = this.selectionEnd = pos + indent.length;
  this.scrollTop = st;
}, false);
</script>
<p>(not considering IE here)
+2

- , Qaru ? :

function example(whatever) {
    Example Stuff // And then hit enter here
}

. , ( , 101010), . , , . , /.

, . keypress, :

  • onKeySomething, "enter"; :

    1. (, ) Date();

    1. setTimeout (X, timeoutFunction), X ,

  • timeoutFunction; "", Date() - timer > X trigger actualFunction()

  • actualFunction , , , .

, , "" IDE, JS .

+1

WndProc? (KeyPress , ).

public class MyTextBox : System.Windows.Forms.TextBox
{
    protected override void WndProc(ref Message m)
    {
        // Let the O/S handle the key first
        base.WndProc(ref m);

        // Then, if it the return key, append tabs
        int numTabs = 3;                 // TODO: insert your own value here
        const int WM_CHAR = 0x0102;
        if(m.Msg == WM_CHAR && (char)m.WParam == '\r')
        {
            m.WParam = new IntPtr('\t'); // Recycle m since base is done with it
            for (int i = 0; i < numTabs; i++)
                WndProc(ref m);
        }
    }
}
0
source

I know that this is not a direct answer, but there are examples on the Internet that you could take a look at, which will really help you realize this.

http://jsfiddle.net
http://teddevito.com/demos/textarea.html
http://thelackthereof.org/JQuery_Autoindent

I would definitely give this chance if I had more time, but, unfortunately, I do not.

0
source

All Articles