Intro
I'm creating a custom Wysiwyg / Rich Text Editor (RTE) because the ones I found didn't fullfill my needs in terms of simplicity and customization.
I have looked at:
- CKEditor
- TinyMCE
- and so on
The Situation
I have a Textarea and an Iframe. The Textarea holds the info from the database. At $(window).load (function () {
the Textarea will hide and it contents will be copied to the Iframe with designMode = "on";
.
NewLines
Everything works great except the newlines when you press 'Enter'.
In every browser the outcome is different:
- <p>
: Opera & IE
- <div>
: Safari & Chrome
- <br>
: Firefox
This is not a solid outcome so I decided that when someone hits Enter a <br>
must be put in place. So when someone wants a new paragraphe he hits Enter twice en two <br>
's are placed.
And therefor I build an KeyDown event with keyCode 13 (Enter).
Within the function I prevented the default action with:
- event.preventDefault ();
IE9+, FF, CHROME, SAFARI AND OPERA
- event.returnValue = false;
IE8-
And then inserted the code for adding the custom <br>
:
- pasteHTML
IE
- insertHTML
FF, CHROME, SAFARI AND OPERA
JSFiddle
Both codes above works in IE7/8/9, Firefox, Chrome, Opera and Safari but then some strange results appear. To see the bugs yourself I've created a JSFiddle here: http://jsfiddle.net/RickS/RZ4Re/
The bugs See JSFiddle above
[1: effects on all browsers] Hit 'Enter' after 'Two' and nothing happens, but hit 'Enter' after 'One' and you'll see the code works. I've figured out that when the cursor is placed before a block element like <ul>
<ol>
or <p>
the <br>
will not be put in place.
The bug doesn't appear on inline elements.
Desired outcome: put a <br>
in place before a block element.
[2: effects on IE7/8/9] Hit 'Enter' after 'Three' (the list item) and 'Four' disappears. Type something and hit 'Enter' again. Now 'Five' disappears.
And the strange part: click on the word 'One'. 'Four' and 'Five' appear again, and were never deleted.
Desired outcome: when you create another list item the contents beneath the list stays visible.
Searched for
I've looked into this problem for over a week now, but can't seem to find the solution anywhere.
On Stack Overflow this article came close I think but didn't resolve my questions: Make a <br> instead of <div></div> by pressing Enter on a contenteditable
The solutions need to work cross-browser in IE9 (if possible also 7 and 8), Firefox, Chrome, Safari and Opera.
I hope someone can help me with this question.
So up front: thank you very much!
Rick
` tags to isolate all of their elements is because the `
– Andrew Klatzke Jan 22 '13 at 20:13` tag has little use semantically. See thread: http://stackoverflow.com/questions/3937515/when-to-use-br-line-breaks-vs-css-positioning