0

I want to check and see if the user's (blinking) cursor position is between two strings in a textarea.

enter image description here

Handling the tab events is it's own thing, I just want to know the method of handling the cursor location in relation to the tags.

  • Cursor as in mouse cursor?? Or blinking cursor? – Kylie Jun 24 '14 at 21:31
  • You are going to have to implement something along these lines to get what you want. http://jsfiddle.net/techfoobar/xEVSu/ – Kylie Jun 24 '14 at 21:32
  • @KyleK please don't tell me you were able to write that all just now.. –  Jun 24 '14 at 21:33
  • hahah....no. Just cant really think of a way....besides faking the caret with divs. Although this post seems like it might set you on the right path... http://stackoverflow.com/questions/1064089/inserting-a-text-where-cursor-is-using-javascript-jquery. Because what you have here is a multi step question. First you have to figure out how to find out where the caret is positioned. Which that post might help you with. Then you gotta figure out how to calculate the positions between tags. Then do math to know if the caret is inside or out. – Kylie Jun 24 '14 at 21:36
  • ta.value.split("")[0].lastIndexOf("") < ta.selectionStart – dandavis Jun 24 '14 at 22:49
  • @dandavis could you explain that? –  Jun 24 '14 at 23:32

1 Answers1

0

to find an inside-tag match, you can split the text into the left and right, then look for the proper open/close tags in the correct half. from there, it's simple to make the tab replacement using the same halfs:

<textarea>blah <code> this is the code </code> and this is not.</textarea>
<script>

function handleKeys(e) {
     var t,n,s,r, pad="    ";

    if (e.shiftKey && e.keyCode == 32) {
            t = this.value,
        n = this.selectionStart,
        s = this.scrollTop,
        r = [t.slice(0, n), t.slice(n)];

        this.value = r.join(pad);
        this.selectionStart = this.selectionEnd = n + pad.length;
        this.scrollTop = s;

        e.preventDefault();
    }
}  



</script>

pressing tab inside a code tag will insert a tab character. You should also consider providing some way for folks who use only the keyboard to escape the tab trap, maybe the escape key or something.

EDIT: i added scroll resume to fix a bug in some browsers that scroll the textarea to the top when inserting. Since it's an event that could fire a lot, i also optimized the function to ignore non-relevant key-presses completely (instead of doing some work each and every keydown).

dandavis
  • 16,370
  • 5
  • 40
  • 36
  • @jt0dd: blind and mobility disabled people primarily use the keyboard for web navigation and interaction. – dandavis Jun 25 '14 at 00:56
  • I used your code for [this feature request.](http://meta.stackexchange.com/questions/234662/should-shift-spacebar-create-a-tab-indent-in-se?noredirect=1#comment771744_234662) –  Jun 27 '14 at 19:08
  • How can I check to see if the character before (behind) the user's cursor is either a whitespace, or a linebreak? I used this to insert 4 spaces when the user holds Shift + Spacebar, but one user points out that I should avoid creating an accidental indent after "XML is.." by ensuring that no letters come before the indent. –  Jun 27 '14 at 19:11
  • 1
    ta.value.slice(ta.selectionStart-4, ta.selectionStart).trim() – dandavis Jun 27 '14 at 19:56
  • re as a feature: we can do even better than that if you're serious about it. in particular, it should handle indenting selections, and managing the scrollTop property of long textareas beacuse re-setting the .value to insert the tab can cause a scroll. i have a nicer implementation using [tab] at http://danml.com/textedit/, but i will extract the functionality and clean it up if there's any interest... – dandavis Jun 27 '14 at 20:02
  • It would be appropriate and humorous if you were to edit and update the feature request's code to your liking, and I'll approve any suggested edits you make. Quite frankly, your method of doing this beats related attempts that I've found on the web 2 : 1. If your code handles it fully, you might be humored to find a trace of your very own code within SE's source after implementation. –  Jun 27 '14 at 20:14
  • i don't have an account "over there". i added the scrolling fix into the answer code above, since that's an important one. the selection indenting is less-needed because we already have [ctrl]+[k], so that's my final answer... – dandavis Jun 27 '14 at 20:19
  • Thanks, very cool. Well in the feature request, you're credited properly and I will update the code there. –  Jun 27 '14 at 20:20
  • Btw, could you comment and explain your code. I'm having some trouble understand how some of it works. –  Jun 27 '14 at 20:48