Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to simulate an artificial caret in a textarea?

What I am trying to do is something similar to how collaborative editor works. I want to allow two people to edit the same document. And for this I have to simulate an artificial caret. I can extract the other user's activity in term of addition and deletion at specified location in a textarea.

I will then transmit the location, along with the action to the other document. There I need to carry out the required change at the sent coordinate. I have searched and found enough ways to set the caret location and insert or delete text at the current caret location, but the problem is that the caret of the document moves to the location at which I make the change.

I don't want that, I want to have two carets, one each for the two users. Transmit their changes to each other documents and make the changes at their respective locations, while showing two different carets.

I just need to know if there are certain libraries that I can use, or even if I have to make this on my own, then how and where do I start. I don't even know how a textarea is represented within a browser. How can I characterize locations within a textarea, if I know that then I save the locations in memory and make the changes based on the input received.

I hope I make sense, thanks for any help.

like image 679
Sachin Avatar asked May 28 '12 07:05

Sachin


2 Answers

have you seen this?

https://github.com/benjamn/kix-standalone/

This is how google docs does it:

https://drive.googleblog.com/2010/05/whats-different-about-new-google-docs.html

like image 51
fabiorocha Avatar answered Oct 07 '22 05:10

fabiorocha


You could mimic a caret with a special character and do the regex to insert the partner text and move his caret, and you can use the real one. This is the simplest design I can think. To get the idead, you could use the pipe | as a artificial caret. but this would easily conflict with user insert a pipe, to avoid this you can use sort of uncommon combination, or find some unicode character to act as a caret.

A real solution but way more complicated is not use a textarea, and use a DIV. this means that you need to handle all the key events and insert the character pressed manually to the document, and register the cursor position. But this way you can insert how many carets you want not just 2, something like this <span class="caret1"></span> you can make it blink, style with css, have diferent color for each caret, etc.

like image 42
Vitim.us Avatar answered Oct 07 '22 03:10

Vitim.us