Welcome to the world of wysiwyg editing. I'm currently working on an
editor for a social networking site, and have found that the code
generated is usually not pretty. How does your logic work when you
press a key? I'm guessing that it's something like
on key down
1. get the letter from the keycode
2. execCommand to set corresponding fore / back color for that
letter.
Instead of on keydown, maybe on keypress, get all the text nodes from
the editor document, and if they don't have a parent node of font (or
span, whatever you want), assign a parent node with the color you want.
Something like (mix of pseudo and javascript)
on keypress
nodes = editor.document.childNodes;
foreach in nodes as i
if nodes[ i ] != text node
continue;
color = possible_colors[ nodes[ i ].lowerCase ];
span_tag = new span tag;
span_tag.color = color;
span_tag.appendChild( nodes[ i ].cloneNode( false ) )
editor.document.replaceChild( span_tag, nodes[ i ] )
pbreah wrote:
Quote:
I'm doing a Rich Text Editor (WYSIWYG) in javascript for a game for
kids. I'm doing a special case in with every keystroke from A-Z creates
a background and foreground color for that letter, witch is the same.
The problem is editing doesn't work that well.
>
For example when I type: ABCDFG , I get this code generated and
displayed by the Rich Text Editor:
>
<P><FONT style="BACKGROUND-COLOR: #ffff00" color=#ffff00>a<FONT
style="BACKGROUND-COLOR: #3399cc" color=#3399cc>b<FONT
style="BACKGROUND-COLOR: #ffccff" color=#ffccff>c<FONT
style="BACKGROUND-COLOR: #cccccc" color=#cccccc>d<FONT
style="BACKGROUND-COLOR: #ffcc99" color=#ffcc99>f<FONT
style="BACKGROUND-COLOR: #d21894"
color=#d21894>g</FONT></FONT></FONT></FONT></FONT></FONT></P>
>
The markup looks ugly but it works.
>
If I put my cursor between the "D" and "F", for example, and try to
insert the letter "E" I get this code:
>
<P><FONT style="BACKGROUND-COLOR: #ff9900"
color=#ff9900>abcdefg</FONT></P>
>
I just want the letter "E" inserted in its own tag without overwriting
the other letters. The whole line ends with the color of "E". :-(
>
By the way, I'm using execCommand wih IE6 sp2. If you think or know a
better way of solving this without execCommand or anything, please help
:~(
>
Any ideas, suggestions, code samples?
>
Thank you...