2

I'm attempting to write my own WYSWG text editor in Javascript, but I'm struggling with what seems to be a rather basic problem.

Given a selection of text inside a contentEditable area such as what is between the pipes '|' here:

<span class="italic">Some text that |I would want</span> to edit or| select

I can figure out what I need to do here to essentially move the closing span tag so that my selection is included in the italic span. However, there are a plethora of other cases that I need to account for such as if I want to remove the span, add another span around an area that already contains an opening or closing span, account for opening/closing other tag types around my selection, etc.

Is there some library that exists anywhere that provides this selection/replace logic for me? Or am I asking too much? Would I be better off using one of the many editors already available on the web?

I would really like to not have to use the last option since most of the ones I've found seem extremely heavy weight since I really only want a few custom editing functions.

9
  • Why are you trying to re-invent the wheel? tinymce.com Commented Feb 13, 2012 at 1:41
  • What do you have so far? Commented Feb 13, 2012 at 1:41
  • @kennis: it's all how its loaded, you can load it barebone without all the extra stuff. tinymce.com/tryit/multiple_configs.php Commented Feb 13, 2012 at 1:43
  • @ZeeTee Touché. That's a fair point. Commented Feb 13, 2012 at 1:45
  • Well I wanted to take this as a kind of learning experience. Also, tinyMCE is really gigantic when I need only a few of the functions it offers and I wanted to add a few functions that it does not offer. I figure if I can get this issue figured out, there won't be too much more difficulty. Commented Feb 13, 2012 at 2:08

1 Answer 1

2

Check out Rangy and its modules http://code.google.com/p/rangy/ http://code.google.com/p/rangy/#Modules

ContentEditable is a nasty beast with many quirks between browsers. Rangy tries to solve that but it's still difficult to work with. My project has a lot of custom rules so we had to use a lower level library like Rangy. Combined with jQuery it is very powerful.

Sign up to request clarification or add additional context in comments.

1 Comment

I'll have to take a deeper look into it, but at first glance this looks like exactly what I need! Great, thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.