45

Hey all i want to know what is the concept behind creating the rich text editor. i mean how to create a rich text editor. I want to learn the implementation.

PS: please donot suggest using YUI or any other built in library. I want to make one my own.

So what's the concept behind?

Thanks :)

Jeromy French
  • 11,812
  • 19
  • 76
  • 129
codeomnitrix
  • 4,179
  • 19
  • 66
  • 102
  • 2
    Here are some simple examples that may make learning the basic concept possible: [Looking for a Rich Text Editor that is simple](http://stackoverflow.com/q/4674609) – Pekka May 15 '11 at 08:12
  • Try this http://codemasala.com/blog/2012/09/22/learn-to-create-your-own-rich-text-editorrte-using-jquery/ – Rajat Saxena Oct 09 '12 at 14:50
  • @Pekka웃 I know it's an old question, but it looks like your link is no longer available. Do you have an alternative? – Panpaper Jul 31 '19 at 03:26
  • @Pekka웃 link no longer exists :( – oldboy Oct 01 '19 at 07:43

2 Answers2

63

The easiest way is the following. It's used by TinyMCE and CKEditor and many others. There are many variations: in particular, if you are creating a code editor, there are clever tricks you can do using textareas and a monospaced font.

  • Dynamically create an iframe and place the editable content within that iframe's document
  • Set the iframe to be editable either by setting its document's designMode property to "on" or by setting its <body> element's contentEditable property to true. Note that designMode support predates contenteditable in Firefox and as a consequence is a lot less buggy.
  • Add buttons (such as bold, italic, insert image, etc) somewhere sensible (such as directly above the editable iframe) in the main document that act on the selected content within the iframe. All browsers supply an execCommand() method (see MSDN and MDN, for example) for doing many of these actions, although there is some variation in exactly how they work and what mark-up they produce.

That's the very basics of how it works. There are tons of other, complicated things that most editors do that aren't immediately obvious, in part to iron out the many differences between browsers and in part to provide extra functionality not covered by the built-in browser commands. It's a very complicated and difficult thing to get right, requires a high degree of expertise and commitment and is not something to be taken on lightly.

Tim Down
  • 318,141
  • 75
  • 454
  • 536
  • Wow. Felt assured at your last line that it does require a high expertise on JS..:) Can you shed some light on how can one add functionality for inserting images. Say the user drags an image from some place into the iFrame. Now the user has typed a fair amount of text. He drops the image in the middle of the text. Now how do one partition the entire content into three sections, one - the portion above the place where he dropped, then the image itself, then the stuff below it. jQuery `sortable` won't work I think, because one can't partition a `div`'s contents based on some coordinates... – SexyBeast Jan 31 '13 at 19:52
1

I won't suggest you look at others to use but I would suggest taking a look at jWYSIWYG to see how it's coded in jQuery.

Felix Dombek
  • 13,664
  • 17
  • 79
  • 131
Chris Bornhoft
  • 4,195
  • 4
  • 37
  • 55