I'm making progress but not quite sure how to get this working right...
I have a contenteditable div which will function similar to a textarea.
I also have a regex to recognize URLs being typed in and automatically link them. I'm having trouble however making this work "live" as the user is typing.
Here is a jsFiddle of what I have so far. One other problem I am having is the cursor jumping to the beginning of the div after a link is entered (because I am replacing the div's .html()
?)
Is there creative solution to use .replace()
on individual strings of text in a div without having to replace the entire content of the div?
Firstly, IE will do this for you automatically.
For other browsers, I would suggest doing the replacement after a period of user inactivity. Here's an answer of mine that illustrates how to do the replacement:
https://stackoverflow.com/a/4045531/96100
Here's a similar one with a (bad) link regex and discussion:
https://stackoverflow.com/a/4026684/96100
For saving and restoring the selection, I'd suggest using a character offset-based approach. There are shortcomings to the following code in general but for the particular case of saving and restoring a selection while changing formatting but leaving text unchanged, it's ideal. Here is an example:
https://stackoverflow.com/a/13950376/96100
Finally, here are a couple of answers with discussion and examples of how to wait for user inactivity:
Putting it all together: